def __init__(self, data, twitterSession, fromCache=False): Timestamped.__init__(self) Hashable.__init__(self) if twitterSession is not None: assert isinstance(twitterSession, TwitterSession) self.data = Tree.make(data) if not fromCache: self.data.applyFunctionInTree(reverse_list, ['coordinates', 'coordinates']) # for use with leaflet. self.isDataNew = not fromCache twitterPlaceData = self.data.getFromTree(['place']) if twitterPlaceData is not None: self.twitter_place = Place(twitterPlaceData) else: self.twitter_place = None userJson = self.data.getFromTree(['user']) if userJson is None: self._user = None else: self._user = User(userJson, twitterSession=twitterSession, isAssociatedWithTweet=True, twitterPlace=self.twitter_place, currentLocationCoordinate=self.coordinate) if twitterSession is not None: self.instance_key = twitterSession.instance_key else: self.instance_key = None self.twitter_session = twitterSession
def __init__(self, userJson, twitterSession, fromCache=False, isFollowee=False, isAssociatedWithTweet=False, twitterPlace=None, currentLocationCoordinate=None, geocodeBias=None, knownFollowees=None): Hashable.__init__(self) Timestamped.__init__(self) if twitterSession is not None: assert isinstance(twitterSession, TwitterSession) if twitterPlace is not None: assert isinstance(twitterPlace, Place) if knownFollowees is None: knownFollowees = set() self.allow_geocode_external = True if userJson is None: self.data = Tree.make() else: self.data = Tree.make(userJson) # If false indicates that self.data is already in the cache, # so no point readding. self.isDataNew = not fromCache self.is_followee = isFollowee # This does not contain all followees, only followees known to our application # through follower enrichment. self.known_followees = knownFollowees self.is_associated_with_tweet = isAssociatedWithTweet self.twitter_session = twitterSession if twitterSession is not None: self.instance_key = twitterSession.instance_key else: self.instance_key = None self.queued_for_follower_enrichment = False self.current_location_coordinate = currentLocationCoordinate self.twitter_place = twitterPlace self.last_follower_enrichment_error = None self.follower_enrichment_progress = UserFollowerEnrichmentProgress(self) if self.num_followers is not None: self.twitter_calls_to_retrieve_follower_ids = math.ceil(float(self.num_followers) / float(TwitterSession.GET_FOLLOWER_IDS_QUANTITY_PER_TWITTER_CALL)) else: self.twitter_calls_to_retrieve_follower_ids = 1 self.geocoded_from = None self.geocode_bias = None self.analysers = None
""" Hash based on value of elements. >>> m = pmap({pbag([1, 2]): "it's here!"}) >>> m[pbag([2, 1])] "it's here!" >>> pbag([1, 1, 2]) in m False """ return hash(self._counts) Container.register(PBag) Iterable.register(PBag) Sized.register(PBag) Hashable.register(PBag) def b(*elements): """ Construct a persistent bag. Takes an arbitrary number of arguments to insert into the new persistent bag. >>> b(1, 2, 3, 2) pbag([1, 2, 2, 3]) """ return pbag(elements)
pmap({'a': 1, 'b': 2}) The changes are kept in the evolver. An updated pmap can be created using the persistent() function on the evolver. >>> m2 = e.persistent() >>> m2 pmap({'c': 3, 'b': 2}) The new pmap will share data with the original pmap in the same way that would have been done if only using operations on the pmap. """ return self._Evolver(self) Mapping.register(PMap) Hashable.register(PMap) def _turbo_mapping(initial, pre_size): if pre_size: size = pre_size else: try: size = 2 * len(initial) or 8 except Exception: # Guess we can't figure out the length. Give up on length hinting, # we can always reallocate later. size = 8 buckets = size * [None]
def get_vmc_version(): import os from subprocess import check_output, CalledProcessError original_directory = os.getcwd() try: os.chdir(os.path.dirname(__file__)) try: return check_output(["git", "describe", "--always"]) except CalledProcessError: return None finally: os.chdir(original_directory) import six if six.PY3: import numpy from collections import Hashable from numbers import Integral # FIXME: track down why we are passing around these int64's anyway if not isinstance(numpy.int64, Integral): Integral.register(numpy.int64) if not isinstance(numpy.int64, Hashable): Hashable.register(numpy.int64) # FIXME: numpy.ndarray is Hashable in python2 but not python3. Honestly, I # don't think it should be in either... Hashable.register(numpy.ndarray)
pmap({'a': 1, 'b': 2}) The changes are kept in the evolver. An updated pmap can be created using the persistent() function on the evolver. >>> m2 = e.persistent() >>> m2 pmap({'c': 3, 'b': 2}) The new pmap will share data with the original pmap in the same way that would have been done if only using operations on the pmap. """ return self._Evolver(self) Mapping.register(PMap) Hashable.register(PMap) def _turbo_mapping(initial, pre_size): size = pre_size or (2 * len(initial)) or 8 buckets = size * [None] if not isinstance(initial, Mapping): # Make a dictionary of the initial data if it isn't already, # that will save us some job further down since we can assume no # key collisions initial = dict(initial) for k, v in six.iteritems(initial): h = hash(k) index = h % size
return result class NamedspaceMeta(type): "Metaclass for namedspace classes" @property def _field_names(self): return self._all_fields @property def _field_names_iter(self): return iter(self._all_fields) _class_template = """\ class {typename}(object): __metaclass__ = NamedspaceMeta "{typename}({arg_list})" class FieldNameError(AttributeError, KeyError): pass class ReadOnlyNamedspaceError(TypeError): pass class ReadOnlyFieldError(TypeError): pass class MutableNamedspaceError(TypeError): pass _all_fields = all_fields _all_fields_set = all_fields_set _required_fields_set = required_fields_set _mutable_fields_set = mutable_fields_set _default_values = default_values _default_value_factories = default_value_factories
__slots__ = ('first', 'rest') def __new__(cls, first, rest): instance = super(PList, cls).__new__(cls) instance.first = first instance.rest = rest return instance def __bool__(self): return True __nonzero__ = __bool__ Sequence.register(PList) Hashable.register(PList) class _EmptyPList(_PListBase): __slots__ = () def __bool__(self): return False __nonzero__ = __bool__ @property def first(self): raise AttributeError("Empty PList has no first") @property
type(index).__name__) if index >= 0: return self.popleft(index).left shifted = len(self) + index if shifted < 0: raise IndexError( "pdeque index {0} out of range {1}".format(index, len(self)), ) return self.popleft(shifted).left index = Sequence.index Sequence.register(PDeque) Hashable.register(PDeque) def pdeque(iterable=(), maxlen=None): """ Return deque containing the elements of iterable. If maxlen is specified then len(iterable) - maxlen elements are discarded from the left to if len(iterable) > maxlen. >>> pdeque([1, 2, 3]) pdeque([1, 2, 3]) >>> pdeque([1, 2, 3, 4], maxlen=2) pdeque([3, 4], maxlen=2) """ t = tuple(iterable) if maxlen is not None: t = t[-maxlen:]
result = result.pop(self._length - (index.stop % self._length)) return result if not isinstance(index, Integral): raise TypeError("'%s' object cannot be interpreted as an index" % type(index).__name__) if index >= 0: return self.popleft(index).left return self.pop(index).right index = Sequence.index Sequence.register(PDeque) Hashable.register(PDeque) def pdeque(iterable=(), maxlen=None): """ Return deque containing the elements of iterable. If maxlen is specified then len(iterable) - maxlen elements are discarded from the left to if len(iterable) > maxlen. >>> pdeque([1, 2, 3]) pdeque([1, 2, 3]) >>> pdeque([1, 2, 3, 4], maxlen=2) pdeque([3, 4], maxlen=2) """ t = tuple(iterable) if maxlen is not None: t = t[-maxlen:]
4 """ return self._tolist().index(value, *args, **kwargs) def count(self, value): """ Return the number of times that value appears in the vector. >>> v1 = v(1, 4, 3, 4) >>> v1.count(4) 2 """ return self._tolist().count(value) Sequence.register(PVector) Hashable.register(PVector) _EMPTY_VECTOR = PVector(0, SHIFT, [], []) def _pvector(sequence=()): """ Factory function, returns a new PVector object containing the elements in sequence. >>> v1 = pvector([1, 2, 3]) >>> v1 (1, 2, 3) """ return _EMPTY_VECTOR.extend(sequence)
print "Container Abstract Base Class" from collections import Container print(Container.__subclasshook__(list)) print(Container.__subclasshook__(dict)) print(Container.__subclasshook__(set)) print(Container.__subclasshook__(tuple)) import string print(Container.__subclasshook__(string)) class WorldContainer(Container): def __contains__(self, item): return False print(Container.__subclasshook__(WorldContainer)) print(WorldContainer().__contains__(4)) print "Hashable Abstract Base Class" from collections import Hashable print(Hashable.__subclasshook__(string)) class WorldCHashable(Hashable): def __hash__(self, item): return id(item) print(Hashable.__subclasshook__(WorldCHashable)) print(WorldCHashable().__hash__(4)) # to read more details https://docs.python.org/2/library/collections.html
__and__ = Set.__and__ __or__ = Set.__or__ __sub__ = Set.__sub__ __xor__ = Set.__xor__ issubset = __le__ issuperset = __ge__ union = __or__ intersection = __and__ difference = __sub__ symmetric_difference = __xor__ isdisjoint = Set.isdisjoint Set.register(PSet) Hashable.register(PSet) _EMPTY_PSET = PSet(pmap()) def pset(iterable=(), pre_size=8): """ Creates a persistent set from iterable. Optionally takes a sizing parameter equivalent to that used for :py:func:`pmap`. >>> s1 = pset([1, 2, 3, 2]) >>> s1 pset([1, 2, 3]) """ if not iterable: return _EMPTY_PSET
@abstractmethod def delete(self, index, stop=None): """ Delete a portion of the vector by index or range. >>> v1 = v(1, 2, 3, 4, 5) >>> v1.delete(1) pvector([1, 3, 4, 5]) >>> v1.delete(1, 3) pvector([1, 4, 5]) """ _EMPTY_PVECTOR = PythonPVector(0, SHIFT, [], []) PVector.register(PythonPVector) Sequence.register(PVector) Hashable.register(PVector) def python_pvector(iterable=()): """ Create a new persistent vector containing the elements in iterable. >>> v1 = pvector([1, 2, 3]) >>> v1 pvector([1, 2, 3]) """ return _EMPTY_PVECTOR.extend(iterable) try: # Use the C extension as underlying trie implementation if it is available import os if os.environ.get('PYRSISTENT_NO_C_EXTENSION'):
""" __slots__ = ('first', 'rest') def __new__(cls, first, rest): instance = super(PList, cls).__new__(cls) instance.first = first instance.rest = rest return instance def __bool__(self): return True __nonzero__ = __bool__ Sequence.register(PList) Hashable.register(PList) class _EmptyPList(_PListBase): __slots__ = () def __bool__(self): return False __nonzero__ = __bool__ @property def first(self): raise AttributeError("Empty PList has no first") @property def rest(self):
def delete(self, index, stop=None): """ Delete a portion of the vector by index or range. >>> v1 = v(1, 2, 3, 4, 5) >>> v1.delete(1) pvector([1, 3, 4, 5]) >>> v1.delete(1, 3) pvector([1, 4, 5]) """ _EMPTY_PVECTOR = PythonPVector(0, SHIFT, [], []) PVector.register(PythonPVector) Sequence.register(PVector) Hashable.register(PVector) def python_pvector(iterable=()): """ Create a new persistent vector containing the elements in iterable. >>> v1 = pvector([1, 2, 3]) >>> v1 pvector([1, 2, 3]) """ return _EMPTY_PVECTOR.extend(iterable) try: # Use the C extension as underlying trie implementation if it is available