473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

sort(cmp=func)

I have a list of objects that generate code. Some
of them depend on others being listed first, to
satisfy dependencies of others.

I wrote a cmp function something like this:

def dep_cmp(ob1, ob2):

if ob1.name in ob2.deps:
return -1
else:
return 1

I also tried returning 0 when there were no dependency
relationships between the objects.

This failed, because every object was not compared with
ever other. I imagine that this is because sort assumes
that if a b and b c, then a c. In my case, this
isn't really true. If a is not dependent on b, and
b is not dependent on c, that doesn't mean that a is not
dependent on c.

Is there a better way?

Thanks

Tobiah
** Posted from http://www.teranews.com **
Jul 10 '08 #1
4 1910
I have a list of objects that generate code. Some
of them depend on others being listed first, to
satisfy dependencies of others.

I wrote a cmp function something like this:

def dep_cmp(ob1, ob2):

if ob1.name in ob2.deps:
return -1
else:
return 1

I also tried returning 0 when there were no dependency
relationships between the objects.

This failed, because every object was not compared with
ever other. I imagine that this is because sort assumes
that if a b and b c, then a c. In my case, this
isn't really true. If a is not dependent on b, and
b is not dependent on c, that doesn't mean that a is not
dependent on c.

Is there a better way?
It's only meaningful to talk about sorting if your particular
definition of "<" is transitive. I.e. a < b and b < c should imply a <
c. If this is not satisfied, it doesn't make sense to sort according
to your "<" definition. It's just not a well-defined operation and no
trick will make it work.

Cheers,
Daniel
--
Psss, psss, put it down! - http://www.cafepress.com/putitdown
Jul 10 '08 #2
On Wed, Jul 9, 2008 at 10:58 PM, Daniel Fetchinson
<fe********@googlemail.comwrote:
>
I have a list of objects that generate code. Some
of them depend on others being listed first, to
satisfy dependencies of others.

I wrote a cmp function something like this:

def dep_cmp(ob1, ob2):

if ob1.name in ob2.deps:
return -1
else:
return 1

I also tried returning 0 when there were no dependency
relationships between the objects.

This failed, because every object was not compared with
ever other. I imagine that this is because sort assumes
that if a b and b c, then a c. In my case, this
isn't really true. If a is not dependent on b, and
b is not dependent on c, that doesn't mean that a is not
dependent on c.

Is there a better way?

It's only meaningful to talk about sorting if your particular
definition of "<" is transitive. I.e. a < b and b < c should imply a <
c. If this is not satisfied, it doesn't make sense to sort according
to your "<" definition. It's just not a well-defined operation and no
trick will make it work.
Presumably what the OP really wants is to sort using the transitive
closure of dep_cmp, which is a topological sort.

http://en.wikipedia.org/wiki/Topological_sorting has details and a
linear-time algorithm.

-- David
Jul 10 '08 #3

Tobiah wrote:
I have a list of objects that generate code. Some
of them depend on others being listed first, to
satisfy dependencies of others.

I wrote a cmp function something like this:

def dep_cmp(ob1, ob2):

if ob1.name in ob2.deps:
return -1
else:
return 1

I also tried returning 0 when there were no dependency
relationships between the objects.

This failed, because every object was not compared with
ever other. I imagine that this is because sort assumes
that if a b and b c, then a c. In my case, this
isn't really true. If a is not dependent on b, and
b is not dependent on c, that doesn't mean that a is not
dependent on c.

Is there a better way?

Thanks

Tobiah
** Posted from http://www.teranews.com **
--
http://mail.python.org/mailman/listinfo/python-list
==================================

If you are running Linux take a look at
/lib/modules/[kernel-ver]/modules.dep

In other words, to create a list like:
this module: depends on these
module-a module-f, module-g

copy ALL modules to a scratch directory
ls -1 | sort >file.lst
(or dir /b/l | sort >file.lst)
concept:
#outer loop
get a filename from file.lst
create a list of all imports in it
compare filename, in turn, to each line in file
if it finds itself loop
#inner loop
otherwise open module and create 2nd list of all imports in this file
compare each item in list one to each item in list two and output
one line for each match to imports1.lst, format:
module name needing, module name wanted (filenames not def names)

#outer loop2
get a line from dep1.lst
parse to f1=first-name, f2=second-name
create list of imports for f2
#inner loop2
check each import of f2 against name of f1
if there is a match, print Circular conflict with: f1 and f2 to
problems.lst

A sort on second filename on line combined with a visual inspection will
often help point out any 3-way (or more) circulars.
(a needs b needs c needs a) No guarantees, though.

You will have to resolve circulars yourself. Usually involves changing
code. If A needs output from B to run and B needs output from A to run
you have an obvious circular. Rewrite/fix the code.

Perhaps make a lib file of all the imports for the project. (Which is
the preferred practice.) Load it with the main logic. Thus none will be
run until all are present. Then eliminate whatever circular nonsense
still exists...

If problems.lst is empty all that needs to be done is for each import to
check the dep1.lst and first load the second filename on the line for
each first-name matching module wanted. For a given first filename not
being in a circular, manual ordering of the dep1.lst can be done if
needed. (Perhaps f1,a1:f1,b1:f1,c1 needs to be f1,c1:f1,a1:f1,b1 ?
Simply because descriptive alpha name sort winds up being wrong load
sequence.)

Just because a piece of code can be reused does not mean it is
generically practical to do so. The why of this is your current problem.

My first reaction to your original post is that you inherited something
and are setting about cleaning it up so you can work on it.
Best of luck. Succeed and you get to claim another T-shirt. :)
Steve
no******@hughes.net
Jul 10 '08 #4
On Wed, 09 Jul 2008 20:58:32 -0700, Daniel Fetchinson wrote:
>I have a list of objects that generate code. Some
of them depend on others being listed first, to
satisfy dependencies of others.

I wrote a cmp function something like this:

def dep_cmp(ob1, ob2):

if ob1.name in ob2.deps:
return -1
else:
return 1

I also tried returning 0 when there were no dependency
relationships between the objects.

This failed, because every object was not compared with
ever other. I imagine that this is because sort assumes
that if a b and b c, then a c. In my case, this
isn't really true. If a is not dependent on b, and
b is not dependent on c, that doesn't mean that a is not
dependent on c.

Is there a better way?

It's only meaningful to talk about sorting if your particular
definition of "<" is transitive. I.e. a < b and b < c should imply a <
c. If this is not satisfied, it doesn't make sense to sort according
to your "<" definition. It's just not a well-defined operation and no
trick will make it work.
There is a meaningful relationship here however. Basically, some
items must precede some other items. There are more than one
correct orders however. Here was my eventual solution. I had to
compare each item with each other item, swapping on failed
dependencies. Recursion simplified this greatly:

def dep_sort(tables):

for this_index in range(len(tables)):
this_table = tables[this_index]
for prev_index in range(this_index):
prev_table = tables[prev_index]
if this_table.name in prev_table.deps:
tables[prev_index] = this_table
tables[this_index] = prev_table
dep_sort(tables)
return

** Posted from http://www.teranews.com **
Jul 10 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jwsacksteder | last post by:
I have a need to sort a list of elements that represent sections of a document in dot-separated notation. The built in sort does the wrong thing. This seems a rather complex problem and I was...
3
by: Derek Basch | last post by:
Hello All, I need to sort a list using an unnatural sequence. I have a list like so: foo = print foo.sort()
0
by: Stefan Behnel | last post by:
Hi! I filed a patch for a Heap class to be integrated into the heapq module (in addition the the currently available functions). The main features are support for iteration and for the standard...
0
by: Cameron Laird | last post by:
QOTW: "This whole charset mess is not meant to be solved by mere mortals." - Thorsten Kampe, a day or so before solving his symptom with a codecs method:...
20
by: Steven D'Aprano | last post by:
Am I the only one who finds that I'm writing more documentation than code? I recently needed to write a function to generate a rank table from a list. That is, a list of ranks, where the rank of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.