This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
helpers.py
60 lines (50 loc) · 1.51 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Utilities for interpreting data that arrives in impractical formats.
This module stores helper functions that transform data for the controllers.
"""
class NotFoundError(Exception):
"""
Helper exception for when we should probably be returning 404s.
"""
def __init__(self, id):
"""Sets the exception message.
Arguments:
- id: The requested ID of the entity that couldn't be found.
"""
self.message = f"Entity could not be found with id {id}"
def doi_to_id(doi, connection):
"""If a request comes in for an author of an ID that indicates
it's part of the old numbering scheme, this will check to see if
we have an updated number to redirect to."""
print(f"LOOKIN FOR {doi}")
result = connection.read("SELECT id FROM articles WHERE doi=%s", (doi,))
if len(result) == 0:
return False
if len(result[0]) == 0:
return False
return result[0][0]
def num_to_month(monthnum):
"""Converts a (1-indexed) numerical representation of a month
of the year into a three-character string for printing. If
the number is not recognized, it returns an empty string.
Arguments:
- monthnum: The numerical representation of a month
Returns:
- The three-character abbreviation of the specified month
"""
months = {
1: "Jan",
2: "Feb",
3: "Mar",
4: "Apr",
5: "May",
6: "Jun",
7: "Jul",
8: "Aug",
9: "Sep",
10: "Oct",
11: "Nov",
12: "Dec"
}
if monthnum is None or monthnum < 1 or monthnum > 12:
return ""
return months[monthnum]