Can I Replace Part of a Python "If" Statement with a Variablized String Literal? -
i have long question may short answer.
i'm new python (almost 2 weeks, now) i've used vbscript many years understand many of basic concepts.
i've searched stack overflow , internet solution haven't been able find anything; i'm not sure if possible in python, i'd bit surprised if it's not. i've written file-search program using python3 allows users search computer files. user can choose search based on several different parameters: name, size range, date-modified range and, non-linux systems, date-created range. search functionaly works quite each individual parameter combinations of parameters (which, way, in no small part many answers / discussions i've found here on stack overflow). problem actual search rather inelegant and, believe, slower be. program uses flags (not true python flags, that's happen call them) set search options. let me illustrate pseudo code:
# variables values user entry sname = "test" # string search sminsize = 2 # minimum search-by size in mb smaxsize = 15 # maximum search-by size in mb smodded1 = 2008-01-23 # earliest modified-by date smodded2 = 2017-08-22 # latest modified-by date screated1 = 2008-01-23 # earliest created-by date screated2 = 2017-08-22 # latest created-by date # search parameters - choosing 1 of these changes value 0 1: flagname = 0 # search name flagsize = 0 # search size flagmodified = 0 # search last modified date flagcreated = 0 # search last created date root, dirs, files in os.walk(strpath, followlinks=false): fname in files: filedate = os.path.getmtime(fname) filesize = os.stat(fname).st_size if flagname = 1: if fname.find(sname) > 0: do_stuff elif flagsize = 1: if sminsize < filesize < smaxsize: do_stuff elif flagname = 1 , flagsize = 1: if fname.find(sname) > 0 , if sminsize < filesize < smaxsize: do_stuff ... etc
that's 3 possible combinations - there 14 total. while don't have problem typing combinations out, believe severely impact speed , efficiency of search.
i've thought of solution bit more elegant , execute faster, still think there's better method:
if flagname = 1: root, dirs, files in os.walk(strpath, followlinks=false): fname in files: filedate = os.path.getmtime(fname) filesize = os.stat(fname).st_size if fname.find(sname) > 0: do_stuff elif flagname = 1 , flagsize = 1: root, dirs, files in os.walk(strpath, followlinks=false): fname in files: filedate = os.path.getmtime(fname) filesize = os.stat(fname).st_size if fname.find(sname) > 0 , if sminsize < filesize < smaxsize: do_stuff ... etc
again, bit more elegant , (i believe) great deal more efficient, still not ideal. i'd create 1 "if" statement based on user's search criteria , use conduct search (note similar possible in vbscript). these statements go before search statements take place:
possible option 1:
if flagname = 1: iclause = "fname.find(sname) > 0" elif flagname = 1 , flagsize = 1: iclause = "fname.find(sname) > 0 , if sminsize < filesize < smaxsize" ... etc
possible option 2:
flagclause = 0 if flagname = 1: iclause = "fname.find(sname) > 0" flagclause = flagclause + 1 if flagclause = 0 iclause = "sminsize < filesize < smaxsize" else: iclause = iclause + "and sminsize < filesize < smaxsize" flagclause = flagclause + 1 ... etc
and plug "iclause" in search statement so:
root, dirs, files in os.walk(strpath, followlinks=false): fname in files: filedate = os.path.getmtime(fname) filesize = os.stat(fname).st_size if **iclause**: do_stuff
this streamline code, making easier read , maintain , (i believe) make more efficient , speedy.
is possible python?
edit:
i thank of taking time read lengthy question, don't believe got asking - due (over)verbosity.
i know how implement following:
= "sminsize < filesize < smaxsize" b = "and sminsize < filesize < smaxsize" iclause = a+b
then plug 'iclause' "if" statement follows:
if iclause: do_stuff
this turning string literal variable, using variablized (probably not real word) string literal statement. hope clearer.
create predicate function, 1 each case. determine cases you're using , use associated predicate. collect chosen predicates in list (or compose them new predicate) apply in loop:
predicates = [] if flagname: predicates.append(lambda filename: filename.find(sname) > 0) if flagsize: predicates.append(lambda filename: sminsize < os.stat(filename).st_size < smaxsize) if flagmodified: predicates.append(lambda filename: smodded1 < os.path.getmtime(filename) < smodded2) if flagcreated: predicates.append(lambda filename: screated1 < os.path.getctime(filename) < screated2) root, dirs, files in os.walk(strpath, followlinks=false): fname in files: if all(p(fname) p in predicates): # stuff
you may want use named function instead of lambda depending on preferences. , more complex scenarios, may want implement these functors instead.
wiki
Comments
Post a Comment