小编Phi*_*ati的帖子

什么是导入和提供可选功能的Python良好实践?

我在github上写了一个软件.它基本上是一个带有一些额外功能的托盘图标.我想提供一段工作代码而不必让用户安装本质上依赖于可选功能的东西,我实际上并不想导入我不会使用的东西所以我认为这样的代码将是"好的解决方案":

---- IN LOADING FUNCTION ----
features = []

for path in sys.path:
       if os.path.exists(os.path.join(path, 'pynotify')):
              features.append('pynotify')
       if os.path.exists(os.path.join(path, 'gnomekeyring.so')):
              features.append('gnome-keyring')

#user dialog to ask for stuff
#notifications available, do you want them enabled?
dlg = ConfigDialog(features)

if not dlg.get_notifications():
    features.remove('pynotify')


service_start(features ...)

---- SOMEWHERE ELSE ------

def service_start(features, other_config):

        if 'pynotify' in features:
               import pynotify
               #use pynotify...
Run Code Online (Sandbox Code Playgroud)

但是有一些问题.如果用户格式化他的机器并安装最新版本的操作系统并重新部署此应用程序,则功能会在没有警告的情况下突然消失.解决方案是在配置窗口中显示:

if 'pynotify' in features:
    #gtk checkbox
else:
    #gtk label reading "Get pynotify and enjoy notification pop ups!"
Run Code Online (Sandbox Code Playgroud)

但是,如果这是一个mac,我怎么知道我不是在寻找一个他们永远无法填充的依赖关系的疯狂追逐用户?

第二个问题是:

if os.path.exists(os.path.join(path, …
Run Code Online (Sandbox Code Playgroud)

python python-import

26
推荐指数
2
解决办法
7594
查看次数

Javascript:如何阻止多个jQuery ajax错误处理程序?

团队成员将此项目纳入我们的项目

$(function() {
    $("body").bind("ajaxError", function(event, XMLHttpRequest, ajaxOptions, thrownError){
        alert(thrownError);
    });
}
Run Code Online (Sandbox Code Playgroud)

但是我想压制我的一个错误(因为它会是噪音并且不需要验证)

function blah() {

    ...

    errFunc = function(event, xhr, opts, errThrown) {
        //what could I do here?
        //event.stopImmediatePropagation()?
    }

    $.ajax({
        url        : '/unimportant_background_refresh',
        type       : 'GET',
        data       : { },
        dataType   : 'json',
        success    : updateBackgroundStuff,
        error      : errFunc,  // Suppresses error message.
    });

}
Run Code Online (Sandbox Code Playgroud)

我该如何阻止所有错误发生?我可以在错误函数中做一些事情,{ event.StopPropogation(); }或者我是否必须找出一些机制让捕获所有选择性地忽略它?

javascript error-handling jquery

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

我的简单poll()示例仅部分有效

我已经包含了以下代码.该程序应该接受端口8888上的telnet连接,然后使用poll和send和recv从每个telnet客户端发送和消息,但它不是100%工作.似乎某些连接总是可以向任何人发送消息,并且程序运行正常,但总有至少一个客户端无法发送消息.所有客户都可以随时收到.(民意调查不会注册传入的数据)

这段代码自己运行,所以如果你把它放在一个文件中并用gcc -o app filename.c编译它,那么你可以通过端口8888 telnet到localhost并看到它无效.:-(这段代码是为Fedora编写的,但不应该有任何非特定于Linux的内容.任何帮助都会非常感激.

#include <stdio.h>
#include <stdlib.h>
#include <poll.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

#define PORT 8888
#define MAX_CONN 10
#define SECOND 1000
#define TIMEOUT (30 * SECOND)

static int listen_socket();

int main(int argc, char **argv)
{
    struct pollfd **my_fds;                  //array of pollfd structures for poll()
    struct pollfd *curr, *new_conn;          //so I can loop through
    int num_fds;                             //count of how many are being used …
Run Code Online (Sandbox Code Playgroud)

c sockets networking polling

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

帮我完成我应用的最后一部分?它通过强制解决每个可能的等式来解决第4频道上的任何倒计时数字游戏

对于那些不熟悉游戏的人.您将获得8个数字,并且必须使用+, - ,/和*来达到目标​​.

因此,如果目标是254并且您的游戏数量是2,50,5,2,1,那么您将通过说5*50 = 250来正确回答问题.然后2 + 2是4.添加它以及得到254.

有些游戏视频在这里:

视频1 视频2

基本上我通过生成所有大小的所有权限来为数字和符号的所有权限强制使用游戏,并使用基本的inflix计算器来计算解决方案.

然而,它包含一个缺陷,因为所有解决方案都解决如下:((((1 + 1)*2)*3)*4).它没有排列括号,这让我很头疼.

因此我无法解决所有方程式.例如,给定

目标16和数字1,1,1,1,1,1,1,1它应该做(1 + 1 + 1 + 1)*(1 + 1 + 1 + 1)= 16时失败.

我喜欢它,有人可以帮助完成这个...用任何语言.

这是我到目前为止所写的:

 #!/usr/bin/env perl

use strict;
use warnings;

use Algorithm::Permute;

# GAME PARAMETERS TO FILL IN
my $target = 751;
my @numbers = ( '2', '4', '7', '9', '1', '6', '50', '25' );


my $num_numbers = scalar(@numbers);

my @symbols = ();

foreach my $n (@numbers) {
    push(@symbols, ('+', '-', '/', …
Run Code Online (Sandbox Code Playgroud)

perl permutation

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

如何为具有引用成员变量的类实现operator =()?

我想要一个包含引用的对象,并将该对象放入向量中...

我必须在我想要推送到矢量的任何对象中使用智能指针而不是成员引用吗?这就是我想要做的:

#include <string>
#include <vector>
using namespace std;

class MyClass {
   public:
    MyClass(const string& str_ref);    //constructor
        MyClass(const MyClass& mc);        //copy constructor
   private:
        string& my_str;
};

MyClass::MyClass(const string& str_ref) :
    my_str(str_ref)
{}

MyClass::MyClass(const MyClass& mc) :
    my_str(mc.my_str)
{}


int main() {

    //create obj and pass in reference
    string s = "hello";
    MyClass my_cls(s);

    //put into vector
    vector<MyClass> vec;
    vec.push_back(my_cls);

    return 0;
}

//Throws Error
//ref.cpp:6:7: error: non-static reference member ‘std::string& MyClass::my_str’, can’t use default assignment operator
Run Code Online (Sandbox Code Playgroud)

但是它说我需要实现我自己的operator =(),因为默认生成的一个是无效的,但当然,没有合法的方法可以这样做......

#include <string> …
Run Code Online (Sandbox Code Playgroud)

c++ oop operator-overloading unassigned-variable

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

将tinyxml2与Visual Studio,Visual C++ MFC应用程序一起使用

我正在尝试习惯MFC开发和Visual Studio,因此我安装了Visual Studio 2012 RC并创建了一个简单的MFC应用程序.目前,该应用程序只是为我生成的MFC向导.

我决定要合并一个XML库,所以我在github上找到了这个.我下载包含源代码的ZIP文件,解压缩然后在Visual Studio中转到解决方案资源管理器,选择我的解决方案,右键单击并选择"添加">"现有项目".我选择源代码的项目文件,它出现在我的解决方案资源管理器树中.

我测试代码编译,它确实.但是我不太确定如何从我当前的解决方案中使用它.

我尝试在我的doc中使用此代码:

#include "../../TinyXML2/leethomason-tinyxml2-a3efec0/tinyxml2.h"

<...snip...>

BOOL LoadDocumentFromXML(const CString& filename) {

    CT2CA pszConvertedAnsiString (filename);
    std::string s(pszConvertedAnsiString);

    tinyxml2::XMLDocument doc(true);
    if (tinyxml2::XML_NO_ERROR != doc.LoadFile(s.c_str())) {
        return FALSE;
    }



    return TRUE;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试构建项目时,我收到此链接器错误:

------ Build started: Project: GraphApp, Configuration: Debug Win32 ------
  GraphAppDoc.cpp
GraphAppDoc.obj : error LNK2019: unresolved external symbol "public: __thiscall tinyxml2::XMLDocument::XMLDocument(bool)" (??0XMLDocument@tinyxml2@@QAE@_N@Z) referenced in function "int __cdecl LoadDocumentFromXML(class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?LoadDocumentFromXML@@YAHABV?$CStringT@_WV?$StrTraitMFC_DLL@_WV?$ChTraitsCRT@_W@ATL@@@@@ATL@@@Z)
GraphAppDoc.obj : error LNK2019: unresolved …
Run Code Online (Sandbox Code Playgroud)

visual-studio visual-c++

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

C++:如果所有数据都已经有效,是否存在在构造函数中验证的开销?

我想在我的构造函数中抛出异常,这样我就不必处理僵尸对象了.但是,我还想提前提供验证方法,以便人们可以避免在没有原因的情况下"处理异常".在GUI中,期望无效数据并不例外.但是我也想避免代码重复和开销.GCC/Microsoft Visual C++编译器是否足够聪明,可以消除两次​​验证输入的低效率,如果没有,是否会有一个微妙的变化可以取悦?

我的观点的示例代码块如下:

#include <string>
#include <exception>
#include <iostream>

using std::string;
using std::cout;
using std::endl;
using std::exception;


// a validation function
bool InputIsValid(const string& input) {
    return (input == "hello");
}

// a class that uses the validation code in a constructor
class MyObject {
    public:

    MyObject(string input) {
        if (!InputIsValid(input))    //since all instances of input
            throw exception();       //has already been validated
                                     //does this code incur an overhead
                                     //or get optimised out?

        cout << input << endl; …
Run Code Online (Sandbox Code Playgroud)

c++ validation optimization code-duplication

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