Skip to content

Instantly share code, notes, and snippets.

@cyberbikepunk
Last active September 19, 2015 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberbikepunk/0d2c86e7297ae3e0a42d to your computer and use it in GitHub Desktop.
Save cyberbikepunk/0d2c86e7297ae3e0a42d to your computer and use it in GitHub Desktop.
Random string processors
from string import ascii_letters, digits
from re import match, sub
from time import strptime
from unicodedata import normalize
from datetime import datetime
SLUG_SEPERATOR = '-'
_multiple_seperators_regex = '%s{2,}' % SLUG_SEPERATOR # eg. bla-bla--bla
_edge_seperators_regex = r'^-|-$' # eg. -bla-bla-bla-
_author_regex = r'(?P<author>^.+?)(?= on)'
_date_regex = r'(?:.*)(?P<date>\d{2}\.\d{2}\.\d{4})' # eg. 12.03.1978
_price_regex = r'(?P<price>(\d+\.)*\d+,\d{2})' # eg. 87,43 or 1.986,98
_price_regex = r'(?P<price>(\d+\.)*\d+,\d{2}*)' # eg. 87,43 or 1.986,98
_id_regex = r'(\d)' # eg. 9835483
def parse_stock(input_string):
return 'stock' in input_string.lower()
def parse_rating(rating_string):
return rating_string[0]
def parse_author(author_and_date):
return match(_author_regex, author_and_date).group('author')
def parse_date(author_and_date):
return datetime(*strptime(match(_date_regex, author_and_date).group('date'), '%d.%m.%Y')[0:6])
def trim_edges(text):
return sub(_edge_seperators_regex, '', text)
def strip_edges(text):
return text.strip()
def squeeze_seperators(input_string):
return sub(_multiple_seperators_regex, SLUG_SEPERATOR, input_string)
def slugify(text):
return ''.join([c if c in ascii_letters + digits else SLUG_SEPERATOR for c in text])
def asciify(text):
return text if isinstance(text, str) else normalize('NFKD', text).encode('ASCII', 'ignore')
def force_lower(text):
return text.lower()
def parse_price(raw_price):
return match(_price_regex, raw_price).group('price').replace('.', '').replace(',', '.')
def parse_id(raw_id):
a = match(_id_regex, raw_id)
if a:
return a.group(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment