How to apply a decorator with arguments to an imported function - import

This similar to a previous question but not quite.
Like the other question, I want to apply a decorator to an imported function, but in my case, my decorator requires arguments
Working
from flask_restx import Namespace
from . import exceptions as e
api = Namespace('v2', 'API Version 2' )
#api.errorhandler(e.MissingPrompt)
def handle_bad_requests(error):
'''Namespace error handler'''
logger.warning(error.log)
return({'message': error.specific}, error.code)
But I want to move handle_bad_requests() to its own file
so I want it like
from flask_restx import Namespace
from . import exceptions as e
api = Namespace('v2', 'API Version 2' )
#api.errorhandler(e.MissingPrompt)
e.handle_bad_requests # ??
I tried like the other answers here suggested like this
handle_bad_requests = api.errorhandler(e.handle_bad_requests, e.MissingPrompt)
But that just gives me an error I am sending too many arguments

I just discovered the answer nested within the comments of a previous answer
handle_bad_requests = api.errorhandler(e.MissingPrompt)(e.handle_bad_requests)
And that seems to work fine.
The double parentheses would never have occured to me

Related

Nanopb strip package option raises error at import

I am struggling with nanopb to get the enum of another package which has * mangle_names:M_STRIP_PACKAGE in .options file. Here is a way to reproduce the problem easily :
I have a root_folder containing folder_A and folder_B.
In folder_A, I have file_A.proto and file_A.options :
file_A.proto:
syntax = "proto2";
package folder_A;
enum my_enum {
ENUM_0 = 0;
ENUM_1 = 1;
ENUM_2 = 2;
}
file_A.options:
* mangle_names:M_STRIP_PACKAGE
In folder_B, I have file_B.proto :
syntax = "proto2";
package folder_B;
import "folder_A/file_A.proto";
message dummy {
required folder_A.my_enum value = 1;
}
I try to generate proto file with the following command:
nanopb_generator.py -D . -I . -I .\folder_B\ .\folder_A\file_A.proto .\folder_B\file_B.proto
The script fails with error Exception: Could not find enum type folder_A_my_enum while generating default values for folder_B_dummy.
But if I remove the file_A.options, it works correctly.
Also if I replace the enum by a message, it works correctly even with file_A.options.
My question is : do you know if it is possible to use the option * mangle_names:M_STRIP_PACKAGE and import enum at the same time ?
I use nanopb-0.4.5.
Thank you !
Currently M_STRIP_PACKAGE does not work when there are imports from another package. I have added the problem to the issue tracker: https://github.com/nanopb/nanopb/issues/783
Imports with name mangling seem to work fine, as long as all imported files belong to the same package and have same name mangling settings.
It is also questionable whether stripping the package name from types is a good idea when you are using multiple package names. It sounds like a recipe for name collisions.

Unable to get IntelliJ's (Velocity) Code Template to work correctly with conditional variable assignment

I am attempting to improve a Scala Companion Object plus Case Class code generating template in IntelliJ 2021.3 (Build #IU-213.5744.223, built on November 27, 2021).
I would like to allow the user to be able to leave some of the input parameters unspecified (empty) and then have the template generate sane defaults. This is because the generated code can then be refactored by the client to something more pertinent later.
My question is this: Is there another way to approach achieving my goal than the way I show below using the Velocity #macro (which I got from here, and then doesn't appear to work anyway)?
With the current approach using the #marco, when I invoke it...
With:
NAME = "Longitude"
PROPERTY_1_VARIABLE = "value"
PROPERTY_1_TYPE - "Double"
The Scala code target:
final case class Longitude(value: Double)
With:
NAME = "Longitude"
PROPERTY_1_VARIABLE = ""
PROPERTY_1_TYPE - ""
The Scala code target using defaults for 2 and 3:
final case class Longitude(property1: Property1Type)
And here's my attempt to make it work with the IntelliJ (Velocity) Code Template:
#macro(condOp $check, $default)
#if ($check == "")
$default
#else
$check
#end
#end
#set (${PROPERTY_1_VARIABLE} = "#condOp(${PROPERTY_1_VARIABLE}, 'property1')")
#set (${PROPERTY_1_TYPE} = "#condOp(${PROPERTY_1_TYPE}, 'Property1Type')")
#if ((${PACKAGE_NAME} && ${PACKAGE_NAME} != ""))package ${PACKAGE_NAME} #end
final case class ${NAME} (${PROPERTY_1_VARIABLE}: ${PROPERTY_1_TYPE})
First, the IntelliJ popup box for entering the parameters correctly shows "Name:". And then incorrectly shows "default:" and "check". I have submitted a ticket with Jetbrains for this IntelliJ issue. Please vote for it, if you get a chance so it increases in priority.
And then when the macro finishes executing (I provide the Name of "LongLat", but leave the default and check parameters empty), I don't understand why I get the following gibberish.
package org.public_domain
${PROPERTY_1_TYPE})
Any guidance on fixing my current Velocity Code Template script or suggest another way to effectively approach solving the problem would be deeply appreciated.

org_scalajs_dom_raw_HTMLDocument(...).createRange is not a function

I'm upgrading scalatags from 0.6.7 to 0.9.3 as part of upgrading scalaJS from 0.6.x to 1.4.0.
I got the following error in some of my tests:
scala.scalajs.js.JavaScriptException: TypeError: $m_Lorg_scalajs_dom_package$(...).document__Lorg_scalajs_dom_raw_HTMLDocument(...).createRange is not a function
Tracing the code, I believe it occurs while executing line 141 of the following scalatags code in `scalatags.JsDom:
I extracted just the createRange call into a separate test and got the same error. "creating range" was printed; "created range" was not and it produced the same exception as above.
createRange() is a native function.
Googling "createRange is not a function" yields a number of similar issues, all seem to be related to testing (but not with ScalaJS). Many of them indicate the "fix" is to monkey-patch document with your own version of createRange. Really?
I initially thought this was an issue with scalatags. Then I thought it's with the scalajs library. Now I'm thinking it's something with Node.js, although Google is not producing any smoking guns.
Suggestions on how to proceed? Try to monkey patch document?
Summary: jsdom appears to be missing the document.createRange function when using Node.js for testing. Others in other languages have similar problems.
The following monkey patch worked for me. After developing this, I noticed that Facade Types has a section on monkey typing.
Also, the library code that tickled this bug (scalatags) actually calls document.createRange().createContextualFragment(v). So I needed to provide something for that as well.
import scala.scalajs.js
import org.scalajs.dom.document
js.Dynamic.global.document.createRange = () ⇒
js.Dynamic.literal(
setStart = () => js.Dynamic.literal(),
setEnd = () => js.Dynamic.literal(),
commonAncestorContainer = js.Dynamic.literal(
nodeName = "BODY",
ownerDocument = document
),
createContextualFragment = (v: String) ⇒ {
val p = document.createElement("p")
p.appendChild(document.createTextNode(v))
}
)

Obsolete/erroneous code in convolutional_mlp.py at DeepLearningTutorials?

This code contains the following tidbit:
from theano.tensor.nnet import conv2d
...
# convolve input feature maps with filters
conv_out = conv2d(
input=input,
filters=self.W,
filter_shape=filter_shape,
input_shape=image_shape
)
which raises an exception due to 'input_shape' not being found, despite being mentioned in the documentation where it says that:
"image_shape ... – Deprecated alias for input_shape"
Looking at conv.py both locally and in the source I found:
def conv2d(input, filters, image_shape=None, filter_shape=None,
border_mode='valid', subsample=(1, 1), **kargs):
Needless to say, there is no trace of input_shape.
If one modifies the code above as follows
# convolve input feature maps with filters
conv_out = conv2d(
input=input,
filters=self.W,
filter_shape=filter_shape,
image_shape=image_shape
)
, the exception disappears and the code runs fine.
What am I missing? If image_shape is deprecated, how come it works while input_shape does not?
Is the theano version at the repository obsolete?
PS: I would have liked to ask directly the folks at http://deeplearning.net, but I could not find how.
Are you sure you have the latest version installed?
conv.py contains the deprecated implementation of conv2d. The new implementation can be found in __init__.py
Make sure you are using the import statement
from theano.tensor.nnet import conv2d
and not
from theano.tensor.nnet.conv import conv2d
since the second one is going to import the deprecated implementation

making a GET request to a webservice from the playframework 2.0

I'm trying to call a webservice from the play framework, and I think I'm doing it wrong. I have an example call to http://www.myweather2.com/developer/forecast.ashx?uac=eDKGlpcBQN&query=52.6%2C-4.4&output=xml
A snippet from what I'm trying from the playframework is the following:
val response = WS.url("http://www.myweather2.com/developer/forecast.ashx?uac=eDKGlpcBQN&query=52.6%2C-4.4&output=xml").get.get()
val body = response.getBody
When I call this, the body consists of "useraccount does not exist". When I just put this url in a browser, I get the response I'm looking for. What am I doing wrong here?
For some reason, I was getting WS from the wrong import. When I fixed the imports to import play.api.libs.ws.WS, it worked. I'm still amazed it worked halfway with the wrong import
Don't know about "useraccount does not exist" but this seems to work:
val promise = WS.url("http://www.myweather2.com/developer/forecast.ashx?uac=eDKGlpcBQN&query=52.6%2C-4.4&output=xml").get()
val body = promise.value.get.body
Edit: Removed the space.
Also make sure your editor is not inserting a \n or \r after ?
I know this is old, but I just solved this problem while trying to do the same thing - getting the same results.
GET variables must be passed with WS.url("http://...").setQueryParameter(key, value)
Example:
val promise = WS.url("http://www.myweather2.com/developer/forecast.ashx").setQueryParameter("uac", "eDKGlpcBQN").setQueryParameter("query", "52.6%2C-4.4").setQueryParameter("output", "xml").get()
Annoying, but a relatively simple fix.