Re: Merging overlapping spans/ranges

2005-05-10 Thread Mike Rovner
Max M wrote:
> I am writing a "find-free-time" function for a calendar. There are a lot 
> of time spans with start end times, some overlapping, some not.
> 
> To find the free time spans, I first need to convert the events into a 
> list of non overlapping time spans "meta-spans".
> 

"Almost" linear method (includes .sort() which is afaik N*logN):
[Set all priority to 1, or you will get changes in priorities as well.]

def merge(p):
 """ p is a seq of (start, priority, end)
 returns list of merged events:
   (start1, pri), (end1, 0), (start2, pri), (end2, 0), ...
 """
 all=[] # list of x, h, up, id
 for id,(l,h,r) in enumerate(p):
 all.append((l,h,1,id))
 all.append((r,h,0,id))
 all.sort()

 sl = [] # skyline solution
 slx = slh = 0 # current values
 vis = {} # active {id:h}
 for x,h,up,id in all:
 if up:
 vis[id]=h
 if h>slh:
 sl.append((x,h))
 slh=h
 else:
 del vis[id]
 assert h<=slh
 if h==slh:
 v = vis.values()
 if v:
 h = max(v)
 else:
 h = 0
 sl.append((x,h))
 slh=h
 slx=x
 # merge same time events
 s=dict(sl)
 sl=s.keys()
 sl.sort()
 return [(k,s[k]) for k in sl]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to track down all required shared libraries?

2005-04-30 Thread Mike Rovner
sdhyok wrote:
Recently, I installed many shared libraries to run a program written in
Python.
Now, I am in the situation to run the same program but on several
different machines with the same Linux OS. To avoid the reinstallation,
I like to pack up all shared libraries into a directory. Is there a
good way to track down all shared libraries required to run a Python
program?
Daehyok Shin
To get executable requirements use ldd.
When python can't load a lib at run-time usually ImportError is raised.
As with other errors you can never know which libs will be dynamically 
required.

Mike
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python Challenge ahead [NEW] for riddle lovers

2005-04-30 Thread Mike Rovner
Tim Peters wrote:
[Mike Rovner]
3 IS wrong because if you use any not BIG letter after bodyguard on both
sides, you get extra 'eCQQmSXK\n' which slow me down for 5 minutes.

Get rid of the newlines first.
On level 7, I'm not sure whether there's something more to do, or
whether I'm looking at a bug in how IE displays .png files.  Using
Windows is good practice in solving maddening riddles every day
.
There is! That black and white line contains the message.
Mike
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python Challenge ahead [NEW] for riddle lovers

2005-04-29 Thread Mike Rovner
David Murmann wrote:
Shane Hathaway wrote:
That was pretty fun.  Good for a Friday.  Too bad it comes to an abrupt
"temporary end".
Shane
P.S. I hope I didn't hammer your server on step 3.  I was missing the
mark. :-)

Interestingly step 3 is actually wrong... there is an additional 
solution, which looks like cqqmsxk. (I don't think that spoils the fun :))
3 IS wrong because if you use any not BIG letter after bodyguard on both 
sides, you get extra 'eCQQmSXK\n' which slow me down for 5 minutes.

Mike
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass a arg of object to python's script in a c++ program?

2005-04-26 Thread Mike Rovner
Couple notes:
- boost.python issues better discuss in comp.lang.python.c++ group;
- debug your extension from python first, then embed it
- don't call PyFinalize()
zghelp wrote:
but the exception occur when run to "print t.greet()"
How can I solve it?
Providing exception text will be helpful.
Mike
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python sleep doesn't work right in a loop?

2005-04-06 Thread Mike Rovner
[EMAIL PROTECTED] wrote:
Nope. Does't work. Running Python 2.3.4 on Debian, Linux kernel 2.6.
This is actually test code for a larger project...
# flash the selected wx.TextControl
for flasher in range(4):
self.textField.SetBackgroundColour(255, 0, 0)
  self.textField.Update()
time.sleep(0.8)
self.textField.SetBackgroundColour(255, 255, 223)
  self.textField.Update()
time.sleep(0.8)
Even when I add an explicit call to repaint the TextCtrl between each
sleep, things appear to be 'queued' until after the loop is fnished.
Very bizarre.
If you use .Refresh() request still queued.
/m
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python sleep doesn't work right in a loop?

2005-04-06 Thread Mike Rovner
[EMAIL PROTECTED] wrote:
... SHOULD toggle On and Off four times with one-second pauses. When I
run this, the loop pauses the full eight seconds then prints the Ons
and Offs all at once. What's up with that?
Run your script as:
python -u script.py
for unbuffered output.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does IronPython indicate MS interest in dynamic languages?

2005-04-05 Thread Mike Rovner
Thomas Gagne wrote:
Assuming (I don't know for certain) that MS's PR approves all messages 
that leave the building, I'm wondering if this foray into dynamic 
languages doesn't signal something greater on MS' part.  While Sun and 
Java (and C# for the most part) have remained statically-typed, do you 
think IronPython might indicate a new direction for MS language 
development?
Sun abandoned dynamic approach (Tcl) in favor of Java.
MS using dynamic for a long long time (recall prolog in NT loader).
So it's new step with .NET and prove that Python (as well as VBscript) 
will work well on it in is in best MS (read commercial) interests.

/m
--
http://mail.python.org/mailman/listinfo/python-list


Re: os.path query functions behavior incorrect?

2005-04-05 Thread Mike Rovner
Beman Dawes wrote:
So are these os.path functions specified and implemented incorrectly? Should 
they instead throw exceptions for the above examples?
Works for me. (Win XP SP2, Py 2.4, only have c and d drives)
>>> os.path.exists('d:\\')
True
>>> os.path.exists('e:\\')
False
>>> os.path.exists('a:\\')
False
>>>
--
http://mail.python.org/mailman/listinfo/python-list


Re: truncating a file from the top down

2005-03-29 Thread Mike Rovner
Right. Thanks for the correction.
Fredrik Lundh wrote:
Mike Rovner wrote:

if os.stat says the_file is too big:
 fh = open(the_file, 'rb')
 fh.seek(2008, 2)

should be
fh.seek(-2008, 2)
right?

 data = fh.read()
 fh.close()
 assert len(data)==2008 # you may want some error processing here
 fh = open(the_file, 'wb')
 fh.write(data)
 fh.close()

or
if os.path.getsize(the_file) > TOO_BIG:
fh = open(the_file, 'rb+')
fh.seek(-2008, 2)
data = fh.read()
fh.seek(0) # rewind
fh.write(data)
fh.truncate()
fh.close()
 


--
http://mail.python.org/mailman/listinfo/python-list


Re: truncating a file from the top down

2005-03-29 Thread Mike Rovner
rbt wrote:
if os.stat says the file is too big:
read the file
trim = only keep the last 2008 bytes (This is where I get stuck)
write trim back out to the original file
Would someone demonstrate the *best* most efficient way of doing this?
if os.stat says the_file is too big:
  fh = open(the_file, 'rb')
  fh.seek(2008, 2)
  data = fh.read()
  fh.close()
  assert len(data)==2008 # you may want some error processing here
  fh = open(the_file, 'wb')
  fh.write(data)
  fh.close()
/m
--
http://mail.python.org/mailman/listinfo/python-list


Re: String Splitter Brain Teaser

2005-03-27 Thread Mike Rovner
Jp Calderone wrote:
On Sun, 27 Mar 2005 14:39:06 -0800, James Stroud <[EMAIL PROTECTED]> wrote:
"ATT/GATA/G"
gets split to
[['A'], ['T'], ['T', 'G'], ['A'], ['T'], ['A', 'G']]
I have written a very ugly function to do this (listed below for the curious), 
but intuitively I think this should only take a couple of lines for one 
skilled in regex and/or listcomp. Any takers?
>>> import re
>>> s = 'ATT/GATA/G'
>>> re.findall('(./.|.)', s)
['A', 'T', 'T/G', 'A', 'T', 'A/G']
>>> 
  If it is really important to have ['A'] instead of 'A', etc, looping over the result and noticing strings of length 3 vs length 1, then applying the appropriate transformation, should be simple enough.
>>> [x.split('/') for x in ['A', 'T', 'T/G', 'A', 'T', 'A/G']]
[['A'], ['T'], ['T', 'G'], ['A'], ['T'], ['A', 'G']]
>>>
/m
--
http://mail.python.org/mailman/listinfo/python-list


Re: tree data structure

2005-03-25 Thread Mike Rovner
vivek khurana wrote:
i am a new member on this list. I have to implement 
tree data structure using python. How it can be done 
in python. Is there an existing data structure which
can be used as tree? I have searched archives and 
manuals but no luck.
You can start with Guido's essay
http://python.org/doc/essays/graphs.html
/mr
--
http://mail.python.org/mailman/listinfo/python-list


Re: multi-threaded list update

2005-03-22 Thread Mike Rovner
Peter Hansen wrote:
(But if you can accept those drawbacks, keep doing what you're doing.
Also consider searching the archives for discussions involving the
"dis" module (google for "dis.dis" maybe?) and see how to learn for
yourself what is atomic and what's not.)
Thanks, Peter. I googled groups on 'python dis.dis atomic' and it was
all on target and quite interesting.
Mike
--
http://mail.python.org/mailman/listinfo/python-list


Re: Read from the last line of a file?

2005-03-22 Thread Mike Rovner
Anthony Liu wrote:
I am wondering if it is possible to start reading from
the last line of file, and then the last but one up to
the first line.
If you can afford to keep the whole file in memory, than:
lines = open(..).readlines()
print lines[::-1]
Otherwise you can use seek() to random-access file and read chunks,
then extract lines from them and process them.
See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/120686 for 
implementation details.

Mike
--
http://mail.python.org/mailman/listinfo/python-list


multi-threaded list update

2005-03-22 Thread Mike Rovner
Hello,
Please advise on multi-threaded list *append*:
import time, random, thread
aList = []
def main():
  for i in range(10):
thread.start_new_thread(updater, (i,))
  time.sleep(30)
  print aList
def updater(n):
  global aList
  time.sleep( random.randint(1,n+1) )
  aList.append(n)
if __name__=='__main__':
  main()
I rely on GIL and believe that .append is atomic operation.
It that legal? What are the drawbacks?
Thanks,
Mike
--
http://mail.python.org/mailman/listinfo/python-list


Re: comparison puzzle? bug?

2005-03-22 Thread Mike Rovner
Charles Hixson wrote:
I hesitate to call this a bug, as at my level of expertise that seems 
... unlikely.  But I can't think of any other explanation:
Call it 'typo' ;)
 print"item[0] > lvl = %d > %d = " %(item[0], lvl), bool(item[0] == 
lvl)
use bool(item[0] > lvl)
HTH,
Mike
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Mike Rovner
Reinhold Birkenfeld wrote:
I don't quite understand that. Which dict item are you extending? Don't
you need something like
dl[key].append("word")
Rigth. It was just a typo on my part. Thanks for fixing.
Mike
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Mike Rovner
Paul Rubin wrote:
If the compiler can do some type inference, it can optimize out those
multiple calls pretty straightforwardly.
It can be tipped like that:
di = dict(int)
di.setdefault(0)
di[key] += 1
dl = dict(list)
dl.setdefault([])
dl.append("word")
dl.extend(mylist)
But the point is that if method not found in dict it delegated to 
container type specified in constructor.

It solves dict specialization without bloating dict class and is generic.
Mike
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pre-PEP: Dictionary accumulator methods

2005-03-18 Thread Mike Rovner
Ivan Van Laningham wrote:
Hi All--
Maybe I'm not getting it, but I'd think a better name for count would be
add.  As in
d.add(key)
d.add(key,-1)
d.add(key,399)
etc.
IMHO inc (for increment) is better.
d.inc(key)
add can be read as add key to d
Mike
--
http://mail.python.org/mailman/listinfo/python-list