Commit e6f09a10 authored by Mathieu Rodic's avatar Mathieu Rodic

[DOC] documented the `Node` model

parent 883a52c1
...@@ -12,7 +12,7 @@ __all__ = ['Node'] ...@@ -12,7 +12,7 @@ __all__ = ['Node']
class NodeType(TypeDecorator): class NodeType(TypeDecorator):
"""Define a new type of column to describe a Node's type. """Define a new type of column to describe a Node's type.
This column type is implemented as an SQL integer. Internally, this column type is implemented as an SQL integer.
Values are detailed in `gargantext.constants.NODETYPES`. Values are detailed in `gargantext.constants.NODETYPES`.
""" """
impl = Integer impl = Integer
...@@ -22,6 +22,11 @@ class NodeType(TypeDecorator): ...@@ -22,6 +22,11 @@ class NodeType(TypeDecorator):
return NODETYPES[typeindex] return NODETYPES[typeindex]
class Node(Base): class Node(Base):
"""This model can fit many purposes.
It intends to provide a generic model, allowing hierarchical structure
and NoSQL-like data structuring.
The possible types are defined in `gargantext.constants.NODETYPES`.
"""
__tablename__ = 'nodes' __tablename__ = 'nodes'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
typename = Column(NodeType, index=True) typename = Column(NodeType, index=True)
...@@ -35,18 +40,29 @@ class Node(Base): ...@@ -35,18 +40,29 @@ class Node(Base):
hyperdata = Column(JSONB, default=dict) hyperdata = Column(JSONB, default=dict)
def __init__(self, **kwargs): def __init__(self, **kwargs):
"""Node's constructor.
Initialize the `hyperdata` as a dictionary if no value was given.
"""
if 'hyperdata' not in kwargs: if 'hyperdata' not in kwargs:
kwargs['hyperdata'] = kwargs.get('hyperdata', {}) kwargs['hyperdata'] = kwargs.get('hyperdata', MutableDict())
Base.__init__(self, **kwargs) Base.__init__(self, **kwargs)
def __getitem__(self, key): def __getitem__(self, key):
"""Allow direct access to hyperdata via the bracket operator.
"""
return self.hyperdata[key] return self.hyperdata[key]
def __setitem__(self, key, value): def __setitem__(self, key, value):
"""Allow direct access to hyperdata via the bracket operator.
"""
self.hyperdata[key] = value self.hyperdata[key] = value
@property @property
def ngrams(self): def ngrams(self):
"""Pseudo-attribute allowing to retrieve a node's ngrams.
Returns a query (which can be further filtered), of which returned rows
are the ngram's weight for this node and the ngram.
"""
from . import NodeNgram, Ngram from . import NodeNgram, Ngram
query = (session query = (session
.query(NodeNgram.weight, Ngram) .query(NodeNgram.weight, Ngram)
...@@ -57,6 +73,10 @@ class Node(Base): ...@@ -57,6 +73,10 @@ class Node(Base):
return query return query
def as_list(self): def as_list(self):
"""Retrieve the current node as a list/matrix of ngrams identifiers.
See `gargantext.util.lists` and `gargantext.constants.LISTTYPES`
for more info.
"""
try: try:
return LISTTYPES[self.typename](self.id) return LISTTYPES[self.typename](self.id)
except KeyError: except KeyError:
...@@ -100,17 +120,27 @@ class Node(Base): ...@@ -100,17 +120,27 @@ class Node(Base):
) )
def resources(self): def resources(self):
"""Return all the resources attached to a given node.
Mainly used for corpora.
"""
if 'resources' not in self.hyperdata: if 'resources' not in self.hyperdata:
self['resources'] = MutableList() self['resources'] = MutableList()
return self['resources'] return self['resources']
def add_resource(self, type, path=None, url=None): def add_resource(self, type, path=None, url=None):
"""Attach a resource to a given node.
Mainly used for corpora.
"""
self.resources().append(MutableDict( self.resources().append(MutableDict(
{'type': type, 'path':path, 'url':url, 'extracted': False} {'type': type, 'path':path, 'url':url, 'extracted': False}
)) ))
def status(self, action=None, progress=0, complete=False, error=None): def status(self, action=None, progress=0, complete=False, error=None):
"""Get the status of the given action """Get or update the status of the given action.
If no action is given, the status of the first uncomplete or last item
is returned.
The `complete` parameter should be a boolean.
The `error` parameter should be an exception.
""" """
date = datetime.now() date = datetime.now()
# if the hyperdata do not have data about status # if the hyperdata do not have data about status
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment