假设我为我所工作的公司编写了一个自定义电子邮件管理应用程序.它从公司的支持帐户中读取电子邮件,并将清理后的纯文本版本存储在数据库中,执行其他整洁的操作,例如将其与客户帐户和流程中的订单相关联.当员工回复邮件时,我的程序会生成一封电子邮件,该电子邮件使用格式化的讨论主题版本发送给客户.如果客户响应,则应用程序在主题行中查找唯一编号以读取传入消息,删除先前的讨论,并将其添加为线程中的新项目.例如:
This is a message from Contoso customer service. Recently, you requested customer support. Below is a summary of your request and our reply. -------------------------------------------------------------------- Contoso (Fred) on Tuesday, December 30, 2008 at 9:04 a.m. -------------------------------------------------------------------- John: I've modified your address. You can confirm my work by logging into "Your Account" on our Web site. Your order should ship out today. Thanks for shopping at Contoso. -------------------------------------------------------------------- You on Tuesday, December 30, 2008 at 8:03 a.m. -------------------------------------------------------------------- Oops, I entered …
关于在追加模式下打开文件然后尝试寻找文件的开头,我遇到了这种奇怪的行为.
代码应该是不言自明的:在第二次打开时,我希望能够将一个字符串写入文件的开头,然后f.tell()返回5(在文件开头写入的字节数).
问题是,在Python 2.6.6和2.7.6中,最终的断言触发,但令人惊讶的是,它在Python 3.3.2中有效.
# Create new file, write after the 100th byte.
f = open("test", "w+b")
f.seek(100, 0)
f.write(b"end")
assert f.tell() == 100 + len("end")
f.close()
# Open file for writing without overwriting, attempt to write at start of file
# of the file, but doesn't.
f = open("test", "a+b")
f.seek(0, 0)
f.write(b"start")
assert f.tell() == len("start")
f.close()
Run Code Online (Sandbox Code Playgroud)
所以我做了一个相同的C程序.它实际上表现得像Python 2.x版本:
#include <stdio.h>
int main() {
FILE *f = fopen("tt", "w+b");
fseek(f, 100, 0);
fwrite("x", 1, 1, …Run Code Online (Sandbox Code Playgroud) 我试图让 Boost Python 要求参数由调用者命名。
由于 Python 3 实现了PEP 3102,我可以在纯 Python 中轻松做到这一点:
def foo(*, name=None, age=None):
print("%s is %d years" % (name, age))
Run Code Online (Sandbox Code Playgroud)
上面的意思是:
foo(name="Joe", age=33) # works
foo(age=33, name="Joe") # works
foo("Joe", 33) # raises TypeError
Run Code Online (Sandbox Code Playgroud)
然而,这在 Boost Python 中并不是直接的。以下甚至不接近:
#include <stdio.h>
#include <string>
#include <boost/python.hpp>
#include <boost/python/def.hpp>
void foo(const std::string& name, int age)
{
printf("%s is %d years\n", name.c_str(), age);
}
BOOST_PYTHON_MODULE(hello)
{
using namespace boost::python;
def("foo", foo, (arg("name"), arg("age")));
}
Run Code Online (Sandbox Code Playgroud)
虽然这适用于命名参数,但它们不是必需的,所以我仍然可以做foo("Joe", 33).
我试过了:
我正在尝试生成一种颜色,可以根据当前对象的颜色将项目突出显示为"已选择".我已经尝试增加一些HSB值,但我无法想出一个通用的公式.特别是,使用白色时我遇到了问题(较亮的白色与常规白色看起来没什么不同).没有要求说我需要让它更亮,所以某种"逆"颜色也会很好用.是否有任何标准的算法或技术来做这样的事情(我猜是的,但我找不到 - 我不确定这是否有名字)?
谢谢,
杰夫
我正在寻找一个可用于处理音频文件的库.基本上我想做的是:
加载MP3/WAV文件
获取该文件的15秒剪辑
覆盖其上的另一个MP3/WAV文件
渲染为新的MP3/WAV文件
在APUE第8.3节中fork function,关于父进程和子进程之间的文件共享,
它说:It is important that the parent and the child share the same file offset.
在第8.9节中Race Conditions,有一个例子:父和子都写入
一个在调用fork函数之前打开的文件.该程序包含竞争条件,
因为输出取决于内核运行进程的顺序以及每个进程运行的时间.
但在我的测试代码中,输出是重叠的.
[Langzi @ Freedom apue] $ cat race.out
这是一个漫长的输出,这是父母的长输出
看起来父和子具有单独的文件偏移而不是共享相同的偏移量.
我的代码中有错误吗?或者我是否误解了共享偏移的含义?
任何建议和帮助将不胜感激.
以下是我的代码:
#include "apue.h"
#include <fcntl.h>
void charatatime(int fd, char *);
int main()
{
pid_t pid;
int fd;
if ((fd = open("race.out", (O_WRONLY | O_CREAT | O_TRUNC),
S_IRUSR | S_IWUSR)) < 0)
err_sys("open error");
if ((pid = fork()) < 0)
err_sys("fork error");
else if …Run Code Online (Sandbox Code Playgroud) 我正在将一段代码从Java转换为Python,我不知道如何翻译以下内容:
Field[] fields = getClass().getFields();
for (int i = 0; i < fields.length; i++ ) {
if (fields[i].getName().startsWith((String) param){ ....
Run Code Online (Sandbox Code Playgroud) 我是R的新手,但来自Scheme--它也是词法范围并且有闭包 - 我希望能够在闭包中改变外部变量.
例如,在
foo <- function() {
s <- 100
add <- function() {
s <- s + 1
}
add()
s
}
cat(foo(), "\n") # prints 100 and not 101
Run Code Online (Sandbox Code Playgroud)
我希望foo()返回101,但它实际上返回100:
$ Rscript foo.R
100
Run Code Online (Sandbox Code Playgroud)
我知道Python有global关键字来声明变量的范围(虽然不适用于这个例子).R需要类似的东西吗?
我究竟做错了什么?
更新
啊,问题在于add我在创建一个新的局部变量s来遮蔽外部s?如果是这样,我如何在s不创建局部变量的情况下进行变异?
我想提取ctypes.c_char_p实例指向的整数地址.
例如,在
>>> import ctypes
>>> s = ctypes.c_char_p("hello")
>>> s
c_char_p(4333430692)
Run Code Online (Sandbox Code Playgroud)
我想要获取的值是4333430692 - hello\0内存中字符串的地址:
(lldb) x 4333430692
0x1024ae7a4: 68 65 6c 6c 6f 00 5f 70 00 00 00 00 05 00 00 00 hello._p........
Run Code Online (Sandbox Code Playgroud)
我已经阅读了ctypes文档,但似乎没有办法做到这一点.最接近的是ctypes.addressof,但当然只能给我指针的位置.
我想要这个的原因是因为我正在调用一些实际上需要编码为整数的原始地址的C函数(其大小等于本机指针宽度).
当我的cpp文件使用#include添加一些标题时,我的最终程序的大小是否会变大?标头不被视为编译单元,但是头文件的内容由预处理器添加到实际的源文件中,因此输出文件(exe或dll)的大小是否会受此影响?
编辑:我忘了提到问题不是关于模板/内联函数.我的意思是如果我将#include放在一个没有任何函数实现细节的头文件中会发生什么.谢谢
c++ ×3
python ×3
algorithm ×2
c ×2
file-io ×2
audio ×1
boost-python ×1
closures ×1
colors ×1
compilation ×1
ctypes ×1
fork ×1
java ×1
python-2.7 ×1
python-3.x ×1
r ×1
reflection ×1
scope ×1
string ×1
text ×1
unix ×1
word-wrap ×1