Skip to content

Instantly share code, notes, and snippets.

@vst
Created September 5, 2012 04:51
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 vst/3630694 to your computer and use it in GitHub Desktop.
Save vst/3630694 to your computer and use it in GitHub Desktop.
Month Codes for Constructing Commodities Futures Tickers
no name abbr code
1 january jan F
2 february feb G
3 march mar H
4 april apr J
5 may may K
6 june jun M
7 july jul N
8 august aug Q
9 september sep U
10 october oct V
11 november nov X
12 december dec Z
# Copyright (c) 2012, Vehbi Sinan Tunalioglu <vst@vsthost.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# Define month codes:
F = {"no": 1, "name": "january", "abbr": "jan", "code": "F"}
G = {"no": 2, "name": "february", "abbr": "feb", "code": "G"}
H = {"no": 3, "name": "march", "abbr": "mar", "code": "H"}
J = {"no": 4, "name": "april", "abbr": "apr", "code": "J"}
K = {"no": 5, "name": "may", "abbr": "may", "code": "K"}
M = {"no": 6, "name": "june", "abbr": "jun", "code": "M"}
N = {"no": 7, "name": "july", "abbr": "jul", "code": "N"}
Q = {"no": 8, "name": "august", "abbr": "aug", "code": "Q"}
U = {"no": 9, "name": "september", "abbr": "sep", "code": "U"}
V = {"no": 10, "name": "october", "abbr": "oct", "code": "V"}
X = {"no": 11, "name": "november", "abbr": "nov", "code": "X"}
Z = {"no": 12, "name": "december", "abbr": "dec", "code": "Z"}
# Put month codes into a database:
DB = [F, G, H, J, K, M, N, Q, U, V, X, Z]
# Create key/value databases with key as number, name, abbr and code
# respectively:
DB_BY_NO = dict(zip([c["no"] for c in DB], DB))
DB_BY_NAME = dict(zip([c["name"] for c in DB], DB))
DB_BY_ABBR = dict(zip([c["abbr"] for c in DB], DB))
DB_BY_CODE = dict(zip([c["code"] for c in DB], DB))
class MonthCodeKeyError(KeyError):
"""
Indicates that there is no month code for the given key.
"""
pass
def _get_item_by_month_number(month_number):
"""
Returns the month code item for a given month number.
"""
try:
return DB_BY_NO[int(month_number)]
except ValueError:
raise MonthCodeKeyError(month_number)
def _get_item_by_month_name(month_name):
"""
Returns the month code item for a given month name.
"""
return DB_BY_NAME[month_name.strip().lower()]
def _get_item_by_month_abbr(month_abbr):
"""
Returns the month code item for a given month abbreviation.
"""
return DB_BY_ABBR[month_abbr.strip().lower()]
def _get_item_by_month_code(month_code):
"""
Returns the month code item for a given month code.
"""
return DB_BY_CODE[month_code.strip().lower()]
def get_month_code(key):
"""
Returns the month code for a given arbitrary key of month number,
month name or three letter month code.
"""
try:
return _get_item_by_month_number(key)["code"]
except KeyError:
try:
return _get_item_by_month_abbr(key)["code"]
except KeyError:
try:
return _get_item_by_month_name(key)["code"]
except KeyError:
pass
raise MonthCodeKeyError(key)
if __name__ == "__main__":
# Print out CSV file contents:
print "no,name,abbr,code"
for item in DB:
print "%(no)s,%(name)s,%(abbr)s,%(code)s" % item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment