Created
July 22, 2011 13:56
-
-
Save pelletier/1099501 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
10268104 paths generated | |
4 function calls in 2.919 seconds | |
Ordered by: standard name | |
ncalls tottime percall cumtime percall filename:lineno(function) | |
1 0.135 0.135 2.919 2.919 <string>:1(<module>) | |
1 0.000 0.000 2.784 2.784 test.py:12(sort_key) | |
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} | |
1 2.784 2.784 2.784 2.784 {sorted} | |
10268107 function calls in 4.471 seconds | |
Ordered by: standard name | |
ncalls tottime percall cumtime percall filename:lineno(function) | |
1 0.132 0.132 4.471 4.471 <string>:1(<module>) | |
1 0.000 0.000 4.338 4.338 test.py:1(sort_orig) | |
10268103 2.313 0.000 2.313 0.000 test.py:9(<lambda>) | |
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} | |
1 2.026 2.026 4.338 4.338 {sorted} | |
""" | |
def sort_orig(in_list): | |
""" | |
>>> sort_orig([('bar', '...'), ('bar/baz/bob', '...'), ('bar/baz', '...'), ('foo', '...'), ('foo/bar', '...'), ('a', '...'),]) | |
[('foo/bar', '...'), ('foo', '...'), ('bar/baz/bob', '...'), ('bar/baz', '...'), ('bar', '...'), ('a', '...')] | |
>>> sort_orig([('bar', '...'), ('bar/baz', '...') , ('bar/baz/bob', '...'), ('foo', '...'), ('foo/bar', '...'), ('a', '...'),]) | |
[('foo/bar', '...'), ('foo', '...'), ('bar/baz/bob', '...'), ('bar/baz', '...'), ('bar', '...'), ('a', '...')] | |
""" | |
return sorted(in_list, cmp=lambda a,b: 1 if a[0] <= b[0] else -1) | |
from operator import itemgetter | |
def sort_key(in_list): | |
""" | |
>>> sort_key([('bar', '...'), ('bar/baz/bob', '...'), ('bar/baz', '...'), ('foo', '...'), ('foo/bar', '...'), ('a', '...'),]) | |
[('foo/bar', '...'), ('foo', '...'), ('bar/baz/bob', '...'), ('bar/baz', '...'), ('bar', '...'), ('a', '...')] | |
>>> sort_key([('bar', '...'), ('bar/baz', '...') , ('bar/baz/bob', '...'), ('foo', '...'), ('foo/bar', '...'), ('a', '...'),]) | |
[('foo/bar', '...'), ('foo', '...'), ('bar/baz/bob', '...'), ('bar/baz', '...'), ('bar', '...'), ('a', '...')] | |
""" | |
return sorted(in_list, key=itemgetter(0), reverse=True) | |
def generate_fixtures(size): | |
from random import randint | |
from math import floor | |
from os.path import join # assume we are on unix | |
names = ['foo', 'bar', 'baz','xxx', 'a', 'b', 'c'] | |
def a_name(): | |
return names[randint(0, len(names)-1)] | |
def generate_level(base_path, depth, size): | |
acc = [] | |
for i in range(int(floor(size/(depth+1)^2))): | |
name = a_name() | |
child = join(base_path, name) | |
acc.append(child) | |
acc.extend(generate_level(child, depth+1, size)) | |
return acc | |
return generate_level('/', 0, size) | |
fixtures = generate_fixtures(30) | |
print "%s paths generated" % len(fixtures) | |
import cProfile | |
cProfile.run('sort_key(fixtures)') | |
cProfile.run('sort_orig(fixtures)') | |
see my fork for better comparison (I may be wrong, though)
Sympa, mais en fait, j'ai copié un peu vite mon code a[0] ne test pas
la première lettre, à la base je passe un tuple… My bad
A vrai dire @kizlum nous met tous les 2 d'accord !
https://gist.github.com/1099501
A ces jeunes !
Le vendredi 22 juillet 2011, brunobord
[email protected]
a écrit :
see my fork for better comparison (I may be wrong, though)
https://gist.github.com/1099506
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1099501
##
…---
Twitter : http://twitter.com/nautilebleu/
Skype : nautilebleu
Web : http://nautilebleu.tumblr.com/
Désolé pour la forme criptique : j'ai un faible pour les lambda :)
Nope, à vrai dire je suis assez scié par la simplicité et l'élégance
de ta solution !
Le vendredi 22 juillet 2011, pelletier
[email protected]
a écrit :
Désolé pour la forme criptique : j'ai un faible pour les lambda :)
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1099501
##
…---
Twitter : http://twitter.com/nautilebleu/
Skype : nautilebleu
Web : http://nautilebleu.tumblr.com/
Ah d'ailleurs, si a
et b
sont des tuples, ca change un peu. Cf https://gist.github.com/1099501/0e675e9c708b924079556c313d0ed574e6b7350f
(Edit: note que ca marche a priori pour n'importe quelle profondeur de chemin, et dans n'importe quel ordre)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OK I understand: I get the list from a remote server as ['bar', 'bar/baz', 'bar/baz/bob', 'foo', 'foo/bar'] and not ['bar', 'bar/baz/bob', 'bar/baz', 'foo', 'foo/bar'] so this limitation is not a problem for me.