小编Jon*_*ric的帖子

只选择部分翻译单元禁用GCC警告?

与此MSVC预处理器代码最接近的GCC是什么?

#pragma warning( push )                    // Save the current warning state.
#pragma warning( disable : 4723 )          // C4723: potential divide by 0
// Code which would generate warning 4723.
#pragma warning( pop )                     // Restore warnings to previous state.
Run Code Online (Sandbox Code Playgroud)

我们在常用的标题中包含代码,我们不希望生成特定的警告.但是,我们希望包含这些标头的文件继续生成该警告(如果项目启用了该警告).

c c++ gcc pragma compiler-warnings

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

如何在python 2.4中安全地打开/关闭文件

我目前正在编写一个小脚本,可以在我们的一个服务器上使用Python.服务器只安装了Python 2.4.4.

直到2.5出来之后我才开始使用Python,所以我习惯了以下形式:

with open('file.txt', 'r') as f:
    # do stuff with f
Run Code Online (Sandbox Code Playgroud)

但是,with2.5之前没有声明,我无法找到有关手动清理文件对象的正确方法的示例.

使用旧版本的python时,安全处理文件对象的最佳做法是什么?

python file-io python-2.4

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

通过Xcode中的所有.xib文件进行文本搜索?

这似乎是一项基本任务,但我很难过.

在Xcode中,您如何执行文本搜索(项目中的所有.xib文件的XML内容)?

例如,我们所有的.xib文件都在第二行包含此字符串:com.apple.InterfaceBuilder3.CocoaTouch.XIB.所以我认为搜索该字符串的所有项目文件将返回所有.xib文件,但Xcode坚持"0次出现".我仔细检查了项目查找选项看起来是否正确.

我一定错过了一些明显的东西.(或者Xcode以某种方式硬编码以跳过.xib文件.)

我试图找到引用特定类的所有.xib文件(文本搜索似乎是最直接的方式).

谢谢!

xcode full-text-search xib

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

NameError:未定义全局名称"long"

我有一个Python版本3.3.0,我不知道为什么它不让我为b和m做多久...我试着在这里查找答案,但没有任何帮助...谢谢

我得到一个错误说

NameError: global name 'long' is not defined


power = long(b) % long(m)
Run Code Online (Sandbox Code Playgroud)

nameerror python-3.x long-integer

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

使用JSON.net将枚举容器序列化为字符串

您可以通过添加属性将WebAPI模型中的枚举字段序列化为字符串:

enum Size
{
    Small,
    Medium,
    Large
}

class Example1
{
    [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
    Size Size { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这将序列化为此JSON:

{
  "Size": "Medium"
}
Run Code Online (Sandbox Code Playgroud)

如何为枚举集合完成相同的操作?

class Example2
{
    IList<Size> Sizes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我想序列化为这个JSON:

{
  "Sizes":
  [
    "Medium",
    "Large"
  ]
}
Run Code Online (Sandbox Code Playgroud)

c# json.net asp.net-web-api

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

为什么Thread.CurrentPrincipal需要"await Task.Yield()"才能正确流动?

下面的代码已添加到新创建的Visual Studio 2012 .NET 4.5 WebAPI项目中.

我正在尝试分配两者HttpContext.Current.UserThread.CurrentPrincipal在异步方法中.Thread.CurrentPrincipal流量分配不正确,除非执行await Task.Yield();(或其他任何异步)(传递trueAuthenticateAsync()将导致成功).

这是为什么?

using System.Security.Principal;
using System.Threading.Tasks;
using System.Web.Http;

namespace ExampleWebApi.Controllers
{
    public class ValuesController : ApiController
    {
        public async Task GetAsync()
        {
            await AuthenticateAsync(false);

            if (!(User is MyPrincipal))
            {
                throw new System.Exception("User is incorrect type.");
            }
        }

        private static async Task AuthenticateAsync(bool yield)
        {
            if (yield)
            {
                // Why is this required?
                await Task.Yield();
            }

            var principal = new MyPrincipal();
            System.Web.HttpContext.Current.User …
Run Code Online (Sandbox Code Playgroud)

c# async-await asp.net-web-api

37
推荐指数
1
解决办法
5377
查看次数

Numpy:加入结构化数组?

输入

我在列表中有许多numpy结构化数组,如下例所示:

import numpy

a1 = numpy.array([(1, 2), (3, 4), (5, 6)], dtype=[('x', int), ('y', int)])

a2 = numpy.array([(7,10), (8,11), (9,12)], dtype=[('z', int), ('w', float)])

arrays = [a1, a2]
Run Code Online (Sandbox Code Playgroud)

期望的输出

将它们连接在一起以创建如下所示的统一结构化数组的正确方法是什么?

desired_result = numpy.array([(1, 2, 7, 10), (3, 4, 8, 11), (5, 6, 9, 12)],
                             dtype=[('x', int), ('y', int), ('z', int), ('w', float)])
Run Code Online (Sandbox Code Playgroud)

目前的方法

这是我目前正在使用的,但它非常慢,所以我怀疑必须有一个更有效的方式.

from numpy.lib.recfunctions import append_fields

def join_struct_arrays(arrays):
    for array in arrays:
        try:
            result = append_fields(result, array.dtype.names, [array[name] for name in array.dtype.names], usemask=False)
        except NameError: …
Run Code Online (Sandbox Code Playgroud)

python numpy

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

在Xcode静态库中包含框架?

简而言之:

有没有办法在Xcode中创建静态库,以便当客户端链接到该库时,它们还链接到该库所依赖的框架?

问题:

我们有一个共享的Xcode项目,它包含多个包含所有常用代码的静态库目标.例如,如果项目想要使用共享网络代码,他们应该做的就是在我们的网络库中链接.

问题是库似乎并没有"包含"它们所依赖的框架.

例如,我们的Sound库使用AudioToolkit.framework.即使Sound库在其列表链接库中包含AudioToolbox.framework,客户端在与Sound链接时也会遇到链接器错误,如果它们也不直接与AudioToolkit.framework链接.

这是一个维护麻烦,因为每次库的框架依赖关系发生变化时,我们都会手动更改所有依赖项目中链接框架的列表.

这应该有用吗?有没有更好的办法?

谢谢!

iphone xcode frameworks shared-libraries static-libraries

18
推荐指数
1
解决办法
8138
查看次数

使用Xcode4在OS X Snow Leopard上安装PIL(无PPC支持)

Xcode4放弃了PPC支持,所以当我尝试构建PIL时,它会引发仇恨:

Bens-MacBook-Air:Imaging-1.1.7 bkeating$ python setup.py build
running buildrunning build_pyrunning build_ext
--- using frameworks at /System/Library/Frameworks
building '_imaging' extension
/usr/bin/gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch ppc -arch x86_64 -pipe -DHAVE_LIBJPEG -DHAVE_LIBZ -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tk.framework/Headers -I/usr/local/include/freetype2 -IlibImaging -I/System/Library/Frameworks/Python.framework/Versions/2.6/include -I/usr/local/include -I/usr/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c decode.c -o build/temp.macosx-10.6-universal-2.6/decode.o
/usr/libexec/gcc/powerpc-apple-darwin10/4.2.1/as: assembler (/usr/bin/../libexec/gcc/darwin/ppc/as or /usr/bin/../local/libexec/gcc/darwin/ppc/as) for architecture ppc not installed
Installed assemblers are:
/usr/bin/../libexec/gcc/darwin/x86_64/as for architecture x86_64
/usr/bin/../libexec/gcc/darwin/i386/as for architecture i386
decode.c:688: fatal error: error writing to -: Broken pipe …
Run Code Online (Sandbox Code Playgroud)

python xcode powerpc python-imaging-library

18
推荐指数
3
解决办法
6702
查看次数

运行nosetests警告错误?

nosetests从命令行运行时,如何指定"非忽略"警告应被视为错误?

默认情况下,会打印警告,但不会将其计为失败:

[snip]/service/accounts/database.py:151: SADeprecationWarning: Use session.add()
  self.session.save(state)
[snip]/service/accounts/database.py:97: SADeprecationWarning: Use session.add()
  self.session.save(user)
............
----------------------------------------------------------------------
Ran 12 tests in 0.085s

OK
Run Code Online (Sandbox Code Playgroud)

由于我们不希望我们的代码生成警告,我不希望这种情况发生OK.

谢谢!

编辑: 理想情况下,我想要的是一个nosetests命令行选项,它warnings.simplefilter('error')在每次测试之前发出(并在之后清除它).

任何涉及warnings在测试代​​码中使用该模块的解决方案似乎都失败了.我不想手动编辑每个测试模块以将警告转换为错误.另外,我不希望每个测试模块的作者都忘记"打开"警告错误.

python warnings unit-testing nose

16
推荐指数
3
解决办法
2150
查看次数