0

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 
5
  • 2
    self.a = list(range(100)). Why do you need 100 separate attributes? Commented Jun 22, 2021 at 13:35
  • 2
    Does this answer your question? How do I create variable variables? Commented Jun 22, 2021 at 13:38
  • 1
    Use a dictionary to store the values using the "variable name" as the key. Commented Jun 22, 2021 at 13:40
  • @chepner About 6-9 attributes may be used. Commented Jun 22, 2021 at 14:09
  • @quamrana I have implemented the use of variable variables, but I have not found a way to use variable variables in the class. Commented Jun 22, 2021 at 14:10

2 Answers 2

1

You don't need globals; that's a function in the built-in scope for providing a dict interface to the global variables. Just use a regular dict

class MyClass: def __init__(self, bars): self.d = {} 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.d[f'k_{bar}'] = duration_seconds s1 = MyClass(['10sec', '1min', '5min', '30min', '1d']) for bar in s1.d: print(s1.d[bar]) 

I would move the logic of converting a time string to a number of seconds to a separate function, though:

def parse_time(s): if s.endswith('sec'): return int(s[:-3]) elif s.endswith('min'): return int(s[:-3]) * 60 elif s.endswith('d'): return int(s[:-1]) * 60 * 60 * 24 class MyClass: def __init__(self, bars): self.d = {f'k_{bar}': parse_time(bar) for bar in bars} 
Sign up to request clarification or add additional context in comments.

Comments

1

I wrote a very simple solution, hope that was you intention

import re bars = ['10sec', '1min', '5min', '30min', '1d'] class MyClass: def duration_seconds(self, bar): bar = re.split('(\d+)', bar) if bar[2] == 'sec': secs = int(bar[1]) elif bar[2] == 'min': secs = int(bar[1]) * 60 elif bar[2] == 'd': secs = int(bar[1]) * 60 * 60 * 24 return secs s1 = MyClass() for bar in bars: print(s1.duration_seconds('k_{0}'.format(bar))) 

1 Comment

Finally, I combined the two methods and used them together.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.