Compare JS files and pick according to requirement Problem Statement:


Extract the filename and read if filename has jQuery on it, put them aside.
Second step will be to check one more and see if any jQuery files has ui in the filename.


Input: 

script_ref1 = """&lt;script type="text/javascript" src="js/jquery-1.9.1.min.js"/&gt;
    &lt;script type="text/javascript" src="js/jquery-migrate-1.2.1.min.js"/&gt;
    &lt;script type="text/javascript" src="js/jquery-ui.min.js"/&gt;
    &lt;script type="text/javascript" src="js/abc_bsub.js"/&gt;
    &lt;script type="text/javascript" src="js/abc_core.js"/&gt;
    &lt;script type="text/javascript" src="js/abc_explore.js"/&gt;
    &lt;script type="text/javascript" src="js/abc_qaa.js"/&gt;"""


OR

script_ref2 = """&lt;script type="text/javascript" src="js/jquery-1.9.1.min.js"/&gt;
    &lt;script type="text/javascript" src="js/jquery-migrate-1.2.1.min.js"/&gt;
    &lt;script type="text/javascript" src="js/abc_bsub.js"/&gt;
    &lt;script type="text/javascript" src="js/abc_core.js"/&gt;
    &lt;script type="text/javascript" src="js/abc_explore.js"/&gt;
    &lt;script type="text/javascript" src="js/abc_qaa.js"/&gt;"""


And:

div_attr_info = {"data-js":"jquery-1.8.1.min.js, jquery-ui-1.2.min.js, custom1.js, custom2.js"}


Output:

for script_ref1

doc_js: [' custom1.js', ' custom2.js']


for script_ref2

doc_js: [' jquery-ui-1.2.min.js', ' custom1.js', ' custom2.js']


Algorithm:


Set jquery_flag and jquery_ui_flag flag to False.
Extracting specific src attributes from script tags.
Iterate on step2 result.
Set jquery_flag to True because jquery is substring in the file list.
Check ui is substring or not. and set jquery_ui_flag according to it.
Get JS file list from the dictionary for comparison.
Iterate and Do comparison.
Print result.


Can the code be improved or optimized? Can I reduce the runtime?

jquery_flag = False
jquery_ui_flag = False
for ii in re.findall('src="js/([^"]*jquery[^"]*)"', script_ref):
    jquery_flag = True
    if "ui" in ii:
        jquery_ui_flag = True

tmp = div_attr_info["data-js"].split(",")
doc_js = []

for ii in tmp:
    if jquery_ui_flag and "jquery" in ii:
        pass
    elif jquery_flag and not jquery_ui_flag and "jquery" in ii and "ui" not in ii:
        pass
    else:
        doc_js.append(ii)

print "doc_js:", doc_js
Since your post is a little unclear to me, I'm just going to point out a few small things that could be improved quiet a bit.


The program logic could be separated out into different functions. This allows for easy re-usability.
You should flesh out this program with comments for clarity. There are two types of comments, regular comments, and docstrings. Regular comments start with a # character, followed by a description. Docstrings describe functions and classes, like so.




def my_function(args):
    """
    This is a docstring. Put a description
    of your function here.
    """
    ...


class MyClass(object):
    """
    Put a description of your class here.
    """
    ...





You have some bad variable names, like ii, or tmp. Variable names should generally describe what the variable's purpose is.


There's not much for me to review, but if you want me to cover anything else, just mention it in the comments, and I'll see if I can cover it.