Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34396

Re: Confused compare function :)

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 <pjmakey2@gmail.com>
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 <pjmakey2@gmail.com>
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 <CAKhY55PXaMkdnW7qMb-5ConHMcz05Gnkqw4wvyoprBFBd5UFFw@mail.gmail.com> <mailman.538.1354753193.29569.python-list@python.org> <k9p32p$nek$1@dont-email.me> <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <mailman.549.1354783761.29569.python-list@python.org> <k9q6s4$os9$1@r03.glglgl.gl> <CAPTjJmqhY2Nxcf-MqCZ2Z8EXvfAtDASt_oPVg+=WcwVFwBBqEQ@mail.gmail.com>
In-Reply-To <CAPTjJmqhY2Nxcf-MqCZ2Z8EXvfAtDASt_oPVg+=WcwVFwBBqEQ@mail.gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.565.1354805649.29569.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 12/06/2012 10:58 AM, Chris Angelico wrote:
> On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel
> <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
> 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")






Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Confused compare function :) Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-06 01:19 +0100
  Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 00:42 +0000
    Re: Confused compare function :) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-12-06 13:41 -0500
    Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 19:55 +0100
  Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 03:22 +0000
    Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 04:32 +0000
      Re: Confused compare function :) Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-06 09:49 +0100
        Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 11:47 +0000
          Re: Confused compare function :) peter <pjmakey2@gmail.com> - 2012-12-06 08:55 -0300
            Re: Confused compare function :) Hans Mulder <hansmu@xs4all.nl> - 2012-12-06 14:32 +0100
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 00:47 +1100
          Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-06 23:14 +1100
            Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-07 22:16 +0000
              Re: Confused compare function :) Terry Reedy <tjreedy@udel.edu> - 2012-12-08 02:01 -0500
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-08 18:17 +1100
              Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-08 17:50 +0000
            Re: Confused compare function :) Ramchandra Apte <maniandram01@gmail.com> - 2012-12-08 19:07 -0800
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-09 14:22 +1100
                Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-09 07:39 +0000
            Re: Confused compare function :) Ramchandra Apte <maniandram01@gmail.com> - 2012-12-08 19:07 -0800
          Re: Confused compare function :) Neil Cerutti <neilc@norwich.edu> - 2012-12-06 13:51 +0000
            Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-07 02:55 +0000
              Re: Confused compare function :) Neil Cerutti <neilc@norwich.edu> - 2012-12-07 16:40 +0000
        Re: Confused compare function :) Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-12-06 14:33 +0100
          Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 00:58 +1100
            Re: Confused compare function :) Hans Mulder <hansmu@xs4all.nl> - 2012-12-06 15:21 +0100
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 01:28 +1100
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 15:22 +0100
          Re: Confused compare function :) Dave Angel <d@davea.name> - 2012-12-06 09:40 -0500
          Re: Confused compare function :) peter <pjmakey2@gmail.com> - 2012-12-06 11:46 -0300
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 17:16 +0100
          Re: Confused compare function :) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-12-06 16:52 +0000
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 18:08 +0100
          Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-06 17:10 +0000
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 18:31 +0100
          Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-06 17:52 +0000
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 19:25 +0100
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-07 14:36 +0100
        Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 19:24 +0000
      Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-06 20:34 +1100
      Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 19:25 +0000

csiph-web