Pyjo.Collection - Collection

import Pyjo.Collection

# Manipulate collection
collection = Pyjo.Collection.new(['just', 'works'])
collection.insert(0, 'it')
print(collection.join("\n"))

# Chain methods
collection.map(lambda word: word.capitalize()).shuffle() \
    .each(lambda word, num: print('{0}: {1}'.format(word, num)))

# Use the alternative constructor
from Pyjo.Collection import c
c(['a', 'b', 'c']).join('/').encode().url_escape().decode().say()

Pyjo.Collection is a container for list-based collections which inherits all methods from list and provides own methods.

Classes

class Pyjo.Collection.Pyjo_Collection

Pyjo.Collection inherits all methods from list and implements the following new ones.

__weakref__

list of weak references to the object (if defined)

compact()
new = collection.compact()

Create a new collection with all elements that are defined and not an empty string or list.

# "0, 1, 2, 3"
Pyjo.Collection.new([0, 1, None, 2, '', 3]).compact().join(', ')
each(cb=None)
iterator = collection.each()
collection = collection.each(lambda e, num: ...)

Evaluate callback for each element in collection or return all elements as an iterator if none has been provided. The element will be the first argument passed to the callback.

# Make a numbered list
@collection.each
def cb(e, num):
    print("{0}: {1}".format(e, num))
first(matched=None)
first = collection.first()
first = collection.first('string')
first = collection.first(m(r'pattern', 'flags'))
first = collection.first(lambda i: ...)

Evaluate string or regular expression or callback for each element in collection and return the first one that matched the string or regular expression, or for which the callback returned true. The element will be the first argument passed to the callback.

# Find first value that contains the word "mojo"
interesting = collection.first(m('pyjo', 'i'))

# Find first value that is greater than 5
greater = collection.first(lambda i: i > 5)

Return the first element in collection or None if collection is empty.

flatten()
new = collection.flatten()

Flatten nested collections/lists/tuples recursively and create a new collection with all elements.

# "1, 2, 3, 4, 5, 6, 7"
Pyjo.Collection.new([1, [2, [3, 4], 5, [6]], 7]).flatten().join(', ').say()
get(index, default=None)
value = collection.get(index)
value = collection.get(index, default)

Return the value for index if index is in the collection, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

import sys
from Pyjo.Util import die
address = c(sys.argv).get(1) or die('address missing')
port = int(c(sys.argv).get(2, '80'))
grep(matched)
new = collection.grep('string')
new = collection.grep(m(r'pattern', 'flags'))
new = collection.grep(lambda i: ...)

Evaluate string or regular expression or callback for each element in collection and create a new collection with all elements that matched the regular expression, or for which the callback returned true. The element will be the first argument passed to the callback.

# Find all values that contain the word “mojo” interesting = collection.grep(m(‘mojo’, ‘i’))

# Find all values that are greater than 5 greater = collection.grep(lambda i: i > 5)

item(offset)
item = collection.item(0)

Return element from collection.

# the same as
item = collection[0]
join(string=u'')
stream = collection.join()
stream = collection.join("\n")

Turn collection into Pyjo.String.Bytes.

# Join all values with commas
collection.join(', ').say()
last()
last = collection.last()

Return the last element in collection or None if collection is empty.

map(*args, **kwargs)
new = collection.map(lambda a: ...)
new = collection.map(attribute)
new = collection.map(attribute, value)
new = collection.map(method)
new = collection.map(method, *args, **kwargs)

Evaluate callback for, or get/set attribute from, or call method on, each element in collection and create a new collection from the results. The element will be the first argument passed to the callback.

# Longer version for attribute
new = collection.map(lambda a: getattr(a, attribute))
new = collection.map(lambda a: setattr(a, attribute, value))

# Longer version for method
new = collection.map(lambda a: getattr(a, method)(*args))

# Append the word "pyjo" to all values
pyjoified = collection.map(lambda a: a + 'pyjo')
classmethod new(value=[])
collection = Pyjo.Collection.new([1, 2, 3])

Construct a new Pyjo.Collection object.

reverse()
new = collection.reverse()

Create a new collection with all elements in reverse order.

size
size = collection.size()

Number of elements in collection.

to_dict()
d = params.to_dict()

Turn collection of pairs of key and value into a dict.

# {'b': 2, 'a': 1, 'c': 3}
Pyjo.Collection.new([('a', 1), ('b', 2), ('c', 3)]).to_dict()
to_iter()
i = params.to_iter()

Turn collection into a iter iterator.

for i in Pyjo.Collection.new([1, 2, 3]).to_iter():
    print(i)
to_list()
l = params.to_list()

Turn collection into a list.

# [1, 2, 3]
Pyjo.Collection.new([1, 2, 3]).to_list()
to_set()
s = params.to_set()

Turn collection into a set.

# {1, 2, 3}
Pyjo.Collection.new([1, 2, 3]).to_set()
to_tuple()
t = params.to_tuple()

Turn collection into a tuple.

# (1, 2, 3)
Pyjo.Collection.new([1, 2, 3]).to_tuple()
zip(list2)
new = collection.zip([1, 2, 3])

Agregates elements from collection and iterable and returns new collection of pairs.

# [('a', 1), ('b', 2), ('c', 3)]
Pyjo.Collection.new(['a', 'b', 'c']).zip([1, 2, 3])
Pyjo.Collection.c(value=[])
collection = c([1, 2, 3])

Construct a new Pyjo.Collection object.

Pyjo.Collection.object

alias of Pyjo_Collection