inclusion

rails validation: :allow_nil and :inclusion both needed at the same time

馋奶兔 提交于 2019-12-06 16:29:56
问题 Usually the field 'kind' should be allowed blank. but if it is not blank, the value should included in ['a', 'b'] validates_inclusion_of :kind, :in => ['a', 'b'], :allow_nil => true The code does not work? 回答1: This syntax will perform inclusion validation while allowing nils: validates :kind, :inclusion => { :in => ['a', 'b'] }, :allow_nil => true 回答2: In Rails 5 you can use allow_blank: true outside or inside inclusion block: validates :kind, inclusion: { in: ['a', 'b'], allow_blank: true }

rails validation: :allow_nil and :inclusion both needed at the same time

北城以北 提交于 2019-12-04 23:37:12
Usually the field 'kind' should be allowed blank. but if it is not blank, the value should included in ['a', 'b'] validates_inclusion_of :kind, :in => ['a', 'b'], :allow_nil => true The code does not work? This syntax will perform inclusion validation while allowing nils: validates :kind, :inclusion => { :in => ['a', 'b'] }, :allow_nil => true In Rails 5 you can use allow_blank: true outside or inside inclusion block: validates :kind, inclusion: { in: ['a', 'b'], allow_blank: true } or validates :kind, inclusion: { in: ['a', 'b'] }, allow_blank: true tip: you can use in: %w(a b) for text

Test if python Counter is contained in another Counter

拈花ヽ惹草 提交于 2019-12-04 06:51:11
How to test if a python Counter is contained in another one using the following definition: A Counter a is contained in a Counter b if, and only if, for every key k in a , the value a[k] is less or equal to the value b[k] . The Counter({'a': 1, 'b': 1}) is contained in Counter({'a': 2, 'b': 2}) but it is not contained in Counter({'a': 2, 'c': 2}) . I think it is a poor design choice but in python 2.x the comparison operators ( < , <= , >= , > ) do not use the previous definition, so the third Counter is considered greater-than the first. In python 3.x , instead, Counter is an unorderable type

How to check if all of the following items are in a list?

▼魔方 西西 提交于 2019-11-26 15:12:21
I found, that there is related question, about how to find if at least one item exists in a list: How to check if one of the following items is in a list? But what is the best and pythonic way to find whether all items exists in a list? Searching through the docs I found this solution: >>> l = ['a', 'b', 'c'] >>> set(['a', 'b']) <= set(l) True >>> set(['a', 'x']) <= set(l) False Other solution would be this: >>> l = ['a', 'b', 'c'] >>> all(x in l for x in ['a', 'b']) True >>> all(x in l for x in ['a', 'x']) False But here you must do more typing. Is there any other solutions? Operators like <=

How to check if all of the following items are in a list?

非 Y 不嫁゛ 提交于 2019-11-26 04:19:48
问题 I found, that there is related question, about how to find if at least one item exists in a list: How to check if one of the following items is in a list? But what is the best and pythonic way to find whether all items exists in a list? Searching through the docs I found this solution: >>> l = [\'a\', \'b\', \'c\'] >>> set([\'a\', \'b\']) <= set(l) True >>> set([\'a\', \'x\']) <= set(l) False Other solution would be this: >>> l = [\'a\', \'b\', \'c\'] >>> all(x in l for x in [\'a\', \'b\'])