Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import datetime 

2 

3 

4def camel_to_underscore(name): 

5 '''Convert camel case style naming to underscore style naming 

6 

7 If there are existing underscores they will be collapsed with the 

8 to-be-added underscores. Multiple consecutive capital letters will not be 

9 split except for the last one. 

10 

11 >>> camel_to_underscore('SpamEggsAndBacon') 

12 'spam_eggs_and_bacon' 

13 >>> camel_to_underscore('Spam_and_bacon') 

14 'spam_and_bacon' 

15 >>> camel_to_underscore('Spam_And_Bacon') 

16 'spam_and_bacon' 

17 >>> camel_to_underscore('__SpamAndBacon__') 

18 '__spam_and_bacon__' 

19 >>> camel_to_underscore('__SpamANDBacon__') 

20 '__spam_and_bacon__' 

21 ''' 

22 output = [] 

23 for i, c in enumerate(name): 

24 if i > 0: 

25 pc = name[i - 1] 

26 if c.isupper() and not pc.isupper() and pc != '_': 

27 # Uppercase and the previous character isn't upper/underscore? 

28 # Add the underscore 

29 output.append('_') 

30 elif i > 3 and not c.isupper(): 

31 # Will return the last 3 letters to check if we are changing 

32 # case 

33 previous = name[i - 3:i] 

34 if previous.isalpha() and previous.isupper(): 

35 output.insert(len(output) - 1, '_') 

36 

37 output.append(c.lower()) 

38 

39 return ''.join(output) 

40 

41 

42def timesince(dt, default='just now'): 

43 ''' 

44 Returns string representing 'time since' e.g. 

45 3 days ago, 5 hours ago etc. 

46 

47 >>> now = datetime.datetime.now() 

48 >>> timesince(now) 

49 'just now' 

50 >>> timesince(now - datetime.timedelta(seconds=1)) 

51 '1 second ago' 

52 >>> timesince(now - datetime.timedelta(seconds=2)) 

53 '2 seconds ago' 

54 >>> timesince(now - datetime.timedelta(seconds=60)) 

55 '1 minute ago' 

56 >>> timesince(now - datetime.timedelta(seconds=61)) 

57 '1 minute and 1 second ago' 

58 >>> timesince(now - datetime.timedelta(seconds=62)) 

59 '1 minute and 2 seconds ago' 

60 >>> timesince(now - datetime.timedelta(seconds=120)) 

61 '2 minutes ago' 

62 >>> timesince(now - datetime.timedelta(seconds=121)) 

63 '2 minutes and 1 second ago' 

64 >>> timesince(now - datetime.timedelta(seconds=122)) 

65 '2 minutes and 2 seconds ago' 

66 >>> timesince(now - datetime.timedelta(seconds=3599)) 

67 '59 minutes and 59 seconds ago' 

68 >>> timesince(now - datetime.timedelta(seconds=3600)) 

69 '1 hour ago' 

70 >>> timesince(now - datetime.timedelta(seconds=3601)) 

71 '1 hour and 1 second ago' 

72 >>> timesince(now - datetime.timedelta(seconds=3602)) 

73 '1 hour and 2 seconds ago' 

74 >>> timesince(now - datetime.timedelta(seconds=3660)) 

75 '1 hour and 1 minute ago' 

76 >>> timesince(now - datetime.timedelta(seconds=3661)) 

77 '1 hour and 1 minute ago' 

78 >>> timesince(now - datetime.timedelta(seconds=3720)) 

79 '1 hour and 2 minutes ago' 

80 >>> timesince(now - datetime.timedelta(seconds=3721)) 

81 '1 hour and 2 minutes ago' 

82 >>> timesince(datetime.timedelta(seconds=3721)) 

83 '1 hour and 2 minutes ago' 

84 ''' 

85 if isinstance(dt, datetime.timedelta): 

86 diff = dt 

87 else: 

88 now = datetime.datetime.now() 

89 diff = abs(now - dt) 

90 

91 periods = ( 

92 (diff.days / 365, 'year', 'years'), 

93 (diff.days % 365 / 30, 'month', 'months'), 

94 (diff.days % 30 / 7, 'week', 'weeks'), 

95 (diff.days % 7, 'day', 'days'), 

96 (diff.seconds / 3600, 'hour', 'hours'), 

97 (diff.seconds % 3600 / 60, 'minute', 'minutes'), 

98 (diff.seconds % 60, 'second', 'seconds'), 

99 ) 

100 

101 output = [] 

102 for period, singular, plural in periods: 

103 if int(period): 

104 if int(period) == 1: 

105 output.append('%d %s' % (period, singular)) 

106 else: 

107 output.append('%d %s' % (period, plural)) 

108 

109 if output: 

110 return '%s ago' % ' and '.join(output[:2]) 

111 

112 return default 

113