[Python-Dev] Coding guidelines for os.walk filter

2011-08-30 Thread Jacek Pliszka
Hi!

I would like to get some opinion on possible os.walk improvement.
For the sake of simplicity let's assume I would like to skip all .svn
and tmp directories.

Current solution looks like this:

for t in os.walk(somedir):
t[1][:]=set(t[1])-{'.svn','tmp'}
... do something

This is a very clever hack but... it relies on internal implementation
of os.walk

Alternative is adding os.walk parameter e.g. like this:

def walk(top, topdown=True, onerror=None, followlinks=False, walkfilter=None)

if walkfilter is not None:
dirs,nondirs=walkfilter(top,dirs,nondirs)
.
and remove .svn and tmp in the walkfilter definition.

What I do not like here is that followlinks is redundant - easily
implementable through walkfilter


Simpler but braking backward-compatibility option would be:

def walk(top, topdown=True, onerror=None, skipdirs=islink)
...
-if followlinks or not islink(new_path):
-for x in walk(new_path, topdown, onerror, followlinks):
+if not skipdirs(new_path):
+for x in walk(new_path, topdown, onerror, skipdirs):

And user given skipdirs function should return true for new_path
ending in .svn or tmp

Nothing is redundant and works fine with topdown=False!

What do you think?  Shall we:
a) do nothing and use the implicit hack
b) make the option explicit with backward compatibility but with
redundancy and topdown=False incompatibility
c) make the option explicit braking backward compatibility but no redundancy

Best Regards,

Jacek Pliszka
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Generate Dynamic lists

2011-10-20 Thread Jacek Pliszka
I believe this is not the correct forum for this as it does not
concern development of Python language itself - you should post to
comp.lang.python. However solutions is relatively simply, try this:

list1,list2,list3,list4,list5,list6 = [ list(itertools.product(i,range(0,4)))
for i in itertools.combinations(criteria_list,2) ]

Best Regards,

Jacek Pliszka

2011/10/20 Asif Jamadar :
> So I'm trying to generate dynamic choices for  django form. Here i'm usig
> formset concept (CODE is mentioned below)
>
>
>
> Suppose i have list called criteria_list = ['education', 'know how',
> 'managerial', 'interpersonal', ]
>
>
>
> now i need to generate choices as follows
>
>
>
> list1 = [('education', 1), ('education', 2), ('education', 3), (''education'
> , 4) , ('know how', 1) ('know ho', 2), ('know ho', 3), ('know ho', 4)]
>
>
>
> list2 = [('education', 1), ('education', 2), ('education', 3), (''education'
> , 4) , ('managerial', 1) ('managerial', 2), ('managerial', 3),
> ('managerial', 4)]
>
>
>
> list3 = [('education', 1), ('education', 2), ('education', 3), (''education'
> , 4) , ('interpersonal', 1) ('interpersonal', 2), ('interpersonal', 3),
> ('interpersonal', 4)]
>
>
>
> list4 = [('know how', 1), ('know how', 2), ('know how ', 3), ('know how' ,
> 4) , ('managerial', 1) ('managerial', 2), ('managerial', 3), ('managerial',
> 4)]
>
>
>
> list5 = [('know how', 1), ('know how', 2), ('know how ', 3), ('know how' ,
> 4) , ('interpersonal', 1) ('interpersonal', 2), ('interpersonal', 3),
> ('interpersonal', 4)]
>
>
>
> list6= [('managerial', 1), ('managerial', 2), ('managerial ', 3),
> ('managerial' , 4) , ('interpersonal', 1) ('interpersonal', 2),
> ('interpersonal', 3), ('interpersonal', 4)]
>
>
>
>
>
> How can i achive this in python?
>
>
>
> The above all eachh list become the  choices for each form.
>
>
>
> Suppose i have formset of 6 forms. Then how can i assign above dynamic
> generates list to the choice field of each form.
>
>
>
> I tried by using this following code but no luck
>
>
>
>
>
> view.py
>
>
>
> def evaluation(request):
>
>
>
>     evaluation_formset = formset_factory(EvaluationForm,
> formset=BaseEvaluationFormset, extra=6)
>
>
>
>     if request.POST:
>
>
>
>     formset = evaluation_formset(request.POST)
>
>
>
>     ##validation and save
>
>
>
>     else:
>
>
>
>     formset = evaluation_formset()
>
>
>
>     render_to_response(formset)
>
>
>
> forms.py
>
>
>
>
>
> class EvaluationForm(forms.Form):
>
>
>
>     value =
> forms.ChoiceField(widget=forms.RadioSelect(renderer=HorizontalRadioRenderer))
>
>
>
> class BaseEvaluationFormSet(BaseFormSet):
>
>     def __init__(self, *args, **kwargs):
>
>     super(BaseEvaluationFormSet, self).__init__(*args, **kwargs)
>
>     for form_index, form in enumerate(self.forms):
>
>
>
>     form.fields["value"].choices = self.choice_method(form_index)
>
>     def choice_method(self, form_index):
>
>     list = []
>
>     item_list = []
>
>     criteria_list = []
>
>     criteria_length = len(sub_criterias)-1
>
>     for criteria_index in range(criteria_length):
>
>     counter = 1
>
>     if criteria_index == form_index:
>
>     for j in range(criteria_length-counter):
>     x = 1
>     for i in range(6):
>  criteria_list.append((sub_criterias[criteria_index],
> sub_criterias[criteria_index]))
>
>     item_list.append((sub_criterias[criteria_index+ 1],
> sub_criterias[criteria_index+1]))
>
>     list =  criteria_list +item_list
>
>     counter = counter + 1
>     if x != criteria_length:
>
>     x = x + 1
>
>
>
>     return list
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> ___
> Python-Dev mailing list
> Python-Dev@python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/jacek.pliszka%40gmail.com
>
>
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com