小编Mor*_*ock的帖子

如何在Python中播放声音而不会中断播放中的音乐/其他声音

我正在使用python中的一个计时器,当等待时间结束时,它会发出响声.我使用以下代码:

from wave import open as wave_open
from ossaudiodev import open as oss_open

def _play_chime():
    """
    Play a sound file once.

    """
    sound_file = wave_open('chime.wav','rb')
    (nc,sw,fr,nf,comptype, compname) = sound_file.getparams( )
    dsp = oss_open('/dev/dsp','w')
    try:
      from ossaudiodev import AFMT_S16_NE
    except ImportError:
      if byteorder == "little":
        AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
      else:
        AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
    dsp.setparameters(AFMT_S16_NE, nc, fr)
    data = sound_file.readframes(nf)
    sound_file.close()
    dsp.write(data)
    dsp.close()
Run Code Online (Sandbox Code Playgroud)

它工作得很好,除非任何其他设备已经输出声音.

如果没有播放声音的先决条件,我怎么能基本相同(在linux下)?

如果您认为该过程需要API来确保软件混合,请建议一种方法:)

Thx的支持:)

python linux audio timer

7
推荐指数
1
解决办法
2332
查看次数

每月在特定日期运行作业(使用 anacron?)

我想在我运行 Ubuntu 12.04 的笔记本电脑上每月运行一次作业,假设是每月的 22 日。

由于它是一台笔记本电脑,而且我可能并不总是每个月的第 22 天都使用它,cron因此不是一个很好的选择。

看着anacron,似乎有一个限制。也就是说,您可以指定一个“句点”,但不能指定特定的星期几或一个月的哪一天,如anacrontab文件格式所建议的:

# cat /etc/anacrontab
period   delay   job-identifier   command
7       15      test.daily      /bin/sh /home/myself/backup.sh
Run Code Online (Sandbox Code Playgroud)

我想说的是,如果我们在这个月的第 22 天,当然笔记本电脑正在运行,请运行该作业。如果 22 号过去了,而您还没有运行该作业,请在我启动后立即运行它。

我即将做一些丑陋的事情,比如混合cronanacron使用自定义脚本或编写我自己的 bash 脚本,使用时间戳,可能在这个过程中重新发明方轮。

关于最佳行动方案的任何想法?

干杯。

linux cron automation

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

易于使用的Python时间戳

我正在使用Python编写日志类型的应用程序.该应用程序基本上允许用户在日志中写入条目,并添加时间戳以便以后查询日志.

截至目前,我使用该time.ctime()函数生成视觉上友好的时间戳.因此日记帐分录如下:

Thu Jan 21 19:59:47 2010 Did something
Thu Jan 21 20:01:07 2010 Did something else
Run Code Online (Sandbox Code Playgroud)

现在,我希望能够使用这些时间戳来进行一些搜索/查询.我需要能够搜索"2010","2010年2月"或"2010年2月23日".

我的问题是:

1)我应该使用什么时间模块:timevs datetime

2)创建和使用时间戳对象的适当方法是什么?

非常感谢!

python time datetime timestamp

3
推荐指数
2
解决办法
2114
查看次数

Python中的类,方法和多态

我制作了一个模块原型,目的是在python中构建复杂的计时器计划.类原型模拟Timer对象,每个对象具有等待时间,重复对Timer和其他Repeat对象进行分组的对象,以及Schedule类,仅用于保存整个构造或Timers和Repeat实例.建筑可以根据需要复杂化,并且需要灵活.

这三个类中的每一个都有一个.run()方法,允许完成整个计划.无论是什么类,该.run()方法要么运行计时器,要么运行一定数量的迭代重复组,要么运行计划.

这种面向多态的方法听起来还是愚蠢的?我应该考虑采用哪些其他适当的方法来构建这样一个多功能的实用程序,允许将所有构建块放在一起,以简单的方式将所有构建块组合在一起?

谢谢!

这是模块代码:

#####################
## Importing modules

from time import time, sleep


#####################
## Class definitions

class Timer:
    """
    Timer object with duration.

    """
    def __init__(self, duration):
        self.duration = duration
    def run(self):
        print "Waiting for %i seconds" % self.duration
        wait(self.duration)
        chime()

class Repeat:
    """
    Repeat grouped objects for a certain number of repetitions.

    """
    def __init__(self, objects=[], rep=1):
        self.rep = rep
        self.objects = objects
    def run(self):
        print "Repeating group for %i times" % self.rep …
Run Code Online (Sandbox Code Playgroud)

python oop polymorphism timer

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

如何在C++中声明向量?

我试图在我的代码中使用字符串向量而不是字符串数组,但显然我错过了向量声明中的一些细节.使用以下代码,我收到此错误:‘vector’ was not declared in this scope

// Try to implement a vector of string elements

#include<iostream>

using namespace std;

int main() {
    const int MAX_ITEMS = 10;
    vector<string> my_vector(MAX_ITEMS);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我该如何正确地声明向量?

c++ vector

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

如何正确定义和使用Python生成器

我想从列表中定义一个生成器,它将一次输出一个元素,然后以适当的方式使用此生成器对象.

a = ["Hello", "world", "!"]
b = (x for x in a)
c = next(b, None)
while c != None:
    print c,
    c = next(b, None)
Run Code Online (Sandbox Code Playgroud)

这种while方法有什么不对或可以改进的吗?有没有办法避免在循环之前分配'c'?

谢谢!

python generator while-loop

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

在Python中使用多个输入和输出文件

我需要打开多个文件(2个输入和2个输出文件),对输入文件的行进行复杂的操作,然后在2个输出文件的末尾附加结果.我目前正在使用以下方法:

in_1 = open(input_1)
in_2 = open(input_2)
out_1 = open(output_1, "w")
out_2 = open(output_2, "w")

# Read one line from each 'in_' file
# Do many operations on the DNA sequences included in the input files
# Append one line to each 'out_' file

in_1.close()
in_2.close()
out_1.close()
out_2.close()
Run Code Online (Sandbox Code Playgroud)

文件很大(每个都可能接近1Go,这就是为什么我一次只读取这些输入文件.我猜这不是一种非常恐怖的做事方式.:)使用下面的表格好吗?

with open("file1") as f1:
    with open("file2") as f2:
        with open("file3") as f3:
            with open("file4") as f4:
                    # Read one line from each 'in_' file
                    # Do many operations on the DNA …
Run Code Online (Sandbox Code Playgroud)

python file

0
推荐指数
1
解决办法
1545
查看次数

C++交换通过int&参数传递的整数数组

我需要int i[2]使用void swap(int& x)函数交换格式中的几个整数.如您所见,该函数采用类型的参数int&.这是函数的非工作版本:

int i[2] = {3, 7};

void swap (int& x)
{
    int temp;
    temp = x[1];
    x[1] = x[0];
    x[0] = temp;
}

int main()
{
    cout << i[0] << ", " << i[1] << "\n"; // return the original: (3, 7)
    swap(i);
    cout << i[0] << ", " << i[1] << "\n"; // return i swapped: (7, 3)
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

编辑:答案不能使用任何其他功能参数.它必须使用一个int&参数.这是Bjarne Stroustrup的书中的一个问题:"C++编程语言",第三版.问题#4来自第5章.问题首先要求编写一个带有int*as参数的函数,而不是修改它以接受int&as参数.

c++ pointers

-1
推荐指数
1
解决办法
916
查看次数