在OSX上运行Python 2.6.1,将部署到CentOS.想要从命令行调用包,如下所示:
python [-m] tst
Run Code Online (Sandbox Code Playgroud)
为此,这是目录结构:
$PYTHONPATH/
tst/
__init__.py # empty
__main__.py # below
dep.py # below
Run Code Online (Sandbox Code Playgroud)
以下是文件:
$ cat tst/__main__.py
from .dep import DepClass
print "Hello there"
$ cat tst/dep.py
class DepClass(object):
pass
$
Run Code Online (Sandbox Code Playgroud)
但是,python给了我相互矛盾的诊断:
$ python -m tst
/usr/bin/python: tst is a package and cannot be directly executed
Run Code Online (Sandbox Code Playgroud)
好的,所以它被认为是一个包.所以我应该能够将其作为脚本运行?它有__main__......
$ python tst
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/runpy.py", line 121, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/runpy.py", line 34, in _run_code
exec code in …Run Code Online (Sandbox Code Playgroud) 为了获得今天时间对象的本地开始,我提取YMD并重建新日期.这看起来像一个kludge.我是否会错过其他一些标准库函数?
代码也可以在http://play.golang.org/p/OSRl0nxyB7上运行:
func Bod(t time.Time) time.Time {
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}
func main() {
fmt.Println(Bod(time.Now()))
}
Run Code Online (Sandbox Code Playgroud) 发出defer依赖于顺序的多个语句,或者推迟打包逻辑的匿名函数,是更安全还是更惯用?
例子:
defer os.Remove(tempFile.Name())
defer tempFile.Close()
Run Code Online (Sandbox Code Playgroud)
在上面的情况中,语法是最小的,但是延迟的顺序与要执行的逻辑相反.
在下面的情况下,有更多的行,更多的"语法",但逻辑是一个更自然的顺序:
defer func() {
tempFile.Close()
os.Remove(tempFile.Name())
}()
Run Code Online (Sandbox Code Playgroud)
哪一个使用?
在c++11代码中,每次使用枚举值时避免提及特定的枚举限定符会很好 - 因为它是一个新代码,并且它被重构了很多.
为此目的,这个伪代码的最后一行的精神可能是:
enum abc { a,b,c };
// some long code of events which returns the enum's value
auto e = []()->abc{return abc::b;}();
if (e == std::declval(e)::a) { ...
Run Code Online (Sandbox Code Playgroud)
如果不可能,C++11它会成为C++14或17?
在该连接对象升压ASIO HTTP服务器实例的方法do_read,并do_write在shared_from_this()捕捉到地址的连接对象的寿命问题,如先前回答.仍然不清楚为什么在第67和88行代码shared_from_this()再次调用,而不是使用self:
40 auto self(shared_from_this());
41 socket_.async_read_some(boost::asio::buffer(buffer_),
42 [this, self](boost::system::error_code ec, std::size_t bytes_transferred)
43 {
....
67 connection_manager_.stop(shared_from_this());
```
Run Code Online (Sandbox Code Playgroud) 当尝试将日志设置代码移动到单独的函数时,我遇到无法从main函数中隐藏目标文件对象.在下面的INCORRECT简化示例中,尝试通过单个函数调用设置日志写入Stderr和文件:
package main
import (
"io"
"log"
"os"
)
func SetupLogging() {
logFile, err := os.OpenFile("test.log", os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
log.Panicln(err)
}
defer logFile.Close()
log.SetOutput(io.MultiWriter(os.Stderr, logFile))
}
func main() {
SetupLogging()
log.Println("Test message")
}
Run Code Online (Sandbox Code Playgroud)
显然是不起作用因为defer在SetupLogging函数结束时关闭日志文件.
下面的一个工作示例添加了额外的代码,如果在较大的应用程序中重复作为模式,IMHO会失去一些清晰度:
package main
import (
"io"
"log"
"os"
)
func SetupLogging() *os.File {
logFile, err := os.OpenFile("test.log", os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
log.Panicln(err)
}
log.SetOutput(io.MultiWriter(os.Stderr, logFile))
return logFile
}
func main() { …Run Code Online (Sandbox Code Playgroud) 问题是关于在MS Visual C++ 11中开发的代码,只能访问STL,没有Boost.
有一个包装器模板类,大致有这个标头:
template <typename Payload>
class Wrapper {
Payload p;
std::string src;
Wrapper( std::string, Payload );
Payload get(); // returns payload
void set(Payload); // replaces payload
void operator ()(); // uses payload
}
Run Code Online (Sandbox Code Playgroud)
Payload 可能是任何东西 - 指针,int,甚至是重物.
之后,Wrapper需要进入一个容器,比如std::vector- 但不管它们的具体参数类型如何.这给我带来了麻烦,因为容器需要同质的元素.
我已经尝试了基类建议,这样从KennyTM,但它给了我与方法的一些问题get()和set()-那些需要投从载体使用时,因为元素,看起来像一个基类,如果通过回答提出的模式中使用(?).
c++ ×3
go ×3
c++11 ×2
boost ×1
boost-asio ×1
datetime ×1
generics ×1
module ×1
package ×1
python ×1
python-2.6 ×1
standards ×1
stl ×1
templates ×1
visual-c++ ×1