jeudi 18 juillet 2019

Optimal Way to Apply a Conditonal If statment

I have a relatively simple problem but Im trying to think about the optimal way to apply this here. I'm fairly new to python so this could be way off but I wasn't sure if this would be a good use case for lambda expression.

I'm basically defining a set of parameters below in the Start() method. Most of the params will be the same except for the request.Continuation which can have a value of either 6 or 3. My thinking was to have a list of the symbols that would get the value 6 (in self.continuation) and just say something like "If in self.continuation, value=6, else 3".

class BarsRequestSample(CELEnvironment.CELSinkBase):
    def __init__(self):

        self.symbols = ['CLES12Z', 'HOE']
        self.continuation=['ClES12Z', 'CLES6M']

    def Start(self):    
        for symbol in self.symbols:
            request.Symbol = symbol
            request.RangeStart = 0
            request.RangeEnd = -60
            request.IntradayPeriod = 5
            request.Continuation = 6
            request.UpdatesEnabled = True
            request.EqualizeCloses = True
            request.SessionsFilter = 31

What I know I could do:

def Start(self):

    for symbol in self.symbols:
        request.Symbol = symbol
        request.RangeStart = 0
        request.RangeEnd = -60
        request.IntradayPeriod = 5

        #potential attempt
        if symbol in self.continuation:
            request.Continuation = 6
        else:
            request.Continuation = 3

        request.UpdatesEnabled = True
        request.EqualizeCloses = True
        request.SessionsFilter = 31

Im just getting an intuition that there may be a more concise way to do the same thing - would appreciate any thoughts!

Aucun commentaire:

Enregistrer un commentaire