我有一个sqlalchemy(实际上是Flask-sqlalchemy因此所有的数据库.*),我希望能够通过他们与之相关的"投票"的平均投票价值来排序我的"事物".投票的值为0到100.
遇到sqlalchemy想将average_vote_value @attribute翻译成sql并且失败的问题我发现我应该使用hybrids:
但是,我无法弄清楚在这种情况下如何做到这一点.有人可以帮忙吗?
class Thing(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
votes = db.relationship('Vote', backref='thing', lazy='dynamic')
@hybrid_property
def average_vote_value(self):
'''average of vote.values'''
values = [v.value for v in self.votes]
try:
return sum(scores) / len(values)
except ZeroDivisionError:
return 50 # the default value
average_vote_value.expression
def average_vote_value(cls):
pass ### help ###
class Vote(db.Model):
id = db.Column(db.Integer, primary_key=True)
thing_id = db.Column(db.Integer, db.ForeignKey('thing.id'))
value = db.Column(db.Float, default=50.0)
Run Code Online (Sandbox Code Playgroud) 在我的剧本中,我以非root用户身份SSH进入,然后使用become成为root。
Ansible变量名称是什么,它将最初通过SSH加密的用户存储在框中?
在Ansible <2.0中,我可以使用它{{ ansible_ssh_user }}来访问用户名SSH'到框中。
刚刚尝试使用Ansible 2.0.2,然后返回null。我ansible_user按照FAQ的建议进行了尝试,但同样可以返回null。我也尝试过ansible_user_id,但是返回的结果become不是原始用户。
绝对是绝对的还是不绝对的?
我正在尝试使用嵌套元素创建一个输入表单,每个元素包含其他元素,但它不能正确显示(根据屏幕标尺(和肉眼)).
HTML是有效的,所以这是一个"好吧,它是绝对的,但只相对于它包含文件夹"的案例或者其他一些?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
<body>
<form action="C:\temp\a_test.php" method="get"><div class="TGroupBox" id="GroupBox1">
<fieldset style="position: absolute; top:24px; left:24px; width: 449px; height: 473px;">
<legend>GroupBox1</legend>
<div class="TPanel" id="Panel1">
<fieldset style="position: absolute; top:64px; left:64px; width: 361px; height: 217px;">
<legend></legend>
<div class="TComboBox" id="ComboBox1" style="position: absolute; top:88px; left: 256px; width: 145px; height: 21px;">
<select name="ComboBox1">
<option value="- one -" selected="selected">- one -</option>
<option value="- two -">- two -</option>
<option value="- three -">- three -</option>
</select>
</div> …Run Code Online (Sandbox Code Playgroud) 我在macbook air上运行python脚本,处理来自外部API的数据,通常需要几个小时或偶尔几天.
但是,有时我需要在运行脚本的过程中暂停我的笔记本电脑,以便我可以去上班或回家或类似.
如何在for循环中间暂停/恢复这些脚本?
是否有一些非常简单的东西,我可以在脚本级别添加,只是侦听特定的键击停止/启动?或者我可以在*nix流程管理级别做些什么?
我很清楚,Pickle但我宁愿不处理序列化/反序列化我的数据的麻烦 - 因为我正在做的就是休眠mac,我希望如果脚本暂停然后我休眠,那OS X将处理将RAM保存到磁盘,然后在重新打开计算机时恢复到RAM.那时,我可以点击一个简单的按键继续python脚本.
由于我在不同的wifi网络之间切换,因此当我的脚本试图访问互联网以访问第三方API时,不确定不同的IP是否会导致问题.
我在PostgreSQL 9.3表中有几百万行,而我正在努力计算汇总统计数据.
以下是列:
object_id | date | column_with_json_data_in_key-value_format | bunch_of_irrelevant_columns
每个object_id/date组合都有一行.JSON列中大约有70个键,但任何给定的行只包含~5-20个键.
在所有object_id中,对于每个键,每日平均分数值是多少?
所以我想要的输出是一个包含以下列的表:
date | key1_name | key1_fractional_average | keyN_name | keyN_fractional_average
我尝试了这个查询单行,但它无法正常工作:
SELECT value2, total, (value2/total * 100) AS percent FROM
(SELECT SUM(value) AS total FROM
(SELECT CAST(CAST(value AS text) AS integer) FROM json_each((SELECT json_column
FROM original_table
WHERE primary_key = 1)::json)) as table1) as total_table,
(SELECT CAST(CAST(value AS text) AS integer) AS value2 FROM json_each((SELECT json_column
FROM original_table
WHERE primary_key = 1)::json)) as value_table
Run Code Online (Sandbox Code Playgroud)
对于此特定的page_id/insight_date行,查询将返回以下内容: …
今天,我为nginx安装了mod_security.我将以下块添加到/etc/nginx/nginx:
server {
listen 80;
server_name localhost;
location / {
ModSecurityEnabled on;
ModSecurityConfig modsecurity.conf;
}
}
Run Code Online (Sandbox Code Playgroud)
重新启动Nginx后,我收到以下错误:
nginx: [emerg] unknown directive "ModSecurityEnabled" in /etc/nginx/conf.d/nginx.conf:6
nginx: configuration file /etc/nginx/nginx.conf test failed
Run Code Online (Sandbox Code Playgroud)
产量nginx -V:
nginx version: nginx/1.4.7
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module …Run Code Online (Sandbox Code Playgroud) 我有一个SLQALchemy对象,我正在使用marshmallow序列化.
该对象有N个赞和N个评论.它看起来像这样:
class Parent():
__tablename__ = 'parent'
title = Column(db.String(100), unique=True, nullable=False)
description = Column(db.String(500))
created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
comments = relationship('Comment')
likes = relationship('Like')
Run Code Online (Sandbox Code Playgroud)
序列化器看起来像这样:
class CommentSerializer(Serializer):
class Meta:
fields = ('id', 'text', 'created_at', 'parent_id')
class LikeSerializer(Serializer):
class Meta:
fields = ('id', 'created_at', 'parent_id')
class ParentSerializer(Serializer):
comments = fields.Nested(CommentSerializer)
likes = fields.Nested(LikeSerializer)
class Meta:
fields = ('id', 'title', 'description', 'comments', 'likes')
Run Code Online (Sandbox Code Playgroud)
我尝试在视图中运行它,如下所示:
allParents = Parent.query.all()
Run Code Online (Sandbox Code Playgroud)
并将其转换为JSON:
return jsonify({"parents": ParentSerializer(allParents, many=True).data})
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我收到一个错误list indices must be integers, …
我正在论坛中清理垃圾邮件帐户,并找到了一堆具有以下格式的电子邮件地址:
n.fl.jersey.s@gmail.com
nfl.jerseys@gmail.com
n.flj.er.se.ys@gmail.com
Run Code Online (Sandbox Code Playgroud)
Gmail将这些视为同一个电子邮件帐户,而论坛软件将其视为不同的电子邮件地址,因此垃圾邮件发送者使用此技巧在创建垃圾论坛帐户时一次又一次地重复使用相同的电子邮件地址.
为了识别它们,我需要去除之前的所有时期@gmail.com.然后很容易识别所有重复的帐户.
幸运的是,MariaDB 10具有REGEXP_REPLACE针对这些类型的问题而设计的新功能.不幸的是,我无法弄清楚正确的正则表达式.
我的主要障碍是周期数变化很大,而且当项目数量随整个字符串随机变化时,我不确定如何编写正则表达式.我在其中一个电子邮件地址中找到了多达8个句点,完全随机,它们将显示在字符串中.
如果我可以剥离所有时期,但我不能,因为我需要@gmail.com保持不变,这很容易.此外,此正则表达式应仅匹配@ gmail.com地址并忽略其他电子邮件提供商.
我该怎么做呢?
我有一个Ansible任务,它包装了一个别人维护的相当复杂的shell脚本.此shell脚本安装了大量自定义的Web堆栈,其中许多组件都是从源代码编译的.
shell脚本经常会将各种组件更新为更新的版本.发生这种情况时,我希望管理这些服务的conf文件的Ansible模板任务失败,以便我可以手动查看更改的内容.
使用Ansible,如何检查已安装的某些版本并将其注册为我可以在以后的任务中使用的变量?
示例:MariaDB是从源代码编译的,所以我想创建一个等于输出的Ansible变量mysql --version.
在使用 Flask 的内置test_client进行测试时,我发现无法自动解码 JSON 响应。
我用于测试登录过程的一些 pytest 代码是:
@pytest.mark.parametrize('user_info', [sign_in])
def test_sign_in_post(self, user_info, client):
r = client.post(
f'/{ApiConfig.name}/sign_in',
data=json.dumps(user_info),
content_type='application/json',
)
assert r.status_code == 200
assert 'session_key' in json.loads(r.data.decode())
Run Code Online (Sandbox Code Playgroud)
我要问的部分是json.loads(r.data.decode())片段。有一个更好的方法吗?
requests例如,在模块中,代码将是assert 'session_key' in r.json()
flask ×2
python ×2
sqlalchemy ×2
ansible ×1
ansible-2.x ×1
css ×1
css-position ×1
html ×1
hybrid ×1
json ×1
macos ×1
mariadb ×1
marshmallow ×1
mod-security ×1
nginx ×1
postgresql ×1
python-2.7 ×1
regex ×1
replace ×1
resume ×1