Python slugify 模块,Slugify() 实例源码

我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用slugify.Slugify()

项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def test_to_lower_arg(self):
        slugify = Slugify()
        slugify.to_lower = True

        self.assertEqual(slugify('Test TO lower'), 'test-to-lower')
        self.assertEqual(slugify('Test TO lower', to_lower=False), 'Test-TO-lower')
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def test_pretranslate(self):
        EMOJI_TRANSLATION = {
            u'???': u'smiling',
            u'?_?': u'disapproval',
            u'???': u'enamored',
            u'?': u'love',

            u'(c)': u'copyright',
            u'©': u'copyright',
        }
        slugify_emoji = Slugify(pretranslate=EMOJI_TRANSLATION)
        self.assertEqual(slugify_emoji(u'???'), u'smiling')
        self.assertEqual(slugify_emoji(u'?_?'), u'disapproval')
        self.assertEqual(slugify_emoji(u'(c)'), u'copyright')
        self.assertEqual(slugify_emoji(u'©'), u'copyright')
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def test_pretranslate_lambda(self):
        slugify_reverse = Slugify(pretranslate=lambda value: value[::-1])
        self.assertEqual(slugify_reverse('slug'), 'guls')
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def test_wrong_argument_type(self):
        self.assertRaises(ValueError, lambda: Slugify(pretranslate={1, 2}))
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def test_safe_chars(self):
        slugify = Slugify()

        slugify.safe_chars = '_'
        self.assertEqual(slugify('test_sanitize'), 'test_sanitize')

        slugify.safe_chars = "'"
        self.assertEqual(slugify('????-?????'), "Kon'-Ogon'")
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def test_only_stop_words_text(self):
        slugify = Slugify(stop_words=['a', 'the'])

        self.assertEqual(slugify('The A'), 'The-A')
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def test_prevent_double_pretranslation(self):
        slugify = Slugify(pretranslate={'s': 'ss'})
        self.assertEqual(slugify('BOOST'), 'BOOSST')
项目:SuperOcto    作者:mcecchi    | 项目源码 | 文件源码
def __init__(self, basefolder, create=False):
        """
        Initializes a ``LocalFileStorage`` instance under the given ``basefolder``, creating the necessary folder
        if necessary and ``create`` is set to ``True``.

        :param string basefolder: the path to the folder under which to create the storage
        :param bool create:       ``True`` if the folder should be created if it doesn't exist yet, ``False`` otherwise
        """
        self._logger = logging.getLogger(__name__)

        self.basefolder = os.path.realpath(os.path.abspath(basefolder))
        if not os.path.exists(self.basefolder) and create:
            os.makedirs(self.basefolder)
        if not os.path.exists(self.basefolder) or not os.path.isdir(self.basefolder):
            raise StorageError("{basefolder} is not a valid directory".format(**locals()), code=StorageError.INVALID_DIRECTORY)

        import threading
        self._metadata_lock_mutex = threading.RLock()
        self._metadata_locks = dict()

        self._metadata_cache = pylru.lrucache(10)

        from slugify import Slugify
        self._slugify = Slugify()
        self._slugify.safe_chars = "-_.()[] "

        self._old_metadata = None
        self._initialize_metadata()
项目:SuperOcto    作者:mcecchi    | 项目源码 | 文件源码
def __init__(self):
        self._cached_channel_configs = None
        self._cached_channel_configs_mutex = threading.RLock()

        from slugify import Slugify
        self._slugify = Slugify()
        self._slugify.safe_chars = "-_."

    # StartupPlugin
项目:dbs-back    作者:Beit-Hatfutsot    | 项目源码 | 文件源码
def create_slug(document, collection_name):
    collection_slug_map = {
        'places': {'En': 'place',
                   'He': u'????',
                  },
        'familyNames': {'En': 'familyname',
                        'He': u'???????',
                       },
        'lexicon': {'En': 'lexicon',
                    'He': u'????',
                   },
        'photoUnits': {'En': 'image',
                       'He': u'?????',
                      },
        'photos': {'En': 'image',
                   'He': u'?????',
                  },
        # TODO: remove references to the genTreeIndividuals collection - it is irrelevant and not in use
        'genTreeIndividuals': {'En': 'person',
                               'He': u'???',
                              },
        'synonyms': {'En': 'synonym',
                     'He': u'?? ????',
                    },
        'personalities': {'En': 'luminary',
                          'He': u'??????',
                          },
        'movies': {'En': 'video',
                   'He': u'?????',
                  },
    }
    try:
        headers = document['Header'].items()
    except KeyError:
        # persons collection will be handled here as the cllection's docs don't have a Header
        # it's the calling function responsibility to add a slug
        # TODO: refactor to more specific logic, instead of relying on them not having a Header
        return

    ret = {}
    slugify = Slugify(translate=None, safe_chars='_')
    for lang, val in headers:
        if val:
            collection_slug = collection_slug_map[collection_name].get(lang)
            if collection_slug:
                slug = slugify('_'.join([collection_slug, val.lower()]))
                ret[lang] = slug.encode('utf8')
    return ret