我有一些带有时间戳字段的模型:
models.py
class Event(models.Model):
event_type = models.CharField(
max_length=100,
choices=EVENT_TYPE_CHOICES,
verbose_name=_("Event Type")
)
event_model = models.CharField(
max_length=100,
choices=EVENT_MODEL_CHOICES,
verbose_name=_("Event Model")
)
timestamp = models.DateTimeField(auto_now=True, verbose_name=_("Timestamp"))
Run Code Online (Sandbox Code Playgroud)
然后我使用Django-rest-framework为这个类创建一个API端点,django-filter提供了如下的过滤功能:
from .models import Event
from .serializers import EventSerializer
from rest_framework import viewsets, filters
from rest_framework import renderers
from rest_framework_csv import renderers as csv_renderers
class EventsView(viewsets.ReadOnlyModelViewSet):
"""
A read only view that returns all audit events in JSON or CSV.
"""
queryset = Event.objects.all()
renderer_classes = (csv_renderers.CSVRenderer, renderers.JSONRenderer)
serializer_class = EventSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_fields = …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置虚拟环境,但是当我尝试激活它时,我收到以下错误:
virtualenv hello
-bash: /usr/local/bin/virtualenv: /usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/Resour: bad interpreter: No such file or directory
Run Code Online (Sandbox Code Playgroud)
什么可能出错?我的$ PATH如下:
Users/me/workspace/google-cloud-sdk/bin:/usr/local/bin:/Users/me/workspace/google-cloud-sdk/bin:/urs/local/bin:/Users/me/workspace/google-cloud-sdk/bin:/urs/local/bin:/Users/me/workspace/google-cloud-sdk/bin:usr/local/python:usr/bin/python:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/texbin: No such file or directory
Run Code Online (Sandbox Code Playgroud)
- 它返回错误的事实使我怀疑我以某种方式设置了我的.bash_profile错误.这些是我所做的唯一修改:
#Set architecture flags
export ARCHFLAGS="-arch x86_64"
#Ensure user-installed binaries take precedence
export PATH=/usr/local/bin:$PATH
#Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc
# The next line updates PATH for the Google Cloud SDK.
source '/Users/me/workspace/google-cloud-sdk/path.bash.inc'
# The next line enables bash completion for gcloud.
source '/Users/me/workspace/google-cloud-sdk/completion.bash.inc'
Run Code Online (Sandbox Code Playgroud)
这是我的.bashrc:
# pip should only run if there is …Run Code Online (Sandbox Code Playgroud) 我目前有一些数据集,结构如下:
data = {'participant': [100, 101, 102, 103, 104, 105, 106, 107, 108, 109],
'step_name': ['first', 'first', 'second', 'third', 'second', 'first', 'first', 'first', 'second', 'third'],
'title': ['acceptable', 'acceptable', 'not acceptable', 'acceptable', 'not acceptable', 'acceptable', 'not acceptable', 'acceptable', 'acceptable', 'acceptable'],
'colour': ['blue', 'blue', 'blue', 'green', 'green', 'blue', 'green', 'blue', 'blue', 'green'],
'class': ['A', 'B', 'B', 'A', 'B', 'A', 'A', 'A', 'A', 'B']}
df = pd.DataFrame(data, columns=['participant', 'step_name', 'title', 'colour', 'class'])
Run Code Online (Sandbox Code Playgroud)
看起来像:
+----+---------------+-------------+----------------+----------+---------+
| | participant | step_name | title | colour …Run Code Online (Sandbox Code Playgroud) 当我在管理员中看到日期和时间时,它们以UTC显示。我希望它们显示在我的本地时区。我检查了TIME_ZONE设置的文档,虽然它看起来不像是我所需的东西。TIME_ZONE确定存储在数据库中,这是不是我想设置的日期时间的时间段-我只是想localize为admin时区,但不会改变他们是如何保存在数据库级别。
有没有办法做到这一点?
我有一个应用程序,它接收一些信息,使用pandas执行一些计算,并将最终的pandas数据框转换为CSV,然后使用Flask应用程序下载.如何在一个视图中下载多个CSV?似乎我一次只能返回一个响应.
一个示例代码段:
def serve_csv(dataframe,filename):
buffer = StringIO.StringIO()
dataframe.to_csv(buffer, encoding='utf-8', index=False)
buffer.seek(0)
return send_file(buffer,
attachment_filename=filename,
mimetype='text/csv')
def make_calculation(arg1, arg2):
'''Does some calculations.
input: arg1 - string, arg2- string
returns: a pandas data frame'''
@app.route('test_app', methods=['GET', 'POST']
def test_app():
form = Form1()
if form.validate_on_submit():
calculated_dataframe = make_calculation(str(form.input_1.data), str(form.input_2.data))
return serve_csv(calculated_dataframe, 'Your_final_output.csv')
return render_template('test_app.html', form=form)
Run Code Online (Sandbox Code Playgroud)
所以我们在上面的例子中说make_calculation返回了两个pandas数据帧.如何将它们打印到CSV?
假设我有以下文件,学生
John
Mary
Bill
Joe
Run Code Online (Sandbox Code Playgroud)
然后我加载我的数据如下:
a = load 'student' as (name:chararray);
Run Code Online (Sandbox Code Playgroud)
我希望b的结果不区分大小写:
b = filter a by name matches ('.*john.*');
Run Code Online (Sandbox Code Playgroud)
也就是说,返回一场比赛,(约翰).我已经尝试使用正则表达式设置'?i.*john.*',但这似乎不起作用.
我正在为同时拥有公司和部门的应用程序设计 REST API。一定数量的用户可以是公司的成员。这导致了以下 API 结构:
/companies/ - 可以获取,发布。
/companies/<pk>/ - 可以获取、发布、放置、修补、删除。
/companies/<pk>/membership/ - 可以 GET(提供公司成员的所有用户)、POST。
/companies/<pk>/membership/<pk>/ - 可以删除。
我已经设法实现了前 3 个端点,但是在实现最后一个端点时遇到了麻烦——如何实现<pk>在 URL中有多个值的端点?这是我到目前为止所拥有的:
目前在api应用程序中有一个 urls.py 文件,如下所示:
...
url(r'^company', include(company_urls.company_router.urls,
namespace="company")),
...
Run Code Online (Sandbox Code Playgroud)
urls.py在company应用程序中。
from rest_framework import routers
from .views import CompanyViewSet
company_router = routers.DefaultRouter()
company_router.register(r'^', CompanyViewSet)
Run Code Online (Sandbox Code Playgroud)
serializers.py 文件:
from rest_framework import serializers
from .models import Company, CompanyMembership
from My_App.users.models import Profile
class CompanySerializer(serializers.ModelSerializer):
class Meta:
model = Company
fields = ('pk', 'name', 'departments', 'members')
read_only_fields …Run Code Online (Sandbox Code Playgroud) 我试图从confusionMatrix()输出中提取Accuracy值 - 我尝试使用以下代码:
cl <- train.data[,1]
knn.res <- knn.cv(train.data[,c(2:783)], cl, k = i, algorithm = "cover_tree")
confus.knn.res <- confusionMatrix(knn.res, train.data[,1])
confus.knn.res
k.accuracy[which(k.accuracy[,2]==i),2] <- confus.knn.res$Accuracy
Run Code Online (Sandbox Code Playgroud)
虽然只是称它为$ Accuracy似乎不起作用.
我有一个html文件,我通过使用Bokeh库创建可视化来生成.我想将它包含在我的django站点中,但是,当我尝试将其添加为视图时,我得到了TemplateSyntaxError.似乎页面中的某些语法与Django的模板系统相冲突.
如何让Django服务于页面而不试图将其解析为模板?
我想设置我的 Django 应用程序以使用异步任务队列通过 Gmail 设置电子邮件。我正在使用 Celery,并以 Redis 作为我的经纪人。但是,当我将 Celery 定义为电子邮件后端时,我无法发送电子邮件 - 我收到一条错误消息,指出连接失败:
...:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/Users/user/virtualenvs/myapp/lib/python3.4/site-packages/kombu/utils/functional.py in __call__(self)
35 try:
---> 36 return self.__value__
37 except AttributeError:
AttributeError: 'ChannelPromise' object has no attribute '__value__'
During handling of the above exception, another exception occurred:
ConnectionRefusedError Traceback (most recent call last)
/Users/user/virtualenvs/myapp/lib/python3.4/site-packages/kombu/connection.py in _ensured(*args, **kwargs)
493 try:
--> 494 return fun(*args, **kwargs)
495 except conn_errors as exc:
/Users/user/virtualenvs/myapp/lib/python3.4/site-packages/kombu/messaging.py in _publish(self, body, priority, content_type, content_encoding, headers, properties, routing_key, …Run Code Online (Sandbox Code Playgroud) 我有多个应用程序,一个Django项目app_1,app_2和app_3。当前,为了运行测试套件,我使用标准的测试命令:python manage.py test。这将为我所有的应用程序运行测试。我还能够为单个应用程序运行测试,例如- python manage.py test app_1/。但是,我希望能够在部分而非全部应用中运行所有测试。例如,我会跑python manage.py test main_tests,只是在测试app_1和app_2运行。有没有办法做到这一点?
我看到这个问题指定了如何在单个应用程序中仅运行几个测试,却没有指定如何在项目中仅运行几个应用程序的测试。