Python List Comprehensions Help

Before you overthink it: This last version is completely overkill for your use case.

But as I wasn't sure if you need to match characters in a string as your code suggest, or words in a string as one of your post mentioned... This one can do both.

1 Like

Thanks @bkarabinchak.psi for explaining those advantages.

Thank you for apologizing @pascal.fragnoud .
Thanks for your examples.
I hope I had not upset you, and I am sorry as well if I have.

If I knew half of everything, I could be wrong in the other half.
Some things that are true today, can be false tomorrow.
I am prone to changing my mind as I often collect more information.
So I often say "I think" as a result.


Regarding this script
image

../ProductionReport/PrintableProducitonReport
../ProductionReport/CollectedData
../ProductionReport/ProductionReport

In this case, ProductionReport is both a category of pages and a page.


Regarding my style
My style is to prefer code in one spot.
I like small footprints.
I like my full script in one spot.
I don't like looking up a function I wrote in another spot.
I recognize the increase in work usually, and choose tradeoffs.


Regarding the navigation column which contains the label with the script
In this case, I limit my production report pages to 5.
I have more subsets of reports in the nav column as well.
I copy the navigation flex column page to page.
I find repeaters and embedded views to be slow.
Embedded views would still have to be copied to views.
Copying the nav column is more work as changes require copying again.


Something I was confused about in the previous code example
That last example, 'a' is in excluded.
'Bla' contains 'a'.
Printed true.


Additional notes and gaps in my understanding of Python
I do not know how to parse my path string to analyze each part of it for if it is in the excluded words.
With Jordan's help though, I was able to check the excluded words in the path.
I thought to code this way as the excludes I considered as subsets of the path.
I don't know how .split() works, and will research it.


Point and concerns
I think articulating these many points will help you help me.
I am very detail oriented, and it might be too big of a wall of text.
Sorry if so. I spent a long time trying to organize the post.
I know it is still a bit disorganized. Trying to conserve on length and still get to points.

1 Like

The script is not looking to see if "a" is in "bla", but if "a" is in the List t.

The first test ['bla',42,'b','foo',{'baz':"BAZ"}] includes all of the required items and none of the excluded items and so returns true.
The second list ['42', 'foo', 'baz'] does not include all of the required items and so it returns false.
The third list doesn't meet either requirement and so it returns false.

Split takes in a delimiter and splits the string where those delimiters are found. A space character is used by default if none is provided.

print "This is a sentance with a/in the middle".split()

return:
["This","is","a","sentance","with","a/in","the","middle"]

print "This is a sentance with a/in the middle".split('/')

return:
["This is a sentance with a","in the middle"]
2 Likes

That's a whole lot of points !

Let's take a few points where I feel like we could dig a bit deeper.

Starting with your bit of code. There's something that tingles my python senses.

result = ['nReport' in  value2 and x not in value2 for x in exludes]

This checks the same thing for every item in excludes.
nReport is a constant, and value2 is always the same. So it checks for the presence of nReport in the same string multiple times. Get this out of the loop.

result = all(x not in value2 for x in exludes) and 'nReport' in value2

If those functions are simple and well named, it shouldn't be an issue.
Take something like sum() for example. It's not defined in your script, but you don't need to go and check on it. It's simple, it does one thing, and you don't need to wonder about how it's implemented or what it does.
Build functions in that same way, and you can externalize basic and common tasks to a library. This will make your actual scripts cleaner and simpler, because you can focus on the business logic.

value2 = $something
excl = {'Enter', 'Print', 'Collect', 'Hist'}
req = {'nReport'}
string_is_valid = validate_string(value2, forbidden_words=excl, required_words=req)
return '#AAAAAA' if string_is_valid else '#FFFFFF'

Functions and libraries make life so much easier. Believe me on this.

Side note: note the use of sets ({}) instead of lists ([]). This is not a functional change, but you might like to know about sets. I really like sets.
They cannot contain duplicate, they allow for some interesting operations.
In this particular case, they're of no interest though. Actually, they're slightly worst than lists for iteration. I'm just casually introducing something I think is cool.

'a' is in 'bla', but is not in ['bla'].
Imagine it like this:

for c in 'bla':
    if c == 'a':
        return True
return False

and

for w in ['bla', 'foo', 'whatever']:
    if w == 'a':
        return True
return False
2 Likes

Thanks

result = all(x not in value2 for x in exludes) and nReport in value2

I didn't realize how to pull that out.

I forgot to mention, but using this line, you don't need to do all(result) afterward.

result = all(x not in value2 for x in exludes) and 'nReport' in value2
color = "#A" if result else "#F"
2 Likes