这里有多个帖子可以捕获价值,但我只是想查看价值是否有所作为.更模糊地说; 我想了解检查值和"捕获"值之间的区别.在目前的情况下,价值将是以下可接受的货币格式:
这是一篇文章,解释了一些钱正则表达式,但我不太了解它.
.50
50
50.00
50.0
$5000.00
$.50
Run Code Online (Sandbox Code Playgroud)
我不想要逗号(人们应该知道这很荒谬).
我遇到麻烦的是:
我目前的正则表达式(显然不能正常工作)是:
# I'm checking the Boolean of the following:
re.compile(r'^[\$][\d\.]$').search(value)
Run Code Online (Sandbox Code Playgroud)
(注意:我在使用Python)
我已经看过这个问题:Python迭代器 - 如何在新的样式类中动态分配self.next?
但这对我没有帮助,因为我想迭代一个列表的错误属性(即已经可迭代)而不必明确使用该属性.我希望这样做:
class SCE(Exception):
"""
An error while performing SCE functions.
"""
def __init__(self, value=None):
"""
Message: A string message or an iterable of strings.
"""
if value is None:
self._values = ['A general SCE error has occured.']
elif isinstance(value, str):
self._values = [value]
else:
self._values = list(value)
def __iter__(self):
return self._values
def __repr__(self):
return repr(self._values)
Run Code Online (Sandbox Code Playgroud)
但是,在shell中我得到了这个:
try:
raise CSE(['error one', 'error two'])
except CSE, e:
for i in e:
print(i)
Traceback (most recent call last):
File …Run Code Online (Sandbox Code Playgroud) 这是我的意思的一个例子:
class MyDecorator(object):
def __call__(self, func):
# At which point would I be able to access the decorated method's parent class's instance?
# In the below example, I would want to access from here: myinstance
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
class SomeClass(object):
##self.name = 'John' #error here
name="John"
@MyDecorator()
def nameprinter(self):
print(self.name)
myinstance = SomeClass()
myinstance.nameprinter()
Run Code Online (Sandbox Code Playgroud)
我需要装饰实际的课吗?
最近我看着使用list(),dict(),tuple()到位的[],{}和(),分别需要创建的三个空单的时候.原因是它看起来更具可读性.我打算就风格征求意见,但后来我决定测试性能.我这样做了:
>>> from timeit import Timer
>>> Timer('for x in range(5): y = []').timeit()
0.59327821802969538
>>> from timeit import Timer
>>> Timer('for x in range(5): y = list()').timeit()
1.2198944904251618
Run Code Online (Sandbox Code Playgroud)
我试过了dict(),tuple()并且list()每个函数调用版本都比语法版本差得多({} [],())所以,我有3个问题:
timeit关闭垃圾收集,但考虑到我只使用时,这可能不会产生影响range(5).我对统计数学等不太了解.我一直在想,如果我使用以下内容:
import uuid
unique_str = str(uuid.uuid4())
double_str = ''.join([str(uuid.uuid4()), str(uuid.uuid4())])
Run Code Online (Sandbox Code Playgroud)
被double_str串平方独特的unique_str或只是一些量更独特之处?此外,做这样的事情(比如生日问题情况等)是否有任何负面影响?这可能听起来无知,但我根本不会知道,因为我的数学充其量只能代数代数2.
我的问题是,使用Django South 将null=True场变成null=False场地的最佳做法是什么.具体来说,我正在使用ForeignKey.
我有一个功能:
# utils.py
def hello(name='World'):
# Detect where I'm being called from.
print('Hi, %s. You called this from %s at line # %d.' % (name, mod, lineno))
# ``mod`` and ``lineno`` on previous line would have been set in real use.
Run Code Online (Sandbox Code Playgroud)
我导入该函数并在其他地方运行它
# other.py (this comment at line # 138)
from utils import hello
hello('Johnny') # From inside ``hello`` I want to be able to detect that this
# was called from other.py at line # 140
Run Code Online (Sandbox Code Playgroud) 出于好奇,我运行了以下内容:
>>> int(1e100)
Run Code Online (Sandbox Code Playgroud)
而且,输出是:
10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469985856815104L
Run Code Online (Sandbox Code Playgroud)
为什么?为什么这看起来不像:
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L
Run Code Online (Sandbox Code Playgroud)
这是int功能的产品,还是大型存储的产品long?
我想创建一个mock.Mock()对象,然后添加一个称为session实例方法的方法,该方法传递self对模拟对象的引用,允许该方法向模拟对象添加状态。这是可能的(无需手动使用types.MethodType,例如,使用模拟的内置 API),还是我应该找到解决方法?
请注意,我发现了这个问题,它是针对 Ruby 的,似乎涵盖了类似的内容,如果不是同一件事的话。不幸的是,我根本不了解 Ruby。
我正在寻找一种在发出 CORS 请求时查看 CORS 飞行前选项请求的方法。我想查看服务器的响应标头以帮助我调试遇到的 CORS 问题,但我无法在 Chrome 或 Firefox 的“网络”选项卡或控制台中找到执行此操作的方法。
我还安装了HTTP Header Live插件,但没有帮助。
我用来jQuery.get(url);触发我的 CORS 请求,其中url是不同域的 URL。
python ×8
cors ×1
currency ×1
decorator ×1
django ×1
django-south ×1
exception ×1
firefox ×1
http-headers ×1
iterator ×1
math ×1
python-mock ×1
random ×1
regex ×1
unique ×1
unique-key ×1
uuid ×1