當前位置: 首頁>>代碼示例>>Python>>正文


Python mongoengine.NotUniqueError方法代碼示例

本文整理匯總了Python中mongoengine.NotUniqueError方法的典型用法代碼示例。如果您正苦於以下問題:Python mongoengine.NotUniqueError方法的具體用法?Python mongoengine.NotUniqueError怎麽用?Python mongoengine.NotUniqueError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在mongoengine的用法示例。


在下文中一共展示了mongoengine.NotUniqueError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: save_proxies

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def save_proxies(url):
    proxies = []
    try:
        r = fetch(url)
    except requests.exceptions.RequestException:
        return False
    addresses = re.findall(PROXY_REGEX, r.text)
    for address in addresses:
        proxy = Proxy(address=address)
        try:
            proxy.save()
        except NotUniqueError:
            pass
        else:
            proxies.append(address)
    return proxies 
開發者ID:dongweiming,項目名稱:web_develop,代碼行數:18,代碼來源:proxy_fetcher_with_queue.py

示例2: save_article

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def save_article(article_):
    img_url = article_.find(class_='img_box2').find(
        'img').attrs['src'].split('url=')[1]
    text_box = article_.find(class_='txt-box')
    title = text_box.find('h4').find('a').text
    article_url = text_box.find('h4').find('a').attrs['href']
    summary = text_box.find('p').text
    create_at = datetime.fromtimestamp(float(text_box.find(
        class_='s-p').attrs['t']))
    publisher_name = text_box.find(class_='s-p').find('a').attrs['title']

    article = Article(img_url=img_url, title=title, article_url=article_url,
                      summary=summary, create_at=create_at,
                      publisher=Publisher.get_or_create(publisher_name))
    try:
        article.save()
    except (NotUniqueError, InvalidBSON):
        pass 
開發者ID:dongweiming,項目名稱:web_develop,代碼行數:20,代碼來源:search_result.py

示例3: insert_system_roles

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def insert_system_roles():
    """
    Migration which inserts the default system roles.
    """
    system_roles = SystemRole.get_valid_values()

    LOG.debug('Inserting system roles (%s)' % (str(system_roles)))

    for role_name in system_roles:
        description = role_name
        role_db = RoleDB(name=role_name, description=description, system=True)

        try:
            role_db.save()
        except (StackStormDBObjectConflictError, NotUniqueError):
            # Role already exists error is not fatal
            pass 
開發者ID:StackStorm,項目名稱:st2,代碼行數:19,代碼來源:migrations.py

示例4: test_create_dup_uuid_fail

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def test_create_dup_uuid_fail(self):
        """
        This test will attempt to create targets with the same uuids,
        and it will fail as mongo should throw a not unique exception.
        """

        # basic test
        with self.assertRaises(NotUniqueError):
            target1 = Database.create_target(None, 'AA:BB:CC:DD:EE:FF')
            target2 = Database.create_target(None, 'AA:BB:CC:DD:EE:FF')
            self.assertEqual(target1.uuid, target2.uuid)

        # different encoding
        with self.assertRaises(NotUniqueError):
            target1 = Database.create_target(None, 'AA:BB:CC:DD:EE:FF'.encode('utf-8'))
            target2 = Database.create_target(None, 'AA:BB:CC:DD:EE:FF'.encode('ascii'))
            self.assertEqual(target1.uuid, target2.uuid) 
開發者ID:KCarretto,項目名稱:Arsenal,代碼行數:19,代碼來源:target_test.py

示例5: _create_call_sets

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def _create_call_sets(self):
        for sample in self.vcf_reader.samples:
            try:
                cs = VariantCallSet.create_and_save(
                    name="_".join(
                        [
                            sample,
                            self.method]),
                    variant_sets=self.variant_sets,
                    sample_id=sample,
                    info={
                        "variant_caller": self.method})
            except NotUniqueError:
                raise ValueError(
                    "There is already a call set for sample %s with method %s " %
                    (sample, self.method))
            else:
                self.call_sets[sample] = cs 
開發者ID:Mykrobe-tools,項目名稱:mykrobe,代碼行數:20,代碼來源:models.py

示例6: run_make_probes_from_vcf_file

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def run_make_probes_from_vcf_file(args):
    # Make VariantSet from vcf
    reference = os.path.basename(args.reference_filepath).split(".fa")[0]
    try:
        reference_set = ReferenceSet.objects.get(name=reference)
    except DoesNotExist:
        reference_set = ReferenceSet.create_and_save(name=reference)
        # Hack
    try:
        reference = Reference.create_and_save(
            name=reference, reference_sets=[reference_set], md5checksum=reference
        )
    except NotUniqueError:
        pass
    vcf = VCF(
        args.vcf,
        reference_set.id,
        method="tmp",
        force=True,
        append_to_global_variant_set=False,
    )
    vcf.add_to_database() 
開發者ID:Mykrobe-tools,項目名稱:mykrobe,代碼行數:24,代碼來源:makeprobes.py

示例7: create_group

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def create_group(groupname):
    try:
        return Group(groupname=groupname).save()
    except NotUniqueError:
        return False 
開發者ID:yeti-platform,項目名稱:yeti,代碼行數:7,代碼來源:group_management.py

示例8: save_proxies

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def save_proxies(url):
    try:
        r = fetch(url)
    except requests.exceptions.RequestException:
        return False
    addresses = re.findall(PROXY_REGEX, r.text)
    for address in addresses:
        proxy = Proxy(address=address)
        try:
            proxy.save()
        except NotUniqueError:
            pass 
開發者ID:dongweiming,項目名稱:web_develop,代碼行數:14,代碼來源:proxy_fetcher.py

示例9: insert

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def insert(cls, model_object, publish=True, dispatch_trigger=True,
               log_not_unique_error_as_debug=False):
        # Late import to avoid very expensive in-direct import (~1 second) when this function
        # is not called / used
        from mongoengine import NotUniqueError

        if model_object.id:
            raise ValueError('id for object %s was unexpected.' % model_object)
        try:
            model_object = cls._get_impl().insert(model_object)
        except NotUniqueError as e:
            if log_not_unique_error_as_debug:
                LOG.debug('Conflict while trying to save in DB: %s.', six.text_type(e))
            else:
                LOG.exception('Conflict while trying to save in DB.')
            # On a conflict determine the conflicting object and return its id in
            # the raised exception.
            conflict_object = cls._get_by_object(model_object)
            conflict_id = str(conflict_object.id) if conflict_object else None
            message = six.text_type(e)
            raise StackStormDBObjectConflictError(message=message, conflict_id=conflict_id,
                                                  model_object=model_object)

        # Publish internal event on the message bus
        if publish:
            try:
                cls.publish_create(model_object)
            except:
                LOG.exception('Publish failed.')

        # Dispatch trigger
        if dispatch_trigger:
            try:
                cls.dispatch_create_trigger(model_object)
            except:
                LOG.exception('Trigger dispatch failed.')

        return model_object 
開發者ID:StackStorm,項目名稱:st2,代碼行數:40,代碼來源:base.py

示例10: _register_internal_trigger_type

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def _register_internal_trigger_type(trigger_definition):
    try:
        trigger_type_db = create_trigger_type_db(trigger_type=trigger_definition,
                                                 log_not_unique_error_as_debug=True)
    except (NotUniqueError, StackStormDBObjectConflictError):
        # We ignore conflict error since this operation is idempotent and race is not an issue
        LOG.debug('Internal trigger type "%s" already exists, ignoring error...' %
                  (trigger_definition['name']))

        ref = ResourceReference.to_string_reference(name=trigger_definition['name'],
                                                    pack=trigger_definition['pack'])
        trigger_type_db = get_trigger_type_db(ref)

    if trigger_type_db:
        LOG.debug('Registered internal trigger: %s.', trigger_definition['name'])

    # trigger types with parameters do no require a shadow trigger.
    if trigger_type_db and not trigger_type_db.parameters_schema:
        try:
            trigger_db = create_shadow_trigger(trigger_type_db,
                log_not_unique_error_as_debug=True)

            extra = {'trigger_db': trigger_db}
            LOG.audit('Trigger created for parameter-less internal TriggerType. Trigger.id=%s' %
                      (trigger_db.id), extra=extra)
        except (NotUniqueError, StackStormDBObjectConflictError):
            LOG.debug('Shadow trigger "%s" already exists. Ignoring.',
                      trigger_type_db.get_reference().ref, exc_info=True)

        except (ValidationError, ValueError):
            LOG.exception('Validation failed in shadow trigger. TriggerType=%s.',
                          trigger_type_db.get_reference().ref)
            raise

    return trigger_type_db 
開發者ID:StackStorm,項目名稱:st2,代碼行數:37,代碼來源:triggers.py

示例11: register_internal_trigger_types

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def register_internal_trigger_types():
    """
    Register internal trigger types.

    NOTE 1: This method blocks until all the trigger types have been registered.

    NOTE 2: We log "NotUniqueError" errors under debug and not error. Those errors are not fatal
    because this operation is idempotent and NotUniqueError simply means internal trigger type
    has already been registered by some other service.
    """
    action_sensor_enabled = cfg.CONF.action_sensor.enable

    registered_trigger_types_db = []

    for _, trigger_definitions in six.iteritems(INTERNAL_TRIGGER_TYPES):
        for trigger_definition in trigger_definitions:
            LOG.debug('Registering internal trigger: %s', trigger_definition['name'])

            is_action_trigger = trigger_definition['name'] == ACTION_SENSOR_TRIGGER['name']
            if is_action_trigger and not action_sensor_enabled:
                continue
            try:
                trigger_type_db = _register_internal_trigger_type(
                    trigger_definition=trigger_definition)
            except Exception:
                LOG.exception('Failed registering internal trigger: %s.', trigger_definition)
                raise
            else:
                registered_trigger_types_db.append(trigger_type_db)

    return registered_trigger_types_db 
開發者ID:StackStorm,項目名稱:st2,代碼行數:33,代碼來源:triggers.py

示例12: test_create_dup_name_fail

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def test_create_dup_name_fail(self):
        """
        This test will attempt to create two targets with identitical names,
        and it will fail as Mongo should throw a not unique exception.
        """
        with self.assertRaises(NotUniqueError):
            target1 = Database.create_target(self.TEST_NAME)
            target2 = Database.create_target(self.TEST_NAME)
            self.assertEqual(target1.name, target2.name) 
開發者ID:KCarretto,項目名稱:Arsenal,代碼行數:11,代碼來源:target_test.py

示例13: map_ids_from_line

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def map_ids_from_line(self, line: CrisprLine, mode, wges_dict=None):
        crispr: Crispr = self.create_crispr_from_crispr_line(line)
        try:
            crispr.extract_and_save_wge(mode, wges_dict)
        except NoWGEException as ex:
            logger.error(ex.msg())
            pass
        except (DuplicateKeyError, NotUniqueError) as ex:
            logger.warning("Skipping duplicate wge_id")
            pass
        except Exception as e:
            traceback.print_exc()
            logger.error(f"A problem happened for {self.filename}, oligo_id {crispr.crispr_line.get_oligo_id}")
            pass 
開發者ID:felicityallen,項目名稱:SelfTarget,代碼行數:16,代碼來源:crispr.py

示例14: _create_new_variant_set

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def _create_new_variant_set(self):
        variant_set_name = os.path.basename(
            self.f)
        if VariantSet.objects(
                name=variant_set_name,
                reference_set=self.reference_set):
            if not self.force:
                raise NotUniqueError(
                    "VariantSet %s already exists. Rerun with -f to recreate." %
                    variant_set_name)
            else:
                self._remove_variant_set(variant_set_name)
        self.vcf_variant_set = VariantSet.create_and_save(
            name=variant_set_name,
            reference_set=self.reference_set) 
開發者ID:Mykrobe-tools,項目名稱:mykrobe,代碼行數:17,代碼來源:models.py

示例15: api

# 需要導入模塊: import mongoengine [as 別名]
# 或者: from mongoengine import NotUniqueError [as 別名]
def api(uri, login=False, **kwargs):
    def decorator(f):
        @wraps(f)
        def wrapped_f(*func_args, **func_kwargs):
            if login:
                user_token = request.cookies.get('user-token')
                if user_token is not None:
                    try:
                        func_kwargs['user'] = users.validate_token(user_token)
                    except utils.AuthenticationError:
                        return JSONResponse(status=httplib.UNAUTHORIZED)
                else:
                    return JSONResponse(status=httplib.UNAUTHORIZED)

            try:
                results, status_code = f(*func_args, **func_kwargs)
                if 'count' in request.args:
                    if isinstance(results, BaseQuerySet):
                        results = results.count()
                    else:
                        results = len(results)

            except mongoengine.ValidationError:
                traceback.print_exc()
                status_code = httplib.BAD_REQUEST
                results = {"error": "invalid input"}

            except mongoengine.NotUniqueError:
                traceback.print_exc()
                status_code = httplib.BAD_REQUEST
                results = {"error": "not unique"}

            except Exception as e:
                traceback.print_exc()
                status_code = httplib.INTERNAL_SERVER_ERROR
                results = None

            if request.args.get('format', 'json') == 'bson':
                output = bsonify(results)  # .json_util.dumps(results, indent=2, sort_keys=True)
                return Response(output, status=status_code, content_type='application/json')
            else:
                return JSONResponse(results, status=status_code)

        endpoint = kwargs.pop('endpoint', f.__name__)  # + str(len(api_endpoints)))
        app.add_url_rule(uri, endpoint, wrapped_f, **kwargs)
        assert endpoint not in api_endpoints
        api_endpoints[endpoint] = f, uri, kwargs.get('methods', ['GET']), rest_doc(f)
        return f
    return decorator 
開發者ID:mitre,項目名稱:cascade-server,代碼行數:51,代碼來源:api.py


注:本文中的mongoengine.NotUniqueError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。