signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
def save(self):
files = [f.depotFile for f in self._files]<EOL>cmd = ['<STR_LIT>', '<STR_LIT:-c>', '<STR_LIT:default>']<EOL>self._connection.run(cmd + files)<EOL>self._dirty = False<EOL>
Saves the state of the changelist
f15471:c4:m1
def query(self):
self._p4dict = self._connection.run(['<STR_LIT>', '<STR_LIT>', '<STR_LIT:1>', self._p4dict['<STR_LIT>']])[<NUM_LIT:0>]<EOL>self._head = HeadRevision(self._p4dict)<EOL>self._filename = self.depotFile<EOL>
Runs an fstat for this file and repopulates the data
f15471:c5:m5
def edit(self, changelist=<NUM_LIT:0>):
command = '<STR_LIT>' if self.action in ('<STR_LIT>', '<STR_LIT>') else '<STR_LIT>'<EOL>if int(changelist):<EOL><INDENT>self._connection.run([command, '<STR_LIT:-c>', str(changelist.change), self.depotFile])<EOL><DEDENT>else:<EOL><INDENT>self._connection.run([command, self.depotFile])<EOL><DEDENT>self.query()<EOL>
Checks out the file :param changelist: Optional changelist to checkout the file into :type changelist: :class:`.Changelist`
f15471:c5:m6
def lock(self, lock=True, changelist=<NUM_LIT:0>):
cmd = '<STR_LIT>' if lock else '<STR_LIT>'<EOL>if changelist:<EOL><INDENT>self._connection.run([cmd, '<STR_LIT:-c>', changelist, self.depotFile])<EOL><DEDENT>else:<EOL><INDENT>self._connection.run([cmd, self.depotFile])<EOL><DEDENT>self.query()<EOL>
Locks or unlocks the file :param lock: Lock or unlock the file :type lock: bool :param changelist: Optional changelist to checkout the file into :type changelist: :class:`.Changelist`
f15471:c5:m7
def sync(self, force=False, safe=True, revision=<NUM_LIT:0>, changelist=<NUM_LIT:0>):
cmd = ['<STR_LIT>']<EOL>if force:<EOL><INDENT>cmd.append('<STR_LIT>')<EOL><DEDENT>if safe:<EOL><INDENT>cmd.append('<STR_LIT>')<EOL><DEDENT>if revision:<EOL><INDENT>cmd.append('<STR_LIT>'.format(self.depotFile, revision))<EOL><DEDENT>elif changelist:<EOL><INDENT>cmd.append('<STR_LIT>'.format(self.depotFile, changelist))<EOL><DEDENT>else:<EOL><INDENT>cmd.append(self.depotFile)<EOL><DEDENT>self._connection.run(cmd)<EOL>self.query()<EOL>
Syncs the file at the current revision :param force: Force the file to sync :type force: bool :param safe: Don't sync files that were changed outside perforce :type safe: bool :param revision: Sync to a specific revision :type revision: int :param changelist: Changelist to sync to :type changelist: int
f15471:c5:m8
def revert(self, unchanged=False):
cmd = ['<STR_LIT>']<EOL>if unchanged:<EOL><INDENT>cmd.append('<STR_LIT>')<EOL><DEDENT>wasadd = self.action == '<STR_LIT>'<EOL>cmd.append(self.depotFile)<EOL>self._connection.run(cmd)<EOL>if '<STR_LIT>' in self._p4dict:<EOL><INDENT>self._p4dict['<STR_LIT>'] = self._p4dict['<STR_LIT>']<EOL><DEDENT>if not wasadd:<EOL><INDENT>self.query()<EOL><DEDENT>if self._changelist:<EOL><INDENT>self._changelist.remove(self, permanent=True)<EOL><DEDENT>
Reverts any file changes :param unchanged: Only revert if the file is unchanged :type unchanged: bool
f15471:c5:m9
def shelve(self, changelist=None):
if changelist is None and self.changelist.description == '<STR_LIT:default>':<EOL><INDENT>raise errors.ShelveError('<STR_LIT>')<EOL><DEDENT>cmd = ['<STR_LIT>']<EOL>if changelist:<EOL><INDENT>cmd += ['<STR_LIT:-c>', str(changelist)]<EOL><DEDENT>cmd.append(self.depotFile)<EOL>self._connection.run(cmd)<EOL>self.query()<EOL>
Shelves the file if it is in a changelist :param changelist: Changelist to add the move to :type changelist: :class:`.Changelist`
f15471:c5:m10
def move(self, dest, changelist=<NUM_LIT:0>, force=False):
cmd = ['<STR_LIT>']<EOL>if force:<EOL><INDENT>cmd.append('<STR_LIT>')<EOL><DEDENT>if changelist:<EOL><INDENT>cmd += ['<STR_LIT:-c>', str(changelist)]<EOL><DEDENT>if not self.isEdit:<EOL><INDENT>self.edit(changelist)<EOL><DEDENT>cmd += [self.depotFile, dest]<EOL>self._connection.run(cmd)<EOL>self._p4dict['<STR_LIT>'] = dest<EOL>self.query()<EOL>
Renames/moves the file to dest :param dest: Destination to move the file to :type dest: str :param changelist: Changelist to add the move to :type changelist: :class:`.Changelist` :param force: Force the move to an existing file :type force: bool
f15471:c5:m11
def delete(self, changelist=<NUM_LIT:0>):
cmd = ['<STR_LIT>']<EOL>if changelist:<EOL><INDENT>cmd += ['<STR_LIT:-c>', str(changelist)]<EOL><DEDENT>cmd.append(self.depotFile)<EOL>self._connection.run(cmd)<EOL>self.query()<EOL>
Marks the file for delete :param changelist: Changelist to add the move to :type changelist: :class:`.Changelist`
f15471:c5:m12
@property<EOL><INDENT>def hash(self):<DEDENT>
if '<STR_LIT>' not in self._p4dict:<EOL><INDENT>self._p4dict = self._connection.run(['<STR_LIT>', '<STR_LIT>', '<STR_LIT:1>', '<STR_LIT>', self.depotFile])[<NUM_LIT:0>]<EOL><DEDENT>return self._p4dict['<STR_LIT>']<EOL>
The hash value of the current revision
f15471:c5:m13
@property<EOL><INDENT>def clientFile(self):<DEDENT>
return path.path(self._p4dict['<STR_LIT>'])<EOL>
The local path to the revision
f15471:c5:m14
@property<EOL><INDENT>def depotFile(self):<DEDENT>
return path.path(self._p4dict['<STR_LIT>'])<EOL>
The depot path to the revision
f15471:c5:m15
@property<EOL><INDENT>def isMapped(self):<DEDENT>
return '<STR_LIT>' in self._p4dict<EOL>
Is the file mapped to the current workspace
f15471:c5:m16
@property<EOL><INDENT>def isShelved(self):<DEDENT>
return '<STR_LIT>' in self._p4dict<EOL>
Is the file shelved
f15471:c5:m17
@property<EOL><INDENT>def revision(self):<DEDENT>
rev = self._p4dict.get('<STR_LIT>', -<NUM_LIT:1>)<EOL>if rev == '<STR_LIT:none>':<EOL><INDENT>rev = <NUM_LIT:0><EOL><DEDENT>return int(rev)<EOL>
Revision number
f15471:c5:m18
@property<EOL><INDENT>def action(self):<DEDENT>
return self._p4dict.get('<STR_LIT:action>')<EOL>
The current action: add, edit, etc.
f15471:c5:m20
@property<EOL><INDENT>def changelist(self):<DEDENT>
if self._changelist:<EOL><INDENT>return self._changelist<EOL><DEDENT>if self._p4dict['<STR_LIT>'] == '<STR_LIT:default>':<EOL><INDENT>return Default(connection=self._connection)<EOL><DEDENT>else:<EOL><INDENT>return Changelist(str(self._p4dict['<STR_LIT>']), self._connection)<EOL><DEDENT>
Which :class:`.Changelist` is this revision in
f15471:c5:m21
@property<EOL><INDENT>def type(self):<DEDENT>
if self.action == '<STR_LIT>':<EOL><INDENT>return self._p4dict['<STR_LIT:type>']<EOL><DEDENT>return None<EOL>
Best guess at file type. text or binary
f15471:c5:m23
@property<EOL><INDENT>def isResolved(self):<DEDENT>
return self.unresolved == <NUM_LIT:0><EOL>
Is the revision resolved
f15471:c5:m24
@property<EOL><INDENT>def resolved(self):<DEDENT>
return int(self._p4dict.get('<STR_LIT>', <NUM_LIT:0>))<EOL>
The number, if any, of resolved integration records
f15471:c5:m25
@property<EOL><INDENT>def unresolved(self):<DEDENT>
return int(self._p4dict.get('<STR_LIT>', <NUM_LIT:0>))<EOL>
The number, if any, of unresolved integration records
f15471:c5:m26
@property<EOL><INDENT>def openedBy(self):<DEDENT>
return self._p4dict.get('<STR_LIT>', [])<EOL>
Who has this file open for edit
f15471:c5:m27
@property<EOL><INDENT>def lockedBy(self):<DEDENT>
return self._p4dict.get('<STR_LIT>', [])<EOL>
Who has this file locked
f15471:c5:m28
@property<EOL><INDENT>def isLocked(self):<DEDENT>
return '<STR_LIT>' in self._p4dict or '<STR_LIT>' in self._p4dict<EOL>
Is the file locked by anyone excluding the current user
f15471:c5:m29
@property<EOL><INDENT>def head(self):<DEDENT>
return self._head<EOL>
The :class:`.HeadRevision` of this file
f15471:c5:m30
@property<EOL><INDENT>def isSynced(self):<DEDENT>
return self.revision == self.head.revision<EOL>
Is the local file the latest revision
f15471:c5:m31
@property<EOL><INDENT>def isEdit(self):<DEDENT>
return self.action == '<STR_LIT>'<EOL>
Is the file open for edit
f15471:c5:m32
@property<EOL><INDENT>def root(self):<DEDENT>
return path.Path(self._p4dict['<STR_LIT:root>'])<EOL>
Root path fo the client
f15471:c7:m2
@property<EOL><INDENT>def view(self):<DEDENT>
spec = []<EOL>for k, v in six.iteritems(self._p4dict):<EOL><INDENT>if k.startswith('<STR_LIT>'):<EOL><INDENT>match = RE_FILESPEC.search(v)<EOL>if match:<EOL><INDENT>spec.append(FileSpec(v[:match.end() - <NUM_LIT:1>], v[match.end():]))<EOL><DEDENT><DEDENT><DEDENT>return spec<EOL>
A list of view specs
f15471:c7:m14
@property<EOL><INDENT>def access(self):<DEDENT>
return datetime.datetime.strptime(self._p4dict['<STR_LIT>'], DATE_FORMAT)<EOL>
The date and time last accessed
f15471:c7:m15
@property<EOL><INDENT>def update(self):<DEDENT>
return datetime.datetime.strptime(self._p4dict['<STR_LIT>'], DATE_FORMAT)<EOL>
The date and time the client was updated
f15471:c7:m16
@property<EOL><INDENT>def stream(self):<DEDENT>
stream = self._p4dict.get('<STR_LIT>')<EOL>if stream:<EOL><INDENT>return Stream(stream, self._connection)<EOL><DEDENT>
Which stream, if any, the client is under
f15471:c7:m17
@property<EOL><INDENT>def description(self):<DEDENT>
return self._p4dict.get('<STR_LIT:description>', '<STR_LIT>').strip()<EOL>
Stream description tha thas been trimmed
f15471:c8:m2
@property<EOL><INDENT>def view(self):<DEDENT>
spec = []<EOL>for k, v in six.iteritems(self._p4dict):<EOL><INDENT>if k.startswith('<STR_LIT>'):<EOL><INDENT>match = RE_FILESPEC.search(v)<EOL>if match:<EOL><INDENT>spec.append(FileSpec(v[:match.end() - <NUM_LIT:1>], v[match.end():]))<EOL><DEDENT><DEDENT><DEDENT>return spec<EOL>
A list of view specs
f15471:c8:m3
@property<EOL><INDENT>def access(self):<DEDENT>
return datetime.datetime.strptime(self._p4dict['<STR_LIT>'], DATE_FORMAT)<EOL>
The date and time last accessed
f15471:c8:m4
@property<EOL><INDENT>def update(self):<DEDENT>
return datetime.datetime.strptime(self._p4dict['<STR_LIT>'], DATE_FORMAT)<EOL>
The date and time the client was updated
f15471:c8:m5
def connect(*args, **kwargs):
global __CONNECTION<EOL>if __CONNECTION is None:<EOL><INDENT>__CONNECTION = Connection(*args, **kwargs)<EOL><DEDENT>return __CONNECTION<EOL>
Creates or returns a singleton :class:`.Connection` object
f15472:m0
def edit(filename, connection=None):
c = connection or connect()<EOL>rev = c.ls(filename)<EOL>if rev:<EOL><INDENT>rev[<NUM_LIT:0>].edit()<EOL><DEDENT>
Checks out a file into the default changelist :param filename: File to check out :type filename: str :param connection: Connection object to use :type connection: :py:class:`Connection`
f15472:m1
def sync(filename, connection=None):
c = connection or connect()<EOL>rev = c.ls(filename)<EOL>if rev:<EOL><INDENT>rev[<NUM_LIT:0>].sync()<EOL><DEDENT>
Syncs a file :param filename: File to check out :type filename: str :param connection: Connection object to use :type connection: :py:class:`Connection`
f15472:m2
def info(connection=None):
c = connection or connect()<EOL>return c.run(['<STR_LIT:info>'])[<NUM_LIT:0>]<EOL>
Returns information about the current :class:`.Connection` :param connection: Connection object to use :type connection: :py:class:`Connection` :returns: dict
f15472:m3
def changelist(description=None, connection=None):
c = connection or connect()<EOL>return c.findChangelist(description)<EOL>
Gets or creates a :class:`.Changelist` object with a description :param description: Description of changelist to find or create :type description: str :param connection: Connection object to use :type connection: :py:class:`Connection` :returns: :class:`.Changelist`
f15472:m4
def open(filename, connection=None):
c = connection or connect()<EOL>res = c.ls(filename)<EOL>if res and res[<NUM_LIT:0>].revision:<EOL><INDENT>res[<NUM_LIT:0>].edit()<EOL><DEDENT>else:<EOL><INDENT>c.add(filename)<EOL><DEDENT>
Edits or Adds a filename ensuring the file is in perforce and editable :param filename: File to check out :type filename: str :param connection: Connection object to use :type connection: :py:class:`Connection`
f15472:m5
def __init__(self, data=[]):
if not hasattr(data, "<STR_LIT>"):<EOL><INDENT>raise TypeError(<EOL>u"<STR_LIT>")<EOL><DEDENT>self._data = data<EOL>
Constructor ** Note: no type checking of the data elements are performed during instantiation. ** :param data: iterable object :return: None
f15479:c0:m0
@property<EOL><INDENT>def data(self):<DEDENT>
return self._data<EOL>
The iterable of the Enumerable instance :return: iterable
f15479:c0:m1
def to_list(self):
return list(element for element in self)<EOL>
Converts the iterable into a list :return: list object
f15479:c0:m4
def count(self):
return sum(<NUM_LIT:1> for element in self)<EOL>
Returns the number of elements in iterable :return: integer object
f15479:c0:m5
def select(self, func=lambda x: x):
return Enumerable3(map(func, self))<EOL>
Transforms data into different form :param func: lambda expression on how to perform transformation :return: new Enumerable object containing transformed data
f15479:c0:m6
def sum(self, func=lambda x: x):
return sum(self.select(func))<EOL>
Returns the sum of af data elements :param func: lambda expression to transform data :return: sum of selected elements
f15479:c0:m7
def min(self, func=lambda x: x):
if self.count() == <NUM_LIT:0>:<EOL><INDENT>raise NoElementsError(u"<STR_LIT>")<EOL><DEDENT>return min(self.select(func))<EOL>
Returns the min value of data elements :param func: lambda expression to transform data :return: minimum value
f15479:c0:m8
def max(self, func=lambda x: x):
if self.count() == <NUM_LIT:0>:<EOL><INDENT>raise NoElementsError(u"<STR_LIT>")<EOL><DEDENT>return max(self.select(func))<EOL>
Returns the max value of data elements :param func: lambda expression to transform data :return: maximum value
f15479:c0:m9
def avg(self, func=lambda x: x):
count = self.count()<EOL>if count == <NUM_LIT:0>:<EOL><INDENT>raise NoElementsError(u"<STR_LIT>")<EOL><DEDENT>return float(self.sum(func)) / float(count)<EOL>
Returns the average value of data elements :param func: lambda expression to transform data :return: average value as float object
f15479:c0:m10
def median(self, func=lambda x: x):
if self.count() == <NUM_LIT:0>:<EOL><INDENT>raise NoElementsError(u"<STR_LIT>")<EOL><DEDENT>result = self.order_by(func).select(func).to_list()<EOL>length = len(result)<EOL>i = int(length / <NUM_LIT:2>)<EOL>return result[i] if length % <NUM_LIT:2> == <NUM_LIT:1> else (float(result[i - <NUM_LIT:1>]) + float(result[i])) / float(<NUM_LIT:2>)<EOL>
Return the median value of data elements :param func: lambda expression to project and sort data :return: median value
f15479:c0:m11
def element_at(self, n):
result = list(itertools.islice(self.to_list(), max(<NUM_LIT:0>, n), n + <NUM_LIT:1>, <NUM_LIT:1>))<EOL>if len(result) == <NUM_LIT:0>:<EOL><INDENT>raise NoElementsError(u"<STR_LIT>".format(n))<EOL><DEDENT>return result[<NUM_LIT:0>]<EOL>
Returns element at given index. * Raises NoElementsError if no element found at specified position :param n: index as int object :return: Element at given index
f15479:c0:m12
def element_at_or_default(self, n):
try:<EOL><INDENT>return self.element_at(n)<EOL><DEDENT>except NoElementsError:<EOL><INDENT>return None<EOL><DEDENT>
Returns element at given index or None if no element found * Raises IndexError if n is greater than the number of elements in enumerable :param n: index as int object :return: Element at given index
f15479:c0:m13
def first(self):
return self.element_at(<NUM_LIT:0>)<EOL>
Returns the first element :return: data element as object or NoElementsError if transformed data contains no elements
f15479:c0:m14
def first_or_default(self):
return self.element_at_or_default(<NUM_LIT:0>)<EOL>
Return the first element :return: data element as object or None if transformed data contains no elements
f15479:c0:m15
def last(self):
return Enumerable3(reversed(self.to_list())).first()<EOL>
Return the last element :return: data element as object or NoElementsError if transformed data contains no elements
f15479:c0:m16
def last_or_default(self):
return Enumerable3(reversed(self.to_list())).first_or_default()<EOL>
Return the last element :return: data element as object or None if transformed data contains no elements
f15479:c0:m17
def order_by(self, key):
if key is None:<EOL><INDENT>raise NullArgumentError(u"<STR_LIT>")<EOL><DEDENT>kf = [OrderingDirection(key, reverse=False)]<EOL>return SortedEnumerable3(kf, self._data)<EOL>
Returns new Enumerable sorted in ascending order by given key :param key: key to sort by as lambda expression :return: new Enumerable object
f15479:c0:m18
def order_by_descending(self, key):
if key is None:<EOL><INDENT>raise NullArgumentError(u"<STR_LIT>")<EOL><DEDENT>kf = [OrderingDirection(key, reverse=True)]<EOL>return SortedEnumerable3(kf, self._data)<EOL>
Returns new Enumerable sorted in descending order by given key :param key: key to sort by as lambda expression :return: new Enumerable object
f15479:c0:m19
def skip(self, n):
return Enumerable3(itertools.islice(self, n, None, <NUM_LIT:1>))<EOL>
Returns new Enumerable where n elements have been skipped :param n: Number of elements to skip as int :return: new Enumerable object
f15479:c0:m20
def take(self, n):
return Enumerable3(itertools.islice(self, <NUM_LIT:0>, n, <NUM_LIT:1>))<EOL>
Return new Enumerable where first n elements are taken :param n: Number of elements to take :return: new Enumerable object
f15479:c0:m21
def where(self, predicate):
if predicate is None:<EOL><INDENT>raise NullArgumentError(u"<STR_LIT>")<EOL><DEDENT>return Enumerable3(filter(predicate, self))<EOL>
Returns new Enumerable where elements matching predicate are selected :param predicate: predicate as a lambda expression :return: new Enumerable object
f15479:c0:m22
def single(self, predicate):
result = self.where(predicate).to_list()<EOL>count = len(result)<EOL>if count == <NUM_LIT:0>:<EOL><INDENT>raise NoMatchingElement(u"<STR_LIT>")<EOL><DEDENT>if count > <NUM_LIT:1>:<EOL><INDENT>raise MoreThanOneMatchingElement(<EOL>u"<STR_LIT>")<EOL><DEDENT>return result[<NUM_LIT:0>]<EOL>
Returns single element that matches given predicate. Raises: * NoMatchingElement error if no matching elements are found * MoreThanOneMatchingElement error if more than one matching element is found :param predicate: predicate as a lambda expression :return: Matching element as object
f15479:c0:m23
def single_or_default(self, predicate):
try:<EOL><INDENT>return self.single(predicate)<EOL><DEDENT>except NoMatchingElement:<EOL><INDENT>return None<EOL><DEDENT>
Return single element that matches given predicate. If no matching element is found, returns None Raises: * MoreThanOneMatchingElement error if more than one matching element is found :param predicate: predicate as a lambda expression :return: Matching element as object or None if no matches are found
f15479:c0:m24
def select_many(self, func=lambda x: x):
return Enumerable3(itertools.chain.from_iterable(self.select(func)))<EOL>
Flattens an iterable of iterables returning a new Enumerable :param func: selector as lambda expression :return: new Enumerable object
f15479:c0:m25
def add(self, element):
if element is None:<EOL><INDENT>return self<EOL><DEDENT>return self.concat(Enumerable3([element]))<EOL>
Adds an element to the enumerable. :param element: An element :return: new Enumerable object
f15479:c0:m26
def concat(self, enumerable):
if not isinstance(enumerable, Enumerable3):<EOL><INDENT>raise TypeError(<EOL>u"<STR_LIT>")<EOL><DEDENT>return Enumerable3(itertools.chain(self._data, enumerable.data))<EOL>
Adds enumerable to an enumerable :param enumerable: An iterable object :return: new Enumerable object
f15479:c0:m27
def group_by(self, key_names=[], key=lambda x: x, result_func=lambda x: x):
result = []<EOL>ordered = sorted(self, key=key)<EOL>grouped = itertools.groupby(ordered, key)<EOL>for k, g in grouped:<EOL><INDENT>can_enumerate = isinstance(k, list) or isinstance(k, tuple)and len(k) > <NUM_LIT:0><EOL>key_prop = {}<EOL>for i, prop in enumerate(key_names):<EOL><INDENT>key_prop.setdefault(prop, k[i] if can_enumerate else k)<EOL><DEDENT>key_object = Key(key_prop)<EOL>result.append(Grouping3(key_object, list(g)))<EOL><DEDENT>return Enumerable3(result).select(result_func)<EOL>
Groups an enumerable on given key selector. Index of key name corresponds to index of key lambda function. Usage: Enumerable([1,2,3]).group_by(key_names=['id'], key=lambda x: x) _ .to_list() --> Enumerable object [ Grouping object { key.id: 1, _data: [1] }, Grouping object { key.id: 2, _data: [2] }, Grouping object { key.id: 3, _data: [3] } ] Thus the key names for each grouping object can be referenced through the key property. Using the above example: Enumerable([1,2,3]).group_by(key_names=['id'], key=lambda x: x) _ .select(lambda g: { 'key': g.key.id, 'count': g.count() } :param key_names: list of key names :param key: key selector as lambda expression :param result_func: lambda function to transform group_join into desired structure :return: Enumerable of grouping objects
f15479:c0:m28
def distinct(self, key=lambda x: x):
return Enumerable3(self.group_by(key=key).select(lambda g: g.first()).to_list())<EOL>
Returns enumerable containing elements that are distinct based on given key selector :param key: key selector as lambda expression :return: new Enumerable object
f15479:c0:m29
def join(<EOL>self,<EOL>inner_enumerable,<EOL>outer_key=lambda x: x,<EOL>inner_key=lambda x: x,<EOL>result_func=lambda x: x<EOL>):
if not isinstance(inner_enumerable, Enumerable3):<EOL><INDENT>raise TypeError(<EOL>u"<STR_LIT>"<EOL>)<EOL><DEDENT>return Enumerable3(<EOL>itertools.product(<EOL>filter(<EOL>lambda x: outer_key(x) in map(inner_key, inner_enumerable),<EOL>self<EOL>),<EOL>filter(<EOL>lambda y: inner_key(y) in map(outer_key, self),<EOL>inner_enumerable<EOL>)<EOL>)<EOL>).where(lambda x: outer_key(x[<NUM_LIT:0>]) == inner_key(x[<NUM_LIT:1>])).select(result_func)<EOL>
Return enumerable of inner equi-join between two enumerables :param inner_enumerable: inner enumerable to join to self :param outer_key: key selector of outer enumerable as lambda expression :param inner_key: key selector of inner enumerable as lambda expression :param result_func: lambda expression to transform result of join :return: new Enumerable object
f15479:c0:m30
def default_if_empty(self, value=None):
if self.count() == <NUM_LIT:0>:<EOL><INDENT>return Enumerable3([value])<EOL><DEDENT>return self<EOL>
Returns an enumerable containing a single None element if enumerable is empty, otherwise the enumerable itself :return: an Enumerable object
f15479:c0:m31
def group_join(<EOL>self,<EOL>inner_enumerable,<EOL>outer_key=lambda x: x,<EOL>inner_key=lambda x: x,<EOL>result_func=lambda x: x<EOL>):
if not isinstance(inner_enumerable, Enumerable3):<EOL><INDENT>raise TypeError(<EOL>u"<STR_LIT>"<EOL>)<EOL><DEDENT>return Enumerable3(<EOL>itertools.product(<EOL>self,<EOL>inner_enumerable.default_if_empty()<EOL>)<EOL>).group_by(<EOL>key_names=['<STR_LIT:id>'],<EOL>key=lambda x: outer_key(x[<NUM_LIT:0>]),<EOL>result_func=lambda g: (<EOL>g.first()[<NUM_LIT:0>],<EOL>g.where(lambda x: inner_key(x[<NUM_LIT:1>]) == g.key.id).select(<EOL>lambda x: x[<NUM_LIT:1>]<EOL>)<EOL>)<EOL>).select(result_func)<EOL>
Return enumerable of group join between two enumerables :param inner_enumerable: inner enumerable to join to self :param outer_key: key selector of outer enumerable as lambda expression :param inner_key: key selector of inner enumerable as lambda expression :param result_func: lambda expression to transform the result of group join :return: new Enumerable object
f15479:c0:m32
def any(self, predicate):
if predicate is None:<EOL><INDENT>raise NullArgumentError(<EOL>u"<STR_LIT>")<EOL><DEDENT>return self.where(predicate).count() > <NUM_LIT:0><EOL>
Returns true if any elements that satisfy predicate are found :param predicate: condition to satisfy as lambda expression :return: boolean True or False
f15479:c0:m33
def intersect(self, enumerable, key=lambda x: x):
if not isinstance(enumerable, Enumerable3):<EOL><INDENT>raise TypeError(<EOL>u"<STR_LIT>")<EOL><DEDENT>return self.join(enumerable, key, key).select(lambda x: x[<NUM_LIT:0>])<EOL>
Returns enumerable that is the intersection between given enumerable and self :param enumerable: enumerable object :param key: key selector as lambda expression :return: new Enumerable object
f15479:c0:m34
def union(self, enumerable, key=lambda x: x):
if not isinstance(enumerable, Enumerable3):<EOL><INDENT>raise TypeError(<EOL>u"<STR_LIT>")<EOL><DEDENT>if self.count() == <NUM_LIT:0>:<EOL><INDENT>return enumerable<EOL><DEDENT>if enumerable.count() == <NUM_LIT:0>:<EOL><INDENT>return self<EOL><DEDENT>return self.concat(enumerable).distinct(key)<EOL>
Returns enumerable that is a union of elements between self and given enumerable :param enumerable: enumerable to union self to :param key: key selector used to determine uniqueness :return: new Enumerable object
f15479:c0:m35
def except_(self, enumerable, key=lambda x: x):
if not isinstance(enumerable, Enumerable3):<EOL><INDENT>raise TypeError(<EOL>u"<STR_LIT>")<EOL><DEDENT>membership = (<EOL><NUM_LIT:0> if key(element) in enumerable.intersect(self, key).select(key) else <NUM_LIT:1><EOL>for element in self<EOL>)<EOL>return Enumerable3(<EOL>itertools.compress(<EOL>self,<EOL>membership<EOL>)<EOL>)<EOL>
Returns enumerable that subtracts given enumerable elements from self :param enumerable: enumerable object :param key: key selector as lambda expression :return: new Enumerable object
f15479:c0:m36
def contains(self, element, key=lambda x: x):
return self.select(key).any(lambda x: x == key(element))<EOL>
Returns True if element is found in enumerable, otherwise False :param element: the element being tested for membership in enumerable :param key: key selector to use for membership comparison :return: boolean True or False
f15479:c0:m37
def aggregate(self, func, seed=None):
if self.count() == <NUM_LIT:0>:<EOL><INDENT>raise NoElementsError("<STR_LIT>")<EOL><DEDENT>result = seed if seed is not None else self.first()<EOL>for i, e in enumerate(self):<EOL><INDENT>if i == <NUM_LIT:0> and seed is None:<EOL><INDENT>continue<EOL><DEDENT>result = func(result, e)<EOL><DEDENT>return result<EOL>
Perform a calculation over a given enumerable using the initial seed value :param func: calculation to perform over every the enumerable. This function will ingest (aggregate_result, next element) as parameters :param seed: initial seed value for the calculation. If None, then the first element is used as the seed :return: result of the calculation
f15479:c0:m38
def all(self, predicate):
return self.where(predicate).count() == self.count()<EOL>
Determines whether all elements in an enumerable satisfy the given predicate :param predicate: the condition to test each element as lambda function :return: boolean True or False
f15479:c0:m39
def append(self, element):
return self.concat(Enumerable3([element]))<EOL>
Appends an element to the end of an enumerable :param element: the element to append to the enumerable :return: Enumerable object with appended element
f15479:c0:m40
def prepend(self, element):
return Enumerable3([element]).concat(self)<EOL>
Prepends an element to the beginning of an enumerable :param element: the element to prepend to the enumerable :return: Enumerable object with the prepended element
f15479:c0:m41
@staticmethod<EOL><INDENT>def empty():<DEDENT>
return Enumerable3()<EOL>
Returns an empty enumerable :return: Enumerable object that contains no elements
f15479:c0:m42
@staticmethod<EOL><INDENT>def range(start, length):<DEDENT>
return Enumerable3(range(start, start + length, <NUM_LIT:1>))<EOL>
Generates a sequence of integers starting from start with length of length :param start: the starting value of the sequence :param length: the number of integers in the sequence :return: Enumerable of the generated sequence
f15479:c0:m43
@staticmethod<EOL><INDENT>def repeat(element, length):<DEDENT>
return Enumerable3(itertools.repeat(element, length))<EOL>
Generates an enumerable containing an element repeated length times :param element: the element to repeat :param length: the number of times to repeat the element :return: Enumerable of the repeated elements
f15479:c0:m44
def reverse(self):
return self.aggregate(lambda *args: args[<NUM_LIT:0>].prepend(args[<NUM_LIT:1>]), Enumerable3())<EOL>
Inverts the order of the elements in a sequence :return: Enumerable with elements in reversed order
f15479:c0:m45
def skip_last(self, n):
return self.take(self.count() - n)<EOL>
Skips the last n elements in a sequence :param n: the number of elements to skip :return: Enumerable with n last elements removed
f15479:c0:m46
def skip_while(self, predicate):
return Enumerable3(itertools.dropwhile(predicate, self))<EOL>
Bypasses elements in a sequence while the predicate is True. After predicate fails remaining elements in sequence are returned :param predicate: a predicate as a lambda expression :return: Enumerable
f15479:c0:m47
def take_last(self, n):
return self.skip(self.count() - n)<EOL>
Takes the last n elements in a sequence :param n: the number of elements to take :return: Enumerable containing last n elements
f15479:c0:m48
def take_while(self, predicate):
return Enumerable3(itertools.takewhile(predicate, self))<EOL>
Includes elements in a sequence while the predicate is True. After predicate fails remaining elements in a sequence are removed :param predicate: a predicate as a lambda expression :return: Enumerable
f15479:c0:m49
def zip(self, enumerable, func):
if not isinstance(enumerable, Enumerable3):<EOL><INDENT>raise TypeError()<EOL><DEDENT>return Enumerable3(itertools.zip_longest(self, enumerable)).where(lambda x: x[<NUM_LIT:0>] is not None and x[<NUM_LIT:1>] is not None).select(lambda x: func(x))<EOL>
Merges 2 Enumerables using the given function. If the 2 collections are of unequal length, then merging continues until the end of one of the collections is reached :param enumerable: Enumerable collection to merge with :param func: a function to perform the merging :return: Enumerable
f15479:c0:m50
def __init__(self, key, data):
if not isinstance(key, Key):<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>self.key = key<EOL>super(Grouping3, self).__init__(data)<EOL>
Constructor of Grouping class used for group by operations of Enumerable class :param key: Key instance :param data: iterable object :return: void
f15479:c1:m0
def __init__(self, key_funcs, data):
if key_funcs is None:<EOL><INDENT>raise NullArgumentError(u"<STR_LIT>")<EOL><DEDENT>if not isinstance(key_funcs, list):<EOL><INDENT>raise TypeError(u"<STR_LIT>")<EOL><DEDENT>self._key_funcs = [<EOL>f for f in key_funcs if isinstance(f, OrderingDirection)<EOL>]<EOL>super(SortedEnumerable3, self).__init__(data)<EOL>
Constructor :param key_funcs: list of OrderingDirection instances in order of primary key --> less important keys :param data: data as iterable
f15479:c2:m0
def then_by(self, func):
if func is None:<EOL><INDENT>raise NullArgumentError(<EOL>u"<STR_LIT>")<EOL><DEDENT>self._key_funcs.append(OrderingDirection(key=func, reverse=False))<EOL>return SortedEnumerable3(self._key_funcs, self._data)<EOL>
Subsequent sorting function in ascending order :param func: lambda expression for secondary sort key :return: SortedEnumerable instance
f15479:c2:m2
def then_by_descending(self, func):
if func is None:<EOL><INDENT>raise NullArgumentError(<EOL>u"<STR_LIT>")<EOL><DEDENT>self._key_funcs.append(OrderingDirection(key=func, reverse=True))<EOL>return SortedEnumerable3(self._key_funcs, self._data)<EOL>
Subsequent sorting function in descending order :param func: lambda function for secondary sort key :return: SortedEnumerable instance
f15479:c2:m3
def __init__(self, data=None):
if data is None:<EOL><INDENT>data = []<EOL><DEDENT>if not hasattr(data, "<STR_LIT>"):<EOL><INDENT>raise TypeError(<EOL>u"<STR_LIT>"<EOL>)<EOL><DEDENT>self._data = data<EOL>
Constructor ** Note: no type checking of the data elements are performed during instantiation. ** :param data: iterable object :return: None
f15480:c0:m0
@property<EOL><INDENT>def data(self):<DEDENT>
return self._data<EOL>
The iterable of the Enumerable instance :return: iterable
f15480:c0:m1
def to_list(self):
return list(element for element in self)<EOL>
Converts the iterable into a list :return: list object
f15480:c0:m4
def count(self):
return sum(<NUM_LIT:1> for element in self)<EOL>
Returns the number of elements in iterable :return: integer object
f15480:c0:m5
def select(self, func=lambda x: x):
return Enumerable(itertools.imap(func, self))<EOL>
Transforms data into different form :param func: lambda expression on how to perform transformation :return: new Enumerable object containing transformed data
f15480:c0:m6