Questions regarding the daemon module.

2012-01-28 Thread David Lambert

I was looking for a simple way to daemonize a Python process, and found:

http://www.python.org/dev/peps/pep-3143/

I used easy_install to add this package (I thought), but when I 
attempted to use the example in the above link, I got the error:



AttributeError: 'module' object has no attribute 'DaemonContext'

To my surprise when looking at the module that was installed, I found 
something completely different to what was in the documentation:


>>> dir(daemon)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 
'basic_daemonize', 'checkPID', 'daemonize', 'errno', 'os', 'sys', 
'writePID']

>>>
>>> print daemon.__doc__
None
>>>
>>> print daemon.daemonize.__doc__
None
>>>


Further experimentation with this module yielded a working daemon, but I 
am concerned regarding its parentage and lack of documentation. Could 
someone explain these discrepancies?



Best regards,

Dave.

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


Re: add two strings

2012-01-30 Thread David Lambert

On 01/30/2012 07:02 AM, contro opinion wrote:

>>> s1='\x45'
>>> s2='\xe4'
>>> s1+s2
'E\xe4'
>>> print  s1+s2
E

why  s1+s2  not  =  '\x45\xe4'??


It is, but '\x45' is ASCII 'E', and '\xe4' is not a printable character:
>>> print '\x45'
E
>>> print  '\xe4'

>>>
Try printing s1 and s2 separately in your example.

HTH,

Dave.

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


Re: A question of style (finding item in list of tuples)

2012-05-21 Thread David Lambert

One suggestion is to construct the dictionary first:


CHOICES = dict(
NONE = 'No experience required',
SAIL = 'Sailing experience, new to racing',
RACE = 'General racing experience',
GOOD = 'Experienced racer',
ROCK = 'Rock star'
)

def experience_text(self):
try:
return CHOICES[self]
except:
return ""



On 05/21/2012 07:37 AM, Roy Smith wrote:

I've got this code in a django app:

 CHOICES = [
 ('NONE', 'No experience required'),
 ('SAIL', 'Sailing experience, new to racing'),
 ('RACE', 'General racing experience'),
 ('GOOD', 'Experienced racer'),
 ('ROCK', 'Rock star'),
 ]

 def experience_text(self):
 for code, text in self.CHOICES:
 if code == self.level:
 return text
 return ""

Calling experience_text("ROCK") should return "Rock star".  Annoyingly,
django handles this for you automatically inside a form, but if you also
need it in your application code, you have to roll your own.

The above code works, but it occurs to me that I could use the much
shorter:

 def experience_text(self):
 return dict(CHOICES).get("self.level", "???")

So, the question is, purely as a matter of readability, which would you
find easier to understand when reading some new code?  Assume the list
of choices is short enough that the cost of building a temporary dict on
each call is negligible.  I'm just after style and readability here.


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