Python Slugify(python 生成 slug )
来源:互联网 发布:marryu软件怎么样 编辑:程序博客网 时间:2024/05/16 07:04
source:https://github.com/un33k/python-slugify
Overview
A Python slugify application that handles unicode.
How to install
1. easy_install python-slugify2. pip install python-slugify3. git clone http://github.com/un33k/python-slugify a. cd python-slugify b. run python setup.py4. wget https://github.com/un33k/python-slugify/zipball/master a. unzip the downloaded file b. cd into python-slugify-* directory c. run python setup.py
How to use
from slugify import slugify txt = "This is a test ---" r = slugify(txt) self.assertEqual(r, "this-is-a-test") txt = "___This is a test ---" r = slugify(txt) self.assertEqual(r, "this-is-a-test") txt = "___This is a test___" r = slugify(txt) self.assertEqual(r, "this-is-a-test") txt = "This -- is a ## test ---" r = slugify(txt) self.assertEqual(r, "this-is-a-test") txt = '影師嗎' r = slugify(txt) self.assertEqual(r, "ying-shi-ma") txt = 'C\'est déjà l\'été.' r = slugify(txt) self.assertEqual(r, "c-est-deja-l-ete") txt = 'Nín hǎo. Wǒ shì zhōng guó rén' r = slugify(txt) self.assertEqual(r, "nin-hao-wo-shi-zhong-guo-ren") txt = 'jaja---lol-méméméoo--a' r = slugify(txt) self.assertEqual(r, "jaja-lol-mememeoo-a") txt = 'Компьютер' r = slugify(txt) self.assertEqual(r, "kompiuter") txt = 'jaja---lol-méméméoo--a' r = slugify(txt, max_length=9) self.assertEqual(r, "jaja-lol") txt = 'jaja---lol-méméméoo--a' r = slugify(txt, max_length=15) self.assertEqual(r, "jaja-lol-mememe") txt = 'jaja---lol-méméméoo--a' r = slugify(txt, max_length=50) self.assertEqual(r, "jaja-lol-mememeoo-a") txt = 'jaja---lol-méméméoo--a' r = slugify(txt, max_length=15, word_boundary=True) self.assertEqual(r, "jaja-lol-a") txt = 'jaja---lol-méméméoo--a' r = slugify(txt, max_length=17, word_boundary=True) self.assertEqual(r, "jaja-lol-mememeoo") txt = 'jaja---lol-méméméoo--a' r = slugify(txt, max_length=18, word_boundary=True) self.assertEqual(r, "jaja-lol-mememeoo") txt = 'jaja---lol-méméméoo--a' r = slugify(txt, max_length=19, word_boundary=True) self.assertEqual(r, "jaja-lol-mememeoo-a") txt = 'jaja---lol-méméméoo--a' r = slugify(txt, max_length=20, word_boundary=True, separator=".") self.assertEqual(r, "jaja.lol.mememeoo.a") txt = 'jaja---lol-méméméoo--a' r = slugify(txt, max_length=20, word_boundary=True, separator="ZZZZZZ") self.assertEqual(r, "jajaZZZZZZlolZZZZZZmememeooZZZZZZa") txt = 'one two three four five' r = slugify(txt, max_length=13, word_boundary=True, save_order=True) self.assertEqual(r, "one-two-three") txt = 'one two three four five' r = slugify(txt, max_length=13, word_boundary=True, save_order=False) self.assertEqual(r, "one-two-three") txt = 'one two three four five' r = slugify(txt, max_length=12, word_boundary=True, save_order=False) self.assertEqual(r, "one-two-four") txt = 'one two three four five' r = slugify(txt, max_length=12, word_boundary=True, save_order=True) self.assertEqual(r, "one-two") txt = 'this has a stopword' r = slugify(txt, stopwords=['stopword']) self.assertEqual(r, 'this-has-a') txt = 'the quick brown fox jumps over the lazy dog' r = slugify(txt, stopwords=['the']) self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog') txt = 'Foo A FOO B foo C' r = slugify(txt, stopwords=['foo']) self.assertEqual(r, 'a-b-c') txt = 'Foo A FOO B foo C' r = slugify(txt, stopwords=['FOO']) self.assertEqual(r, 'a-b-c') txt = 'the quick brown fox jumps over the lazy dog in a hurry' r = slugify(txt, stopwords=['the', 'in', 'a', 'hurry']) self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog') txt = 'foo & bar' r = slugify(txt) self.assertEqual(r, 'foo-bar')
Running the tests
To run the tests against the current environment:
python test.py
0 0
- Python Slugify(python 生成 slug )
- 批量修改 wordpress 标题 和对应的 slug (post_title,post_name): sanitize_title($title);生成slug
- 排列生成算法(python)
- 使用 Chinese_pinyin + Friendly_id 为中文标题生成 Slug
- python笔记, python 生成注释
- Python生成黑客字典程序(一)
- 学习Python (5)生成随机密码
- Python学习笔记(四)生成字典
- Python动态生成URL(__getattr__)
- ASCII表(python生成脚本)
- python 中生成随机数(random)
- Python生成(导出)csv文件
- Python生成PDF文件(1)
- [python]自动生成版本号
- Python生成随机数
- python 随机数生成
- Python生成exe文件
- python随机数生成
- Hello My Blog
- web第三课html课堂笔记
- c#中的异步编程
- JS高级(作业域链,闭包,面向对象)
- C++走向远洋——56(项目二1、动物这样叫、虚函数)
- Python Slugify(python 生成 slug )
- poj2109Power of Cryptography
- ZZULIOJ 1432: 背包again <类似进制问题>
- 【mybatis基础】高级映射:多对多查询
- eclipse中不能找到dubbo.xsd解决方法
- 进度汇报
- 13周练J - Little Artem and Presents【规律】
- url 重写,设置伪静态页面
- 不要习惯于过去