小编Jus*_*tin的帖子

在装饰器中修改功能

我正在考虑为提高性能而制作一个装饰器.一个装饰器,修改它装饰的函数的源代码,并返回修改后的函数.

在考虑这一点时,我想如果我能得到函数的源代码,我就可以做到这一点.但是可以在装饰器内访问函数的源代码吗?如果我有这样的装饰者:

import inspect

def decorate(f):
    exec(inspect.getsource(f))
    return eval(f.__name__)

@decorate
def test():
    return 1
Run Code Online (Sandbox Code Playgroud)

我得到一个OSError:

OSError: could not get source code
Run Code Online (Sandbox Code Playgroud)

这似乎是因为test在传入之前没有完全形成decorate.但是,这有效:

import inspect

def decorate(f):
    exec(inspect.getsource(f))
    return eval(f.__name__)

def test():
    return 1
test = decorate(test)
Run Code Online (Sandbox Code Playgroud)

然而,它并没有那种装饰风格.似乎这可能是可能的,因为f.__code__ 定义.


经过进一步检查,似乎只有当我inspect.getsource(f)进入时才会发生这种情况exec.否则,似乎我可以获得源代码.


作为我脑海中第一件事的粗略草图,我正在考虑尾部递归.我编写的这个装饰器很慢,并且需要一种非常具体的写入要装饰的函数的样式:

def tail_recurse(acc_default):
    def decorate(f):
        def wrapper(*args, acc=acc_default):
            args = args + (acc,)
            while True:
                return_type, *rargs = f(*args)
                if return_type is None:
                    return rargs[-1]
                args = rargs
        return wrapper
    return …
Run Code Online (Sandbox Code Playgroud)

python function decorator python-decorators

13
推荐指数
1
解决办法
2279
查看次数

用户定义的数字文字可以紧跟一个点吗?

从C++ 11开始,就可以创建用户定义的文字.正如预期的那样,可以从这些文字返回复杂的结构.但是,在尝试使用此类运算符时123_foo.bar():

struct foo {
    int n;
    int bar() const { return n; }
};

constexpr foo operator ""_foo(unsigned long long test)
{
    return foo{ static_cast<int>(test) };
}

int main() {
    return 123_foo.bar();
}
Run Code Online (Sandbox Code Playgroud)

海湾合作委员会和克朗拒绝了,说他们找不到operator""_foo.bar.MSVC接受它.如果我改为写123_foo .bar(),那么所有三个编译器都接受它

谁在这?是123_foo.bar()以往任何时候都有效吗?


一些额外的信息:


我倾向于认为这是一个GCC和Clang错误,因为.它不是有效标识符的一部分.

c++ language-lawyer user-defined-literals c++11

13
推荐指数
1
解决办法
421
查看次数

如何使用AngularJS在我的http请求中发送参数?

我使用以下代码:

$http({
    method: 'GET',
    url: '/Admin/GetTestAccounts',
    data: { applicationId: 3 }
}).success(function (result) {
    $scope.testAccounts = result;
});
Run Code Online (Sandbox Code Playgroud)

代码将以下内容发送到我的服务器:

http://127.0.0.1:81/Admin/GetTestAccounts
Run Code Online (Sandbox Code Playgroud)

当我的MVC控制器接收到它时:

[HttpGet]
public virtual ActionResult GetTestAccounts(int applicationId)
{
    var testAccounts =
        (
            from testAccount in this._testAccountService.GetTestAccounts(applicationId)
            select new
            {
                Id = testAccount.TestAccountId,
                Name = testAccount.Name
            }
        ).ToList();

    return Json(testAccounts, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)

它抱怨没有applicationId.

参数字典包含非可空类型'System.Int32'参数'applicationId'的空条目用于方法

有人可以解释为什么不将applicationId作为参数发送?以前我使用以下非Angular代码执行此操作并且它工作得很好:

$.ajax({
    url: '/Admin/GetTestAccounts',
    data: { applicationId: 3 },
    type: 'GET',
    success: function (data) {
        eViewModel.testAccounts(data);
    }
});
Run Code Online (Sandbox Code Playgroud)

angularjs

12
推荐指数
2
解决办法
4万
查看次数

Netbeans小写快捷方式不起作用

Netbeans 有一套很好的键盘快捷键,不幸的是,我似乎无法让所有这些快捷键都能运行.具体来说,有一个键盘快捷键,用于将所选文本转换为小写:Ctrl+ U L.然而,无论我尝试多少次(或其上的变化),没有任何反应我只是得到一个'叮当'的声音(过去没有发生任何事情).我检查了Netbeans上的Keymap选项,它显示这仍然是映射到Ctrl+ 的快捷方式U L:

在此输入图像描述

我究竟做错了什么?如何使用此键盘快捷键?

请注意,Ctrl+ ;Ctrl+ Shift+ ;在许多其他快捷方式中工作得很好.

netbeans keyboard-shortcuts netbeans-8

12
推荐指数
2
解决办法
9314
查看次数

std :: copy和std :: vector :: assign的转换警告

当一个浮点数插入a时std::vector<int>,该数字必须通过某种舍入来转换.通常这会更改数字,1.5会更改为1或2,我希望编译器至少会警告此转换.所以我-Wconversion在g ++或clang ++上使用了标志.这可以启用警告std::vector::push_back或直接分配,但不能用于std::copystd::vector::assign(iterator first, iterator end).

现在的问题是:我如何转换警告,std::copystd::vector::assign

这是我的示例程序:

#include <iostream>
#include <vector>
#include <algorithm>

using source_type = std::vector<double>;
using target_type = std::vector<int>;

int main() {
    source_type vsource;
    target_type vtarget1;
    target_type vtarget2;
    target_type vtarget3;
    target_type vtarget4;

    // Fill source with a number
    vsource.push_back(1.5);

    // This will give a compiler warning as expected
    vtarget1.push_back(vsource.at(0));

    // This does not give a warning, why not?
    vtarget2.assign(vsource.begin(), vsource.end());

    // Also this …
Run Code Online (Sandbox Code Playgroud)

c++

12
推荐指数
1
解决办法
290
查看次数

Java KeyListener:KeyTyped Backspace,Esc作为输入

里面的KeyTyped方法,我怎么知道,如果Backspace还是Esc被按下时?

java keylistener backspace

11
推荐指数
3
解决办法
5万
查看次数

MSVC constexpr函数'xyz'不能导致常量表达式

我已经创建了一个函数,它将多个较小的值连接成一个较大的值,同时保留值的bianry表示(例如,int argb从多个构建一个unsigned char r, g, b, a).我知道我也可以通过改变价值来实现这一点,但这不是这个问题的问题.

但是,如果我使用该函数实际从这些值生成一个整数,msvc会抛出编译器错误:

error C3615: constexpr function 'Color::operator int' cannot result in a constant expression
note: failure was caused by call of undefined function or one not declared 'constexpr'
note: see usage of '<lambda_dcb9c20fcc2050e56c066522a838749d>::operator ()'
Run Code Online (Sandbox Code Playgroud)

是一个完整的样本.Clang和gcc编译代码但msvc拒绝:

#include <type_traits>
#include <memory>

namespace detail
{
    template <typename From, typename To, size_t Size>
    union binary_fusion_helper
    {
        const From from[Size];
        const To to;
    };

    template <typename To, typename Arg, typename ...Args, typename = std::enable_if_t<(... …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++ c++17

10
推荐指数
1
解决办法
1081
查看次数

好hashCode()实现

hashCode方法的Best实现中接受的答案给出了一种看似很好的方法来查找哈希码.但我是Hash Codes的新手,所以我不知道该怎么做.

对于1),我选择的非零值是否重要?是1就像其他号码如黄金一样好31

对于2),我是否将每个值添加到c?如果我有两个字段都是一个long,int,double,等?


我是否在本课程中正确解释了它:

public MyClass{
    long a, b, c; // these are the only fields
    //some code and methods
    public int hashCode(){
        return 37 * (37 * ((int) (a ^ (a >>> 32))) + (int) (b ^ (b >>> 32))) 
                 + (int) (c ^ (c >>> 32));
    }
}
Run Code Online (Sandbox Code Playgroud)

java hash

9
推荐指数
2
解决办法
2万
查看次数

在Visual Studio中使用Lambdas进行模板化变量错误?

提供了可变模板,它在,但在lambdas中它们似乎分崩离析.例如:

template <typename T>
const auto PI = std::acos(static_cast<T>(-1));

int main() {
  auto func = []() { cout << PI<float> << endl; };

  func();
}
Run Code Online (Sandbox Code Playgroud)

在gcc 6.3上输出:

3.14159

在Visual Studio 2017上,此输出:

0.0

c++ lambda variable-templates c++14 visual-studio-2017

9
推荐指数
1
解决办法
297
查看次数

在noexcept规范中允许使用`this`吗?

我有一些代码需要我使用*this,但我希望它是noexcept友好的:

struct foo;

// Would actually be something with conditional noexcept
void do_something(foo&);

struct foo {
    void fn()
        noexcept(noexcept(::do_something(*this)))
    {
        ::do_something(*this);
    }
};
Run Code Online (Sandbox Code Playgroud)

但是,gcc拒绝这样做:

<source>:7:43: error: invalid use of 'this' at top level
         noexcept(noexcept(::do_something(*this)))
Run Code Online (Sandbox Code Playgroud)

如果我只是访问一个成员,gcc很好:

void do_something(int);

struct bar {
    int x;

    void fn()
        noexcept(noexcept(::do_something(x)))
    {
        ::do_something(x);
    }
};
Run Code Online (Sandbox Code Playgroud)

但是,如果我通过this指针访问该成员,gcc会再次抱怨:

struct baz {
    int x;

    void fn()
        noexcept(noexcept(::do_something(this->x)))
    {
        ::do_something(this->x);
    }
};
Run Code Online (Sandbox Code Playgroud)

诊断:

<source>:7:42: error: invalid use of 'this' at top level …
Run Code Online (Sandbox Code Playgroud)

c++ this language-lawyer noexcept

9
推荐指数
1
解决办法
195
查看次数