I have a class named MyClass. I want to generate the attributes k_10sec to k_1d of the instance in batches when generating instances of the class.
How do I modify the code?
bars = ['10sec', '1min', '5min', '30min', '1d']
class MyClass:
    def __init__(self):
        for bar in bars:
            if bar.endswith('sec'):
                duration_seconds = int(bar[:-3])
            elif bar.endswith('min'):
                duration_seconds = int(bar[:-3] * 60)
            elif bar.endswith('d'):
                duration_seconds = int(bar[:-1] * 60 * 60 * 24)
            self.globals()[f'k_{bar}'] = duration_seconds
s1 = MyClass()
for bar in bars:
    print(s1.[f'k_{bar}'])
My expected result:
10
60
300
1800
86400
Finally, I combined the two methods and used them together.
import re
def parse_time(bar='', secs=0):
    if bar != '':
        bar = re.split('(\d+)', bar)
        if bar[2] in ('sec', 's'):
            secs = int(bar[1])
        elif bar[2] == 'min':
            secs = int(bar[1]) * 60
        elif bar[2] in ('hour', 'h'):
            secs = int(bar[1]) * 60 * 60
        elif bar[2] in ('d', 'day', 'D'):
            secs = int(bar[1]) * 60 * 60 * 24
        else:
            pass
        return secs
    elif secs != 0:
        if secs % (60 * 60 * 24) == 0:
            bar = str(secs // (60 * 60 * 24)) + 'day'
        elif secs % (60 * 60) == 0:
            bar = str(secs // (60 * 60)) + 'hour'
        elif secs % 60 == 0:
            bar = str(secs // 60) + 'min'
        else:
            bar = str(secs) + 'sec'
        return bar
    else:
        pass
class MyClass:
    def __init__(self, bars):
        self.d = {f'k_{bar}': parse_time(bar=bar) for bar in bars}
if __name__ == '__main__':
    bars = ['10sec', '1min', '5min', '30min', '1d']
    s1 = MyClass(bars)
    pass
 
     
    