Club 15CC

Python ~ 15CC Code

Task Code
Python Pythonic Way to Check For Any Overlapping Values in Two Lists »
main_list = [1, 2, 3]
test_a = [1, 8, 9]
test_b = [7, 8, 9]

any(item in main_list for item in test_a)  # True, number 1 matches
any(item in main_list for item in test_b)  # False, no matches
# The first "in" is a boolean conditional.  
# The second "in" creates a generator
Python Boilerplate Code for a Python Command Line Utility »
#!/usr/bin/python
import os.path
import sys
from optparse import OptionParser  # <= 2.6. Use argparse for >= 2.7

def main(args=None):
    # Allows calling main directly with args passed as a list.
    if args is None:
        args = sys.argv

    parser = OptionParser()
    parser.add_option("-f", "--file", dest="filename",
                      help="write report to FILE", metavar="FILE")
    parser.add_option("-q", "--quiet",
                      action="store_false", dest="verbose", default=True,
                      help="don't print status messages to stdout")

    (options, args) = parser.parse_args(args)

    # return > 0 for errors

if __name__ == "__main__":
    sys.exit(main())
Python Accessing Nested Modules in Python »
# To allow code such as 
import fifteencc.utils
utils.files.do_something()

# ...when you have a module structure...
# fifteencc/
#     utils/
#          __init__.py
#          files.py  (contains do_something())

# Import in your __init__.py...
import files
Python Swap Dictionary Keys/Values in Python »
# Method 1
dict((value, key) for key, value in my_dict.iteritems())

# Method 2 (less efficient)
new_dict = dict (zip(my_dict.values(),my_dict.keys()))
Python Recurse up from the current working directory until a file is found »
newpath = os.getcwd()
path = ''
while "fabfile.py" not in os.listdir(newpath) and path != newpath:
    path = newpath
    print path
    os.chdir('../')
    newpath = os.getcwd()
		
if path == newpath:
    exit("WARNING: NO FAB FILE FOUND")
Python Python Equivalent of PHP’s Magic Constant __FILE__ » See the important note on Stack Overflow....
os.path.dirname(os.path.abspath(__file__))