Last active
December 31, 2015 19:49
-
-
Save shazow/8036248 to your computer and use it in GitHub Desktop.
Human helpers (unstdlib.py candidates)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from unstdlib import html | |
_Default = object() | |
RE_HUMAN_URL = re.compile('^(\w*://)?(www\.)?(.+)/?$') | |
def human_url(s, max_length=None): | |
if not s: | |
return '' | |
m = RE_HUMAN_URL.match(s) | |
r = m.group(3) if m else s | |
return truncate(r, max_length=max_length) | |
def human_link(href, label=None, attrs=None, max_length=None): | |
if href.startswith('/'): | |
return href | |
elif '://' not in href: | |
if '.' in href: | |
href = 'http://' + href | |
else: | |
return href | |
if not label: | |
label = human_url(href, max_length=max_length) | |
return html.tag('a', label, attrs={'href': href}) | |
def num_ordinal(n): # lol | |
return 'th' if 11 <= n <=13 else {1:'st', 2:'nd', 3:'rd'}.get(n % 10, 'th') | |
def human_date(d, max_unit='month'): | |
if not d: | |
return | |
f = '{:%A %b {day}}' | |
if max_unit == 'year': | |
f = '{:%A, %b {day} %Y}' | |
return f.format(d, day=str(d.day) + num_ordinal(d.day)) | |
def human_time(seconds=None): | |
if not seconds: | |
return '0s' | |
r = [] | |
rem = 0 | |
for div, unit in [(3600.0, 'hr'), (60.0, 'min'), (1.0, 'sec')]: | |
rem = seconds % div | |
v = (seconds - rem) / div | |
seconds = rem | |
if not v: | |
continue | |
r.append('%d%s' % (v, unit)) | |
return ' '.join(r) | |
def human_int(n): | |
return u'{:,}'.format(int(n)) | |
def human_percent(f): | |
fmt = u'{:0.1%}' | |
if f < 0.1: | |
fmt = u'{:0.2%}' | |
return fmt.format(float(f)) | |
def human_delta(f): | |
fmt = u'{:+0.1%}' | |
if f < 0.1: | |
fmt = u'{:+0.2%}' | |
return fmt.format(float(f)) | |
def format_int(n, singular=_Default, plural=_Default): | |
""" | |
Return `singular.format(n)` if n is 1, or `plural.format(n)` otherwise. If | |
plural is not specified, then it is assumed to be same as singular but | |
suffixed with an 's'. | |
:param n: | |
Integer which determines pluralness. | |
:param singular: | |
String with a format() placeholder for n. (Default: `u"{:,}"`) | |
:param plural: | |
String with a format() placeholder for n. (Default: If singular is not | |
default, then it's `singular + u"s"`. Otherwise it's same as singular.) | |
Example: :: | |
>>> format_int(1000) | |
u'1,000' | |
>>> format_int(1, u"{} day") | |
u'1 day' | |
>>> format_int(2, u"{} day") | |
u'2 days' | |
>>> format_int(2, u"{} box", u"{} boxen") | |
u'2 boxen' | |
>>> format_int(20000, u"{:,} box", u"{:,} boxen") | |
u'20,000 boxen' | |
""" | |
n = int(n) | |
if singular in (None, _Default): | |
if plural is _Default: | |
plural = None | |
singular = u'{:,}' | |
elif plural is _Default: | |
plural = singular + u's' | |
if n == 1 or not plural: | |
return singular.format(n) | |
return plural.format(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment