Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can this Python script be improved? [closed]

Tags:

python

yield

Can this Python code be improved?

   def build_list(types):
        for x in types:
            for a in ['short', 'long', 'average']:
                for b in ['square', 'sloped', 'average']:
                    for c in ['small', 'large', 'average']:
                        for d in ['thin', 'thick', 'average']:
                            for e in ['high', 'low', 'average']:
                                for f in [True, False]:
                                    for g in [True, False]:
                                        for h in ['flat', 'thick', 'average']:
                                            for i in ['long', 'short', 'average']:
                                                for j in [True, False]:
                                                    for k in ['thin', 'thick', 'average']:
                                                        for l in ['thin', 'thick', 'average']:
                                                            yield [x, a, b, c, d, e, f, g, h, i, j, k, l]
    facets_list = list(build_list(xrange(1,121)))
    print len(facets_list)
like image 378
lxneng Avatar asked Jul 19 '11 02:07

lxneng


People also ask

Can a Python script modify itself?

Yes it is possible.

How do you make a Python script not close?

cmd /k is the typical way to open any console application (not only Python) with a console window that will remain after the application closes. The easiest way I can think to do that, is to press Win+R, type cmd /k and then drag&drop the script you want to the Run dialog. Fantastic answer. You should have got this.

How can I protect my Python code but still make it available to run?

The best solution to this vulnerability is to encrypt Python source code. Encrypting Python source code is a method of “Python obfuscation,” which has the purpose of storing the original source code in a form that is unreadable to humans.


1 Answers

Yes. You can use itertools.product()

import itertools
facets_list = list(itertools.product(types,
                                    ['short', 'long', 'average'],
                                    ['square', 'sloped', 'average'],
                                    ['small', 'large', 'average'],
                                     ...))
like image 140
John La Rooy Avatar answered Oct 17 '22 14:10

John La Rooy