Restarting a daemon

2011-04-26 Thread Jeffrey Barish
Not exactly a Python question, but I thought I would start here.

I have a server that runs as a daemon.  I can restart the server manually 
with the command 

myserver restart

This command starts a new myserver which first looks up the pid for the one 
that is running and sends it a terminate signal.  The new one then 
daemonizes itself.

I want the server to be able to restart itself.  Will it work to have 
myserver issue myserver restart using os.system?  I fear that the new 
myserver, which will be running in a subshell, will terminate the subshell 
along with the old myserver when it sends the terminate signal to the old 
myserver.  If so, what is the correct way to restart the daemon?  Will it 
work to run the restart command in a subprocess rather than a subshell or 
will a subprocess also terminate when its parent terminates?
-- 
Jeffrey Barish

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


Re: Problem with multithreading

2009-06-25 Thread Jeffrey Barish
Lou Pecora wrote:

 In article h20d7k$nih$0...@news.t-online.com,
  larudwer larud...@freenet.de wrote:
 
 Jeffrey Barish jeff_bar...@earthlink.net schrieb im Newsbeitrag
 news:mailman.2091.1245902997.8015.python-l...@python.org...
  Jeffrey Barish wrote:
 
  I have a program that uses multithreading to monitor two loops.  When
  something happens in loop1, it sends a message to loop2 to have it
  execute
  a command.  loop2 might have to return a result.  If it does, it puts
  the
  result in a queue.  loop1, meanwhile, would have blocked waiting for
  something to appear in the queue.  The program works for a while, but
  eventually freezes.  I know that freezing is a sign of deadlock.
  However,
  I put in print statements to localize the problem and discovered
  something
  weird.  The freeze always occurs at a point in the code with the
  following
  statements:
 
  print about to try
  try:
  print in try
  do something
 
  I get about to try, but not in try.  Is this observation
  consistent
 
 Try putting a flush in after the 2nd print statement in case the output
 is left in some I/O buffer when the thing terminates.  e.g.
 
 import sys
 
 
 
 try:
print 'in try
sys.stdout.flush()
do something
 

I was hoping for some suggestions of things to think about, so thanks
especially to those who had such suggestions.  Believe it or not (and I'm
having trouble believing it myself), I didn't think to use flush.  When I
did, I found that, indeed, the program did progress past the try statement. 
It made it to a call to GStreamer (playbin2), which has been proving itself
intractable in my experience.  Note that my test program (which works)
excised GStreamer.  The next step will be to try again to compile the
latest version of PyGST as the version in Ubuntu 9.04 is one generation
old.  The last time I tried, the compile failed.  This is the first time in
days that I have had any hope.
-- 
Jeffrey Barish

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


Problem with multithreading

2009-06-24 Thread Jeffrey Barish
I have a program that uses multithreading to monitor two loops.  When
something happens in loop1, it sends a message to loop2 to have it execute
a command.  loop2 might have to return a result.  If it does, it puts the
result in a queue.  loop1, meanwhile, would have blocked waiting for
something to appear in the queue.  The program works for a while, but
eventually freezes.  I know that freezing is a sign of deadlock.  However,
I put in print statements to localize the problem and discovered something
weird.  The freeze always occurs at a point in the code with the following
statements:

print about to try
try:
print in try
do something

I get about to try, but not in try.  Is this observation consistent with
the deadlock theory?  If not, what could be making the program freeze at
the try statement?  I wrote a test program using the same techniques to
illustrate the problem, but the test program works perfectly.  I could post
it, though, if it would help to understand what I am doing -- and what
might be wrong in the real program.
-- 
Jeffrey Barish

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


Re: Problem with multithreading

2009-06-24 Thread Jeffrey Barish
Jeffrey Barish wrote:

 I have a program that uses multithreading to monitor two loops.  When
 something happens in loop1, it sends a message to loop2 to have it execute
 a command.  loop2 might have to return a result.  If it does, it puts the
 result in a queue.  loop1, meanwhile, would have blocked waiting for
 something to appear in the queue.  The program works for a while, but
 eventually freezes.  I know that freezing is a sign of deadlock.  However,
 I put in print statements to localize the problem and discovered something
 weird.  The freeze always occurs at a point in the code with the following
 statements:
 
 print about to try
 try:
 print in try
 do something
 
 I get about to try, but not in try.  Is this observation consistent
 with
 the deadlock theory?  If not, what could be making the program freeze at
 the try statement?  I wrote a test program using the same techniques to
 illustrate the problem, but the test program works perfectly.  I could
 post it, though, if it would help to understand what I am doing -- and
 what might be wrong in the real program.

As I ponder this problem, I am beginning to believe that the problem is not
related to multithreading.  If the problem were due to a collision between
the two threads then timing would matter, yet I find that the program
always freezes at exactly the same statement (which executes perfectly
hundreds of times before the freeze).  Moreover, the test program that I
wrote to test the multithreading implementation works perfectly. And
finally, there is nothing going on related to multithreading at this point
in the code.  Why else might the program freeze at a try statement?
-- 
Jeffrey Barish

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


Threads and daemon processes

2009-05-08 Thread Jeffrey Barish
I have a program that uses threading.Timer to execute a function after a
delay.  It works fine when I run the program normally, but when I run the
program as a daemon process, I can't find any evidence that the function
ever runs.  Is it the case that daemon processes are not allowed to create
a thread?
-- 
Jeffrey Barish

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


Re: Beep

2008-12-22 Thread Jeffrey Barish
Tobias Andersson wrote:

 Jeffrey Barish skrev:
 Chris Rebert wrote:
 Is the 'pcspkr' kernel module built and loaded?
 
 Yes.  And I should have mentioned that I get sound from Ubuntu
 applications that produce sound.
 
 Also, is the terminal bell set to visual? If so chr(7) only
 produces a brief flash (or similar).

On Ubuntu, it is possible to set visual and audible beeps separately.  When
I set both, I get the visual beep, but not the audible one.  It's not a
Python issue -- so I should take this thread to Ubuntu -- because I observe
this behavior even when I hit backspace at the beginning of a line in a
terminal.
-- 
Jeffrey Barish

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


Beep

2008-12-21 Thread Jeffrey Barish
I use sys.stdout.write('\a') to beep.  It works fine on Kubuntu, but not on
two other platforms (one of which is Ubuntu).  I presume that the problem
is due to a system configuration issue.  Can someone point me in the right
direction?  Thanks.
-- 
Jeffrey Barish

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


Re: Beep

2008-12-21 Thread Jeffrey Barish
Chris Rebert wrote:
 
 Is the 'pcspkr' kernel module built and loaded?

Yes.  And I should have mentioned that I get sound from Ubuntu applications
that produce sound.
-- 
Jeffrey Barish

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


Multiprocessing vs. [Pyro, RPyC]

2008-11-14 Thread Jeffrey Barish
With the release of multiprocessing in Python 2.6, is there any reason to
use Pyro or RPyC?
-- 
Jeffrey Barish

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


Re: Multiprocessing vs. [Pyro, RPyC]

2008-11-14 Thread Jeffrey Barish
[EMAIL PROTECTED] wrote:

 
 Jeffrey With the release of multiprocessing in Python 2.6, is there
 any Jeffrey reason to use Pyro or RPyC?
 
 As far as I know the multiprocessing module only works on one machine
 (multi-cpu or multi-core), not across machines.

So I thought at first, but then I saw this statement in the documentation:

It is possible to run a manager server on one machine and have clients use
it from other machines (assuming that the firewalls involved allow it).
-- 
Jeffrey Barish

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


Single-instance daemons

2008-11-12 Thread Jeffrey Barish
As per Stevens/Rago, file and record locking provides a convenient
mutual-exclusion mechanism.  They note the convention of putting the lock
file in /var/run in a file called name.pid, where name is the name of
the daemon and content is the pid.  Seems like a good suggestion as I see
pid files from many other daemons there.  However, /var/run is owned by
root, so it is not possible to write in it without root permission.  I
could put the pid file in /tmp, but doing so would make it harder to find. 
I could write a C program to write the lock file that takes command-line
arguments for passing the name of the daemon and the pid and give the
executable suid root, but that's a lot of bother.  Has anyone else dealt
with this problem?
-- 
Jeffrey Barish

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


Re: Single-instance daemons

2008-11-12 Thread Jeffrey Barish
Jeff McNeil wrote:

 Sure, start the daemon as root, write the appropriate files, and then
 drop permissions using os.setegid and then os.seteuid. You can chown
 the file before priv. drop to your target user so that it can be
 removed when your exit handlers run.  Alternatively, you can reclaim
 root at cleanup as it's stored as your saved UID.

Nice.  One thing: how do I get the uid and gid for the target user?  In
general, I know the name of the target user, but the uid/gid assigned by
the OS to that user could be different on different systems.
-- 
Jeffrey Barish

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


Re: Single-instance daemons

2008-11-12 Thread Jeffrey Barish
Cameron Simpson wrote:

 Or, more simply, get root to make an empty pid file once and chown it to
 the daemon user. Then the daemon can rewrite the file as needed. You need
 to move to truncating the file instead of removing it on daemon shutdown,
 but that is trivial. And no mucking with privileges, like starting the
 daemon as root instead of directly as the daemon user, need be done.

Although the file locking that I described is happening during boot (which I
did not make clear), so I believe that the user is root already. 
Accordingly, I need to drop privileges to a user anyway.  Still, I like
your suggestion, so I'll remember it for another occasion.
-- 
Jeffrey Barish

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


Regular expressions and Unicode

2008-10-02 Thread Jeffrey Barish
I have a regular expression that I use to extract the surname:

surname = r'(?u).+ (\w+)'

However, when I apply it to this Unicode string, I get only the first 3
letters of the surname:

name = 'Anton\xc3\xadn Dvo\xc5\x99\xc3\xa1k'

surname_re = re.compile(surname)
m = surname_re.search(name)
m.groups()
('Dvo\xc5',)

I suppose that there is an encoding problem, but I don't understand Unicode
well enough to know what to do to digest properly the Unicode characters in
the surname.
-- 
Jeffrey Barish


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


Re: Problem using copy.copy with my own class

2008-04-24 Thread Jeffrey Barish
George Sakkis wrote:

 First off, inheriting from a basic builtin type such as int and
 changing its constructor's signature is not typical; you should
 rethink your design unless you know what you're doing.

Nah, I would never claim to know what I'm doing.  However, I have to say
that I have been finding this technique very useful.  When I started
developing this program, I used an int.  Then I discovered that I needed to
have a string associated with the int.  By subclassing int to add a string,
I managed to make the change transparent to the code I had already written. 
Only the new code that needed the associated string knew that it was
available.  In another case, I subclassed str so that I could have a long
form for a string (e.g., a full name attached to the surname).  Are these
applications of subclassing bad form?  What is the motivation for your
warning?
 
 One way to make this work is to define the special __copy__ method
 [1], specifying explicitly how to create a copy of a Test instance:
 
 Normally (i.e. for pure Python classes that don't
 subclass a builtin other than object) copy.copy() is smart enough to
 know how to create a copy without an explicit __copy__ method, so in
 general you don't have to define it for every class that has to be
 copyable.

Yes, I noted in my original posting (which seems to have fallen off this
thread) that the __copy__method solved the problem (at least on one
platform).  However, I was wondering why it was necessary when what I was
defining was supposedly the default action.  Thanks for your explanation.

 The traceback is not obvious indeed. It turns out it involves calling
 the arcane __reduce_ex__ special method [2] defined for int, which
 returns a tuple of 5 items; the second is the tuple
 (class '__main__.Test', 0) and these are the arguments passed to
 Test.__new__. So another way of fixing it is keep Test.__new__
 compatible with int.__new__ by making optional all arguments after the
 first:

This suggestion is very interesting.  It seems to be an alternative to the
solution suggested by Gabriel.  The reference that Gabriel provided
includes the statement:

  Instances of a new-style type C are created using

  obj = C.__new__(C, *args)
 
  where args is the result of calling __getnewargs__() on the original
  object; if there is no __getnewargs__(), an empty tuple is assumed.

Gabriel's solution using __getnewargs__ assures that args receives a
non-empty tuple.  Your solution renders the empty tuple impotent by
specifying default values.  Correct me if I am wrong.

I would be interested in a translation into English of the following
statement from the same reference that Gabriel provided:

  Implementing this method [i.e., __getnewargs__] is needed if the 
  type establishes some internal invariants when the instance is 
  created, or if the memory allocation is affected by the values 
  passed to the __new__() method for the type (as it is for tuples 
  and strings).

What is an internal invariant?  How do I know when the memory allocation
is affected?  Does my Test class affect the memory allocation?

 As a sidenote, your class works fine without changing anything when
 pickling/unpickling instead of copying, although pickle calls
 __reduce_ex__ too:
 
 from pickle import dumps,loads
 t = Test(0, 0)
 assert loads(dumps(t)) == t
 
 Perhaps someone more knowledgeable can explain the subtle differences
 between pickling and copying here.

I have a situation in the full program where pickling seems to be failing in
the same manner as copy, but I have not been able yet to reproduce the
problem in a simple test program.

Thanks to all for your comments.
-- 
Jeffrey Barish

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


Re: Problem using copy.copy with my own class

2008-04-23 Thread Jeffrey Barish
Marc 'BlackJack' Rintsch wrote:
 Please simplify the code to a minimal example that still has the problem
 and *show it to us*.  It's hard to spot errors in code that nobody except
 you knows.

Here it is:

import copy

class Test(int):
def __new__(cls, arg1, arg2):
return int.__new__(cls, arg1)

def __init__(self, arg1, arg2):
self.arg2 = arg2

if __name__ == '__main__':
t = Test(0, 0)
t_copy = copy.copy(t)

Traceback (most recent call last):
  File copytest.py, line 12, in module
t_copy = copy.copy(t)
  File /usr/lib/python2.5/copy.py, line 95, in copy
return _reconstruct(x, rv, 0)
  File /usr/lib/python2.5/copy.py, line 322, in _reconstruct
y = callable(*args)
  File /usr/lib/python2.5/copy_reg.py, line 92, in __newobj__
return cls.__new__(cls, *args)
TypeError: __new__() takes exactly 3 arguments (2 given)
-- 
Jeffrey Barish

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


Problem using copy.copy with my own class

2008-04-22 Thread Jeffrey Barish
(Pdb) myclass
MyClass( 0, 0, 'A string', 123.45)
(Pdb) copy.copy(myclass)
*** TypeError: TypeError('__new__() takes at least 4 arguments (2 given)',)

I see 4 arguments (actually, 5 because Python is passing cls invisibly to
__new__).  Does anyone have an idea what is going on here?

I have not defined __new__ in MyClass above.  I can make the problem go away
on one platform by defining __new__ as

return MyClass(self, self.arg1, self.arg2, self.arg3)

but I understand that return value to be the default, so I don't see why
defining that method makes a difference.  On another platform, that
definition causes a different problem (I seem to be getting None as the
return value of the copy in some cases).

By the way, I have simplified somewhat the code in the explanation.  In case
it might matter, know that there are actually two classes that exhibit this
problem.  In one, there is actually a fourth argument in the __new__ method
that has a default value (the float above), which is why the error message
says that __new__ expects *at least* 4 arguments.  In the other, the last
argument is a parg collector.

I have also had trouble pickling these classes.  I surmise that pickle uses
copy.
-- 
Jeffrey Barish

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


Re: __init__.py file

2008-04-09 Thread Jeffrey Barish
cesco wrote:
 I need to instantiate an object (my_object) whose methods I have to
 use in two files (file1.py and file2.py) which are in the same
 directory. Is it possible to instantiate such object in the
 __init__.py file and then directly use it in file1.py and file2.py?
 If not, as I seem to experience, what is the best practice to follow
 in this case? (I thought __init__.py was somehow useful for that).

I do this and find the technique fantastically convenient.  You don't
explain what problems you encountered -- or perhaps I don't understand what
you mean by using the instances directly -- but here's what I do: In
dirA/dirB/__init__.py, I import my_object to import the my_object.py
module.  I then instantiate an object in __init__.py.  (Note that it is a
singleton because Python imports modules only once.)  Import the object in
file{1,2}.py with import dirA.dirB.my_object or from dirA.dirB import
my_object.  I use the technique only with objects that are legitimately
global.
-- 
Jeffrey Barish

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


Re: for-else

2008-03-10 Thread Jeffrey Barish
egbert wrote:

 The idea of the if-else is:
 .  depending on some condition either do this or do something else,
 .  don't do them both.

Indubitably, this statement is correct for other programming languages.  I
was initially surprised by loop-else when learning Python because I
approached these constructs from the perspective of other programming
languages I knew, as you are doing.  Before rejecting the Python
constructs, I asked myself whether the application of a different idea
resulted in a consistent, sensible interpretation.  The key is to ask not
whether the Python constructs fit a particular idea of if-else and
loop-else, but whether a reasonable idea exists within which the Python
constructs make sense.  For me and others in this thread, it does. 
Different keywords would, no doubt, result in constructs that fit other
ideas better, but personally I am content with the current solution.
-- 
Jeffrey Barish

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


Re: for-else

2008-03-06 Thread Jeffrey Barish
Terry Reedy wrote:

 A for-loop is equivalent to a while loop with the condition 'iterator is
 not exhausted'.  So do_else when that condition is false -- the iterator
 is exhausted.

I think that this is the most important statement in this thread.  As others
have expressed, I too found for-else surprising when I first encountered
it.  It made sense to me when I analogized for with if:

for x in range(5):
do_something()
else:
do_something_else()

means

do_something repeatedly when the condition (iterator not exhausted) is true
and do_something_else when the condition is not true, just as

if condition:
do_something()
else:
do_something_else()

means do_something once when the condition is true and do_something_else
when the condition is not true.  I find it elegant that Python does not
introduce additional keywords to deal with situations that are comparable.
-- 
Jeffrey Barish

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


Sending Python statement over socket in chunks

2008-02-11 Thread Jeffrey Barish
I have a python module that contains an assignment statement binding a long
list of things to a name:

list_of_things = [thing1, thing2, ...]

where each thing instantiates class Thing when executed.  I send this
statement through a socket to a remote module that executes it.  The
problem is that it takes too long to send the entire statement. 
Accordingly, I am thinking of a solution that sends the list in chunks. 
The client then executes each chunk and reassembles the complete list. 
Thus, I would first send something like:

chunk = [thing1, thing2, ... thing10]

and then

chunk = [thing11, thing12, ... thing20]

until all things have been transmitted.  At the client end, I would execute
each chunk statement and then do

list_of_things.append(chunk)

The point of this solution is that I can start doing useful work in the
client as soon as I receive the first chunk, and the others can arrive in
the background and be available by the time I need them.

One way I could implement this solution is to execute the statement for the
entire list_of_things in the server and then create chunk = [...]
statements with the lists filled using the repr of the class.  I believe
that this solution will work, but it seems a shame to execute the
list_of_things statement in the server rather than have the server stupidly
handle strings (because executing the statement takes time and because the
server currently doesn't understand Thing).  Should I investigate using a
parser to carve up the list_of_things = [...] statement?  Basically, I just
need to identify each Thing(...) expression and then count out some number
of them.  I should be able to do that using re.  Or perhaps I should write
my own parser using Python string operations as all I need to do is count
out some number of Things delimited by Thing( at one end and ),\nThing(
at the other (or )]\n at the end of the list).  Did I just answer my own
question?

Of course, I need to balance the complexity of any alternative solution
against simply executing the statement on the server.
-- 
Jeffrey Barish

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


Re: Sending Python statement over socket in chunks

2008-02-11 Thread Jeffrey Barish
Diez B. Roggisch wrote:

 Stop reinventing the wheel, start using pyro. Then either return the
 list as whole, or if it really is to big, return subsequent slices of it.

I am using Pyro.  Great package.  The problem is getting the chunks to send. 
I am trying to avoid executing the statement, so I have to carve up the
list as source.  Using Pyro to send the chunks once I have them should work
fine.  Note that I am avoiding the use of the term slice.  It is not
possible to slice the list in the way that we normally do in Python because
it is still in the form of a string.
-- 
Jeffrey Barish

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


Re: Biased random?

2007-08-31 Thread Jeffrey Barish
Robert Kern wrote:

 Ivan Voras wrote:
 Jeffrey Barish wrote:
 
 If you take the difference between two uniformly distributed random
 variables, the probability density function forms an isosceles triangle
 centered at 0.  Take the absolute value of that variable and the pdf is
 a
 straight line with maximum value at 0 tapering to 0 at max.  Thus,

 z = abs(randint(0, max) - randint(0, max))

 ought to do the trick.
 
 It's elegant :)
 
 I've noticed something interesting in my test: the value 0 appears less
 often than other values (which behave as they should).
 
 The distribution of the difference (before the abs()) looks like this
 (max=4):
 
 #
###
   #
  ###
  ---0+++
  321 123
 
 Taking the absolute value doubles up the non-zero masses, but there's no
 negative 0 to add to the 0s stack.
 
   #
   #
  ###
  ###
  
  
  0123
 
 The method does not work because of that.
 
The math says that it works, so we must not be implementing it correctly.  I
suspect that our mistake is quantizing the random variable first and then
taking the difference and absolute value.  What result do you get when you
quantize once?  That is, obtain two random values (floats) with uniform
pdf.  Take the difference.  Abs. Round to int.  This way, everything in the
band from (-0.5, +0.5) goes to 0, and that's the highest part of the
triangle.
-- 
Jeffrey Barish

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


Re: Biased random?

2007-08-31 Thread Jeffrey Barish
I'm sorry that I took the time to respond.
-- 
Jeffrey Barish

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


Re: Biased random?

2007-08-28 Thread Jeffrey Barish
Ivan Voras wrote:

 Hi,
 
 I have a list of items, and need to choose several elements from it,
 almost random. The catch is that the elements from the beginning
 should have more chance of being selected than those at the end (how
 much more? I don't care how the envelope of probability looks like at
 this point - can be linear). I see that there are several functions in
 Python standard libraries for various distribution, but is there an easy
 pythonic way to make them do what I need?

If you take the difference between two uniformly distributed random
variables, the probability density function forms an isosceles triangle
centered at 0.  Take the absolute value of that variable and the pdf is a
straight line with maximum value at 0 tapering to 0 at max.  Thus,

z = abs(randint(0, max) - randint(0, max))

ought to do the trick.
-- 
Jeffrey Barish

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


Moving class used in pickle

2007-05-15 Thread Jeffrey Barish
I have a class derived from string that is used in a pickle.  In the new
version of my program, I moved the module containing the definition of the
class.  Now the unpickle fails because it doesn't find the module.  I was
thinking that I could make the unpickle work by putting a copy of the
module in the original location and then redefine the class by sticking a
__setstate__ in the class thusly:

def __setstate__(self, state):
self.__dict__.update(state)
self.__class__ = NewClassName

My plan was to specify the new location of the module in NewClassName. 
However, when I do this I get the message 'class' object layout differs
from 'class'.  I don't think that they do as the new module is a copy of
the old one.  I suspect that I am not allowed to make the class assignment
because my class is derived from string.  What is the best way to update
the pickle?  The only thought I have is to read all the data with the old
class module, store the data in some nonpickle format, and then, with
another program, read the nonpickle-format file and rewrite the pickle with
the class module in the new location.
-- 
Jeffrey Barish

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


Using DCOP from Python

2006-12-18 Thread Jeffrey Barish
The package python-dcop makes it possible to use DCOP from Python.  Does
anyone know of a tutorial for this package or an example of its use?  I
would like to use it to read a journal entry in Korganizer.  I got as far
as figuring out that DCOP offers the interface
korganizer.CalendarIface.openJournalEditor(QString text, QDate date)
but I'm having trouble figuring out how to call this function from Python. 
And will it become clear how to control whether I am reading or writing the
journal?
-- 
Jeffrey Barish

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


Adding a comment to an image using PIL

2006-10-30 Thread Jeffrey Barish
I am trying to use PIL to add a comment to an image.  I have never used PIL
before, but I discovered that it is possible to print an existing comment
with the following:

im = PIL.Image.open('filename.jpg')
print im.app['COM']

I figured that I could write a comment by reversing this procedure:

im.app['COM'] = 'New comment'
im.save('newfilename.jpg')

However, when I open newfilename.jpg, I find that key 'COM' does not
exist -- the comment is not being written.  Presumably, I am doing
something wrong.
-- 
Jeffrey Barish

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


Re: Record Audio Analysis

2006-08-24 Thread Jeffrey Barish
Frank LaFond wrote:

 Jo Chase wrote:
 I would like to record audio from a mic and perform some basic analysis
 on
 the audio wave patterns produced.  What would be the easiest way to
 accomplish this in Python?
 
 Take a look at http://pymedia.org
 
 I think it allows the features you want.
 
 Frank.

Or try PySonic (http://pysonic.sourceforge.net/) if you prefer a package
that actually works.
-- 
Jeffrey Barish

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


Controlling Windows Media Player from Python

2006-07-03 Thread Jeffrey Barish
Is there a way to interact with Windows Media Player from Python?  I would
like to be able to do things like tell WMP to play a given sound file or to
ask WMP for metadata about a sound file.
-- 
Jeffrey Barish

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


Popen3 on Windows

2006-06-17 Thread Jeffrey Barish
I have an application that has been working fine on Linux, but now I need to
port it to Windows XP.  The program uses Popen3 to run another program.  I
use Popen3 so that I can access the pid attribute, which I use to kill the
auxiliary program when necessary.  Popen3 does not exist on Windows.  I see
os.popen2 and os.popen3, but they provide only file objects for stdin,
stdout, and stderr so I don't see a way to kill the auxiliary program that
I start.  Is there a way to do this on Windows?
-- 
Jeffrey Barish

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


Re: Debugging a pickle

2006-06-08 Thread Jeffrey Barish
Steve Holden wrote:

 First of all, verify that you are opening the file in binary mode. Not
 doing this is the biggest cause of problems for Windows users, which the
 intermittent failure makes me suspect you may be.
 
 regards
   Steve

Right on all counts.  I am on Windows and I was not opening the file in
binary mode either for input or output.  I am amazed that it worked at all. 
Thanks for the suggestion.
-- 
Jeffrey Barish

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


Debugging a pickle

2006-06-07 Thread Jeffrey Barish
I use cPickle to serialize some data so that I can read it back later.  It
works sometimes, but other times I get an EOFError at the

data = cPickle.load(fileobj)

statement.  I assume that this error indicates that cPickle is not able to
retrieve as much data from the file as it expects -- or that the dump did
not write as much data as it should have.  Is that assumption correct?  Are
there any debugging techniques I can use to figure out what is going wrong?
-- 
Jeffrey Barish

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


Name conflict in class hierarchy

2006-05-20 Thread Jeffrey Barish
I believe that the answer to my question is no, but I want to be sure that I
understand this issue correctly:  Suppose that there are two classes
defined as follows:

class A(object):
def f1(self):
print 'In A.f1, calling func'
self.func()

def func(self):
print 'In A.func'

class B(A):
def func(self):
print 'In B.func, calling A.f1'
A.f1(self)

Class A was defined by someone else or it comes from a library, so I have no
prior information about what is in it.  I subclass A to add some new
functionality, and I call the new function func.  The function B.func
uses A.f1, but unbeknownst to me, A.f1 uses A.func.  Unfortunately, class B
overrides func, so the call in A.f1 to self.func actually invokes B.func,
resulting in this case in an infinite loop.  Is there a way from B to
specify that A should use its own version of func and ignore the version in
B?  I know that I could rename A.func to avoid the name clash, but since A
is actually in a library, I will lose that change when I upgrade the
library.  I could rename B.func, but there is already a bunch of code that
calls it so I would have to update all the calls.  That seems like the
correct solution, though.  The other possibility is to use composition
rather than subclassing:

class B:
def func(self):
print 'In B.func, calling A.f1'
a = A()
a.f1()

but then B does not inherit other functions of A that I would like to use. 
It struck me that this must be a common problem in OOP, so I'm wondering
whether there is a simple solution that I am missing.
-- 
Jeffrey Barish

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


Multithreading and Queue

2006-04-25 Thread Jeffrey Barish
Several methods in Queue.Queue have warnings in their doc strings that they
are not reliable (e.g., qsize).  I note that the code in all these methods
is bracketed with lock acquire/release.  These locks are intended to
protect the enclosed code from collisions with other threads.  I am
wondering whether I understand correctly that the reason these methods are
still not reliable is that from the point where a thread calls qsize (for
example) to the point in Queue where a thread acquires a lock there is a
bunch of code, none of which is protected by a lock, (and moreover there is
another bunch of code between the point where a thread releases a lock and
then actually returns to the calling program) and so despite the locks in
Queue it is still possible for values to change before a thread acts on
them.
-- 
Jeffrey Barish

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


Re: ConfigParser: writes a list but reads a string?

2006-01-22 Thread Jeffrey Barish
funkyj wrote:

 making the config file XML and using xml.dom is another option,
 although XML is a bit ugly to edit by hand.
   --jfc
 
I am seriously intrigued by ConfigObj.  I am currently using an crude
improvisation involving tab-delimited fields to store metadata for
recordings -- not configuration data -- in text files.  I had been planning
to convert to XML, but now I am wondering whether ConfigObj would be
easier.  I would like for the metadata files to be editable, but editing
does not have to be easy as it needs to be done rarely.  I've never used
XML, so I am wondering whether there are other tradeoffs between that
approach and ConfigObj that I should be aware of.  I was thinking of XML
mainly to have a more robust format.  For example, it would be nice if it
were possible to add fields without obsoleting early versions of the
reader.  Crossplatform compatibility is also desirable.
-- 
Jeffrey Barish

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


Import DiscID on Windows

2005-04-25 Thread Jeffrey Barish
Has anyone gotten DiscID (and CDDB) to work on Windows?  I installed
python2.4 and CDDB-1.4 on Win98SE.  When I import DiscID I get

DLL load failed: One of the library files needed to run this application
cannot be found.

Python is complaining about mci.dll.  It is surely finding it because it
is in the search path, so I presume that there is something wrong with
the dll.  Any suggestions?  Can Python import a file with a dll
extension?
-- 
Jeffrey Barish

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


Re: Python 2.4 and Tkinter

2004-12-02 Thread Jeffrey Barish
Jean Brouwers wrote:

 
 It is hard to tell what is wrong, exactly.  Two suggestions:
 
 If this is a 32-bit build, why is there a  -L/usr/X11R6/lib64 and
 *before*  the regular -L/usr/X11R6/lib?  Try to rerun just that line
 gcc -pthread  _tkinter.so but without the -L/usr/X11R6/lib64
 option.
 
 If that still fails, try a fresh reconfigure and rebuild from scratch,
 now that the Tcl/Tk libs are there.  Start with ./configure  per
 the instructions in the README file, run make clean etc..
 
 /Jean Brouwers
 
 
 
 In article [EMAIL PROTECTED],
 Jeffrey Barish [EMAIL PROTECTED] wrote:
 
 Jean Brouwers wrote:
 
  
  Here is how we understand this (which may be incomplete and/or
  incorrect).
  
  The _tkinter module is a shared library _tkinter.o and that is
  built
  from C source file _tkinter.c.  That C file and a few other tk
  related C files are included in the Python distribution.
  
  But the Tcl/Tk libraries to build the _tkinter module are coming
  from
  /usr/local/lib in our case.  And those happens to be tcl83 and tk83
  in our case.
  
  If the Tcl/Tk libraries (and include files) are missing, you will
  have
  to get and install those.  But they are not part of the Python
  distribution, as far as we know.
  
  /Jean Brouwers
  
  
  
  In article [EMAIL PROTECTED],
  Jeffrey Barish [EMAIL PROTECTED] wrote:
  
  Jean Brouwers wrote:
  
   
   FWIW, we just installed Python 2.4 (on RH Linx 8), rebuilt it
   from
   scratch and everything is fine.  Tkinter is there, _tkinter as
   well and idle comes up as expected.
   
   /Jean Brouwers
   
   
   
   In article
   [EMAIL PROTECTED], Jeffrey
   Barish [EMAIL PROTECTED] wrote:
   
   http://www.python.org/moin/TkInter
  
  Here's what I get when I import Tkinter at a python prompt:
  
  [EMAIL PROTECTED]:~$ python
  Python 2.4 (#1, Nov 30 2004, 08:58:13)
  [GCC 3.2.3 20030316 (Debian prerelease)] on linux2
  Type help, copyright, credits or license for more
  information.
   import Tkinter
  Traceback (most recent call last):
File stdin, line 1, in ?
File /usr/local/lib/python2.4/lib-tk/Tkinter.py, line 38, in ?
  import _tkinter # If this fails your Python may not be
  configured
  for Tk
  ImportError: No module named _tkinter
  
  I tried make clean and make.  Same result.  Ah, now I see that the
  output from make includes:
  
  INFO: Can't locate Tcl/Tk libs and/or headers
  
  Aren't the libs and headers included in Python-2.4.tgz?
 
 OK, I downloaded tcl8.4.8 and tk8.4.8.  They are now installed.  Back
 to
 python2.4 make.  It now bombs at:
 
 gcc -pthread -shared build/temp.linux-i686-2.4/_tkinter.o
 build/temp.linux-i686-2.4/tkappinit.o -L/usr/X11R6/lib64
 -L/usr/X11R6/lib -L/usr/local/lib -ltk8.4 -ltcl8.4 -lX11 -o
 build/lib.linux-i686-2.4/_tkinter.so
 *** WARNING: renaming _tkinter since importing it failed:
 libtk8.4.so: cannot open shared object file: No such file or
 directory running build_scripts
 
 Here's what I don't get:
 
 [EMAIL PROTECTED]:/tmp/Python-2.4# ls -l /usr/local/lib/libtk8.4.so
 -rwxr-xr-x1 root staff  893866 2004-12-02
 15:28 /usr/local/lib/libtk8.4.so
 
 The library is there but gcc claims that it isn't.  Any suggestions?

It turns out that the problem is in the step after the gcc.  I have now
tracked it into setup.py.  There is a call to imp.load_dynamic() that
fails.  I haven't been able to figure out why yet, in large part
because I am not familiar with module imp (and imp.load_dynamic isn't
even documented).
-- 
Jeffrey Barish

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