小编nck*_*ner的帖子

Python从多个线程记录

我有一个log.py模块,用于至少两个其他模块(server.pydevice.py).

它有这些全局变量:

fileLogger = logging.getLogger()
fileLogger.setLevel(logging.DEBUG)
consoleLogger = logging.getLogger()
consoleLogger.setLevel(logging.DEBUG)

file_logging_level_switch = {
    'debug':    fileLogger.debug,
    'info':     fileLogger.info,
    'warning':  fileLogger.warning,
    'error':    fileLogger.error,
    'critical': fileLogger.critical
}

console_logging_level_switch = {
    'debug':    consoleLogger.debug,
    'info':     consoleLogger.info,
    'warning':  consoleLogger.warning,
    'error':    consoleLogger.error,
    'critical': consoleLogger.critical
}
Run Code Online (Sandbox Code Playgroud)

它有两个功能:

def LoggingInit( logPath, logFile, html=True ):
    global fileLogger
    global consoleLogger

    logFormatStr = "[%(asctime)s %(threadName)s, %(levelname)s] %(message)s"
    consoleFormatStr = "[%(threadName)s, %(levelname)s] %(message)s"

    if html:
        logFormatStr = "<p>" + logFormatStr + "</p>"

    # File Handler for log …
Run Code Online (Sandbox Code Playgroud)

python logging multithreading

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

在Python异常后我需要一个return语句吗?

我对python很新,我想确保我正确地做到这一点.我想要一个异常类:

class UnknownCommandReceived(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)
Run Code Online (Sandbox Code Playgroud)

如果没有正则表达式匹配,我将在此函数结束时引发异常:

def cmdType(self):
    match = re.match(r'(<[ \w]+>),\s*(\d+)?,?\s*(\d+)?', cmd, re.IGNORECASE)
    if match: 
        cmd_type = 'int_tool'
        return cmd_type, match

    match = re.match(r'LCD\(([^\)]*)\)?_?(RED|YELLOW|GREEN|TEAL|BLUE|VIOLET|OFF|ON|SELECT|LEFT|DOWN|RIGHT)?', cmd, re.IGNORECASE)
    if match: 
        cmd_type = 'lcd'
        return cmd_type, match

    match = re.match(r'buffer(_read|_num|_line)(\((\w)\))?', cmd, re.IGNORECASE)
    if match: 
        cmd_type = 'buffer'
        return cmd_type, match

    # ... More regex matches ... 

    raise UnknownCommandReceived( "cmdType received an unknown command" )

    # unecessary return?
    return 'None', None
Run Code Online (Sandbox Code Playgroud)

我的问题是 - 如果总是引发异常,那么我不需要在函数末尾使用return语句吗?道歉......这是一个非常基本的问题.我的理解是异常一旦引发异常,执行将永远不会返回到代码的那一点(除非它是一个循环,或者是一个再次调用的函数).它将直接进入捕获并从那里继续?

python exception

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

可选的jDBI参数

jDBI查询是否可以具有可选(空)参数?我试图让可选参数在数据库查询中工作。我正在与dropwizard合作。

@SqlQuery("SELECT * \n" +
          "FROM posts \n" +
          "WHERE (:authorId IS NULL OR :authorId = author_id)")
public List<Post> findAll(@Bind("authorId") Optional<Long> authorId);
Run Code Online (Sandbox Code Playgroud)

传递authorId时,该查询有效,但为NULL时会出现此错误:

org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $1
Run Code Online (Sandbox Code Playgroud)

这是我从以下位置拨打的资源路线:

@GET
public ArrayList<Post> getPosts(@QueryParam("authorId") Long authorId)
{
    return (ArrayList<Post>)postDao.findAll(Optional.fromNullable(authorId));
}
Run Code Online (Sandbox Code Playgroud)

根据我的阅读,这是可以做到的,所以我猜我正在丢失某些东西或有明显的错误。任何帮助将不胜感激!

仅供参考-我也尝试了不使用番石榴可选(dropwizard支持)的方法-只是将authorId发送为null的Long。只要它不为null,它也可以工作。

java jdbi dropwizard

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

为什么在这种情况下不调用赋值运算符而支持复制构造函数?

从复制构造函数的维基百科页面:

X a = X();     

// valid given X(const X& copy_from_me) but not valid given X(X& copy_from_me)
// because the second wants a non-const X&
// to create a, the compiler first creates a temporary by invoking the default constructor
// of X, then uses the copy constructor to initialize as a copy of that temporary. 
// For some compilers both versions actually work but this behaviour should not be relied 
// upon because it's non-standard.
Run Code Online (Sandbox Code Playgroud)

特别是部分:

"编译器首先通过调用X的默认构造函数创建一个临时文件,然后使用复制构造函数初始化为该临时文件的副本."

我的问题是(假设这是正确的)为什么会这样?从代码中,我猜想编译器在构造X之后会使用赋值运算符.

我猜它是因为赋值发生在与初始化相同的表达式中? …

c++ oop

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

如何将unique_ptr <char []>传递给要使用的函数

这基本上就是我想做的事情:

SerialPort::ReadBytes(int32& errcode, Message::string& msg, uint32 num)
{
    DWORD numBytesRead = 0;
    LPDWORD pNumBytesRead = &numBytesRead;
    errcode = 0;        

    std::unique_ptr <char[]> buff (new char[num]);
    // ^^^^ pass this char buffer to the ReadFile function below

    if (!ReadFile(m_sp_pointer,     // Handle to device
            buff,                  // Receives data from device
            num,                    // num bytes to read (in)
            (LPDWORD)pNumBytesRead, // num bytes read (out)
            NULL))
        {
            errcode = GetLastError();
        }

    if (numBytesRead > 0)
    {
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

我知道我没有正确地做到这一点,所以我的问题是:我如何正确地做到这一点,有什么能让这个想法变得糟糕吗?提前致谢.

编辑:我实际上应该在参数中传递unique_ptr而不是在本地声明它并传入 …

c++ pointers visual-c++ c++11

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