Skip to content

Instantly share code, notes, and snippets.

@pelletier
Created July 22, 2011 13:56
Show Gist options
  • Save pelletier/1099501 to your computer and use it in GitHub Desktop.
Save pelletier/1099501 to your computer and use it in GitHub Desktop.
"""
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)')
@nautilebleu
Copy link

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.

@brunobord
Copy link

see my fork for better comparison (I may be wrong, though)

https://gist.github.com/1099506

@nautilebleu
Copy link

nautilebleu commented Jul 22, 2011 via email

@pelletier
Copy link
Author

Désolé pour la forme criptique : j'ai un faible pour les lambda :)

@nautilebleu
Copy link

nautilebleu commented Jul 22, 2011 via email

@pelletier
Copy link
Author

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