小编com*_*itc的帖子

Bottle.py错误路由

Bottle.py附带一个导入来处理抛出HTTP错误并路由到函数.

首先,文档声称我可以(以及几个例子):

from bottle import error

@error(500)
def custom500(error):
    return 'my custom message'
Run Code Online (Sandbox Code Playgroud)

但是,在导入此语句时,错误仍未解决,但在运行应用程序时忽略此错误,只是将我引导到通用错误页面.

我找到了解决这个问题的方法:

from bottle import Bottle

main = Bottle()

@Bottle.error(main, 500)
def custom500(error):
    return 'my custom message'
Run Code Online (Sandbox Code Playgroud)

但是这段代码阻止我将我的错误全部嵌入到一个单独的模块中来控制如果我将它们保存在我的main.py模块中会产生的肮脏,因为第一个参数必须是一个瓶子实例.

所以我的问题:

  1. 还有其他人经历过这个吗?

  2. 为什么没有错误似乎只解决我的情况(我从pip安装瓶安装)?

  3. 是否有一种无缝方式将我的错误路由从单独的python模块导入主应用程序?

python bottle

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

有条件地包括Dependency package.json

我在npm项目中只有Mac OS需要一个依赖项,并且想知道是否有某种方法只有在兼容平台运行时才有条件地包含此依赖项npm install.

我愿意为此写出逻辑.在下面的例子grunt-appdmg中导致npm安装过程出错(出于相当明显的原因):

'/dev/cuttle/node_modules/grunt-appdmg/node_modules/appdmg/node_modules/ds-store/node_modules/macos-alias/build'
  CXX(target) Release/obj.target/volume/src/volume.o
../src/volume.cc:9:2: error: #error This platform is not implemented yet
 #error This platform is not implemented yet
Run Code Online (Sandbox Code Playgroud)

的package.json

{
  "name": "Cuttle",
  "homepage": "https://github.com/oakmac/cuttle",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/oakmac/cuttle.git"
  },
  "dependencies": {
    "fs-extra": "0.16.3",
    "open": "0.0.5",
    "winston": "0.8.3"
  },
  "devDependencies": {
    "grunt": "0.4.5",
    "grunt-contrib-less": "0.11.4",
    "grunt-contrib-watch": "0.6.1",
    "grunt-curl": "2.0.3",
    "grunt-download-atom-shell": "0.10.0",
    "grunt-appdmg": "0.2.0",
    "winresourcer": "0.9.0",
    "moment": "2.9.0",
    "shelljs": "0.3.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

node.js npm

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

解析时间字符串的更好方法

我一直想知道是否有一种更简洁,更快捷的方法来解析时间戳,该时间戳可能是来自多个位置/日志文件的3种格式.下面的代码是我目前所拥有的,但它是一个非常难看的工作版本,非常慢.有更多的pythonic和更快的方法来实现这一目标吗?

FORMATS = ["%Y-%m-%d %H:%M:%S"      ,
           "%Y%m%d_%H.%M.%S"        ,
           "%a %b %d %H:%M:%S %Y"]


def _hacktime(self, t):
    # CAUTION: 
    # The nastiest time hack of all TIME
    #
    try:
        t = time.mktime(time.strptime(t, self.FORMATS[0]))
    except:
        try:
            t = time.mktime(time.strptime(t, self.FORMATS[1]))
        except:
            try:
                t = time.mktime(time.strptime(' '.join([t, 
                                                time.strftime('%Y')]),
                                                self.FORMATS[2]))
            except Exception as e:
                print('could not convert time %s: %s' % (t, e))
                t = time.time()   
    return [t, time.ctime(t)]
Run Code Online (Sandbox Code Playgroud)

python performance

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

标签 统计

python ×2

bottle ×1

node.js ×1

npm ×1

performance ×1