小编Mic*_*ael的帖子

用Git更改项目的第一次提交?

我希望在我的项目的第一次提交中更改某些内容而不会丢失所有后续提交.有没有办法做到这一点?

我不小心在源代码中的评论中列出了我的原始电子邮件,我想改变它,因为我从机器人索引GitHub收到垃圾邮件.

git rebase git-filter-branch

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

如何使用Python C API创建生成器/迭代器?

如何使用Python C API复制以下Python代码?

class Sequence():
    def __init__(self, max):
        self.max = max
    def data(self):
        i = 0
        while i < self.max:
            yield i
            i += 1
Run Code Online (Sandbox Code Playgroud)

到目前为止,我有这个:

#include <Python/Python.h>
#include <Python/structmember.h>

/* Define a new object class, Sequence. */
typedef struct {
    PyObject_HEAD
    size_t max;
} SequenceObject;

/* Instance variables */
static PyMemberDef Sequence_members[] = {
    {"max", T_UINT, offsetof(SequenceObject, max), 0, NULL},
    {NULL} /* Sentinel */
};

static int Sequence_Init(SequenceObject *self, PyObject *args, PyObject *kwds)
{
    if (!PyArg_ParseTuple(args, "k", &(self->max))) { …
Run Code Online (Sandbox Code Playgroud)

c python iterator generator python-c-api

43
推荐指数
2
解决办法
8375
查看次数

C Profilers的建议?

在执行优化之前,每个人总是说要对程序进行概要分析,但没有人描述过如何执行优化.

您对C代码进行分析的做法是什么?

c profiler profiling

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

如何将ASCII字符转换为CGKeyCode?

我需要一个函数,给定一个字符,返回CGKeyCode与当前键盘布局上该字符的位置相关联的函数.例如,给定"b",kVK_ANSI_B如果使用US QWERTY,或者kVK_ANSI_N使用Dvorak ,它应该返回.

Win32 API具有VkKeyScan()用于此目的的功能; X11具有此功能XStringToKeySym().CG API中有这样的功能吗?

我需要这个来传递参数CGEventCreateKeyboardEvent().我尝试使用CGEventKeyboardSetUnicodeString(),但显然不支持修饰符标志(我需要).

我已经广泛搜索了这个但是找不到合适的答案.目前我正在使用以下代码(在线发现),它有效,但不是很优雅(而且很难解释如何简化),我宁愿不在生产代码中使用它:

#include <stdint.h>
#include <stdio.h>
#include <ApplicationServices/ApplicationServices.h>

CGKeyCode keyCodeForCharWithLayout(const char c,
                                   const UCKeyboardLayout *uchrHeader);

CGKeyCode keyCodeForChar(const char c)
{
    CFDataRef currentLayoutData;
    TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();

    if (currentKeyboard == NULL) {
        fputs("Could not find keyboard layout\n", stderr);
        return UINT16_MAX;
    }

    currentLayoutData = TISGetInputSourceProperty(currentKeyboard,
                                                kTISPropertyUnicodeKeyLayoutData);
    CFRelease(currentKeyboard);
    if (currentLayoutData == NULL) {
        fputs("Could not find layout data\n", …
Run Code Online (Sandbox Code Playgroud)

c macos keycode keypress quartz-graphics

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

为什么不切换指针?

例如:

#include <stdio.h>

void why_cant_we_switch_him(void *ptr)
{
    switch (ptr) {
        case NULL:
            printf("NULL!\n");
            break;
        default:
            printf("%p!\n", ptr);
            break;
    }
}

int main(void)
{
    void *foo = "toast";
    why_cant_we_switch_him(foo);
    return 0;
}

gcc test.c -o test
test.c: In function 'why_cant_we_switch_him':
test.c:5: error: switch quantity not an integer
test.c:6: error: pointers are not permitted as case values
Run Code Online (Sandbox Code Playgroud)

只是好奇.这是技术限制吗?

编辑

人们似乎认为只有一个常量指针表达式.这是真的吗?举例来说,这里是在Objective-C(一种常见的范例是真的是仅含有C除了NSString,id并且nil,这仅仅是一个指针,所以它仍然是相关的-我只是想指出,有,事实上,一个共同的使用它,尽管这只是一个技术问题):

#include <stdio.h>
#include <Foundation/Foundation.h>

static NSString * const kMyConstantObject = @"Foo";

void why_cant_we_switch_him(id ptr) …
Run Code Online (Sandbox Code Playgroud)

c pointers switch-statement

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

是str.replace(..).replace(..)ad令人厌恶Python中的标准习语吗?

例如,假设我想要一个函数来转义字符串以便在HTML中使用(如在Django的转义过滤器中):

    def escape(string):
        """
        Returns the given string with ampersands, quotes and angle 
        brackets encoded.
        """
        return string.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace("'", '&#39;').replace('"', '&quot;')
Run Code Online (Sandbox Code Playgroud)

这样可行,但它很快变得难看并且似乎具有较差的算法性能(在此示例中,字符串重复遍历5次).更好的是这样的事情:

    def escape(string):
        """
        Returns the given string with ampersands, quotes and angle 
        brackets encoded.
        """
        # Note that ampersands must be escaped first; the rest can be escaped in 
        # any order.
        return replace_multi(string.replace('&', '&amp;'),
                             {'<': '&lt;', '>': '&gt;', 
                              "'": '&#39;', '"': '&quot;'})
Run Code Online (Sandbox Code Playgroud)

这样的函数是否存在,或者是使用我之前编写的标准Python习惯用法?

python performance replace idioms

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

NSURLRequest的私有"setAllowsAnyHTTPSCertificate:forHost:"的替代方法?

我的iPhone应用程序仅因使用(非常安全,似乎)私有方法+setAllowsAnyHTTPSCertificate:forHost:而被拒绝NSURLRequest.是否有非私有API来模拟此功能?

iphone cocoa cocoa-touch iphone-privateapi nsurlrequest

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

现代处理器上的内存对齐?

我经常看到如下代码,例如,在内存中表示一个大位图:

size_t width = 1280;
size_t height = 800;
size_t bytesPerPixel = 3;
size_t bytewidth = ((width * bytesPerPixel) + 3) & ~3; /* Aligned to 4 bytes */
uint8_t *pixelData = malloc(bytewidth * height);
Run Code Online (Sandbox Code Playgroud)

(也就是说,一个位图被分配为一个连续的内存块,它bytewidth与一定数量的字节对齐,最常见的是4.)

然后通过以下方式给出图像上的一个点:

pixelData + (bytewidth * y) + (bytesPerPixel * x)
Run Code Online (Sandbox Code Playgroud)

这引出了两个问题:

  1. 对齐像这样的缓冲区会对现代处理器产生性能影响吗?我应该担心对齐,还是编译器会处理这个问题?
  2. 如果它确实有影响,有人可以指向我找到各种处理器的理想字节对齐的资源吗?

谢谢.

c memory performance memory-management alignment

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

二进制上传后,easy_install因错误"无法找到设置脚本"而失败?

上传我的Python C扩展的二进制发行版后python setup.py bdist upload,easy_install [my-package-name]失败"错误:无法在/ tmp/easy_install/package-name-etc-etc中找到安装脚本".

我究竟做错了什么?

python binary easy-install python-c-extension

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

C宏/ #define缩进?

我很好奇为什么我看到几乎所有的C宏格式如下:

#ifndef FOO
#   define FOO
#endif
Run Code Online (Sandbox Code Playgroud)

或这个:

#ifndef FOO
#define FOO
#endif
Run Code Online (Sandbox Code Playgroud)

但从来没有这样:

#ifndef FOO
    #define FOO
#endif
Run Code Online (Sandbox Code Playgroud)

(此外,vim的=运算符似乎只计算前两个是正确的.)

这是由于编译器之间的可移植性问题,还是仅仅是标准做法?

c macros portability indentation

8
推荐指数
2
解决办法
2111
查看次数