小编And*_*Dog的帖子

C#多线程列表操作

如果我有这样的东西(伪代码):

class A
{
    List<SomeClass> list;

    private void clearList()
    {
        list = new List<SomeClass>();
    }

    private void addElement()
    {
        list.Add(new SomeClass(...));
    }
}
Run Code Online (Sandbox Code Playgroud)

当两个函数并行执行时,我是否可能遇到多线程问题(或任何类型的意外行为)?

用例是一个错误列表,可以随时清除(通过简单地分配一个新的空列表).

编辑:我的假设是

  • 只有一个线程添加元素
  • 被遗忘的元素是可以的(即清除和添加新元素之间的竞争条件),只要清除操作成功没有问题
  • .NET 2.0

c# multithreading atomic

5
推荐指数
1
解决办法
8406
查看次数

C#lambda /匿名代表中的词法范围

我想检查一个简单的数学表达式是否会溢出(使用checkedcatch(OverflowException)),但不需要每次都使用try-catch块.因此表达式(不是结果!)应该传递给一个函数checkOverflow,然后在溢出的情况下相应地起作用.

这是我尝试过的,但不起作用,因为似乎没有lambda表达式的词法作用域.

static void Main(string[] args)
{
    double g, h;

    // Check if the expression g+h would overflow, *without* the need to use
    // try/catch around the expression

    // Both of the following attempts fail because there's no lexical scoping
    checkOverflow((FloatReturningExpression) delegate() {return g+h;});
    checkOverflow(() => g+h);

    Console.ReadKey();
}

private static void checkOverflow(FloatReturningExpression exp)
{
    try
    {
        checked { double f = exp(); }
    }
    catch(OverflowException)
    {
        Console.WriteLine("overflow!");
    }
}

private delegate double FloatReturningExpression(); …
Run Code Online (Sandbox Code Playgroud)

c# lambda delegates lexical-scope

4
推荐指数
1
解决办法
599
查看次数

浅拷贝,为什么列表不会改变

我试图理解python中浅拷贝和深拷贝之间的区别.我在这里阅读了很多帖子,他们一直很有帮助.但是,我仍然不太了解这种差异.有人可以解释下面结果的原因.评论中指出了我不理解的结果.

非常感谢.

import copy
import random

class understand_copy(object):
    def __init__(self):
        self.listofvalues = [4, 5]

    def set_listofvalues(self, pos, value):
        self.listofvalues[pos] = value

ins = understand_copy()

newins = copy.copy(ins)

newins.set_listofvalues(1,3)
print "ins = ", ins.listofvalues
print "in newins", newins.listofvalues
# Gives the following output as I would expect based on the explanations.
# prints ins = [4, 3]
# prints newins = [4, 3]


newins.listofvalues.append(5)
print "ins =", ins.listofvalues
print "newins =", newins.listofvalues
# Gives the following output as I would expect …
Run Code Online (Sandbox Code Playgroud)

python copy

4
推荐指数
1
解决办法
3206
查看次数

从词典django/python中获取键值

如何从密钥本身打印密钥的值

dict={}
dict.update({'aa':1})
dict.update({'ab':1})
dict.update({'ac':1})
return render_to_response(t.html,  context_instance=RequestContext(request, {'dict':dict}))
Run Code Online (Sandbox Code Playgroud)

所以在这种情况下,我想打印键,alert('{{dict.aa}}');即,不使用任何循环,我们只需在上面的示例中打印带有aa的引用的键可能有些事情,如果{{dict ['aa']}}应该给出aa的价值

python django django-templates

4
推荐指数
1
解决办法
6993
查看次数

Python 3:打开包含在变量中的磁力链接

我有一个磁链接(例如:磁铁:?xt = urn:btih:1c1b9f5a3b6f19d8dbcbab5d5a43a6585e4a7db6)作为字符串包含在变量中,并希望脚本打开处理磁力链接的默认程序,以便它开始下载torrent(如果我从文件管理器中打开了一个磁铁链接.

为了使答案清楚,我们会说我们在一个变量中有磁链接magnet_link.

python variables bittorrent utorrent python-3.x

4
推荐指数
1
解决办法
3056
查看次数

C#中严格的字符串到字节编码

我刚刚偶然发现了另一个问题,其中某人建议使用new ASCIIEncoding().GetBytes(someString)从字符串转换为字节.对我来说很明显,它不适用于非ASCII字符.但事实证明,ASCIIEncoding很乐意用'?'替换无效字符.我对此非常困惑,因为这种打破了最不惊讶的规则.在Python中,u"some unicode string".encode("ascii")默认情况下转换是严格的,因此非ASCII字符会导致此示例中的异常.

两个问题:

  1. 如何将字符串严格转换为其他编码(如ASCII或Windows-1252),以便在出现无效字符时抛出异常?顺便说一句,我不希望foreach循环将每个Unicode数字转换为一个字节,然后检查第8位.这应该由像.NET(或Python ^^)这样的伟大框架来完成.
  2. 有关此默认行为背后的基本原理的任何想法?对我来说,默认情况下进行严格转换或至少为此目的定义参数更有意义(Python允许"替换","忽略","严格").

c# ascii character-encoding

3
推荐指数
1
解决办法
868
查看次数

Django i18n:仅在网站级别上制作消息吗?

例如,我的网站中有几个字符串不属于任何应用程序

{% block title %}{% trans "Login" %}{% endblock %}
Run Code Online (Sandbox Code Playgroud)

或者用于设置区域设置cookie的修改后的身份验证表单

class AuthenticationFormWithLocaleOption(AuthenticationForm):
    locale = forms.ChoiceField(choices = settings.LANGUAGES,
                               required = False,
                               initial = preselectedLocale,
                               label = _("Locale/language"))
Run Code Online (Sandbox Code Playgroud)

现在,当我django-admin.py makemessages --all -e .html,.template在站点目录中执行时,它会从所有 Python,.html和.template文件中提取字符串,包括我的应用程序中的文件.那是因为我在该目录中开发我的应用程序:

Directory structure:

sitename
     myapp1
     myapp2

有没有办法提取我的应用程序中没有的所有字符串?

我找到的唯一解决方案是将app目录移到站点目录结构之外,但我使用的是bzr-externals(类似于git子模块或svn externals),因此在我的情况下没有意义.

将需要翻译的内容移动到新的应用程序中也是可能的,但我不知道这是否是唯一合理的解决方案.

django internationalization

3
推荐指数
1
解决办法
3565
查看次数

以编程方式将UIBarButtonItem设置为UIBarButtonSystemItem

我可以将系统项分配给a UIBarButtonItem而不必重新创建它吗?只是在现有按钮上调用initWithBarButtonSystemItem似乎对我不起作用.

是我尝试使其工作的代码.我正在尝试使用PhoneGap改进iOS的本机控件支持.该插件允许更改标题/图像没有问题,但我必须弄清楚如何将系统提供的图标分配给现有按钮.

uibarbuttonitem ios

3
推荐指数
1
解决办法
7601
查看次数

Makefile中的简单字符串替换

我想libswscale.so.2libswscale.so(变量分别调用$(SLIBNAME_WITH_MAJOR)和变量$(SLIBNAME))替换字符串.这是我在Makefile中尝试过的:

$(SUBDIR)$(SLIBNAME_WITH_MAJOR): $(OBJS) $(SUBDIR)lib$(NAME).ver
    [...]
    @echo SHFLAGS=$(SHFLAGS)
    @echo SLIBNAME_WITH_MAJOR=$(SLIBNAME_WITH_MAJOR)
    @echo SLIBNAME=$(SLIBNAME)
    @echo A $(patsubst $(SLIBNAME_WITH_MAJOR),$(SLIBNAME),$(SHFLAGS))
    @echo B $(SHFLAGS:$(SLIBNAME_WITH_MAJOR)=$(SLIBNAME))
    @echo C $($(SHFLAGS):$(SLIBNAME_WITH_MAJOR)=$(SLIBNAME))
    @echo D $(SHFLAGS:$(SLIBNAME_WITH_MAJOR)=$(SLIBNAME))
    @echo E $(subst $(SLIBNAME_WITH_MAJOR),$(SLIBNAME),$(SHFLAGS))
    @echo F $(subst l,L,$(SHFLAGS))
Run Code Online (Sandbox Code Playgroud)

输出是

SHFLAGS=-shared -Wl,-soname,libswscale.so.2 -Wl,-Bsymbolic -Wl,--version-script,libswscale/libswscale.ver
SLIBNAME_WITH_MAJOR=libswscale.so.2
SLIBNAME=libswscale.so
A -shared -Wl,-soname,libswscale.so.2 -Wl,-Bsymbolic -Wl,--version-script,libswscale/libswscale.ver
B -shared -Wl,-soname,libswscale.so.2 -Wl,-Bsymbolic -Wl,--version-script,libswscale/libswscale.ver
C
D -shared -Wl,-soname,libswscale.so.2 -Wl,-Bsymbolic -Wl,--version-script,libswscale/libswscale.ver
E -shared -Wl,-soname,libswscale.so.2 -Wl,-Bsymbolic -Wl,--version-script,libswscale/libswscale.ver
F -shared -WL,-soname,libswscale.so.2 -WL,-BsymboLic -WL,--version-script,LibswscaLe/LibswscaLe.ver
Run Code Online (Sandbox Code Playgroud)

最后一个(F)特别荒谬.这有什么不对?是因为它$(SHFLAGS)也是由变量组成的吗?

makefile gnu-make substitution

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

扩展std :: vector以从其他矢量类型移动元素

假设我有一个与std :: vector兼容的非STL向量类型operator std::vector<T>.是否可以其元素移动到std :: vector而不是默认的复制结构,这样就可以了

OtherVectorType<SomeClass> f()
{
    OtherVectorType<SomeClass> v;
    v.pushBack(SomeClass());
    v.pushBack(SomeClass());
    v.pushBack(SomeClass());
    return v;
}

std::vector<SomeClass> sv = f();
Run Code Online (Sandbox Code Playgroud)

在创建std :: vector时会使用SomeClass的移动构造函数(3次)sv吗?

我想象的东西

template<typename T>
std::vector<T>& operator= (std::vector<T>& self, OtherVectorType<T>&& from)
{
    [...]
}
Run Code Online (Sandbox Code Playgroud)

但还没有找到任何有效的解决方案.


为了说明,这是定义std :: vector运算符的方式:

template<typename T> class OtherVectorType
{
    [...]

    operator std::vector<T>() const
    {
        if (!m_size)
            return std::vector<T>();

        return std::vector<T>(reinterpret_cast<T*>(m_pElements),
                              reinterpret_cast<T*>(m_pElements) + m_size);
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ move-semantics c++11

2
推荐指数
1
解决办法
384
查看次数