# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import datetime


def FormatDate(Date,Format):
    """
    @Input:
    Date: date object eg. datetime.date(2002, 3, 4)
    Format: string - each individual date parts must be defined within DateSwitch object 
    and must its length must be between 2-4 letters and starts with a capital letter
    
    @Note:
    takes a date object and format it according to specification.
    """
    import datetime
    if not Format:
        return ""
    else:
        slen = len(Format)
        LongMonth=["January" , "February" , "March" , "April", "May",
            "June", "July", "August", "September", "October",
            "November", "December"]
        
        ShortMonth=["Jan" , "Feb" , "Mar" , "Apr", "May",
        "Jun", "Jul", "Aug", "Sep", "Oct",
        "Nov", "Dec"]
        
        End = {
            '1':"st",
            '2':"nd",
            '3':"rd",
        }
        
        def HandleSuffix(day):
            temp =  str(day)
            if str(Date.day) in End:
                temp = temp+End[str(Date.day)]
            else:
                temp = temp+"th"
            return temp
                
        
        DateSwitch = {
            "MM": str(Date.month),
            "MMM": LongMonth[Date.month-1],
            "Mmm": ShortMonth[Date.month-1],
            "DD": str(Date.day) if len(str(Date.day)) == 2 else "0"+str(Date.day),
            "Dd": str(Date.day),
            #"DDvb": str(Date.day) if str(Date.day)[-1] not in End else str(Date.day)+End[str(Date.day)[-1]],
            "DDvb": HandleSuffix(Date.day),
            "YYYY": str(Date.year),
            "YY": str(Date.year)[-2:]
        }
        
        for i in range(slen):
            for j in range(4,1,-1):
                if Format[i:j] in DateSwitch:
                    swap = DateSwitch[Format[i:j]]
                    return swap + FormatDate(Date,Format[i+j:])
            return Format[i] + FormatDate(Date,Format[1:])
# -

d = datetime.datetime(2002,2,2)
FormatDate(d,"DDvb-MMM-YYYY")
keys = ['MM', 'MMM', 'Mmm', 'DD', 'Dd', 'DDvb', 'YYYY', 'YY']


def SplitFormat(Date,Format,*arg,**kw):
    import collections
    def GetSeparator(Format):
        keys = ['MM', 'MMM', 'Mmm', 'DD', 'Dd', 'DDvb', 'YYYY', 'YY']
        if not Format:
            return ""
        else:
            slen=len(Format)
            for i in range(0,slen):
                for j in range(4,1,-1):
                    if Format[i:j] in keys:
                        return "@" + GetSeparator(Format[i+j:])
                return Format[i] + GetSeparator(Format[1:])
    
    deq=collections.deque([i for i in GetSeparator(Format).split("@") if i])
    # Provide logic here to map date parts to desired format
    # Deque to maintain FIFO
    deq_cp=deq.copy()
    while deq_cp:
        a = deq_cp.popleft()
        print(a)
    return deq


SplitFormat(232,'DDvb1-1MMM2-2YYYY3-3DD4-4YY')









StarcoderdataJupyterScriptsDedupFiltered