Re: Understanding other people's code

2013-07-16 Thread David M Chess
 Literally any idea will help, pen and paper, printing off all the code 
and doing some sort of highlighting session - anything! 
 I keep reading bits of code and thinking well where the hell has that 
been defined and what does it mean to find it was inherited from 3 
modules up the chain. 
 I really need to get a handle on how exactly all this slots together! 
Any techniques,tricks or methodologies that people find useful would be 
much appreciated.

I'd highly recommend Eclipse with PyDev, unless you have some strong 
reason not to.  That's what I use, and it saves pretty much all of those 
what's this thing? problems, as well as lots of others...

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


Re: Functional vs. Object oriented API

2013-04-12 Thread David M Chess
 Roy Smith r...@panix.com 

 As part of our initial interview screen, we give applicants some small 
 coding problems to do.  One of the things we see a lot is what you could 

 call Java code smell.  This is our clue that the person is really a 
 Java hacker at heart who just dabbles in Python but isn't really fluent. 
 
 ...
 It's not just LongVerboseFunctionNamesInCamelCase().  Nor is it code 
 that looks like somebody bought the Gang of Four patterns book and is 
 trying to get their money's worth out of the investment.  The real dead 
 giveaway is when they write classes which contain a single static method 

 and nothing else.

I may have some lingering Java smell myself, although I've been working 
mostly in Python lately, but my reaction here is that's really I don't 
know BASIC smell or something; a class that contains a single static 
method and nothing else isn't wonderful Java design style either.

 That being said, I've noticed in my own coding, it's far more often that 

 I start out writing some functions and later regret not having initially 

 made it a class, than the other way around.  That's as true in my C++ 
 code as it is in my Python.

Definitely.

 Once you start having state (i.e. data) and behavior (i.e. functions) in 

 the same thought, then you need a class.  If you find yourself passing 
 the same bunch of variables around to multiple functions, that's a hint 
 that maybe there's a class struggling to be written.

And I think equally to the point, even if you have only data, or only 
functions, right now, if the thing in question has that thing-like feel to 
it :) you will probably find yourself with both before you're done, so you 
might as well make it a class now...

DC

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


Re: Cannot run a single MySQLdb execute....

2013-03-28 Thread David M Chess
Νίκος Γκρ33κ nikos.gr...@gmail.com :

 What paramstyle are you using?

Yes it is Chris, but i'am not sure what exactly are you asking me.
Please if you cna pout it even simper for me, thank you.

For instance:

 import MySQLdb
 MySQLdb.paramstyle
'format'

FWIW and HTH,
DC

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


Re: Getting a TimedRotatingFileHandler not to put two dates in the same file?

2012-10-24 Thread David M Chess
 d...@davea.name

 On 10/23/2012 11:23 AM, David M Chess wrote:
  We have a TimedRotatingFileHandler with when='midnight'

 You give us no clue what's in this class, or how it comes up with the
 filenames used.

Sorry if I was unclear.  This isn't my own subclass of 
TimedRotatingFileHandler or anything, this is the bog-standard 
logging.handlers.TimedRotatingFileHandler I'm talking about.

So all clues about what's in the class, and how it comes up with the 
filenames used, is available at

http://docs.python.org/library/logging.handlers.html#timedrotatingfilehandler 


:)

The specific Python version involved here is Python 2.6.6 (r266:84297, Aug 
24 2010, 18:46:32), to the extent that that matters...

 This works great, splitting the log information across files by date, 
as 
 long as the process is actually up at midnight.

 But now the users have noticed that if the process isn't up at 
midnight, 
 they can end up with lines from two (or I guess potentially more) dates 
in 
 the same log file.

 Is there some way to fix this, either with cleverer arguments into the 
 TimedRotatingFileHandler, or by some plausible subclassing of it or its 

 superclass?

Tx,
DC

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


Re: Getting a TimedRotatingFileHandler not to put two dates in the same file?

2012-10-24 Thread David M Chess
 w...@mac.com 

 Something like:

 Does a log file exist? - No -  First run; create log file  continue
  |
 Yes
  |
  Read backwards looking for date change, copy lines after change
  to new file, delete from old file.

Yep, I'm concluding that also.

It just wasn't clear to me from the documentation whether or not the 
existing TimedRotatingFileHandler had any at startup, see if we missed 
any rollovers, and do them now if so function, or if there was some known 
variant that does.  The answer, apparently, being nope.  :)  Shouldn't 
be that hard to write, so that's probably what we'll do.

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


A lock that prioritizes acquire()s?

2012-10-24 Thread David M Chess
Okay, next silly question.  :) 

We have a very simple multi-threaded system where a request comes in, 
starts running in a thread, and then (zero, one, or two times per request) 
gets to a serialization point, where the code does: 

with lock: 
  do_critical_section_stuff_that_might_take_awhile() 

and then continues. 

Which is almost the same as: 

lock.acquire() 
try: 
  do_critical_section_stuff_that_might_take_awhile() 
finally: 
  lock.release() 

Now we discover that It Would Be Nice if some requests got priority over 
others, as in something like: 

lock.acquire(importance=request.importance) 
try: 
  do_critical_section_stuff_that_might_take_awhile() 
finally: 
  lock.release() 

and when lock.release() occurs, the next thread that gets to run is one of 
the most important ones currently waiting in acquire() (that's the 
exciting new thing). 

Other requirements are that the code to do this be as simple as possible, 
and that it not mess anything else up.  :) 

My first thought was something like a new lock-ish class that would do 
roughly: 

class PriorityLock(object): 

def __init__(self): 
self._lock = threading.Lock() 
self._waiter_map = {}  # maps TIDs to importance
 
def acquire(self,importance=0): 
this_thread = threading.currentThread() 
self._waiter_map[this_thread] = importance # I want in 
while True: 
self._lock.acquire() 
if ( max( self._waiter_map.values())=importance ): # we win 
del self._waiter_map[this_thread]  # not waiting anymore 
return # return with lock acquired 
self._lock.release()  # We are not most impt: release/retry
 
def release(self): 
self._lock.release() 

(Hope the mail doesn't garble that too badly.) 

Basically the acquire() method just immediately releases and tries again 
if it finds that someone more important is waiting. 

I think this is semantically correct, as long as the underlying lock 
implementation doesn't have starvation issues, and it's nice and simple, 
but on the other hand it looks eyerollingly inefficient. 

Seeking any thoughts on other/better ways to do this, or whether the 
inefficiency will be too eyerolling if we get say one request per second 
with an average service time a bit under a second but maximum service time 
well over a second, and most of them are importance zero, but every (many) 
seconds there will be one or two with higher importance. 

Tx, 
DC 

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


Re: A lock that prioritizes acquire()s?

2012-10-24 Thread David M Chess
Lovely, thanks for the ideas!  I remember considering having release() 
pick the next thread to notify, where all the waiters were sitting on 
separate Conditions or whatever; not sure why I didn't pursue it to the 
end.  Probably distracted by something shiny; or insufficient brainpower. 
:) DC
--

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


Getting a TimedRotatingFileHandler not to put two dates in the same file?

2012-10-23 Thread David M Chess
We have a TimedRotatingFileHandler with when='midnight'. 

This works great, splitting the log information across files by date, as 
long as the process is actually up at midnight.

But now the users have noticed that if the process isn't up at midnight, 
they can end up with lines from two (or I guess potentially more) dates in 
the same log file.

Is there some way to fix this, either with cleverer arguments into the 
TimedRotatingFileHandler, or by some plausible subclassing of it or its 
superclass?

Or am I misinterpreting the symptoms somehow?

Tx much!
DC
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with ThreadingTCPServer Handler

2012-10-23 Thread David M Chess
 jorge jaoro...@estudiantes.uci.cu

 I'm programming a server that most send a message to each client 
 connected to it and nothing else. this is obviously a base of what i 
 want to do. the thing is, I made a class wich contains the Handler class 

 for the ThreadingTCPServer and starts the server but i don't know how 
 can i access the message variable contained in the class from the 
 Handler since I have not to instance the Handler by myself.

The information about the request is in attributes of the Handler object. 
From the socketserver docs (
http://docs.python.org/library/socketserver.html ):

RequestHandler.handle() 
This function must do all the work required to service a request. The 
default implementation does nothing. Several instance attributes are 
available to it; the request is available as self.request; the client 
address asself.client_address; and the server instance as self.server, in 
case it needs access to per-server information. 

If that's what you meant by the message variable contained in the class. 
 

If, on the other hand, you meant that you want to pass some specific data 
into the handler about what it's supposed to be doing, I've generally 
stashed that in the server, since the handler can see the server via 
self.server.

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


Re: Py3.3 unicode literal and input()

2012-06-18 Thread David M Chess
 If you (the programmer) want a function that asks the user to enter a
 literal at the input prompt, you'll have to write a post-processing for
 it, which looks for prefixes, for quotes, for backslashes, etc., and
 encodes the result.  There very well may be such a decoder in the Python
 library, but input does nothing of the kind.

As it says at the end of eval() (which you definitely don't want to use 
here due to side effects):

See ast.literal_eval() for a function that can safely evaluate strings 
with expressions containing only literals. 

DC


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


bus errors when the network interface is reset?

2012-05-01 Thread David M Chess
We have a system running Python 2.6.6 under RHEL 6.1.  A bunch of 
processes spend most of their time sitting in a BaseHTTPServer.HTTPServer 
waiting for requests.

Last night an update pushed out via xcat whimsically restarted all of the 
network interfaces, and at least some of our processes died with bus 
errors (i.e. no errors or exceptions reflected up to the Python level, 
just a crash).

This is just my initial looking into this.  Seeking opinions of the form, 
say:

Yeah, that happens, don't reset the network interfaces.
Yeah, that happens, and you can prevent the crash by doing X in your OS.
Yeah, that happens, and you can prevent the crash by doing X in your 
Python code.
That wouldn't happen if you upgraded S to version V.
That sounds like a new bug and/or more information is needed; please 
provide copious details including at least X, Y, and Z.

Any thoughts or advice greatly appreciated.

DC
David M. Chess
IBM Watson Research Center
-- 
http://mail.python.org/mailman/listinfo/python-list