问:
How can you bind exceptions with custom fields and constructors in pybind11 and still have them function as python exception?
This appears to be a known limitation in pybind11. I read through all the docs, whatever bug reports seemed applicable, and everything I could find in the pybind11 gitter. I have a custom exception class in c++ that contains custom constructors and fields. A very basic example of such a class, trimmed for space is here:

```<code>class BadData : public std::exception
{
  public:
    // Constructors
    BadData()
      : msg(),
        stack(),
        _name("BadData")
    {}

    BadData(std::string _msg, std::string _stack)
      : msg(_msg),
        stack(_stack),
        _name("BadData")
    {}

    const std::string&
    getMsg() const
    {
      return msg;
    }

    void
    setMsg(const std::string& arg)
    {
      msg = arg;
    }

    // Member stack
    const std::string&
    getStack() const
    {
      return stack;
    }

    void
    setStack(const std::string& arg)
    {
      stack = arg;
    }
  private:
    std::string msg;
    std::string stack;
    std::string _name;
</code>```

I currently have python binding code that binds this into python, but it is custom generated and we'd much rather use pybind11 due to its simplicity and compile speed.

The default mechanism for binding an exception into pybind11 would look like

```<code>py::register_exception<BadData>(module, "BadData");
</code>```

That will create an automatic translation between the C++ exception and the python exception, with the what() value of the c++ exception translating into the message of the python exception. However, all the extra data from the c++ exception is lost and if you're trying to throw the exception in python and catch it in c++, you cannot throw it with any of the extra data.

You can bind extra data onto the python object using the attr and I even went somewhat down the path of trying to extend the pybind11:exception class to make it easier to add custom fields to exceptions.

```<code>  template <typename type>
  class exception11 : public ::py::exception<type>
  {
   public:
    exception11(::py::handle scope, const char *name, PyObject *base = PyExc_Exception)
      : ::py::exception<type>(scope, name, base)
    {}

    template <typename Func, typename... Extra>
    exception11 &def(const char *name_, Func&& f, const Extra&... extra) {
      ::py::cpp_function cf(::py::method_adaptor<type>(std::forward<Func>(f)),
                            ::py::name(name_),
                            ::py::is_method(*this),
                            ::py::sibling(getattr(*this, name_, ::py::none())),
                            extra...);
      this->attr(cf.name()) = cf;
      return *this;
    }
  };
</code>```

This adds a def function to exceptions similar to what is done with class_. The naive approach to using this doesn't work

```<code>    exception11< ::example::data::BadData>(module, "BadData")
      .def("getStack", &::example::data::BadData::getStack);
</code>```

Because there is no automatic translation between BadData in c++ and in python. You can try to work around this by binding in a lambda:

```<code>    .def("getStack", [](py::object& obj) {
      ::example::data::BadData *cls = obj.cast< ::example::data::BadData* >();
      return cls->getStack();
    });
</code>```

The obj.cast there also fails because there is no automatic conversion. Basically, with no place to store the c++ instance, there isn't really a workable solution for this approach that I could find. In addition I couldn't find a way to bind in custom constructors at all, which made usability on python very weak.

The next attempt was based on a suggestion in the pybind11 that you could use the python exception type as a metaclass a normal class_ and have python recognize it as a valid exception. I tried a plethora of variations on this approach.

```<code>py::class_< ::example::data::BadData >(module, "BadData", py::dynamic_attr(), py::reinterpret_borrow<py::object>(PyExc_Exception))
py::class_< ::example::data::BadData >(module, "BadData", py::dynamic_attr(), py::cast(PyExc_Exception))
py::class_< ::example::data::BadData >(module, "BadData", py::dynamic_attr(), py::cast(PyExc_Exception->ob_type))
py::class_< ::example::data::BadData>(module, "BadData", py::metaclass((PyObject *) &PyExc_Exception->ob_type))
</code>```

There were more that I don't have saved. But the overall results was either 1) It was ignored completely or 2) it failed to compile or 3) It compiled and then immediately segfaulted or ImportError'd when trying to make an instance. There might have been one that segfaulted on module import too. It all blurs together. Maybe there is some magic formula that would make such a thing work, but I was unable to find it. From my reading of the pybind11 internals, I do not believe that such a thing is actually possible. Inheriting from a raw python type does not seem to be something it is setup to let you do.

The last thing I tried seemed really clever. I made a python exception type

```<code>  static py::exception<::example::data::BadData> exc_BadData(module, "BadDataBase");
</code>```

and then had my pybind11 class_ inherit from that.

```<code>  py::class_< ::example::data::BadData >(module, "BadData", py::dynamic_attr(), exc_BadData)
</code>```

But that also segfaulted on import too. So I'm basically back to square one with this.

答:
So I figured out a way to actually do this but it involves 1) doing some hacking of the pybind11 code itself and 2) introducing some size inefficiencies to the bound python types. From my point of view, the size issues are fairly immaterial. Yes it would be better to have everything perfectly sized but I'll take some extra bytes of memory for ease of use. Given this inefficiency, though, I'm not submitting this as a PR to the pybind11 project. While I think the trade-off is worth it, I doubt that making this the default for most people would be desired. It would be possible, I guess to hide this functionality behind a #define in c++ but that seems like it would be super messy long-term. There is probably a better long-term answer that would involve a degree of template meta-programming (parameterizing on the python container type for class_) that I'm just not up to.

I'm providing my changes here as diffs against the current master branch in git when this was written (hash a54eab92d265337996b8e4b4149d9176c2d428a6).

The basic approach was

Modify pybind11 to allow the specification of an exception base class for a class_ instance.

Modify pybind11's internal container to have the extra fields needed for a python exception type

Write a small amount of custom binding code to handle setting the error correctly in python.

For the first part, I added a new attribute to type_record to specify if a class is an exception and added the associated process_attribute call for parsing it.

```<code>diff --git a/src/pybind11/include/pybind11/attr.h b/src/pybind11/include/pybind11/attr.h
index 58390239..b5535558 100644
--- a/src/pybind11/include/pybind11/attr.h
+++ b/src/pybind11/include/pybind11/attr.h
@@ -73,6 +73,9 @@ struct module_local { const bool value; constexpr module_local(bool v = true) :
 /// Annotation to mark enums as an arithmetic type
 struct arithmetic { };

+// Annotation that marks a class as needing an exception base type.
+struct is_except {};
+
 /** \rst
     A call policy which places one or more guard variables (``Ts...``) around the function call.

@@ -211,7 +214,8 @@ struct function_record {
 struct type_record {
     PYBIND11_NOINLINE type_record()
         : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false),
 -          default_holder(true), module_local(false), is_final(false) { }
 -          default_holder(true), module_local(false), is_final(false),
 -          is_except(false) { }

     /// Handle to the parent scope
     handle scope;
@@ -267,6 +271,9 @@ struct type_record {
     /// Is the class inheritable from python classes?
     bool is_final : 1;

 -    // Does the class need an exception base type?
 -    bool is_except : 1;
 -      PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) {
         auto base_info = detail::get_type_info(base, false);
         if (!base_info) {
@@ -451,6 +458,11 @@ struct process_attribute<is_final> : process_attribute_default<is_final> {
     static void init(const is_final &, type_record *r) { r->is_final = true; }
 };

+template <>
+struct process_attribute<is_except> : process_attribute_default<is_except> {
 -    static void init(const is_except &, type_record *r) { r->is_except = true; }
+};

</code>```

I modified the internals.h file to add a separate base class for exception types. I also added an extra bool argument to make_object_base_type.

```<code>diff --git a/src/pybind11/include/pybind11/detail/internals.h b/src/pybind11/include/pybind11/detail/internals.h
index 6224dfb2..d84df4f5 100644
--- a/src/pybind11/include/pybind11/detail/internals.h
+++ b/src/pybind11/include/pybind11/detail/internals.h
@@ -16,7 +16,7 @@ NAMESPACE_BEGIN(detail)
 // Forward declarations
 inline PyTypeObject *make_static_property_type();
 inline PyTypeObject *make_default_metaclass();
-inline PyObject *make_object_base_type(PyTypeObject *metaclass);
+inline PyObject *make_object_base_type(PyTypeObject *metaclass, bool is_except);

 // The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new
 // Thread Specific Storage (TSS) API.
@@ -107,6 +107,7 @@ struct internals {
     PyTypeObject *static_property_type;
     PyTypeObject *default_metaclass;
     PyObject *instance_base;
+    PyObject *exception_base;
 #if defined(WITH_THREAD)
     PYBIND11_TLS_KEY_INIT(tstate);
     PyInterpreterState *istate = nullptr;
@@ -292,7 +293,8 @@ PYBIND11_NOINLINE inline internals &get_internals() {
         internals_ptr->registered_exception_translators.push_front(&translate_exception);
         internals_ptr->static_property_type = make_static_property_type();
         internals_ptr->default_metaclass = make_default_metaclass();
-        internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass);
+        internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass, false);
+        internals_ptr->exception_base = make_object_base_type(internals_ptr->default_metaclass, true);
</code>```

And then in class.h I added the necessary code to generate the exception base type. The first caveat is here. Since PyExc_Exception is a garbage collected type, I had to scope the assert call that checked the GC flag on the type. I have not currently seen any bad behavior from this change, but this is definitely voiding the warranty right here. I would highly, highly recommend always passing the py:dynamic_attr() flag to any classes you are using py:except on, since that turns on all the necessary bells and whistles to handle GC correctly (I think). A better solution might be to turn all those things on in make_object_base_type without having to invoke py::dynamic_attr.

```<code>diff --git a/src/pybind11/include/pybind11/detail/class.h b/src/pybind11/include/pybind11/detail/class.h
index a05edeb4..bbb9e772 100644
--- a/src/pybind11/include/pybind11/detail/class.h
+++ b/src/pybind11/include/pybind11/detail/class.h
@@ -368,7 +368,7 @@ extern "C" inline void pybind11_object_dealloc(PyObject *self) {
 /** Create the type which can be used as a common base for all classes.  This is
     needed in order to satisfy Python's requirements for multiple inheritance.
     Return value: New reference. */
-inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
+inline PyObject *make_object_base_type(PyTypeObject *metaclass, bool is_except=false) {
     constexpr auto *name = "pybind11_object";
     auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));

@@ -387,7 +387,12 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {

     auto type = &heap_type->ht_type;
     type->tp_name = name;
-    type->tp_base = type_incref(&PyBaseObject_Type);
+    if (is_except) {
+      type->tp_base = type_incref(reinterpret_cast<PyTypeObject*>(PyExc_Exception));
+    }
+    else {
+      type->tp_base = type_incref(&PyBaseObject_Type);
+    }
     type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
     type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;

@@ -404,7 +409,9 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
     setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
     PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);

-    assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
+    if (!is_except) {
+      assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
+    }
     return (PyObject *) heap_type;
 }

@@ -565,7 +572,8 @@ inline PyObject* make_new_python_type(const type_record &rec) {

     auto &internals = get_internals();
     auto bases = tuple(rec.bases);
-    auto base = (bases.size() == 0) ? internals.instance_base
+    auto base = (bases.size() == 0) ? (rec.is_except ? internals.exception_base
+                                                     : internals.instance_base)
</code>```

And then the final change, which is the inefficiency part. In Python, everything is a PyObject, but that is really only two fields (setup with the PyObject_HEAD macro) and the actual object struct may have a lot of extra fields. And having a very precise layout is important because python uses offsetof to seek into these things some times.  From the Python 2.7 source code (Include/pyerrord.h) you can see the struct that is used for base exceptions

```<code>typedef struct {
    PyObject_HEAD
    PyObject *dict;
    PyObject *args;
    PyObject *message;
} PyBaseExceptionObject;
</code>```

Any pybind11 type that extends PyExc_Exception has to have a instance struct that contains the same initial layout. And in pybind11 currently, the instance struct just has PyObject_HEAD. That means if you don't change the instance struct, this will all compile, but when python seeks into this object, it will do with the assumption that hose extra fields exist and then it will seek right off the end of viable memory and you'll get all sorts of fun segfaults. So this change adds those extra fields to every class_ in pybind11. It does not seem to break normal classes to have these extra fields and it definitely seems to make exceptions work correctly. If we broke the warranty before, we just tore it up and lit it on fire.

```<code>diff --git a/src/pybind11/include/pybind11/detail/common.h b/src/pybind11/include/pybind11/detail/common.h
index dd626793..b32e0c70 100644
--- a/src/pybind11/include/pybind11/detail/common.h
+++ b/src/pybind11/include/pybind11/detail/common.h
@@ -392,6 +392,10 @@ struct nonsimple_values_and_holders {
 /// The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
 struct instance {
     PyObject_HEAD
+    // Necessary to support exceptions.
+    PyObject *dict;
+    PyObject *args;
+    PyObject *message;
     /// Storage for pointers and holder; see simple_layout, below, for a description
</code>```

However, once these changes are all made, here is what you can do.
Bind in the class

```<code> auto PyBadData = py::class_< ::example::data::BadData>(module, "BadData", py::is_except(), py::dynamic_attr())
    .def(py::init<>())
    .def(py::init< std::string, std::string >())
    .def("__str__", &::example::data::BadData::toString)
    .def("getStack", &::example::data::BadData::getStack)
    .def_property("message", &::example::data::BadData::getMsg, &::example::data::BadData::setMsg)
    .def("getMsg", &::example::data::BadData::getMsg);
</code>```

And take a function in c++ that throws the exception

```<code>void raiseMe()
{
  throw ::example::data::BadData("this is an error", "");
}
</code>```

and bind that in too

```<code>module.def("raiseMe", &raiseMe, "A function throws");
</code>```

Add an exception translator to put the entire python type into the exception

```<code>    py::register_exception_translator([](std::exception_ptr p) {
      try {
          if (p) {
            std::rethrow_exception(p);
          }
      } catch (const ::example::data::BadData &e) {
        auto err = py::cast(e);
        auto errType = err.get_type().ptr();
        PyErr_SetObject(errType, err.ptr());
      }
    });
</code>```

And then you get all the things you could want!

```<code>>>> import example
>>> example.raiseMe()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
example.BadData: BadData(msg=this is an error, stack=)
</code>```

You can, of course, also instantiate and raise the exception from python as well

```<code>>>> import example
>>> raise example.BadData("this is my error", "no stack")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
example.BadData: BadData(msg=this is my error, stack=no stack)
</code>```

Body:
This appears to be a known limitation in pybind11. I read through all the docs, whatever bug reports seemed applicable, and everything I could find in the pybind11 gitter. I have a custom exception class in c++ that contains custom constructors and fields. A very basic example of such a class, trimmed for space is here:

class BadData : public std::exception
{
  public:
    // Constructors
    BadData()
      : msg(),
        stack(),
        _name("BadData")
    {}

    BadData(std::string _msg, std::string _stack)
      : msg(_msg),
        stack(_stack),
        _name("BadData")
    {}

    const std::string&
    getMsg() const
    {
      return msg;
    }

    void
    setMsg(const std::string& arg)
    {
      msg = arg;
    }

    // Member stack
    const std::string&
    getStack() const
    {
      return stack;
    }

    void
    setStack(const std::string& arg)
    {
      stack = arg;
    }
  private:
    std::string msg;
    std::string stack;
    std::string _name;

I currently have python binding code that binds this into python, but it is custom generated and we'd much rather use pybind11 due to its simplicity and compile speed.

The default mechanism for binding an exception into pybind11 would look like

py::register_exception<BadData>(module, "BadData");

That will create an automatic translation between the C++ exception and the python exception, with the what() value of the c++ exception translating into the message of the python exception. However, all the extra data from the c++ exception is lost and if you're trying to throw the exception in python and catch it in c++, you cannot throw it with any of the extra data.

You can bind extra data onto the python object using the attr and I even went somewhat down the path of trying to extend the pybind11:exception class to make it easier to add custom fields to exceptions.

  template <typename type>
  class exception11 : public ::py::exception<type>
  {
   public:
    exception11(::py::handle scope, const char *name, PyObject *base = PyExc_Exception)
      : ::py::exception<type>(scope, name, base)
    {}

    template <typename Func, typename... Extra>
    exception11 &def(const char *name_, Func&& f, const Extra&... extra) {
      ::py::cpp_function cf(::py::method_adaptor<type>(std::forward<Func>(f)),
                            ::py::name(name_),
                            ::py::is_method(*this),
                            ::py::sibling(getattr(*this, name_, ::py::none())),
                            extra...);
      this->attr(cf.name()) = cf;
      return *this;
    }
  };

This adds a def function to exceptions similar to what is done with class_. The naive approach to using this doesn't work

    exception11< ::example::data::BadData>(module, "BadData")
      .def("getStack", &::example::data::BadData::getStack);

Because there is no automatic translation between BadData in c++ and in python. You can try to work around this by binding in a lambda:

    .def("getStack", [](py::object& obj) {
      ::example::data::BadData *cls = obj.cast< ::example::data::BadData* >();
      return cls->getStack();
    });

The obj.cast there also fails because there is no automatic conversion. Basically, with no place to store the c++ instance, there isn't really a workable solution for this approach that I could find. In addition I couldn't find a way to bind in custom constructors at all, which made usability on python very weak.

The next attempt was based on a suggestion in the pybind11 that you could use the python exception type as a metaclass a normal class_ and have python recognize it as a valid exception. I tried a plethora of variations on this approach.

py::class_< ::example::data::BadData >(module, "BadData", py::dynamic_attr(), py::reinterpret_borrow<py::object>(PyExc_Exception))
py::class_< ::example::data::BadData >(module, "BadData", py::dynamic_attr(), py::cast(PyExc_Exception))
py::class_< ::example::data::BadData >(module, "BadData", py::dynamic_attr(), py::cast(PyExc_Exception->ob_type))
py::class_< ::example::data::BadData>(module, "BadData", py::metaclass((PyObject *) &PyExc_Exception->ob_type))

There were more that I don't have saved. But the overall results was either 1) It was ignored completely or 2) it failed to compile or 3) It compiled and then immediately segfaulted or ImportError'd when trying to make an instance. There might have been one that segfaulted on module import too. It all blurs together. Maybe there is some magic formula that would make such a thing work, but I was unable to find it. From my reading of the pybind11 internals, I do not believe that such a thing is actually possible. Inheriting from a raw python type does not seem to be something it is setup to let you do.

The last thing I tried seemed really clever. I made a python exception type

  static py::exception<::example::data::BadData> exc_BadData(module, "BadDataBase");

and then had my pybind11 class_ inherit from that.

  py::class_< ::example::data::BadData >(module, "BadData", py::dynamic_attr(), exc_BadData)

But that also segfaulted on import too. So I'm basically back to square one with this.

Body:
So I figured out a way to actually do this but it involves 1) doing some hacking of the pybind11 code itself and 2) introducing some size inefficiencies to the bound python types. From my point of view, the size issues are fairly immaterial. Yes it would be better to have everything perfectly sized but I'll take some extra bytes of memory for ease of use. Given this inefficiency, though, I'm not submitting this as a PR to the pybind11 project. While I think the trade-off is worth it, I doubt that making this the default for most people would be desired. It would be possible, I guess to hide this functionality behind a #define in c++ but that seems like it would be super messy long-term. There is probably a better long-term answer that would involve a degree of template meta-programming (parameterizing on the python container type for class_) that I'm just not up to.

I'm providing my changes here as diffs against the current master branch in git when this was written (hash a54eab92d265337996b8e4b4149d9176c2d428a6).

The basic approach was

Modify pybind11 to allow the specification of an exception base class for a class_ instance.

Modify pybind11's internal container to have the extra fields needed for a python exception type

Write a small amount of custom binding code to handle setting the error correctly in python.

For the first part, I added a new attribute to type_record to specify if a class is an exception and added the associated process_attribute call for parsing it.

diff --git a/src/pybind11/include/pybind11/attr.h b/src/pybind11/include/pybind11/attr.h
index 58390239..b5535558 100644
--- a/src/pybind11/include/pybind11/attr.h
+++ b/src/pybind11/include/pybind11/attr.h
@@ -73,6 +73,9 @@ struct module_local { const bool value; constexpr module_local(bool v = true) :
 /// Annotation to mark enums as an arithmetic type
 struct arithmetic { };

+// Annotation that marks a class as needing an exception base type.
+struct is_except {};
+
 /** \rst
     A call policy which places one or more guard variables (``Ts...``) around the function call.

@@ -211,7 +214,8 @@ struct function_record {
 struct type_record {
     PYBIND11_NOINLINE type_record()
         : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false),
 -          default_holder(true), module_local(false), is_final(false) { }
 -          default_holder(true), module_local(false), is_final(false),
 -          is_except(false) { }

     /// Handle to the parent scope
     handle scope;
@@ -267,6 +271,9 @@ struct type_record {
     /// Is the class inheritable from python classes?
     bool is_final : 1;

 -    // Does the class need an exception base type?
 -    bool is_except : 1;
 -      PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) {
         auto base_info = detail::get_type_info(base, false);
         if (!base_info) {
@@ -451,6 +458,11 @@ struct process_attribute<is_final> : process_attribute_default<is_final> {
     static void init(const is_final &, type_record *r) { r->is_final = true; }
 };

+template <>
+struct process_attribute<is_except> : process_attribute_default<is_except> {
 -    static void init(const is_except &, type_record *r) { r->is_except = true; }
+};

I modified the internals.h file to add a separate base class for exception types. I also added an extra bool argument to make_object_base_type.

diff --git a/src/pybind11/include/pybind11/detail/internals.h b/src/pybind11/include/pybind11/detail/internals.h
index 6224dfb2..d84df4f5 100644
--- a/src/pybind11/include/pybind11/detail/internals.h
+++ b/src/pybind11/include/pybind11/detail/internals.h
@@ -16,7 +16,7 @@ NAMESPACE_BEGIN(detail)
 // Forward declarations
 inline PyTypeObject *make_static_property_type();
 inline PyTypeObject *make_default_metaclass();
-inline PyObject *make_object_base_type(PyTypeObject *metaclass);
+inline PyObject *make_object_base_type(PyTypeObject *metaclass, bool is_except);

 // The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new
 // Thread Specific Storage (TSS) API.
@@ -107,6 +107,7 @@ struct internals {
     PyTypeObject *static_property_type;
     PyTypeObject *default_metaclass;
     PyObject *instance_base;
+    PyObject *exception_base;
 #if defined(WITH_THREAD)
     PYBIND11_TLS_KEY_INIT(tstate);
     PyInterpreterState *istate = nullptr;
@@ -292,7 +293,8 @@ PYBIND11_NOINLINE inline internals &get_internals() {
         internals_ptr->registered_exception_translators.push_front(&translate_exception);
         internals_ptr->static_property_type = make_static_property_type();
         internals_ptr->default_metaclass = make_default_metaclass();
-        internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass);
+        internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass, false);
+        internals_ptr->exception_base = make_object_base_type(internals_ptr->default_metaclass, true);

And then in class.h I added the necessary code to generate the exception base type. The first caveat is here. Since PyExc_Exception is a garbage collected type, I had to scope the assert call that checked the GC flag on the type. I have not currently seen any bad behavior from this change, but this is definitely voiding the warranty right here. I would highly, highly recommend always passing the py:dynamic_attr() flag to any classes you are using py:except on, since that turns on all the necessary bells and whistles to handle GC correctly (I think). A better solution might be to turn all those things on in make_object_base_type without having to invoke py::dynamic_attr.

diff --git a/src/pybind11/include/pybind11/detail/class.h b/src/pybind11/include/pybind11/detail/class.h
index a05edeb4..bbb9e772 100644
--- a/src/pybind11/include/pybind11/detail/class.h
+++ b/src/pybind11/include/pybind11/detail/class.h
@@ -368,7 +368,7 @@ extern "C" inline void pybind11_object_dealloc(PyObject *self) {
 /** Create the type which can be used as a common base for all classes.  This is
     needed in order to satisfy Python's requirements for multiple inheritance.
     Return value: New reference. */
-inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
+inline PyObject *make_object_base_type(PyTypeObject *metaclass, bool is_except=false) {
     constexpr auto *name = "pybind11_object";
     auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));

@@ -387,7 +387,12 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {

     auto type = &heap_type->ht_type;
     type->tp_name = name;
-    type->tp_base = type_incref(&PyBaseObject_Type);
+    if (is_except) {
+      type->tp_base = type_incref(reinterpret_cast<PyTypeObject*>(PyExc_Exception));
+    }
+    else {
+      type->tp_base = type_incref(&PyBaseObject_Type);
+    }
     type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
     type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;

@@ -404,7 +409,9 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
     setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
     PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);

-    assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
+    if (!is_except) {
+      assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
+    }
     return (PyObject *) heap_type;
 }

@@ -565,7 +572,8 @@ inline PyObject* make_new_python_type(const type_record &rec) {

     auto &internals = get_internals();
     auto bases = tuple(rec.bases);
-    auto base = (bases.size() == 0) ? internals.instance_base
+    auto base = (bases.size() == 0) ? (rec.is_except ? internals.exception_base
+                                                     : internals.instance_base)

And then the final change, which is the inefficiency part. In Python, everything is a PyObject, but that is really only two fields (setup with the PyObject_HEAD macro) and the actual object struct may have a lot of extra fields. And having a very precise layout is important because python uses offsetof to seek into these things some times.  From the Python 2.7 source code (Include/pyerrord.h) you can see the struct that is used for base exceptions

typedef struct {
    PyObject_HEAD
    PyObject *dict;
    PyObject *args;
    PyObject *message;
} PyBaseExceptionObject;

Any pybind11 type that extends PyExc_Exception has to have a instance struct that contains the same initial layout. And in pybind11 currently, the instance struct just has PyObject_HEAD. That means if you don't change the instance struct, this will all compile, but when python seeks into this object, it will do with the assumption that hose extra fields exist and then it will seek right off the end of viable memory and you'll get all sorts of fun segfaults. So this change adds those extra fields to every class_ in pybind11. It does not seem to break normal classes to have these extra fields and it definitely seems to make exceptions work correctly. If we broke the warranty before, we just tore it up and lit it on fire.

diff --git a/src/pybind11/include/pybind11/detail/common.h b/src/pybind11/include/pybind11/detail/common.h
index dd626793..b32e0c70 100644
--- a/src/pybind11/include/pybind11/detail/common.h
+++ b/src/pybind11/include/pybind11/detail/common.h
@@ -392,6 +392,10 @@ struct nonsimple_values_and_holders {
 /// The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
 struct instance {
     PyObject_HEAD
+    // Necessary to support exceptions.
+    PyObject *dict;
+    PyObject *args;
+    PyObject *message;
     /// Storage for pointers and holder; see simple_layout, below, for a description

However, once these changes are all made, here is what you can do.
Bind in the class

 auto PyBadData = py::class_< ::example::data::BadData>(module, "BadData", py::is_except(), py::dynamic_attr())
    .def(py::init<>())
    .def(py::init< std::string, std::string >())
    .def("__str__", &::example::data::BadData::toString)
    .def("getStack", &::example::data::BadData::getStack)
    .def_property("message", &::example::data::BadData::getMsg, &::example::data::BadData::setMsg)
    .def("getMsg", &::example::data::BadData::getMsg);

And take a function in c++ that throws the exception

void raiseMe()
{
  throw ::example::data::BadData("this is an error", "");
}

and bind that in too

module.def("raiseMe", &raiseMe, "A function throws");

Add an exception translator to put the entire python type into the exception

    py::register_exception_translator([](std::exception_ptr p) {
      try {
          if (p) {
            std::rethrow_exception(p);
          }
      } catch (const ::example::data::BadData &e) {
        auto err = py::cast(e);
        auto errType = err.get_type().ptr();
        PyErr_SetObject(errType, err.ptr());
      }
    });

And then you get all the things you could want!

>>> import example
>>> example.raiseMe()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
example.BadData: BadData(msg=this is an error, stack=)

You can, of course, also instantiate and raise the exception from python as well

>>> import example
>>> raise example.BadData("this is my error", "no stack")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
example.BadData: BadData(msg=this is my error, stack=no stack)


来源: stackexchange

问:
PHP mySQL JOIN and display (3 tables)
Hi I am new in php and for my study project trying to resolve some challenges

I have 3 tables "products", "categories", "product_to_categories"

products table:

```<code>product_id    product_name    product_price    product_img    product_link    date
1             item1_name      item1_price      item1_img      item1_link      item1_date
2             item2_name      item2_price      item2_img      item2_link      item2_date
</code>```

categories table:

```<code>category_id    category_name
1              category1_name
2              category2_name
3              category3_name
</code>```

and product_to_categories table:

```<code>relation_id    product_id    category_id
1              product1_id   category1_id
2              product1_id   category2_id
3              product2_id   category1_id
4              product2_id   category3_id
</code>```

I am trying to get 2 things:

write a list of all products together with the all categories names (no ids) they belong to.

list only those products that belong to the given categories

Unfortunately, I don't know how to approach this.

show_products.php:

<?php
$per_page=100;

    if(isset($_GET['page'])){
        $page = $_GET['page'];
    }else{
        $page=1;
    }
    $start_from = ($page-1) * $per_page;

    $get_products = "SELECT * FROM products ORDER BY 1 DESC LIMIT $start_from,$per_page";

    $run_products = mysqli_query($conn,$get_products);

    while($row_products=mysqli_fetch_array($run_products)){

        $pro_id = $row_products['product_id'];
        $pro_name = $row_products['product_name'];
        $pro_price = $row_products['product_price'];
        $pro_img = $row_products['product_img'];
        $pro_link = $row_products['product_link'];
        $pro_date = $row_products['date'];

        echo "

            $pro_id

            $pro_name
            ok. $pro_price zł
            $pro_link

            $pro_date

        ";
    }
?>```

答:
This is really about the SQL language and learning how to use it. Hopefully you have a GUI for your database such as PhpMyAdmin or HeidiSQL, because it is often easier to use that when writing SQL code.

Assuming you have these tables:

CREATE TABLE products
(
    product_id int PRIMARY KEY,
    product_name varChar(255)
);

CREATE TABLE categories
(
    category_id int PRIMARY KEY,
    category_name varChar(255)
);

CREATE TABLE product_to_categories
(
    product_id int,
    category_id int,
   FOREIGN KEY (product_id) REFERENCES products(product_id),
   FOREIGN KEY (category_id) REFERENCES categories(category_id)
);
```

<p>With this sample data:</p>

<pre class="lang-sql prettyprint-override"><code>INSERT INTO products VALUES (1, 'Product 1'), (2, 'Product 2');
INSERT INTO categories VALUES (1, 'Category 1'), (2, 'Category 2');
INSERT INTO product_to_categories VALUES (1,1), (2,1), (2,2);
</code>```

You can use this long-form query to get the data out:

SELECT
    P.*,
    C.category_name
FROM
    products P
JOIN
    product_to_categories PC
    ON
    P.product_id = PC.product_id
JOIN
    categories C
    ON
    PC.category_id = C.category_id
```

<p>Which returns</p>

```1   Product 1   Category 1
2   Product 2   Category 1
2   Product 2   Category 2
```

<p>However, because you are using the same columns names across your join table, you can also use the <code>NATURAL JOIN</code> command to produce the same</p>

<pre class="lang-sql prettyprint-override"><code>SELECT
    P.*,
    C.category_name
FROM
    products P
NATURAL JOIN
    categories C
NATURAL JOIN
    product_to_categories

</code>```

To get the product from a single category you could use:

SELECT
    *
FROM
    products P
NATURAL JOIN
    product_to_categories PC
WHERE
    PC.category_id = 2
```

<p>which returns</p>

```2   Product 2   2
```

ContentLicense: CC BY-SA 4.0

Title: PHP mySQL JOIN and display (3 tables)

Body:
Hi I am new in php and for my study project trying to resolve some challenges

I have 3 tables "products", "categories", "product_to_categories"

products table:

product_id    product_name    product_price    product_img    product_link    date
1             item1_name      item1_price      item1_img      item1_link      item1_date
2             item2_name      item2_price      item2_img      item2_link      item2_date

categories table:

category_id    category_name
1              category1_name
2              category2_name
3              category3_name

and product_to_categories table:

relation_id    product_id    category_id
1              product1_id   category1_id
2              product1_id   category2_id
3              product2_id   category1_id
4              product2_id   category3_id

I am trying to get 2 things:

write a list of all products together with the all categories names (no ids) they belong to.

list only those products that belong to the given categories

Unfortunately, I don't know how to approach this.

show_products.php:

<?php
$per_page=100;

    if(isset($_GET['page'])){
        $page = $_GET['page'];
    }else{
        $page=1;
    }
    $start_from = ($page-1) * $per_page;

    $get_products = "SELECT * FROM products ORDER BY 1 DESC LIMIT $start_from,$per_page";

    $run_products = mysqli_query($conn,$get_products);

    while($row_products=mysqli_fetch_array($run_products)){

        $pro_id = $row_products['product_id'];
        $pro_name = $row_products['product_name'];
        $pro_price = $row_products['product_price'];
        $pro_img = $row_products['product_img'];
        $pro_link = $row_products['product_link'];
        $pro_date = $row_products['date'];

        echo "

            $pro_id

            $pro_name
            ok. $pro_price zł
            $pro_link

            $pro_date

        ";
    }
?>

Body:
This is really about the SQL language and learning how to use it. Hopefully you have a GUI for your database such as PhpMyAdmin or HeidiSQL, because it is often easier to use that when writing SQL code.

Assuming you have these tables:

CREATE TABLE products
(
    product_id int PRIMARY KEY,
    product_name varChar(255)
);

CREATE TABLE categories
(
    category_id int PRIMARY KEY,
    category_name varChar(255)
);

CREATE TABLE product_to_categories
(
    product_id int,
    category_id int,
   FOREIGN KEY (product_id) REFERENCES products(product_id),
   FOREIGN KEY (category_id) REFERENCES categories(category_id)
);

With this sample data:

INSERT INTO products VALUES (1, 'Product 1'), (2, 'Product 2');
INSERT INTO categories VALUES (1, 'Category 1'), (2, 'Category 2');
INSERT INTO product_to_categories VALUES (1,1), (2,1), (2,2);

You can use this long-form query to get the data out:

SELECT
    P.*,
    C.category_name
FROM
    products P
JOIN
    product_to_categories PC
    ON
    P.product_id = PC.product_id
JOIN
    categories C
    ON
    PC.category_id = C.category_id

Which returns

1   Product 1   Category 1
2   Product 2   Category 1
2   Product 2   Category 2

However, because you are using the same columns names across your join table, you can also use the NATURAL JOIN command to produce the same

SELECT
    P.*,
    C.category_name
FROM
    products P
NATURAL JOIN
    categories C
NATURAL JOIN
    product_to_categories

To get the product from a single category you could use:

SELECT
    *
FROM
    products P
NATURAL JOIN
    product_to_categories PC
WHERE
    PC.category_id = 2

which returns

2   Product 2   2


问:
How to create a resizable Qt thumbnail preview?
I'm working on a basic image viewer/tagger that will need a thumbnail view to select an image. So far, I've used a QDockWidget enclosing a QScrollArea with a QHBoxLayout to contain a series of QLabels, each of which has its QPixMap set.

This seems very inelegant, and it only gets uglier to consider how I might implement auto-scaling of the thumbnails when the QDockWidget is resized. It's further complicated by the additional need to resize the thumbnails when the scroll bar appears and disappears.

There must be a better way to do this?

答:
I've ran into a similar problem when trying to animate resizing a qlabel with a qpixmap.  The method I found that worked best was to use a QWidget instead and re-implement the paintEvent function. Then your QWidget image will automatically be scaled if it's resized. Here is an example:

In my case I had the private variables in a private object called private_:

```<code>bool image_set_;
QImage image_;
QBrush paintbrush_;

void MyClass::paintEvent( QPaintEvent* event )
{
    // if the QWidget has an image set, then we use our custom painting.
    if( this->private_->image_set_ )
    {
        //I've made it so that my QWidget has a 1px white border
        this->private_->paintbrush_.setTextureImage( this->private_->image_.scaled(QSize( this->width() - 2, this->height() - 2 ) ) );
        QPainter painter( this );
        QRect temp_rect = QRect( 1, 1, this->width()-2, this->height() - 2 );
        painter.fillRect( this->rect(), Qt::white );
        painter.fillRect( temp_rect, this->private_->paintbrush_ );
    }
    else
    {
        QWidget::paintEvent( event );
    }
</code>```

}

ContentLicense: CC BY-SA 3.0

Body:
I've ran into a similar problem when trying to animate resizing a qlabel with a qpixmap.  The method I found that worked best was to use a QWidget instead and re-implement the paintEvent function. Then your QWidget image will automatically be scaled if it's resized. Here is an example:

In my case I had the private variables in a private object called private_:

bool image_set_;
QImage image_;
QBrush paintbrush_;

void MyClass::paintEvent( QPaintEvent* event )
{
    // if the QWidget has an image set, then we use our custom painting.
    if( this->private_->image_set_ )
    {
        //I've made it so that my QWidget has a 1px white border
        this->private_->paintbrush_.setTextureImage( this->private_->image_.scaled(QSize( this->width() - 2, this->height() - 2 ) ) );
        QPainter painter( this );
        QRect temp_rect = QRect( 1, 1, this->width()-2, this->height() - 2 );
        painter.fillRect( this->rect(), Qt::white );
        painter.fillRect( temp_rect, this->private_->paintbrush_ );
    }
    else
    {
        QWidget::paintEvent( event );
    }

}


问:
If I build a new docker image with the same name as existing ones, will the old ones be overwritten?
If I build a new docker image with the same name as existing ones, will the old ones be overwritten?</p>

答:
It is not possible to overwrite docker image that has the same name in the name:tag format, but you could remove it automatically after the build if you set label my-label for the image my-image:latest:

```<code>docker build --tag my-image:latest --label my-label ubuntu:latest
docker image prune --force --filter='label=my-label'
</code>```

Update: It is mandatory to follow strict sequence:

docker build ...

docker image prune ..

docker build ...

docker image prune ..

If you run:

docker build ...

docker image prune ..

docker build ...

docker build ...

docker image prune ..

you will get image from step 3. not removed.

Body:
It is not possible to overwrite docker image that has the same name in the name:tag format, but you could remove it automatically after the build if you set label my-label for the image my-image:latest:

docker build --tag my-image:latest --label my-label ubuntu:latest
docker image prune --force --filter='label=my-label'

Update: It is mandatory to follow strict sequence:

docker build ...

docker image prune ..

docker build ...

docker image prune ..

If you run:

docker build ...

docker image prune ..

docker build ...

docker build ...

docker image prune ..

you will get image from step 3. not removed.


问:
How to debug Google app engine Sever Error 500?
I deployed a Django web app to GAE, no errors during deployment.
But whenI try to open the website, it shows Server Error (500).

I tried to see some logs using gcloud app logs read, it only shows

```<code>2020-05-28 16:07:48 default[20200528t144758]  [2020-05-28 16:07:48 +0000] [1] [INFO] Handling signal: term
2020-05-28 16:07:48 default[20200528t144758]  [2020-05-28 16:07:48 +0000] [8] [INFO] Worker exiting (pid: 8)
2020-05-28 16:07:49 default[20200528t144758]  [2020-05-28 16:07:49 +0000] [1] [INFO] Shutting down: Master
2020-05-28 16:07:49 default[20200528t144758]  [2020-05-28 16:07:49 +0000] [1] [INFO] Handling signal: term
2020-05-28 16:07:49 default[20200528t144758]  [2020-05-28 16:07:49 +0000] [8] [INFO] Worker exiting (pid: 8)
2020-05-28 16:07:50 default[20200528t144758]  [2020-05-28 16:07:50 +0000] [1] [INFO] Shutting down: Master
2020-05-28 16:08:06 default[20200528t165550]  "GET /" 500
</code>```

The logs are not informative, so I wonder

1) if I could logon to the App Engine machine, and run my web application manually and see what's the error?

2) if not, what are the suggested ways to debug app engine errors?

答:
In App Engine Flex environment, you can debug your instance by enabling the debug mode and SSH to the instance.

You may also write app logs and structured logs to stdout and stderr so that you can look into your application logs and request logs via Logs Viewer or the command line. You may also consider using Cloud Profiler which is currently a free service to capture profiling data of you application so that you would get a better undrestanding of the characteristics of your application as it runs.

Cloud Debugger would also let you inspect the state of your application while running without adding logging statements. Note that Cloud Debugger is currently a free service as well.

Body:
I deployed a Django web app to GAE, no errors during deployment.
But whenI try to open the website, it shows Server Error (500).

I tried to see some logs using gcloud app logs read, it only shows

2020-05-28 16:07:48 default[20200528t144758]  [2020-05-28 16:07:48 +0000] [1] [INFO] Handling signal: term
2020-05-28 16:07:48 default[20200528t144758]  [2020-05-28 16:07:48 +0000] [8] [INFO] Worker exiting (pid: 8)
2020-05-28 16:07:49 default[20200528t144758]  [2020-05-28 16:07:49 +0000] [1] [INFO] Shutting down: Master
2020-05-28 16:07:49 default[20200528t144758]  [2020-05-28 16:07:49 +0000] [1] [INFO] Handling signal: term
2020-05-28 16:07:49 default[20200528t144758]  [2020-05-28 16:07:49 +0000] [8] [INFO] Worker exiting (pid: 8)
2020-05-28 16:07:50 default[20200528t144758]  [2020-05-28 16:07:50 +0000] [1] [INFO] Shutting down: Master
2020-05-28 16:08:06 default[20200528t165550]  "GET /" 500

The logs are not informative, so I wonder

1) if I could logon to the App Engine machine, and run my web application manually and see what's the error?

2) if not, what are the suggested ways to debug app engine errors?


问:
Need a regex command to find a value in an xml file
I have a large xml file that is within a logging statement.  I am using Splunk to extract values from the xml file.  I have to use regex to find these values because I cannot change the config files. I requested the change but it is pending...
This is an example of the xml file:

```<code><?xml version="1.0" encoding="UTF-8"?>
<tmsTrip xmlns="http://ground.fedex.com/schemas/linehaul/trip" xmlns:ns2="http://ground.fedex.com/schemas/linehaul/TMSCommon">
   <tripNumber>129271010</tripNumber>
   <tripLegNumber>1</tripLegNumber>
   <origin>
      <ns2:numberCode>5902</ns2:numberCode>
  ...many more fields....
   </origin>
   <destination>
      <ns2:numberCode>5087</ns2:numberCode>
   ...many more fields....
   </destination>
  ...many more fields....
   <purchasedCost>
      <purchasedCostTripSegment>
         <purchCostReference>2644025</purchCostReference>
         <carrier>BNSF</carrier>
         <vendorType>RAIL</vendorType>
         <carrierTrailerType>53PC</carrierTrailerType>
         <origin>
            <ns2:numberCode>4022</ns2:numberCode>
...many more fields....
         </origin>
         <destination>
            <ns2:numberCode>4040</ns2:numberCode>
...many more fields....
         <stopOff>
            <ns2:stopOffLocation>
               <ns2:numberCode>9996</ns2:numberCode>
...many more fields....
            </ns2:stopOffLocation>
         </stopOff>
         <schedDispatchDate>2020-05-27T05:00:00.000Z</schedDispatchDate>
...many more fields....
      </purchasedCostTripSegment>
      <purchasedCostTripSegment>
         <purchCostReference>2644025</purchCostReference>
         <carrier>NS</carrier>
         <vendorType>RAIL</vendorType>
         <carrierTrailerType>53PC</carrierTrailerType>
         <origin>
            <ns2:numberCode>4061</ns2:numberCode>
...many more fields....
         </origin>
         <destination>
            <ns2:numberCode>4040</ns2:numberCode>
...many more fields....
         </destination>
         <stopOff>
            <ns2:stopOffLocation>
               <ns2:numberCode>4040</ns2:numberCode>
 ...many more fields....
            </ns2:stopOffLocation>
         </stopOff>
         <schedDispatchDate>2020-05-27T05:00:00.000Z</schedDispatchDate>
 ...many more fields....
      </purchasedCostTripSegment>
   </purchasedCost>
</tmsTrip>
</code>```

I need to identify the ns2:numberCode for the origin and destination for each of the purchasedCostTripSegment.

I am doing this in Splunk so the regex might be particular to Splunk.
I am able to get find the origins and destinations if I use the function mvindex() and count the instance of the ns2:numberCode.  But then they are individual fields and do not display clearly in a table.
This is the regex command that will return the first origin of a PurchaseCostTripSegment:

```<code>| rex max_match=0 "\<ns2\:numberCode\>(?P<location>[^\<]+)" | eval Segment1_Origin =  mvindex(location, 7)
</code>```

I need a regex that will return all of the origins of the PurchaseCostTripSegments
I tried this:

```<code>| rex max_match=0 "\<purchasedCostTripSegment\>*\<origin\>*\<ns2\:numberCode\>(?P<Origin>[^\<]+)"
</code>```

It returned no value.
How can I write the regex to find all of the ns2:numberCode values that are in this section of the xml:

```<code>     <purchasedCostTripSegment>
             <purchCostReference>2644025</purchCostReference>
             <carrier>BNSF</carrier>
             <vendorType>RAIL</vendorType>
             <carrierTrailerType>53PC</carrierTrailerType>
             <origin>
                <ns2:numberCode>4022</ns2:numberCode>
</purchasedCostTripSegment>
      <purchasedCostTripSegment>
         <purchCostReference>2644025</purchCostReference>
         <carrier>NS</carrier>
         <vendorType>RAIL</vendorType>
         <carrierTrailerType>53PC</carrierTrailerType>
         <origin>
            <ns2:numberCode>4061</ns2:numberCode>
</purchasedCostTripSegment>
</code>```

In the above instance, I want to return values, 4022 and 4061,

答:
You can use this as a temporary workaround:

```<code>| rex max_match=0 "<purchasedCostTripSegment>[\s\S]*?<origin>\s*<ns2:numberCode>(?P<Origin>\d+)"
</code>```

See the regex demo.

Details

<purchasedCostTripSegment> - some literal text

[\s\S]*? - zero or more chars, as few as possible

<origin> - some text

\s* - 0+ whitespace chars

<ns2:numberCode>  - some text

(?P<Origin>\d+) - Named capturing group (for Splunk, it must be a named group):  1 or more digits.

Body:
I have a large xml file that is within a logging statement.  I am using Splunk to extract values from the xml file.  I have to use regex to find these values because I cannot change the config files. I requested the change but it is pending...
This is an example of the xml file:

<?xml version="1.0" encoding="UTF-8"?>
<tmsTrip xmlns="http://ground.fedex.com/schemas/linehaul/trip" xmlns:ns2="http://ground.fedex.com/schemas/linehaul/TMSCommon">
   <tripNumber>129271010</tripNumber>
   <tripLegNumber>1</tripLegNumber>
   <origin>
      <ns2:numberCode>5902</ns2:numberCode>
  ...many more fields....
   </origin>
   <destination>
      <ns2:numberCode>5087</ns2:numberCode>
   ...many more fields....
   </destination>
  ...many more fields....
   <purchasedCost>
      <purchasedCostTripSegment>
         <purchCostReference>2644025</purchCostReference>
         <carrier>BNSF</carrier>
         <vendorType>RAIL</vendorType>
         <carrierTrailerType>53PC</carrierTrailerType>
         <origin>
            <ns2:numberCode>4022</ns2:numberCode>
...many more fields....
         </origin>
         <destination>
            <ns2:numberCode>4040</ns2:numberCode>
...many more fields....
         <stopOff>
            <ns2:stopOffLocation>
               <ns2:numberCode>9996</ns2:numberCode>
...many more fields....
            </ns2:stopOffLocation>
         </stopOff>
         <schedDispatchDate>2020-05-27T05:00:00.000Z</schedDispatchDate>
...many more fields....
      </purchasedCostTripSegment>
      <purchasedCostTripSegment>
         <purchCostReference>2644025</purchCostReference>
         <carrier>NS</carrier>
         <vendorType>RAIL</vendorType>
         <carrierTrailerType>53PC</carrierTrailerType>
         <origin>
            <ns2:numberCode>4061</ns2:numberCode>
...many more fields....
         </origin>
         <destination>
            <ns2:numberCode>4040</ns2:numberCode>
...many more fields....
         </destination>
         <stopOff>
            <ns2:stopOffLocation>
               <ns2:numberCode>4040</ns2:numberCode>
 ...many more fields....
            </ns2:stopOffLocation>
         </stopOff>
         <schedDispatchDate>2020-05-27T05:00:00.000Z</schedDispatchDate>
 ...many more fields....
      </purchasedCostTripSegment>
   </purchasedCost>
</tmsTrip>

I need to identify the ns2:numberCode for the origin and destination for each of the purchasedCostTripSegment.

I am doing this in Splunk so the regex might be particular to Splunk.
I am able to get find the origins and destinations if I use the function mvindex() and count the instance of the ns2:numberCode.  But then they are individual fields and do not display clearly in a table.
This is the regex command that will return the first origin of a PurchaseCostTripSegment:

| rex max_match=0 "\<ns2\:numberCode\>(?P<location>[^\<]+)" | eval Segment1_Origin =  mvindex(location, 7)

I need a regex that will return all of the origins of the PurchaseCostTripSegments
I tried this:

| rex max_match=0 "\<purchasedCostTripSegment\>*\<origin\>*\<ns2\:numberCode\>(?P<Origin>[^\<]+)"

It returned no value.
How can I write the regex to find all of the ns2:numberCode values that are in this section of the xml:

     <purchasedCostTripSegment>
             <purchCostReference>2644025</purchCostReference>
             <carrier>BNSF</carrier>
             <vendorType>RAIL</vendorType>
             <carrierTrailerType>53PC</carrierTrailerType>
             <origin>
                <ns2:numberCode>4022</ns2:numberCode>
</purchasedCostTripSegment>
      <purchasedCostTripSegment>
         <purchCostReference>2644025</purchCostReference>
         <carrier>NS</carrier>
         <vendorType>RAIL</vendorType>
         <carrierTrailerType>53PC</carrierTrailerType>
         <origin>
            <ns2:numberCode>4061</ns2:numberCode>
</purchasedCostTripSegment>

In the above instance, I want to return values, 4022 and 4061,

Body:
You can use this as a temporary workaround:

| rex max_match=0 "<purchasedCostTripSegment>[\s\S]*?<origin>\s*<ns2:numberCode>(?P<Origin>\d+)"

See the regex demo.

Details

<purchasedCostTripSegment> - some literal text

[\s\S]*? - zero or more chars, as few as possible

<origin> - some text

\s* - 0+ whitespace chars

<ns2:numberCode>  - some text

(?P<Origin>\d+) - Named capturing group (for Splunk, it must be a named group):  1 or more digits.


问:
Gradle: Make a 3rd party jar available to local gradle repository
currently, I'm testing Gradle as an alternative to Maven. In my projects, there are some 3rd party jars, which aren't available in any (Maven) repositories. My problem is now, how could I manage it to install these jars into my local .gradle repository. (If it's possible, I don't want to use the local Maven repository, because Gradle should run independently.) At the moment, I get a lot of exceptions because of missing jars. In Maven, it's quite simple by running the install command. However, my Google search for something similar to the Maven install command wasn't successful. Has anybody an idea?</p>

答:
In short: deploy to repository manager. It can local, on company LAN.

An altogether different way of thinking about this type of problem, specially if it happens often, is to use a repository manager. There are some great open source options out there such as Artifactory, Nexus or Archiva.

Lets assume you have a jar file from some dubious origin that needs to be included in your build until you have the opportunity of refactoring it out. A repository manager would allow you to upload the file to your own repository as, for the sake of this example, dubious-origin-UNKNOWN.jar

Then your build.gradle would look something like this:

```<code>repositories {
    mavenRepo urls: "http://your.own.repository/url";
}

dependencies {
    compile "dubious:origin:UNKNOWN";
}
</code>```

There are a lot of other advantages to using a repository manager such as caching of remote artifacts, remove artifacts from scm, staging releases, more granular user permissions, and so forth.

On the down side, you would be adding a server which carries some maintenance overhead to keep your builds running.

Depends on the size if your project, I suppose.

Body:
In short: deploy to repository manager. It can local, on company LAN.

An altogether different way of thinking about this type of problem, specially if it happens often, is to use a repository manager. There are some great open source options out there such as Artifactory, Nexus or Archiva.

Lets assume you have a jar file from some dubious origin that needs to be included in your build until you have the opportunity of refactoring it out. A repository manager would allow you to upload the file to your own repository as, for the sake of this example, dubious-origin-UNKNOWN.jar

Then your build.gradle would look something like this:

repositories {
    mavenRepo urls: "http://your.own.repository/url";
}

dependencies {
    compile "dubious:origin:UNKNOWN";
}

There are a lot of other advantages to using a repository manager such as caching of remote artifacts, remove artifacts from scm, staging releases, more granular user permissions, and so forth.

On the down side, you would be adding a server which carries some maintenance overhead to keep your builds running.

Depends on the size if your project, I suppose.


问:
Cannot upload images captured from camera using react-native-image-crop-picker
I am using react-native-image-crop-picker for images,,,i can display and upload images taken from gallery by same plugin using same code....whereas i can display the images captured from camera using same plugin...but cannot upload them to server.

the error i get is

```<code>Error: Request failed with status code 500
    at createError (C:\Repos\247client\node_modules\axios\lib\core\createError.js:16)
    at settle (C:\Repos\247client\node_modules\axios\lib\core\settle.js:18)
    at XMLHttpRequest.handleLoad (C:\Repos\247client\node_modules\axios\lib\adapters\xhr.js:77)
    at XMLHttpRequest.dispatchEvent (C:\Repos\247client\node_modules\event-target-shim\lib\event-target.js:172)
    at XMLHttpRequest.setReadyState (C:\Repos\247client\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:572)
    at XMLHttpRequest.__didCompleteResponse (C:\Repos\247client\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:394)
    at C:\Repos\247client\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:507
    at RCTDeviceEventEmitter.emit (C:\Repos\247client\node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190)
    at MessageQueue.__callFunction (C:\Repos\247client\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:349)
    at C:\Repos\247client\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:106
</code>```

my image information is:

```<code>{uri: "file:///storage/emulated/0/Pictures/b9e9acdd-5807-43c8-9c6b-08af4f9e37ce.jpg", width: 3456, height: 4608, mime: "image/jpeg", size: 247313}
height: 4608
mime: "image/jpeg"
size: 247313
uri: "file:///storage/emulated/0/Pictures/b9e9acdd-5807-43c8-9c6b-08af4f9e37ce.jpg"
width: 3456
</code>```

i am using same upload code for both i.e uploading images taken from galery and camera.

the code is

export const uploadPrescription = async (
  id,
  address,
  contact_no,
  textPrescription,
  data
) => {
  const formData = new FormData();
  formData.append("user_id", id);
  formData.append("contact_no", contact_no);
  formData.append("medicine_description", textPrescription);
  formData.append("location", address);
  var i = 0;
  data.forEach(photo => {
    formData.append(`prescriptions[${i}]`, data[i]);
    i++;
  });
  console.log(formData);

  return (await makeRequest(urls.uploadByPrescription, "POST", formData)).data;

};

export const makeRequest = async (url, method, data) => {
  console.log("i am here");
  const headers = await getHeaders();
  console.log(headers);
  let response = await axios({
    method,
    url,
    data,
    headers: headers,
    timeout: url.includes("register") ? 30000 : 10000
  });
  console.log(response);
  var data = await response;
  return data;
};
```

答:
Please refer to the answer given in https://stackoverflow.com/a/62087375/6508545

I had used react-native-image-crop-picker npm package to get images from Camera Roll and Photo Gallery. And then upload it to AWS S3 Server.

I could upload it easily from IOS devices but not from Android.

The issue was URI or file path for images fetched in android and IOS devices were different.

Sample filePath variable data for

IOS device -

"/Users/Niveditha/Library/Developer/CoreSimulator/Devices/B41EB910-F22B-4236-8286-E6BA3EA75C70/data/Containers/Data/Application/B88777C6-6B10-4095-AB67-BB11E045C1DE/tmp/react-native-image-crop-picker/img.jpg"

Android device -

"file:///storage/emulated/0/Android/data/com.uploadcameraroll/files/Pictures/img.jpg"

Body:
I am using react-native-image-crop-picker for images,,,i can display and upload images taken from gallery by same plugin using same code....whereas i can display the images captured from camera using same plugin...but cannot upload them to server.

the error i get is

Error: Request failed with status code 500
    at createError (C:\Repos\247client\node_modules\axios\lib\core\createError.js:16)
    at settle (C:\Repos\247client\node_modules\axios\lib\core\settle.js:18)
    at XMLHttpRequest.handleLoad (C:\Repos\247client\node_modules\axios\lib\adapters\xhr.js:77)
    at XMLHttpRequest.dispatchEvent (C:\Repos\247client\node_modules\event-target-shim\lib\event-target.js:172)
    at XMLHttpRequest.setReadyState (C:\Repos\247client\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:572)
    at XMLHttpRequest.__didCompleteResponse (C:\Repos\247client\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:394)
    at C:\Repos\247client\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:507
    at RCTDeviceEventEmitter.emit (C:\Repos\247client\node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190)
    at MessageQueue.__callFunction (C:\Repos\247client\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:349)
    at C:\Repos\247client\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:106

my image information is:

{uri: "file:///storage/emulated/0/Pictures/b9e9acdd-5807-43c8-9c6b-08af4f9e37ce.jpg", width: 3456, height: 4608, mime: "image/jpeg", size: 247313}
height: 4608
mime: "image/jpeg"
size: 247313
uri: "file:///storage/emulated/0/Pictures/b9e9acdd-5807-43c8-9c6b-08af4f9e37ce.jpg"
width: 3456

i am using same upload code for both i.e uploading images taken from galery and camera.

the code is

export const uploadPrescription = async (
  id,
  address,
  contact_no,
  textPrescription,
  data
) => {
  const formData = new FormData();
  formData.append("user_id", id);
  formData.append("contact_no", contact_no);
  formData.append("medicine_description", textPrescription);
  formData.append("location", address);
  var i = 0;
  data.forEach(photo => {
    formData.append(`prescriptions[${i}]`, data[i]);
    i++;
  });
  console.log(formData);

  return (await makeRequest(urls.uploadByPrescription, "POST", formData)).data;

};

export const makeRequest = async (url, method, data) => {
  console.log("i am here");
  const headers = await getHeaders();
  console.log(headers);
  let response = await axios({
    method,
    url,
    data,
    headers: headers,
    timeout: url.includes("register") ? 30000 : 10000
  });
  console.log(response);
  var data = await response;
  return data;
};


问:
Parsing HTML Data in SAS
Hi I am an R user and this is my first time trying to parse HTML data in SAS. I was able to get the info in a text file and then read the file using the lines below but I cannot parse the data:

```<code>filename src "D:\testwebpage.txt";
proc http
 method="GET"
 url="xxxxx/yyyyyy"
 out=src;
run;

data rep;
infile src length=len lrecl=32767;
input line $varying32767. len;
line = strip(line);
if len>0;
run;
</code>```

The data in "rep" look like:

```<code><html><body style='font-family:arial'><style type="text/css">tr.head {

background-color: #FFFFFF;

font-weight: bold;

}

tr.even {background-color: #EEEEEE}

tr.odd {background-color: #FFFFFF}</style><table><tr class="head"><td>station_no</td><td>ts_path</td><td>parametertype_name</td></tr>

<tr class="even"><td>23349</td><td>17/23349/path1</td><td>WL</td></tr>

<tr class="odd"><td>23349</td><td>17/23349/path2</td><td>WL</td></tr>

<tr class="even"><td>23349</td><td>17/23349/path3</td><td>WL</td></tr>

<tr class="odd"><td>23349</td><td>17/23349/path4</td><td>WL</td></tr>

<tr><th colspan="3"><img src="images/path.gif" align="right"/>

</th></tr>

</table>

</body></html>
</code>```

I need to parse "rep" and get a dataset with station_no (23349 in this case), ts_path (17/23349/path1....), and parametertype_name (WL). Could someone please help me do this? Like I said I don't use SAS and know very little about it.

Thanks.

答:
HTML can be a little tricky to parse depending on what you're trying to read. I only have a basic understanding of HTML, but I could identify some patterns to get it read in. This is the way I approached it:

Remove HTML tags and replace them with spaces using PERL Regular Expressions

Check for the table header row in HTML by seeing if it contains a keyword. In this case, I used station_no.

If the previous row was a header row and the line is not missing after removing HTML tags, then we're currently in the data row

Grab each of the values that we need using scan()

If you want to see how the logic works, remove the keep and if() statements to output everything line-by-line.

```<code>data rep;
    infile src length=len lrecl=32767;
    input line $varying32767. len;

    line        = strip(line);

    /* PERL regular expression to remove HTML tags.
       compbl() changes multiple spaces into one space
    */
    line_notags = compbl(prxchange('s/<[^>]*>/ /', -1, line));

    if(len>0);

    /* Do not reset these values at the start of each row */
    retain flag_table_header lag_flag_table_header;

    /* Set a flag that we've encountered the table header */
    if(index(line, 'station_no')) then flag_table_header = 1;

    /* Check if we are currently under the table header */
    lag_flag_table_header = lag(flag_table_header);

    /* If we're under the table header and the line isn't missing after removing tags,
       grab what we need and output. Do not output if anything else is missing.
    */
    if(lag_flag_table_header AND NOT missing(line_notags) ) then do;
        station_no         = scan(line_notags, 1, ' ');
        ts_path            = scan(line_notags, 2, ' ');
        parametertype_name = scan(line_notags, 3, ' ');

        output;
    end;

    keep station_no ts_path parametertype_name;
run;
</code>```

The biggest thing to remember with SAS data step language is that it's an inherently looping language. The program you write performs every action for each row of the dataset. Each time the program hits the run statement, SAS goes to a new row and resets all of your columns to missing to prepare for another row read. You can allow a column to persist between reads with a retain statement.

Get yourself familiar with the Program Data Vector (PDV) concept. It will really help you understand the data step language and how it processes things. There's a really great communities post here on it. Once you've mastered it, you can bounce between SQL, data steps, and procs to write flexible programs that can process monstrous datasets.

Title: Parsing HTML Data in SAS

Body:
Hi I am an R user and this is my first time trying to parse HTML data in SAS. I was able to get the info in a text file and then read the file using the lines below but I cannot parse the data:

filename src "D:\testwebpage.txt";
proc http
 method="GET"
 url="xxxxx/yyyyyy"
 out=src;
run;

data rep;
infile src length=len lrecl=32767;
input line $varying32767. len;
line = strip(line);
if len>0;
run;

The data in "rep" look like:

station_no	ts_path	parametertype_name

23349	17/23349/path1	WL

23349	17/23349/path2	WL

23349	17/23349/path3	WL

23349	17/23349/path4	WL

I need to parse "rep" and get a dataset with station_no (23349 in this case), ts_path (17/23349/path1....), and parametertype_name (WL). Could someone please help me do this? Like I said I don't use SAS and know very little about it.

Thanks.

Body:
HTML can be a little tricky to parse depending on what you're trying to read. I only have a basic understanding of HTML, but I could identify some patterns to get it read in. This is the way I approached it:

Remove HTML tags and replace them with spaces using PERL Regular Expressions

Check for the table header row in HTML by seeing if it contains a keyword. In this case, I used station_no.

If the previous row was a header row and the line is not missing after removing HTML tags, then we're currently in the data row

Grab each of the values that we need using scan()

If you want to see how the logic works, remove the keep and if() statements to output everything line-by-line.

data rep;
    infile src length=len lrecl=32767;
    input line $varying32767. len;

    line        = strip(line);

    /* PERL regular expression to remove HTML tags.
       compbl() changes multiple spaces into one space
    */
    line_notags = compbl(prxchange('s/<[^>]*>/ /', -1, line));

    if(len>0);

    /* Do not reset these values at the start of each row */
    retain flag_table_header lag_flag_table_header;

    /* Set a flag that we've encountered the table header */
    if(index(line, 'station_no')) then flag_table_header = 1;

    /* Check if we are currently under the table header */
    lag_flag_table_header = lag(flag_table_header);

    /* If we're under the table header and the line isn't missing after removing tags,
       grab what we need and output. Do not output if anything else is missing.
    */
    if(lag_flag_table_header AND NOT missing(line_notags) ) then do;
        station_no         = scan(line_notags, 1, ' ');
        ts_path            = scan(line_notags, 2, ' ');
        parametertype_name = scan(line_notags, 3, ' ');

        output;
    end;

    keep station_no ts_path parametertype_name;
run;

The biggest thing to remember with SAS data step language is that it's an inherently looping language. The program you write performs every action for each row of the dataset. Each time the program hits the run statement, SAS goes to a new row and resets all of your columns to missing to prepare for another row read. You can allow a column to persist between reads with a retain statement.

Get yourself familiar with the Program Data Vector (PDV) concept. It will really help you understand the data step language and how it processes things. There's a really great communities post here on it. Once you've mastered it, you can bounce between SQL, data steps, and procs to write flexible programs that can process monstrous datasets.


问:
How to use margin on checkboxes so they will be on each other
I have the following code:

```<code>    <LinearLayout
        android:id="@+id/availability_sunday"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tile_divider"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text_sunday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sunday:" />

        <CheckBox
            android:id="@+id/checkbox_sunday_morning"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:layout_width="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp" />

        <TextView
            android:id="@+id/text_sunday_morning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Morning"/>

        <CheckBox
            android:id="@+id/checkbox_sunday_evening"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:layout_width="wrap_content" />

        <TextView
            android:id="@+id/text_sunday_evening"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Evening"/>
    </LinearLayout>
</code>```

In a RelativeLayout. I would like the layout to be:

```<code>Sunday     [] Morning [] Evening
Monday     [] Morning [] Evening
...
Thursday   [] Morning [] Evening
</code>```

But If I add the same block for monday I will get:

Because the work android:layout_marginStart and android:layout_marginLeft. I want the checkbox to be above each other. How can I do it?

答:
I do not know a lot about Android Studio but try this:

-- Create a TableLayout

-- Put all your data (textView etc) inside it.

The goal is to keep your data symmetrical (i think).

Also check out this

Body:
I have the following code:

    <LinearLayout
        android:id="@+id/availability_sunday"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tile_divider"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text_sunday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sunday:" />

        <CheckBox
            android:id="@+id/checkbox_sunday_morning"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:layout_width="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp" />

        <TextView
            android:id="@+id/text_sunday_morning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Morning"/>

        <CheckBox
            android:id="@+id/checkbox_sunday_evening"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:layout_width="wrap_content" />

        <TextView
            android:id="@+id/text_sunday_evening"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Evening"/>
    </LinearLayout>

In a RelativeLayout. I would like the layout to be:

Sunday     [] Morning [] Evening
Monday     [] Morning [] Evening
...
Thursday   [] Morning [] Evening

But If I add the same block for monday I will get:

Because the work android:layout_marginStart and android:layout_marginLeft. I want the checkbox to be above each other. How can I do it?


问:
How to create XML with <![CDATA[<?xml in PL SQL
Let's suppose that I want to generate a part of the XML extracting data from a table and put this part inside the [CDATA element.

For example, let's suppose that I generate this XML

```<code>select    xmlelement("TAG1",
          xmlelement("TAG2",col2),
          xmlelement("TAG3",
          xmlelement("TAG4",col4)))
from MYTABLE
</code>```

where col2 and col4 are columns of MYTABLE

so as to get the follow XML:

```<code><TAG1>
 <TAG2> 10 </TAG2>
 <TAG3>
  <TAG4> England </TAG4>
 </TAG3>
</TAG1>
</code>```

But now let's suppose I want to get something like this:

```<code><TAG1>
 <TAG2> 10 </TAG2>
 <![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <TAG3>
  <TAG4> England </TAG4>
 </TAG3> ]]>
</TAG1>
</code>```

how can I do it?
What I mean is even in this case I want the part inside the CDATA to be created not with constant long string but alway extracting data from the table and I want this section closed after TAG3

答:
Might not be the best solution,It's a hack with REPLACE.

For a fixed String :

```<code>select   REPLACE(REPLACE(xmlelement("TAG1",
          xmlelement("TAG2",col2),
           xmlelement("TAGX",
          xmlelement("TAG3",
          xmlelement("TAG4",col4)))),'/TAGX',']]'),'TAGX','![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="yes"?')
from MYTABLE;
</code>```

Output:

```<code><?xml version="1.0" encoding="UTF-8"?>
<TAG1>
   <TAG2>10</TAG2>
   <![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="yes"?><TAG3><TAG4>England</TAG4></TAG3>]]>
</TAG1>
</code>```

Now use column value from CDATA.MYTABLE.Col5 Column.The value is StringtoInserttoCDATA.

```<code>select   REPLACE(REPLACE(xmlelement("TAG1",
          xmlelement("TAG2",col2),
           xmlelement("TAGX",
          xmlelement("TAG3",
          xmlelement("TAG4",col4)))),'</TAGX',']]'),'TAGX','![CDATA[<?xml '||col5)
as xmldataStringInsertedfromcol
from MYTABLE;
</code>```

Output :

```<code><?xml version="1.0" encoding="UTF-8"?>
<TAG1>
   <TAG2>10</TAG2>
   <![CDATA[<?xml StringtoInserttoCDATA><TAG3><TAG4>England</TAG4></TAG3>]]>
</TAG1>
</code>```

Complete solution demo can be found in dbfiddlelink https://dbfiddle.uk/?rdbms=oracle_18&fiddle=54dc5d9b1d4c8b2b08912925da59837c

Body:
Let's suppose that I want to generate a part of the XML extracting data from a table and put this part inside the [CDATA element.

For example, let's suppose that I generate this XML

select    xmlelement("TAG1",
          xmlelement("TAG2",col2),
          xmlelement("TAG3",
          xmlelement("TAG4",col4)))
from MYTABLE

where col2 and col4 are columns of MYTABLE

so as to get the follow XML:

<TAG1>
 <TAG2> 10 </TAG2>
 <TAG3>
  <TAG4> England </TAG4>
 </TAG3>
</TAG1>

But now let's suppose I want to get something like this:

<TAG1>
 <TAG2> 10 </TAG2>
 <![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <TAG3>
  <TAG4> England </TAG4>
 </TAG3> ]]>
</TAG1>

how can I do it?
What I mean is even in this case I want the part inside the CDATA to be created not with constant long string but alway extracting data from the table and I want this section closed after TAG3

Body:
Might not be the best solution,It's a hack with REPLACE.

For a fixed String :

select   REPLACE(REPLACE(xmlelement("TAG1",
          xmlelement("TAG2",col2),
           xmlelement("TAGX",
          xmlelement("TAG3",
          xmlelement("TAG4",col4)))),'/TAGX',']]'),'TAGX','![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="yes"?')
from MYTABLE;

Output:

<?xml version="1.0" encoding="UTF-8"?>
<TAG1>
   <TAG2>10</TAG2>
   <![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="yes"?><TAG3><TAG4>England</TAG4></TAG3>]]>
</TAG1>

Now use column value from CDATA.MYTABLE.Col5 Column.The value is StringtoInserttoCDATA.

select   REPLACE(REPLACE(xmlelement("TAG1",
          xmlelement("TAG2",col2),
           xmlelement("TAGX",
          xmlelement("TAG3",
          xmlelement("TAG4",col4)))),'</TAGX',']]'),'TAGX','![CDATA[<?xml '||col5)
as xmldataStringInsertedfromcol
from MYTABLE;

Output :

<?xml version="1.0" encoding="UTF-8"?>
<TAG1>
   <TAG2>10</TAG2>
   <![CDATA[<?xml StringtoInserttoCDATA><TAG3><TAG4>England</TAG4></TAG3>]]>
</TAG1>

Complete solution demo can be found in dbfiddlelink https://dbfiddle.uk/?rdbms=oracle_18&fiddle=54dc5d9b1d4c8b2b08912925da59837c


问:
how to calculate height of Iframe?
I want to insert Iframe between the header component and footer component, how shall I calculate the height of that Iframe screen?
Note: (the content of header and footer may change)</p>

```<code>    <div dangerouslySetInnerHTML={{
         __html: `<iframe
          class="iFrame-screen"
          src="${dynamic-website-inserted-here}" />`
          }}
         />
    </div>

css:
.iFrame-screen {
    position: absolute;
    right: 0;
    top: -100px;
    z-index: 1;
    height: 110%;
    width: 100%;
    border: none;
    overflow-y: auto;
    overflow-x: hidden;
}
</code>```

答:
```<code>function calcHeight()
{
  //find the height of the internal page
  var the_height = document.getElementsByTagName('iframe')[0].contentWindow.document.body.scrollHeight;

   console.log('Iframe Height is ' + the_height + ' pixels');
};
</code>```

Title: how to calculate height of Iframe?

Body:
I want to insert Iframe between the header component and footer component, how shall I calculate the height of that Iframe screen?
Note: (the content of header and footer may change)

    <div dangerouslySetInnerHTML={{
         __html: ``
          }}
         />

css:
.iFrame-screen {
    position: absolute;
    right: 0;
    top: -100px;
    z-index: 1;
    height: 110%;
    width: 100%;
    border: none;
    overflow-y: auto;
    overflow-x: hidden;
}


问:
Angular 9 - Backend with Firebase and Cloud Firestore
For the front end (Angular) have a server and the back end is in Firebase i am using Cloud Firestore.

I tried database realtime, and everything ok, but I want to try Cloud Firestore and its collections.

The problem is the following:

This is the component:

```<code> items: Array<any>;

  constructor(private aboutUsService: AboutUsService) { }

  ngOnInit() {
    this.getData();
  }

  getData() {
    this.aboutUsService.getContent()
    .subscribe(data => {
      this.items = data;
      console.log('items: ',  this.items);
    }, // Bind to view
    err => {
      console.log(err);
    });
  }
</code>```

This is the service:

```<code>  getContent() {
    return this.db.collection('aboutus').snapshotChanges();
  }
</code>```

At the moment I have a collection in Cloud Firestore:

Rules Cloud Firestore:

```<code>service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}
</code>```

The service responds and returns the following array with an element (this is the content of this.items):

```<code>[{…}]
0:
payload: {type: "added", doc: n, oldIndex: -1, newIndex: 0}
type: "added"
</code>```

These are the headers in the Chrome network tab:

```<code>Request URL: http://localhost:4200/aboutus
Request Method: GET
Status Code: 304 Not Modified
Remote Address: 127.0.0.1:4200
</code>```

But I am not able to find the data of the collection to display it in the front-end (variable this.items).

If I add elements to the collection "aboutus" in the response the array, it corresponds to the elements of the collection, but I don't get to visualize the data.

What could be the problem? Thank you

答:
As what I'd mentioned in my comment from over 2 weeks ago (on 15 May):

You should be using AngularFirestoreCollection.valueChanges, which returns an Observable of the value of the collection while AngularFirestoreCollection.snapshotChanges shows additional metadata on snapshots. For more info, consider viewing the documentation: https://github.com/angular/angularfire/blob/master/docs/firestore/collections.md

As such, you should replace the snapshotChanges call with valueChanges:

getContent() {
  return this.db.collection('aboutus').valueChanges();
}
```

Body:
For the front end (Angular) have a server and the back end is in Firebase i am using Cloud Firestore.

I tried database realtime, and everything ok, but I want to try Cloud Firestore and its collections.

The problem is the following:

This is the component:

 items: Array<any>;

  constructor(private aboutUsService: AboutUsService) { }

  ngOnInit() {
    this.getData();
  }

  getData() {
    this.aboutUsService.getContent()
    .subscribe(data => {
      this.items = data;
      console.log('items: ',  this.items);
    }, // Bind to view
    err => {
      console.log(err);
    });
  }

This is the service:

  getContent() {
    return this.db.collection('aboutus').snapshotChanges();
  }

At the moment I have a collection in Cloud Firestore:

Rules Cloud Firestore:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

The service responds and returns the following array with an element (this is the content of this.items):

[{…}]
0:
payload: {type: "added", doc: n, oldIndex: -1, newIndex: 0}
type: "added"

These are the headers in the Chrome network tab:

Request URL: http://localhost:4200/aboutus
Request Method: GET
Status Code: 304 Not Modified
Remote Address: 127.0.0.1:4200

But I am not able to find the data of the collection to display it in the front-end (variable this.items).

If I add elements to the collection "aboutus" in the response the array, it corresponds to the elements of the collection, but I don't get to visualize the data.

What could be the problem? Thank you


问:
DateTime Utility for ASP.net
I was wondering if anyone could suggest a utility library that has useful functions for handling dates in ASP.NET easily taking away some of the leg work you normally have to do when handling dates?

Subsonic Sugar has some really nice functions:

http://subsonichelp.com/html/1413bafa-b5aa-99aa-0478-10875abe82ec.htm
http://subsonicproject.googlecode.com/svn/trunk/SubSonic/Sugar/

Is there anything better out there?

I was wanting to work out the start(mon) and end(sun) dates of the last 5 weeks.

I was thinking something like this:

```<code> DateTime Now = DateTime.Now;

    while(Now.DayOfWeek != DayOfWeek.Monday)
    {
        Now.AddDays(-1);
    }

    for(int i=0; i<5;i++)
    {
        AddToDatesList(Now, Now.AddDays(7));
        Now.AddDays(-7);
    }
</code>```

but this seems crappy? Plus this is not exactly what i want because i need the time of that start date to be 00:00:00 and the time of the end date to be 23:59:59

答: CSLA has a useful helper class called SmartDate that addresses quite a lot of the problems when using dates in real applications. As far as I can recall it's coupled to the rest of the framework.</p>

OwnerDisplayName: Dan

Title: DateTime Utility for ASP.net

Body:
I was wondering if anyone could suggest a utility library that has useful functions for handling dates in ASP.NET easily taking away some of the leg work you normally have to do when handling dates?

Subsonic Sugar has some really nice functions:

http://subsonichelp.com/html/1413bafa-b5aa-99aa-0478-10875abe82ec.htm
http://subsonicproject.googlecode.com/svn/trunk/SubSonic/Sugar/

Is there anything better out there?

I was wanting to work out the start(mon) and end(sun) dates of the last 5 weeks.

I was thinking something like this:

 DateTime Now = DateTime.Now;

    while(Now.DayOfWeek != DayOfWeek.Monday)
    {
        Now.AddDays(-1);
    }

    for(int i=0; i<5;i++)
    {
        AddToDatesList(Now, Now.AddDays(7));
        Now.AddDays(-7);
    }

but this seems crappy? Plus this is not exactly what i want because i need the time of that start date to be 00:00:00 and the time of the end date to be 23:59:59

LastEditorDisplayName: Dan

OwnerDisplayName: Rik Garner

ContentLicense: CC BY-SA 2.5


问:
Why did I get this item wrong on my homework?
I got this problem wrong on my homework, and I can't figure out why:

```<code>procedure Main is
    X: Integer;
    procedure Sub1 is
       X: Integer;
    begin -- of Sub1
       Put(X);
    end; -- of Sub1
    procedure Sub2 is
       X: Integer;
    begin -- of Sub2
       X:=5;
       Sub1;
    end; -- of Sub2
begin -- of Main
    X:=12;
    Sub2;
end; -- of Main
</code>```

The Question is:
Assume the following Ada program was compiled and execute using static-scoping rules. What value of X is printed in procedure of Sub1? What if it was under dyncamic scoping rules?

I got 12 and 5 respectively, but this was marked incorrect. Why?

答: Hint: under static scoping it's uninitialized.  Can you figure out why?</p>

OwnerDisplayName: kylex

Body:
I got this problem wrong on my homework, and I can't figure out why:

procedure Main is
    X: Integer;
    procedure Sub1 is
       X: Integer;
    begin -- of Sub1
       Put(X);
    end; -- of Sub1
    procedure Sub2 is
       X: Integer;
    begin -- of Sub2
       X:=5;
       Sub1;
    end; -- of Sub2
begin -- of Main
    X:=12;
    Sub2;
end; -- of Main

The Question is:
Assume the following Ada program was compiled and execute using static-scoping rules. What value of X is printed in procedure of Sub1? What if it was under dyncamic scoping rules?

I got 12 and 5 respectively, but this was marked incorrect. Why?

OwnerDisplayName: MarkusQ


问:
Scraping webpage using BeautifulSoup
I am attempting to scrape this site: https://www.senate.gov/general/contact_information/senators_cfm.cfm

My Code:

```<code>import requests
from bs4 import BeautifulSoup

URL = 'https://www.senate.gov/general/contact_information/senators_cfm.cfm'
page = requests.get(URL)

soup = BeautifulSoup(page.content, 'html.parser')

print(soup)
</code>```

The issue is that it's not actually going to the site. The HTML that I get in my soup var is not at all what the HTML is in the correct webpage.

答:
DUPLICATE HTTP 503 Error while using python requests module

try that:

```<code>import requests
from bs4 import BeautifulSoup

URL = 'https://www.senate.gov/general/contact_information/senators_cfm.cfm'

page = requests.post(URL, headers=headers)

soup = BeautifulSoup(page.content, 'html.parser')
print(soup)
</code>```

Title: Scraping webpage using BeautifulSoup

Body:
I am attempting to scrape this site: https://www.senate.gov/general/contact_information/senators_cfm.cfm

My Code:

import requests
from bs4 import BeautifulSoup

URL = 'https://www.senate.gov/general/contact_information/senators_cfm.cfm'
page = requests.get(URL)

soup = BeautifulSoup(page.content, 'html.parser')

print(soup)

The issue is that it's not actually going to the site. The HTML that I get in my soup var is not at all what the HTML is in the correct webpage.

Body:
DUPLICATE HTTP 503 Error while using python requests module

try that:

import requests
from bs4 import BeautifulSoup

URL = 'https://www.senate.gov/general/contact_information/senators_cfm.cfm'

page = requests.post(URL, headers=headers)

soup = BeautifulSoup(page.content, 'html.parser')
print(soup)