在我的 models.py 中,我有几个类和几个管理器类。
我有一些方法是模型使用的通用方法,但不一定是模型的一部分。
例如:
def profile_photo_upload_loc(instance, filename):
return "profile_photo/%s/%s" % (instance.user, filename)
class Profile(models.Model):
profile_photo = models.ImageField(
upload_to=profile_photo_upload_loc,
null=True,
blank=True,
width_field='width_field',
height_field='height_field',
verbose_name='Profile Image',
)
Run Code Online (Sandbox Code Playgroud)
模型文件中的类之外可以有独立的函数吗?什么时候合适以及什么时候函数应该位于类内部?
我正在尝试从 Django 中的 JSON 数据 -> Javascript Date 对象。
目前,我已经对其进行了设置,以便我有一个可以访问 API 并将 JSON 数据保存到我的模型对象之一中的函数。
编辑:JSON 日期时间字符串如下所示: '2017-01-14 14:00:00'
然后在我的视图中,我将查询包含 JSON 的对象,并将从 JSON 获取的日期时间字符串作为上下文变量发送到我的模板。
在模板中,我尝试使用 Google Chart 绘制字符串数据的图表,而 Google Charts 要求折线图的第一列是 JS Date 对象。如何将发送的模板变量:{{ date }}转换为与Javascript等效的格式
new Date(2017, 01, 14)
Run Code Online (Sandbox Code Playgroud)
这样它就可以与谷歌图表一起使用?
编辑:问题不是我不知道如何格式化字符串,而是我不知道如何让字符串首先出现,因为它将首先是一个 django 模板变量。
我正在尝试为依赖模块级数据(JSON文件)的程序编写单元测试。
因此,我当时想使用setUpClass类方法设置一个测试JSON文件,然后在运行测试后将其删除。
我遇到的问题是,模块级JSON的设置返回的值是我也打算测试的程序的其他功能所必需的。
这是我的意思的示例:
import unittest
import myProg
class TestProg(unittest.TestCase):
@classmethod
def setUpClass(cls):
# initialize() creates the JSON file
myProg.initialize()
f = myProg.initialize_storage()
return f
def test_prog_func(self):
myProg.prog_func("test_key", "test_value", f)
Run Code Online (Sandbox Code Playgroud)
f是我其余功能需要的项目。此代码不起作用。我正在寻找一种方法,允许我return f从setUpClass中“ ”在整个测试中使用。
该模型做什么?
我看到有关NLP和ML的术语普遍存在,似乎没有一个具体的定义。
关于NLP和SpaCy,这些模型完成了哪些工作?
import spacy
from spacy import displacy
nlp = spacy.load('en_core_web_sm')
doc = nlp(u'This is a sentence.')
displacy.serve(doc, style='dep', options={'compact': True})
Run Code Online (Sandbox Code Playgroud) python ×3
class ×2
django ×2
javascript ×1
json ×1
nlp ×1
python-3.x ×1
spacy ×1
unit-testing ×1