我希望在我的项目的第一次提交中更改某些内容而不会丢失所有后续提交.有没有办法做到这一点?
我不小心在源代码中的评论中列出了我的原始电子邮件,我想改变它,因为我从机器人索引GitHub收到垃圾邮件.
如何使用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代码进行分析的做法是什么?
我需要一个函数,给定一个字符,返回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) 例如:
#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) 例如,假设我想要一个函数来转义字符串以便在HTML中使用(如在Django的转义过滤器中):
def escape(string):
"""
Returns the given string with ampersands, quotes and angle
brackets encoded.
"""
return string.replace('&', '&').replace('<', '<').replace('>', '>').replace("'", ''').replace('"', '"')
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('&', '&'),
{'<': '<', '>': '>',
"'": ''', '"': '"'})
Run Code Online (Sandbox Code Playgroud)
这样的函数是否存在,或者是使用我之前编写的标准Python习惯用法?
我的iPhone应用程序仅因使用(非常安全,似乎)私有方法+setAllowsAnyHTTPSCertificate:forHost:
而被拒绝NSURLRequest
.是否有非私有API来模拟此功能?
我经常看到如下代码,例如,在内存中表示一个大位图:
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)
这引出了两个问题:
谢谢.
上传我的Python C扩展的二进制发行版后python setup.py bdist upload
,easy_install [my-package-name]
失败"错误:无法在/ tmp/easy_install/package-name-etc-etc中找到安装脚本".
我究竟做错了什么?
我很好奇为什么我看到几乎所有的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 ×6
python ×3
performance ×2
alignment ×1
binary ×1
cocoa ×1
cocoa-touch ×1
easy-install ×1
generator ×1
git ×1
idioms ×1
indentation ×1
iphone ×1
iterator ×1
keycode ×1
keypress ×1
macos ×1
macros ×1
memory ×1
nsurlrequest ×1
pointers ×1
portability ×1
profiler ×1
profiling ×1
python-c-api ×1
rebase ×1
replace ×1