Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'example:': 0.03; 'value,': 0.03; 'initialize': 0.05; '%s"': 0.07; '21,': 0.07; 'except:': 0.07; 'exit': 0.07; 'sys,': 0.07; 'try:': 0.07; 'python': 0.09; '%s\\n"': 0.09; '(%d)': 0.09; '**kwargs):': 0.09; '22,': 0.09; 'failed:': 0.09; 'fork': 0.09; 'handling,': 0.09; 'matched': 0.09; 'propagate': 0.09; 'def': 0.10; 'itself.': 0.11; "wouldn't": 0.11; 'dec': 0.15; 'server,': 0.15; 'value.': 0.15; "'r')": 0.16; '(assuming': 0.16; '-f.': 0.16; 'created.': 0.16; 'daemons': 0.16; 'descriptors.': 0.16; 'line)': 0.16; 'nameerror': 0.16; 'present;': 0.16; 'sock': 0.16; 'statement)': 0.16; 'subject:compare': 0.16; 'subscripting': 0.16; 'sys.exit(0)': 0.16; 'sys.exit(1)': 0.16; 'wrote:': 0.17; 'example.': 0.17; '(in': 0.18; 'examples': 0.18; '>>>': 0.18; 'code,': 0.18; 'otherwise,': 0.20; 'bit': 0.21; 'import': 0.21; 'meant': 0.21; 'received:209.85.214.174': 0.21; 'exceptions': 0.22; 'keyerror:': 0.22; 'example': 0.23; 'seems': 0.23; 'raise': 0.24; 'second': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'skip:[ 10': 0.26; 'am,': 0.27; 'environment.': 0.27; 'logging': 0.27; 'chris': 0.28; 'behaviour': 0.29; 'parent': 0.29; 'tail': 0.29; 'though.': 0.29; 'maybe': 0.29; 'connections': 0.30; 'fri,': 0.30; 'version,': 0.30; 'function': 0.30; 'error': 0.30; 'point': 0.31; 'file': 0.32; 'getting': 0.33; 'like:': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'monitor': 0.33; 'received:google.com': 0.34; 'server': 0.35; 'list': 0.35; 'skip:k 20': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'skip:{ 10': 0.36; 'should': 0.36; 'operating': 0.36; 'ok,': 0.37; 'does': 0.37; '(for': 0.37; 'previous': 0.37; 'quite': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'perform': 0.38; 'skip:l 20': 0.38; 'que': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'called': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'help': 0.40; 'think': 0.40; 'your': 0.60; 'first': 0.61; 'thomas': 0.62; 'different': 0.63; 'more': 0.63; 'today': 0.67; 'account': 0.67; 'received:200': 0.71; 'inform': 0.78; ':).': 0.84; 'dict,': 0.84; 'rachel': 0.84; 'server)': 0.84; 'why?': 0.84; 'hate': 0.93; 'ports': 0.93; 'imagine': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=ulKsTTBZt8A0/yAb0P5el6hJWWRnwDtgrJ7iEB+H7fQ=; b=Zizhjez7CEqP/C2JOzUY6R5JZmlMBtLPjCX1HRgEulBXgOqng4e56+YhM59+F+WBvT KiwWGVTFP7xAz+KO57mZ5VnXu6uw1BXuLOmcxeMC1q/wq95VzwiCT0/NGTKYv6YseXPg vhHZQZkcRgWwkHEpOU+JwBTZx+7DtwsiKqX3RNk8rDrF++5ukUiLFG5KHrruaZxdhUPU 4uVr0C6Na15kpoVksnjF8Y/aoLFfGoV3IX2aqWmlZvv/7Ab8vt86TV+XnZAIQTAd9uQ6 n+FV1xJ+XUMxpoBKzEpzgH5EELIwJmqLO2BIei1TnqHm1UGT5ncLvFZvDsItU//V2jE1 q0rw== Date: Thu, 06 Dec 2012 11:46:25 -0300 From: peter User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120824 Thunderbird/15.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Confused compare function :) References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 140 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354805649 news.xs4all.nl 6902 [2001:888:2000:d::a6]:47341 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34396 On 12/06/2012 10:58 AM, Chris Angelico wrote: > On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel > > wrote: >> Am 06.12.2012 09:49 schrieb Bruno Dupuis: >> >>> The point is Exceptions are made for error handling, not for normal >>> workflow. I hate when i read that for example: >>> >>> try: >>> do_stuff(mydict[k]) >>> except KeyError: >>> pass >> I would do >> >> try: >> value = mydict[k] >> except KeyError: >> pass >> else: >> do_stuff(k) >> >> Why? Because do_stuff() might raise a KeyError, which should not go >> undetected. > (Assuming first off that you meant "do_stuff(value)", not > "do_stuff(k)", in that last line) > > That has quite different functionality, though. The original wouldn't > have called do_stuff at all if k is not in dict, behaviour which is > matched by both his EAFP and his LBLY. But your version, in the event > of a KeyError, will call do_stuff with the previous value of value, or > raise NameError if there is no such previous value. I don't think > that's intentional. > > The only way around it that I can see is an extra condition or jump - > something like: > > def call_if_present(mydict,k,do_stuff): > """Equivalent to > do_stuff(mydict[k]) > if the key is present; otherwise, does not call do_stuff, and > returns None.""" > try: > value = mydict[k] > except KeyError: > return > return do_stuff(value) > > It'll propagate any other exceptions from the subscripting (eg > TypeError if you give it a list instead of a dict), and any exceptions > from do_stuff itself. But it's getting a bit unwieldy. > > ChrisA Ok, is seems like my example code, don't like :). Is ok, it was a poor example. This a more complex example that create a python daemons process. In these case you see the help of try catch. To watch an Operating system problem (80% of the cases), when the fork is created. Or you can initialize a logging (in the catch statement) and watch the log file with tail -f. import sys, os def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): # Perform first fork. try: pid = os.fork( ) if pid > 0: sys.exit(0) # Exit first parent. except OSError, e: sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # Decouple from parent environment. os.chdir("/") os.umask(0) os.setsid( ) # Perform second fork. try: pid = os.fork( ) if pid > 0: sys.exit(0) # Exit second parent. except OSError, e: sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # The process is now daemonized, redirect standard file descriptors. for f in sys.stdout, sys.stderr: f.flush( ) si = file(stdin, 'r') so = file(stdout, 'a+') se = file(stderr, 'a+', 0) os.dup2(si.fileno( ), sys.stdin.fileno( )) os.dup2(so.fileno( ), sys.stdout.fileno( )) os.dup2(se.fileno( ), sys.stderr.fileno( )) Or imagine that you are manage master-slave's connections (for examples to an ldap databse - or a monitor system tools), you can use try catch. If all the server's are down, maybe you want to be inform about it, you can put a function to send a email to your account In simple works try: catch is 'GOOD'. ipserver = {"192.168.1.13": ["monitoreo", 22], "192.168.1.18": ["usuarios-dns", 22, 53, 139, 389, 445, 631, 3306, 4900, 8765], "192.168.1.72": ["sistemas-ldap", 22, 80, 139, 389, 445, 631, 3306, 4900, 8765], "192.168.1.74": ["terminales", 22,139, 445, 389, 4900], "192.168.1.80": ["backup", 22, 139, 445], "192.168.1.1": ["router", 21, 22, 25, 80, 110, 143, 465, 3128], "192.168.1.90": ["router", 5900] } def portstatus(self, **kwargs): ports = kwargs.get("ports") server = kwargs.get("server") if len(ports) > 0: logging.info("chequeando puertos %s" % server) for a in ports: try: sock = socket() sock.connect((server,a)) sock.close except: logging.info("informando errores en puerto %s" % a) today = str(datetime.today()) subprocess.Popen("for a in $(seq 1 15); do beep; done", shell=True) self.sendreport(ip=server, server=server, cuerpo="El puerto %s esta caido en %s - %s" % (a, server, today), asunto="El puerto %s esta caido en %s - %s" % (a, server, today)) else: logging.info("no hay puertos que chequear")