pyuseocl.utils.fragments

Classes

ABCMeta Metaclass for defining Abstract Base Classes (ABCs).
Fragment(start[, end, value, parent, children]) A fragment is a decorated
Fragmenter(sequence[, mainValue, firstPosition]) Abstract base class for fragmenters.
RegexpFragmenter(sequence[, openingRegexp, ...])
>>> text = [

Functions

abstractmethod(funcobj) A decorator indicating abstract methods.

ABCMeta

class pyuseocl.utils.fragments.ABCMeta[source]

Metaclass for defining Abstract Base Classes (ABCs).

Use this metaclass to create an ABC. An ABC can be subclassed directly, and then acts as a mix-in class. You can also register unrelated concrete classes (even built-in classes) and unrelated ABCs as ‘virtual subclasses’ – these and their descendants will be considered subclasses of the registering ABC by the built-in issubclass() function, but the registering ABC won’t show up in their MRO (Method Resolution Order) nor will method implementations defined by the registering ABC be callable (not even via super()).

register(subclass)[source]

Register a virtual subclass of an ABC.

__instancecheck__(instance)[source]

Override for isinstance(instance, cls).

__subclasscheck__(subclass)[source]

Override for issubclass(subclass, cls).

Fragment

class pyuseocl.utils.fragments.Fragment(start, end=None, value=None, parent=None, children=None)[source]

A fragment is a decorated

start = None

The index of the beginning of the fragment

end = None

The index of the end of the fragment

value = None

The value associated with the fragment

parent = None

The parent of this fragment or None if this is the root

children = None

The children of this fragment

addChild(fragment)[source]
range()[source]
rangeAndValue()[source]
ancestors(includingSelf=False)[source]
descendants(includingSelf=False)[source]
depth(includingSelf=False)[source]
fragmentsOfValue(value)[source]
fragmentAtPosition(position)[source]
fragmentsAtPosition(position)[source]

Fragmenter

class pyuseocl.utils.fragments.Fragmenter(sequence, mainValue=None, firstPosition=1)[source]

Abstract base class for fragmenters.

Parameters:
  • sequence ([X]) – A sequence of arbitrary elements.
  • mainValue (Y) – Any value attached to the main fragment
  • firstPosition (int) – The position of the first element (e.g. 0 or 1)
Returns:

aa fragmenter object. Use f = Fragmenter(...).fragment

Return type:

Fragmenter

opening(element, enclosingFragments)[source]
closing(element, enclosingFragments, value=None)[source]
here(element, enclosingFragments)[source]

RegexpFragmenter

class pyuseocl.utils.fragments.RegexpFragmenter(sequence, openingRegexp='--oo<< *(?P<value>[^ \n]+) *$', closingRegexp='--oo>> *$', hereRegexp='--oo== *(?P<value>[^ \n]+) *$', mainValue=None, firstPosition=1)[source]
>>> text = [
... 'First line',
... 'This is a text --oo<<first_sentence',
... 'with some',
... 'markers. --oo>>',
... 'We start --oo<< block',
... 'something with',
... 'more text',
... 'and a figure. --oo== figure1',
... 'With a nested --oo<< nested',
... 'block. --oo>>',
... 'Closing block. --oo>>',
... 'This is the end --oo== end',
... ]
>>> print len(text)
12
>>> fragment = RegexpFragmenter(text,mainValue='main').fragment
>>> print len(fragment.descendants(True))
6
>>> print fragment.depth()
2
>>> print fragment.fragmentsOfValue('not there')
[]
>>> print fragment.fragmentsOfValue('nested')
[Fragment(9-10:nested,[])]
>>> print fragment.fragmentAtPosition(2).value
first_sentence
>>> print fragment.fragmentAtPosition(10).value
nested
>>> print fragment.fragmentAtPosition(1).value
main
>>> print fragment.fragmentAtPosition(1000)
None
>>> def values(sequence) : return map(lambda f:f.value, sequence)
>>> print values(fragment.fragmentsAtPosition(1))
['main']
>>> print values(fragment.fragmentsAtPosition(10))
['nested', 'block', 'main']
>>> print values(fragment.fragmentsAtPosition(1000))
[]
opening(element, enclosingFragments)[source]
closing(element, enclosingFragments, value=None)[source]
here(element, enclosingFragments)[source]

abstractmethod()

pyuseocl.utils.fragments.abstractmethod(funcobj)[source]

A decorator indicating abstract methods.

Requires that the metaclass is ABCMeta or derived from it. A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract methods are overridden. The abstract methods can be called using any of the normal ‘super’ call mechanisms.

Usage:

class C:

__metaclass__ = ABCMeta @abstractmethod def my_abstract_method(self, ...):

...