在我的Django应用程序中,我需要从referrer中获取主机名request.META.get('HTTP_REFERER')及其协议,以便从以下URL中获取:
我应该得到:
我查看了其他相关的问题,发现了关于urlparse,但是从那以后就没办法了
>>> urlparse(request.META.get('HTTP_REFERER')).hostname
'docs.google.com'
Run Code Online (Sandbox Code Playgroud) 我正在使用Mock库来测试我的应用程序,但我想断言某些函数没有被调用.模拟文档讨论像mock.assert_called_with和的方法mock.assert_called_once_with,但我没有发现任何类似mock.assert_not_called或与确认模拟相关的东西没有调用.
我可以使用类似下面的东西,虽然它看起来不酷也不像pythonic:
def test_something:
# some actions
with patch('something') as my_var:
try:
# args are not important. func should never be called in this test
my_var.assert_called_with(some, args)
except AssertionError:
pass # this error being raised means it's ok
# other stuff
Run Code Online (Sandbox Code Playgroud)
任何想法如何实现这一目标?
谢谢你的帮助 :)
我们知道,格式化一个参数是可以做到用一个 %s在一个字符串:
>>> "Hello %s" % "world"
'Hello world'
Run Code Online (Sandbox Code Playgroud)
对于两个参数,我们可以使用两个%s(duh!):
>>> "Hello %s, %s" % ("John", "Joe")
'Hello John, Joe'
Run Code Online (Sandbox Code Playgroud)
那么,如何格式化可变数量的参数而不必在基本字符串中明确定义一些%s等于格式化参数的数量?如果这样的事情存在,那将是非常酷的:
>>> "Hello <cool_operator_here>" % ("John", "Joe", "Mary")
Hello JohnJoeMary
>>> "Hello <cool_operator_here>" % ("John", "Joe", "Mary", "Rick", "Sophie")
Hello JohnJoeMaryRickSophie
Run Code Online (Sandbox Code Playgroud)
这是否可能,或者我唯一能做的就是做一些事情:
>>> my_args = ["John", "Joe", "Mary"]
>>> my_str = "Hello " + ("".join(["%s"] * len(my_args)))
>>> my_str % tuple(my_args)
"Hello JohnJoeMary"
Run Code Online (Sandbox Code Playgroud)
注意:我需要使用%s字符串格式化运算符.
更新: …
鉴于我有一个基于CharFieldor CharField的模型字段的遗留模型,如:
class MyModel(models.Model):
name = models.CharField(max_length=1024, ...)
...
Run Code Online (Sandbox Code Playgroud)
我需要进行迁移以使其具有max_length最大值.255.首先,我正在编写一个datamigration使任何超过255个字符的值适应即将到来schemamigration的修复列的最大长度,我将在此工作后立即执行.
问题是我有一个非常大的数据集,我知道并非所有行都包含超过255个字符的值MyModel.name,我想考虑我的迁移只有那些人.
是否有任何方法(使用)django ORM仅过滤满足此条件的对象?就像是:
MyModel.objects.filter(name__len__gte=255)
Run Code Online (Sandbox Code Playgroud)
会很棒,但我相信这是不可能的,或者至少它不是那么简单.
有人知道完成此查询的任何方法吗?
谢谢!
在我的项目中,我需要在这个例子中跟踪一些文件到版本控制,csv文件.但是这些文件包含大量的行并导致Github偶尔会压制必须通过代码审查的文件才能接受和合并拉取请求.
我试图.gitattributes将这些文件标记为二进制文件,或者只是为了不在diff中显示:
+*.csv -diff
+*.csv -merge
+*.csv binary
Run Code Online (Sandbox Code Playgroud)
一次一个,以及组合它们.这适用于终端上的差异:
$ git diff HEAD^
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..8a86f80
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+*.csv -diff -merge binary
diff --git a/AssetsImportCompleteSample.csv b/AssetsImportCompleteSample.csv
new file mode 100644
index 0000000..5b20a6e
Binary files /dev/null and b/AssetsImportCompleteSample.csv differ
Run Code Online (Sandbox Code Playgroud)
但是当分支被推送到Github并与另一个分支进行比较时,Github会忽略这些属性并将文件显示为文本,但.gitattributes是"自定义"diff应该显示的方式:

有没有办法强制在Github中的diff考虑属性,.gitattributes以自定义diff的行为,以便指示文件的差异被抑制?
提前致谢!
在我的一些模板中,我使用{% cache %}模板标签来缓存某些部分,但是对于开发,我不想要任何缓存.我尝试使用一个设置变量在一个单独的设置文件中为dev设置缓存过期时间为零,并使用a调用context_processor它,尽管它不起作用.
有没有人知道为开发环境禁用缓存的方法?
谢谢你的帮助 :)
我有一个课程如下:
class MyClass(object):
int = None
def __init__(self, *args, **kwargs):
for k, v in kwargs.iteritems():
setattr(self, k, v)
def get_params(self):
return {'int': random.randint(0, 10)}
@classmethod
def new(cls):
params = cls.get_params()
return cls(**params)
Run Code Online (Sandbox Code Playgroud)
我希望能够做到:
>>> obj = MyClass.new()
>>> obj.int # must be defined
9
Run Code Online (Sandbox Code Playgroud)
我的意思是没有创建一个新的实例MyClass,但显然它并不那么简单,因为调用MyClass.new()抛出TypeError: unbound method get_params() must be called with MyClass instance as first argument (got nothing instead)
有没有办法实现这个目标?谢谢
我想检查一个对象是否是Classes列表/组中任何类的实例,但我无法找到是否有一个pythonic方式这样做而不做
if isinstance(obj, Class1) or isinstance(obj, Class2) ... or isinstance(obj, ClassN):
# proceed with some logic
Run Code Online (Sandbox Code Playgroud)
我的意思是,逐个比较.
更有可能使用类似于isinstance接收n个Classes的函数来进行比较,如果它甚至存在的话.
在此先感谢您的帮助!!:)
我正在编写一个python应用程序,它依赖于另一个由于开发原因而托管在github存储库(从不在pypi中)的应用程序.
让我们称呼它们:
AppAAppB在App A中,setup.py如下:
# coding=utf-8
import sys
try:
from setuptools import setup, find_packages
except ImportError:
import distribute_setup
distribute_setup.use_setuptools()
from setuptools import setup, find_packages
setup(
...
install_requires=[
# other requirements that install correctly
'app_b==0.1.1'
],
dependency_links=[
'git+https://github.com/user/app_b.git@0.1.1#egg=app_b-0.1.1'
]
)
Run Code Online (Sandbox Code Playgroud)
现在AppA正在逐步建立,Jenkins CI因为下一个错误被抛出而导致失败:
error: Download error for git+https://github.com/user/app_b.git@0.1.1: unknown url type: git+https
Run Code Online (Sandbox Code Playgroud)
有趣的是,这只发生在Jenkins,它在我的电脑上完美运行.我尝试了github提供的其他两个SSH网址,甚至没有考虑下载.
现在,AppA包含在也由Jenkins构建的项目的需求文件中,因此通过手动安装依赖项pip install AppA pip install AppB不是一个选项,依赖项通过包含在中自动安装requirements.txt.
有没有办法让github网址和pit一起工作?
任何帮助将非常感激:)
提前致谢!
我在我的移动网络应用中使用谷歌地图,但我注意到,有时当地图提示位置权限时,用户不小心点击"拒绝"而不是"允许",他们需要重新加载页面才能正确定位.
有没有办法,比如,有一些用户可以触发的JS调用,比如点击一个按钮,改变他们对提供位置访问权限的选择,或者让浏览器重新提示他们?
在此先感谢任何帮助:)
python ×8
django ×4
git ×2
github ×2
class-method ×1
diff ×1
django-south ×1
google-maps ×1
jquery ×1
mobile ×1
mocking ×1
oop ×1
pip ×1
python-2.7 ×1
python-mock ×1
string ×1
unit-testing ×1