class ClassificationSample(MappedClass, JsonOdmHelper): """Python classification sample (training and/or predicted) in MongoDB.""" class __mongometa__: session = session name = 'classification_sample' indexes = [('model',), ('sharedId',)] unique_indexes = [('model', 'seqHash')] _id = FieldProperty(schema.ObjectId) model = FieldProperty(schema.String) seq = FieldProperty(schema.String) sharedId = FieldProperty(schema.String) seqHash = FieldProperty(schema.String) training_labels = FieldProperty( schema.Array(schema.Object(fields={'topic': schema.String}))) # Keep separate to control which samples should # be used even if more have training_labels. use_for_training = FieldProperty(schema.Bool) predicted_labels = FieldProperty( schema.Array( schema.Object(fields={ 'topic': schema.String, 'quality': schema.Float }))) update_timestamp = FieldProperty(datetime, if_missing=datetime.utcnow)
class User(Document): class __mongometa__: session = connection.connection name = 'users' _id = Field(schema.ObjectId) title = Field(schema.String) text = Field(schema.String) name = Field(schema.String) projects = Field(schema.Array(schema.String)) education = Field(schema.Array(schema.String)) skills = Field(schema.Array(schema.String)) learnings = Field(schema.Array(schema.String)) commits = Field(schema.Int) stack = Field( schema.Object(fields={ 'score': schema.Int, 'badges': schema.Int })) githubname = Field(schema.String) stackurl = Field(schema.String) linkedinurl = Field(schema.String) githuburl = Field(schema.String) email = Field(schema.String) avatar = Field(schema.String)
class TestDoc(Document): class __mongometa__: name = 'test_doc' session = self.MockSession a = Field(int) b = Field(S.Object(dict(a=int)))
def test_get_values_casting(self): val = self.provider._cast_value_for_type(S.Int, None) assert val is None val = self.provider._cast_value_for_type(S.Array(S.Int), '1234') assert val == [1234], val val = self.provider._cast_value_for_type(S.Object({'num': S.Int}), {'num': '1234'}) assert val == {'num': 1234}, val val = self.provider._cast_value_for_type( S.Array(S.Object(dict(value=s.Int))), [{ 'value': '1234' }]) assert val == [{'value': 1234}], val
class TestDoc(Document): class __mongometa__: name = 'test_doc' session = self.MockSession indexes = [('a', )] a = Field(S.Int, if_missing=None) b = Field(S.Object(dict(a=S.Int(if_missing=None))))
class TestDoc(Document): class __mongometa__: name='test_doc' session = self.session indexes = [ ('b','c') ] unique_indexes = [ ('cc'), ] _id=Field(S.ObjectId, if_missing=None) a=Field(S.Int, if_missing=None) b=Field(S.Object(dict(a=S.Int(if_missing=None)))) cc=Field(dict(dd=int, ee=int))
class WikiPageWithMetadata(MappedClass): class __mongometa__: session = session name = 'wiki_page_with_metadata' _id = FieldProperty(schema.ObjectId) title = FieldProperty(schema.String(required=True)) text = FieldProperty(schema.String(if_missing='')) metadata = FieldProperty(schema.Object({ 'tags': schema.Array(schema.String), 'categories': schema.Array(schema.String) }))
class WikiPage(MappedClass): class __mongometa__: session = session name = 'wiki_page' version_of = OldWikiPageCollection @staticmethod def migrate(data): result = dict(data, metadata={'tags': data['tags']}, _version=1) del result['tags'] return result _id = FieldProperty(schema.ObjectId) title = FieldProperty(schema.String(required=True)) text = FieldProperty(schema.String(if_missing='')) _version = FieldProperty(1, required=True) metadata = FieldProperty(schema.Object({ 'tags': schema.Array(schema.String), 'categories': schema.Array(schema.String) }))
def test_nested_objects(self): nested_object = S.Object(dict(a=int, b=int), if_missing=None) si = S.SchemaItem.make( dict(a=S.Object(dict(a=int, b=int), if_missing=None))) result = si.validate(dict()) self.assertEqual(result, dict(a=None))
class MeshTerm(MappedClass): class __mongometa__: session = session name = "mesh" polymorphic_on = "_type" polymorphic_identity = "base" _id = FieldProperty(schema.String) uid = FieldProperty(schema.String, index=True) name = FieldProperty(schema.String, index=True) concepts = FieldProperty( schema.Array( schema.Object({ "cuid": schema.String, "preferred": schema.Bool }))) active_mesh_years = FieldProperty(schema.Array(schema.String)) date_created = FieldProperty(schema.DateTime) date_established = FieldProperty(schema.DateTime) date_revised = FieldProperty(schema.DateTime) history_note = FieldProperty(schema.String) online_note = FieldProperty(schema.String) public_mesh_note = FieldProperty(schema.String) # not yet implemented record_originators = FieldProperty(schema.Array(schema.Object({}))) _type = FieldProperty(schema.String(if_missing="base")) def __init__(self, *args, **kwargs): record = kwargs.pop("record", None) super(MeshTerm, self).__init__() if record is not None: if type(self).__name__ == "Descriptor": self._id = record["DescriptorUI"] self.uid = self._id self.name = record["DescriptorName"]["String"] elif type(self).__name__ == "Qualifier": self._id = record["QualifierUI"] self.uid = self._id self.name = record["QualifierName"]["String"] elif type(self).__name__ == "SupplementaryConceptRecord": self._id = record["SupplementalRecordUI"] self.uid = self._id self.name = record["SupplementalRecordName"]["String"] if "ConceptList" in record: if isinstance(record["ConceptList"]["Concept"], list): self.concepts = [{ "cuid": c["ConceptUI"], "preferred": (c["@PreferredConceptYN"] == "Y") } for c in record["ConceptList"]["Concept"]] else: self.concepts = [{ "cuid": record["ConceptList"]["Concept"]["ConceptUI"], "preferred": (record["ConceptList"]["Concept"] ["@PreferredConceptYN"] == "Y") }] if "ActiveMeSHYearList" in record: if isinstance(record["ActiveMeSHYearList"]["Year"], list): self.active_mesh_years = record["ActiveMeSHYearList"][ "Year"] else: self.active_mesh_years = [ record["ActiveMeSHYearList"]["Year"] ] if "DateCreated" in record: self.date_created = datetime.strptime( " ".join([ record["DateCreated"]["Year"], record["DateCreated"]["Month"], record["DateCreated"]["Day"] ]), "%Y %m %d") if "DateEstablished" in record: self.date_establisted = datetime.strptime( " ".join([ record["DateEstablished"]["Year"], record["DateEstablished"]["Month"], record["DateEstablished"]["Day"] ]), "%Y %m %d") if "DateRevised" in record: self.date_revised = datetime.strptime( " ".join([ record["DateRevised"]["Year"], record["DateRevised"]["Month"], record["DateRevised"]["Day"] ]), "%Y %m %d") if "HistoryNote" in record: self.history_note = record["HistoryNote"] if "OnlineNote" in record: self.online_note = record["OnlineNote"] if "PublicMeSHNote" in record: self.public_mesh_note = record["PublicMeSHNote"]
class Project(Document): class __mongometa__: session = doc_session name = 'projects' indexes = [ ('shortname', ), ('source', ), ('sf_id', ), ] unique_indexes = [ ('shortname', 'source'), ] _review = dict(rating=int, useful=float, useless=float, approved=bool, user=str, comments=str, safe_html=bool, source=str, usefulness=float, date=datetime) _screenshot = dict(url=str, thumb=str, name=str, description=str) _category = dict(id=int, shortname=str, name=str, description=str, fullpath=str) _resource = dict(url=str, name=str, feed=str, item_count=int, item_open_count=int) _person = dict(username=None, homepage=None, name=None) _id = Field(S.ObjectId) shortname = Field(str) source = Field(str) sf_id = Field(int) projecttype = Field(int) private = Field(S.Bool(if_missing=False)) name = Field(str) summary = Field(StringNotNone) created = Field(datetime) description = Field(StringNotNone) doap = Field(str) project_url = Field(str) homepage = Field(str) updated = Field(S.Deprecated) _last_changed = Field('last_changed', datetime) ad_keywords = Field( [[str]]) # ['ohl', 'ad20848'] would translate to "ohl=ad20848;" in JS download_info = Field(S.Deprecated) _icon_url = Field('icon_url', S.String(if_missing=S.Missing)) # for backward compat. _features = Field('features', S.Array(str, if_missing=S.Missing)) # for backward compat. reviews_disabled = Field(bool) relations_data = Field( S.Object(dict(is_admin=S.Deprecated, rating=float, code=int, review_count=int, features=[str], tags=[dict(count=int, tag=str, approved=bool)], icon_url=str, latest_reviews=[_review], name=str, reviews=[_review], text=str), if_missing=None)) related = Field([ dict(source=None, shortname=None, name=str, description=str, screenshots=[_screenshot], ok_to_recommend=bool, rating=float, review_count=int, icon_url=str) ]) recommended = Field( [dict(source=None, shortname=None, name=str, description=str)]) screenshots = Field([_screenshot]) resources = Field( dict( other=[_resource], mailing_lists=[_resource], news=[_resource], forums=[_resource], trackers=[_resource], )) feed_recent_items = Field([ dict( _id=str, description=str, title=str, url=str, project=dict(source=str, shortname=str), date=datetime, type=str, description_type=str, author_username=str, permission_required=S.Array( str, if_missing=None ), # controllers/project.py queries mongodb for None which doens't match [] ) ]) categories = Field({ 'Topic': [_category], 'Operating System': [_category], 'Development Status': [_category], 'License': [_category], 'Translations': [_category], 'Intended Audience': [_category], 'User Interface': [_category], 'Programming Language': [_category], 'Database Environment': [_category], }) feeds_last_item = Field( S.Migrate(None, [dict(url=str, date=datetime)], S.Migrate.obj_to_list('url', 'date'))) inactive = Field(datetime) new_project_url = Field(str) donation_page = Field(str) preferred_support = Field(str) code_repositories = Field([ dict(label=str, browse=str, write_operations=int, read_operations=int, location=str, type=str) ]) releases = Field( S.Array( dict( filename=str, url=str, date=datetime, bytes=float, file_type=S.String(if_missing=''), mime_type=str, md5sum=str, sf_download_label=str, sf_platform_default=[str], sf_release_notes_file=str, sf_file_id=int, # FRS data (pre-PFS) sf_release_id=int, sf_package_id=int, sf_type=str, sf_platform=[str], release_notes_url=str, # old FRS data (shouldn't exist any more) # download_count=S.Deprecated, # group=S.Deprecated, #str, # version=S.Deprecated, #str, # changelog=S.Deprecated, #str, # release_notes=S.Deprecated, #str, ), validate_ranges=(slice(0, 5), slice(-5, -1)))) download_page = Field(str) screenshot_page = Field(str) maintainers = Field([_person]) developers = Field([_person]) file_feed = Field(str) awards = Field([dict(category=str, url=str, event=str, img_url=str)]) sf_piwik_siteid = Field(str) license = Field(S.Deprecated()) license_uri = Field(str) license_title = Field(str) developer_page = Field(str) test_foo2 = Field(S.Deprecated) fossforus_id = Field(S.Deprecated) fossforus_screenshots = Field(S.Deprecated) fossforus_features = Field(S.Deprecated) fossforus_tags = Field(S.Deprecated) fossforus_ratings = Field(S.Deprecated) _last_snapshot_id = Field(S.Deprecated)
class Action(MappedClass): class __mongometa__: session = model.DBSession name = 'activity_stream_action' indexes = [ (('actor_id', ), ('timestamp', DESCENDING)), ('actor_type', ), (('_recipients.recipient_id', ), ('timestamp', DESCENDING)), ('_recipients.recipient_type', ), ] _id = FieldProperty(s.ObjectId) actor_type = FieldProperty(s.String) actor_id = FieldProperty(s.ObjectId) verb = FieldProperty(s.String) description = FieldProperty(s.String) extra = FieldProperty(s.String) target_type = FieldProperty(s.String) target_id = FieldProperty(s.ObjectId) action_obj_type = FieldProperty(s.String) action_obj_id = FieldProperty(s.ObjectId) timestamp = FieldProperty(s.DateTime, if_missing=datetime.utcnow) _recipients = FieldProperty( s.Array(s.Object(fields={ '_type': s.String, '_id': s.ObjectId }))) @property def timestamp_24_hh_mm(self): return datetime.strftime(self.timestamp, '%X') @cached_property def actor(self, default=None): if not (self.actor_type and self.actor_id): return default entity = get_obj(self.actor_type, self.actor_id) return getattr(entity, 'as_str', entity) @cached_property def target(self, default=None): if not (self.target_type and self.target_id): return default entity = get_obj(self.target_type, self.target_id) return getattr(entity, 'as_str', entity) @property def target_link(self): entity = get_obj(self.target_type, self.target_id) return getattr(entity, 'as_link', None) @property def action_obj(self, default=None): if not (self.action_obj_type and self.action_obj_id): return default return get_obj(self.action_obj_type, self.action_obj_id) @property def timestamp_since(self): diff = datetime.utcnow() - self.timestamp minutes = diff.total_seconds() / 60 if minutes <= 1: timestamp_since_ = _(u'less than 1 minute ago') elif int(minutes) == 1: timestamp_since_ = _(u'about 1 minute ago') elif minutes < 60: timestamp_since_ = _(u'about %s minutes ago') % int(minutes) elif 60 <= minutes < 119: timestamp_since_ = _(u'about 1 hour ago') elif minutes < 60 * 24: timestamp_since_ = _(u'about %s hours ago') % int(minutes / 60) else: timestamp_since_ = datetime.strftime(self.timestamp, '%x') return timestamp_since_ @property def recipients(self): return (get_obj(r._type, r._id) for r in self._recipients) @classmethod def get_by_recipient(cls, recipient): return cls.query.find({ '$or': [{ '_recipients._id': instance_primary_key(recipient) }, { '_recipients': { '$eq': None } }] }).sort('timestamp', DESCENDING) @classmethod def count_not_seen_by_recipient(cls, recipient): return cls.query.find({ '$or': [ { '_recipients._id': instance_primary_key(recipient) }, { '_recipients': { '$eq': None } }, ], 'timestamp': { '$gt': recipient.last_activity_seen }, }).count() @classmethod def render_str(cls, action, **kw): ''' <actor> <verb> <time> <actor> <verb> <target> <time> <actor> <verb> <action_object> <target> <time> ''' timestamp_format = kw.get('timestamp_format', lambda t: datetime.strftime(t, '%x')) timestamp_since = None if kw.get('timestamp_since', False): timestamp_since = action.timestamp_since str_ = u'{actor} {verb} {action_object} {target} {time}'.format( actor=action.actor, verb=action.verb, target=action.target or '', action_object=action.action_obj or '', time=timestamp_since or timestamp_format(action.timestamp)) return str_