我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用sets.ImmutableSet()。
def attributeValueUnQuotedState(self): data = self.stream.char() if data in spaceCharacters: self.state = self.beforeAttributeNameState elif data == u"&": self.processEntityInAttribute(">") elif data == u">": self.emitCurrentToken() elif data in (u'"', u"'", u"=", u"<", u"`"): self.tokenQueue.append({"type": tokenTypes["ParseError"], "data": "unexpected-character-in-unquoted-attribute-value"}) self.currentToken["data"][-1][1] += data elif data == u"\u0000": self.tokenQueue.append({"type": tokenTypes["ParseError"], "data": "invalid-codepoint"}) self.currentToken["data"][-1][1] += u"\uFFFD" elif data is EOF: self.tokenQueue.append({"type": tokenTypes["ParseError"], "data": "eof-in-attribute-value-no-quotes"}) self.state = self.dataState else: self.currentToken["data"][-1][1] += data + self.stream.charsUntil( frozenset((u"&", u">", u'"', u"'", u"=", u"<", u"`")) | spaceCharacters) return True
def _compute_dependencies(self): """Recompute this distribution's dependencies.""" from _markerlib import compile as compile_marker dm = self.__dep_map = {None: []} reqs = [] # Including any condition expressions for req in self._parsed_pkg_info.get_all('Requires-Dist') or []: distvers, mark = self._preparse_requirement(req) parsed = next(parse_requirements(distvers)) parsed.marker_fn = compile_marker(mark) reqs.append(parsed) def reqs_for_extra(extra): for req in reqs: if req.marker_fn(override={'extra':extra}): yield req common = frozenset(reqs_for_extra(None)) dm[None].extend(common) for extra in self._parsed_pkg_info.get_all('Provides-Extra') or []: extra = safe_extra(extra.strip()) dm[extra] = list(frozenset(reqs_for_extra(extra)) - common) return dm
def __init__(self, project_name, specs, extras): """DO NOT CALL THIS UNDOCUMENTED METHOD; use Requirement.parse()!""" self.unsafe_name, project_name = project_name, safe_name(project_name) self.project_name, self.key = project_name, project_name.lower() index = [(parse_version(v),state_machine[op],op,v) for op,v in specs] index.sort() self.specs = [(op,ver) for parsed,trans,op,ver in index] self.index, self.extras = index, tuple(map(safe_extra,extras)) self.hashCmp = ( self.key, tuple([(op,parsed) for parsed,trans,op,ver in index]), frozenset(self.extras) ) self.__hash = hash(self.hashCmp)
def _compute_dependencies(self): """Recompute this distribution's dependencies.""" from pip._vendor._markerlib import compile as compile_marker dm = self.__dep_map = {None: []} reqs = [] # Including any condition expressions for req in self._parsed_pkg_info.get_all('Requires-Dist') or []: distvers, mark = self._preparse_requirement(req) parsed = next(parse_requirements(distvers)) parsed.marker_fn = compile_marker(mark) reqs.append(parsed) def reqs_for_extra(extra): for req in reqs: if req.marker_fn(override={'extra':extra}): yield req common = frozenset(reqs_for_extra(None)) dm[None].extend(common) for extra in self._parsed_pkg_info.get_all('Provides-Extra') or []: extra = safe_extra(extra.strip()) dm[extra] = list(frozenset(reqs_for_extra(extra)) - common) return dm
def mix_and_match(limit, items, verbose = False): # filter items items = [(size, name) for size, name in items if size <= limit] # sort them by size items.sort(lambda (xsize, xname), (ysize, yname): cmp(xsize, ysize)) # initialize variables added_collections = dict([(set([name]), size) for size, name in items]) collections = added_collections while True: if verbose: sys.stderr.write("%d\n" % len(collections)) # find unique combinations of the recent collections new_collections = {} for names1, size1 in added_collections.iteritems(): for size2, name2 in items: size3 = size1 + size2 if size3 > limit: # we can break here as all collections that follow are # bigger in size due to the sorting above break if name2 in names1: continue names3 = names1.union(set([name2])) if names3 in new_collections: continue new_collections[names3] = size3 if len(new_collections) == 0: break collections.update(new_collections) added_collections = new_collections return [(size, names) for names, size in collections.iteritems()]
def scriptDataDoubleEscapeStartState(self): data = self.stream.char() if data in (spaceCharacters | frozenset(("/", ">"))): self.tokenQueue.append({"type": tokenTypes["Characters"], "data": data}) if self.temporaryBuffer.lower() == "script": self.state = self.scriptDataDoubleEscapedState else: self.state = self.scriptDataEscapedState elif data in asciiLetters: self.tokenQueue.append({"type": tokenTypes["Characters"], "data": data}) self.temporaryBuffer += data else: self.stream.unget(data) self.state = self.scriptDataEscapedState return True
def scriptDataDoubleEscapeEndState(self): data = self.stream.char() if data in (spaceCharacters | frozenset(("/", ">"))): self.tokenQueue.append({"type": tokenTypes["Characters"], "data": data}) if self.temporaryBuffer.lower() == "script": self.state = self.scriptDataEscapedState else: self.state = self.scriptDataDoubleEscapedState elif data in asciiLetters: self.tokenQueue.append({"type": tokenTypes["Characters"], "data": data}) self.temporaryBuffer += data else: self.stream.unget(data) self.state = self.scriptDataDoubleEscapedState return True
def __init__(self, items=()): # Using _dictEntries instead of directly assigning to self is about # twice as fast. Please do careful performance testing before changing # anything here. _dictEntries = [] for name,value in items: if type(name) in (list, tuple, frozenset, set): for item in name: _dictEntries.append((item, value)) else: _dictEntries.append((name, value)) dict.__init__(self, _dictEntries) self.default = None
def generateImpliedEndTags(self, exclude=None): name = self.openElements[-1].name # XXX td, th and tr are not actually needed if (name in frozenset(("dd", "dt", "li", "option", "optgroup", "p", "rp", "rt")) and name != exclude): self.openElements.pop() # XXX This is not entirely what the specification says. We should # investigate it more closely. self.generateImpliedEndTags(exclude)
def test_amf0_encode(self): x = sets.ImmutableSet(['1', '2', '3']) self.assertTrue(check_buffer( pyamf.encode(x, encoding=pyamf.AMF0).getvalue(), ( '\n\x00\x00\x00\x03', ( '\x02\x00\x011', '\x02\x00\x013', '\x02\x00\x012' ) ) ))
def test_amf3_encode(self): x = sets.ImmutableSet(['1', '2', '3']) self.assertTrue(check_buffer( pyamf.encode(x, encoding=pyamf.AMF3).getvalue(), ( '\t\x07\x01', ( '\x06\x031', '\x06\x033', '\x06\x032' ) ) ))
def backchain_to_goal_tree_2_testanswer(val, original_val = None): return ( tree_map(type_encode(val), frozenset) == tree_map(type_encode(result_bc_2), frozenset)) # This test checks to make sure that your backchainer produces # the correct goal tree given the hypothesis 'alice is an # albatross' and using the ZOOKEEPER_RULES.
def backchain_to_goal_tree_3_testanswer(val, original_val = None): return ( tree_map(type_encode(val), frozenset) == tree_map(type_encode(result_bc_3), frozenset)) # This test checks to make sure that your backchainer produces # the correct goal tree given the hypothesis 'geoff is a giraffe' # and using the ZOOKEEPER_RULES.
def backchain_to_goal_tree_4_testanswer(val, original_val = None): return ( tree_map(type_encode(val), frozenset) == tree_map(type_encode(result_bc_4), frozenset) ) # This test checks to make sure that your backchainer produces # the correct goal tree given the hypothesis 'gershwin could not # ask for anything more' and using the rules defined in # backchain_to_goal_tree_4_getargs() above.
def backchain_to_goal_tree_5_testanswer(val, original_args = None): return ( tree_map(type_encode(val), frozenset) == tree_map(type_encode(result_bc_5), frozenset)) # This test checks to make sure that your backchainer produces # the correct goal tree given the hypothesis 'zot' and using the # rules defined in ARBITRARY_EXP above.
def test_constructor(self): inner = Set([1]) outer = Set([inner]) element = outer.pop() self.assertEqual(type(element), ImmutableSet) outer.add(inner) # Rebuild set of sets with .add method outer.remove(inner) self.assertEqual(outer, Set()) # Verify that remove worked outer.discard(inner) # Absence of KeyError indicates working fine #==============================================================================
def setUp(self): self.set = Set((1, 2, 3)) self.other = [Set('ab'), ImmutableSet('cd')] self.otherIsIterable = True #==============================================================================