dimka665/awesome-slugify — pull request #16 by gthole (2014-08-07 15:21:18+00:00)
Title: UniqueSlugify Improvements

Hello,
I'm using "awesome-slugify" for [my work](http://allclasses.com), and there are a few minor improvements I felt I could contribute which we were looking for on my team.

Thanks!
Greg

-------

1) UniqueSlugify should use sets for `self.uids` instead of a list, for performance reasons.

Running the following code on the base branch (with self.uids as a list) yields:
```python
>>> import time
>>> import uuid
>>> from slugify import UniqueSlugify
>>>
>>> def test_time():
... start = time.time()
... slugify = UniqueSlugify()
... for i in xrange(100000):
... _ = slugify(str(uuid.uuid4()))
... return time.time() - start
...
>>> test_time()
212.86210703849792
```

Running the same code with sets:
```python
>>> test_time()
10.824954986572266
```

2) Often, uids are stored in a database or external key/value system. It's helpful to have an option to override the uniqueness check without having to load all the uids into memory. For example, supposing I have a Django project with a `slug` field, instead of having to do:

```python
from django_blog.models import BlogPost
from slugify import UniqueSlugify

slugify = UniqueSlugify(uids=BlogPost.objects.values_list('url_slug', flat=True)
```

I can check with a lightweight existence call to the DB by overriding the check:

```python
from slugify import UniqueSlugify

def my_unique_check(text, uids):
if text in uids:
return False
return not BlogPost.objects.filter(url_slug=text).exists()

custom_slugify_unique = UniqueSlugify(unique_check=my_unique_check)
custom_slugify_unique('te occidere possunt')
```

3) Also added some documentation on running the unit tests.