小编Jah*_*yst的帖子

如何使用Flask-Restful Flask-Cache

如何在Flask-Restful中使用Flask-Cache @ cache.cached()装饰器?例如,我有一个继承自Resource的Foo类,而Foo有get,post,put和delete方法.

如何在一个POST?之后使缓存的结果无效?

@api.resource('/whatever')
class Foo(Resource):
    @cache.cached(timeout=10)
    def get(self):
        return expensive_db_operation()

    def post(self):
        update_db_here()

        ## How do I invalidate the value cached in get()?
        return something_useful()
Run Code Online (Sandbox Code Playgroud)

python redis cache-invalidation flask-cache flask-restful

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

Python Pandas - 读取包含多个表的csv文件

我有一个.csv包含多个表的文件.

使用熊猫,这将是拿到两个数据帧的最佳策略inventory,并HPBladeSystemRack从这个文件?

输入.csv看起来像这样:

Inventory       
System Name            IP Address    System Status
dg-enc05                             Normal
dg-enc05_vc_domain                   Unknown
dg-enc05-oa1           172.20.0.213  Normal

HP BladeSystem Rack         
System Name               Rack Name   Enclosure Name
dg-enc05                  BU40  
dg-enc05-oa1              BU40        dg-enc05
dg-enc05-oa2              BU40        dg-enc05
Run Code Online (Sandbox Code Playgroud)

到目前为止,我提出的最好的方法是将此.csv文件转换为Excel工作簿(xlxs),将表拆分为表并使用:

inventory = read_excel('path_to_file.csv', 'sheet1', skiprow=1)
HPBladeSystemRack = read_excel('path_to_file.csv', 'sheet2', skiprow=2)
Run Code Online (Sandbox Code Playgroud)

然而:

  • 这种方法需要xlrd模块.
  • 必须实时分析这些日志文件,以便找到一种分析它们来自日志的方法.
  • 真实的日志有比这两个更多的表.

python csv excel python-2.7 pandas

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

轻松解决-自定义错误处理

我想为Flask-restful API定义自定义错误处理。

在文档中建议的方法在这里是要做到以下几点:

errors = {
    'UserAlreadyExistsError': {
        'message': "A user with that username already exists.",
        'status': 409,
    },
    'ResourceDoesNotExist': {
        'message': "A resource with that ID no longer exists.",
        'status': 410,
        'extra': "Any extra information you want.",
    },
}
app = Flask(__name__)
api = flask_restful.Api(app, errors=errors)
Run Code Online (Sandbox Code Playgroud)

现在,我发现此格式非常吸引人,但是当发生某些异常时,我需要指定更多参数。例如,遇到时ResourceDoesNotExist,我要指定id不存在的内容。

目前,我正在执行以下操作:

app = Flask(__name__)
api = flask_restful.Api(app)


class APIException(Exception):
    def __init__(self, code, message):
        self._code = code
        self._message = message

    @property
    def code(self):
        return self._code

    @property
    def …
Run Code Online (Sandbox Code Playgroud)

python flask flask-restful

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

Chef-在其他资源失败时运行资源

我有两个execute资源称为command_1command_2

如果command_1失败,我要运行command_2然后重新运行command_1

几乎是这样的:

execute 'command_1' do
  command "ipa host-del #{machine_name}"
  action :run
  ignore_failure true
  on_failure { notifies :run, 'execute['command_2']', :immediately }
end

execute 'command_2' do
  command "ipa host-mod --certificate  #{machine_name}"
  action :nothing
  notifies :run, 'execute['command_1']', :immediately
end
Run Code Online (Sandbox Code Playgroud)

我该如何用实际可行的方法代替on_failure(如果Chef拥有这个,那会很棒)?

ruby chef-infra chef-recipe

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

Chef - 执行vs bash资源

我使用了execute资源或bash资源.

两者都达到了相同的结果:

bash 'Execute my script' do 
  user 'root'
  cwd  '/mydir'
  code <<-EOH
    ./myscript.sh
  EOH
end

execute 'Execute my script' do 
  user    'root'
  cwd     '/mydir'
  command './myscript.sh'
end
Run Code Online (Sandbox Code Playgroud)

我看到的唯一区别是bash实际上创建了一个shell脚本(名为/tmp/chef-script#{date}{#id})code写在哪里.

使用Chef之间executebash资源执行shell脚本的最佳做法是什么?

ruby recipe chef-infra

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

芹菜 - 在另一项任务结束时安排定期任务

我想在另一组任务结束时动态安排Celery的周期性任务.

我知道如何使用Celery创建(静态)周期性任务:

CELERYBEAT_SCHEDULE = {
      'poll_actions': {
          'task': 'tasks.poll_actions',
          'schedule': timedelta(seconds=5)
      }
}
Run Code Online (Sandbox Code Playgroud)

但我想从我的任务中动态创建周期性作业(并且可能有一种方法可以在达到某些条件时停止这些周期性作业(所有任务都已完成).

就像是:

@celery.task
def run(ids):
    group(prepare.s(id) for id in ids) | execute.s(ids) | poll.s(ids, schedule=timedelta(seconds=5))

@celery.task
def prepare(id):
    ...

@celery.task
def execute(id):
    ...

@celery.task
def poll(ids):
    # This task has to be schedulable on demand
    ...
Run Code Online (Sandbox Code Playgroud)

python celery

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

从Python列表中生成树

我有以下Python列表:

[
  ['a.b.c.d.e.rollover', 0],
  ['a.b.c.d.e.f.rollover', 1],
  ['a.b.c.d.e.g.rollover', 0]
]
Run Code Online (Sandbox Code Playgroud)

假设这个列表非常大(很多元素!)

Python中是否有一种有效的方法将其转换为如下所示的多级字典?

{
  'a': {
     'b': {
        'c': {
           'd': {
              'e': {
                 'rollover': 0,
                 'f': {
                    'rollover': 1
                 }
                 'g': {
                    'rollover': 0
                 }
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

python

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

Ruby条件赋值

如果a优于b,那么将变量a分配给另一个变量b的Ruby方法是什么?

到目前为止我有三种可能性:

  • if a > b then a = b end
  • a = b if a > b
  • a = a > b ? b : a

更广泛地说,有没有办法为变量设置上限? 例如:如果我尝试设置a = 50且a的上限为25,则在分配后a将等于25.

ruby

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