Russell Smith | 1 Oct 01:26 2010
Picon

Help - server health check reporter

Hey guys,

 

I’m trying to put together a script using urllib2, smtplib and stripogram/html2text which will use a healthcheck url, read the response after loading it and then email me the results without any unwanted html tags. I was able to do that but, when I get a timeout on one of the servers the script breaks with an error response. I’m very new to python and I don’t know how to take that error and have it sent as well, instead of breaking the script. Any advice or links I can check out that might be helpful? I appreciate it.


I can include the python code if needed though at this point I'm sure it is not very "pythonic". {smile}


Regards,

-russ

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano | 1 Oct 01:32 2010

Re: Help - server health check reporter

On Fri, 1 Oct 2010 09:26:16 am Russell Smith wrote:

> I’m trying to put together a script using urllib2, smtplib and
> stripogram/html2text which will use a healthcheck url, read the
> response after loading it and then email me the results without any
> unwanted html tags. I was able to do that but, when I get a timeout
> on one of the servers the script breaks with an error response. 

Could you copy and paste the exact error message, including the full 
traceback?

Also, what version of Python are you using?

> I’m 
> very new to python and I don’t know how to take that error and have
> it sent as well, instead of breaking the script. Any advice or links
> I can check out that might be helpful? I appreciate it.

try:
    code that times out goes here
except Exception as error:
    do something with error

Obviously this isn't exactly Python code, but I can be more specific 
once you've answered the above two questions.

--

-- 
Steven D'Aprano
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano | 1 Oct 01:45 2010

Re: python Task

On Fri, 1 Oct 2010 08:38:31 am Dipo Elegbede wrote:

> 4.4 An integer greater than 1 is said to be prime if it is divisible
> by only 1 and itself. For example,
> 2, 3, 5 and 7 are prime numbers, but 4, 6, 8 and 9 are not.
> a) Write a function that determines whether a number is prime.

The first thing you need to know is that you can find out the remainder 
after division by using the % operator. For example:

10 % 5 => 0  # 10 = 5*2
11 % 5 => 1  # 11 = 5*2 + 1
12 % 5 => 2  # 12 = 5*2 + 2

and so forth.

So you can check whether a number is divisible by another with:

if n % divisor == 0:
    print "n is exactly divisible by divisor"
else:
    print "n is not divisible by divisor"

So to find out whether a number n is prime, you need to check whether it 
is divisible by each whole number starting from 2:

for divisor in range(2, n):
    if n is divisible by divisor then n is not prime
# outside the loop
print "prime"

The above does a LOT of unnecessary work. Some improvements:

* Once you've discovered that a number is not prime, you can 
  exit the loop.
* There's no need to test every number up to n-1. Think about
  what the greatest number you need to test would be.
* If a number isn't divisible by 2, then it can't possibly be
  divisible by 4, 6, 8, 10, ... either. So apart from 2, there's
  no need to check even numbers.

--

-- 
Steven D'Aprano
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Steven D'Aprano | 1 Oct 01:48 2010

Re: just what does read() return?

On Fri, 1 Oct 2010 08:49:31 am Alex Hall wrote:

> Ah-ha!!
> re.split(r"\n+", self.original)
> That did it, and my program once again runs as expected. Thanks!

There is no need to crack that tiny peanut with the 40 lb sledgehammer 
of a regular expression.

list_of_lines = string.split('\n')

Much faster, simpler, and does the job. To get rid of empty lines:

list_of_lines = filter(None, string.split('\n'))

--

-- 
Steven D'Aprano
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Alan Gauld | 1 Oct 01:59 2010

Re: inheritance problem

"Roelof Wobben" <rwobben <at> hotmail.com> wrote

> So i have this programm now :

> class Deck:
>    def __init__(self):
>        self.cards = []
>        for suit in range(4):
>            for rank in range(1, 14):
>                self.cards.append(Card(suit, rank))
>
>    def deal(self, hands, num_cards=999):
>        num_hands = len(hands)
>        for i in range(num_cards):
>            if self.is_empty(): break   # break if out of cards
>            card = self.pop()           # take the top card
>            hand = hands[i % num_hands] # whose turn is next?
>            hand.add(card)              # add the card to the hand
>
>    def shuffle(self):
>        import random
>        num_cards = len(self.cards)
>        for i in range(num_cards):
>            j = random.randrange(i, num_cards)
>            self.cards[i], self.cards[j] = self.cards[j], 
> self.cards[i]
>
>    def remove(self, card):
>        if card in self.cards:
>            self.cards.remove(card)
>            return True
>        else:
>            return False
>    def is_empty(self):
>        return (len(self.cards) == 0)

> But now Im getting this error message:
>
> Traceback (most recent call last):
>  File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 126, 
> in <module>
>    game.deck.deal([hand], 13)
>  File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 24, 
> in deal
>    card = self.pop()           # take the top card
> AttributeError: Deck instance has no attribute 'pop'
>
>
> What went wrong here.

The error tells you what is wrong, the Deck has no attribute called 
pop.
Can you see a pop anywhere?

So if the code you copied has an error how should it assign a card?
Where are the cards stored? How would you get the first card from
the collection? Does that work?

Think about what the error is telling you and think about how you
would fix it. If you don't understand what the error is saying then 
tell
us and we can explain it, but in this case its pretty clearly stated
and is one you have seen before.

--

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Steven D'Aprano | 1 Oct 02:01 2010

Re: just what does read() return?

On Fri, 1 Oct 2010 08:32:40 am Alex Hall wrote:

> I fully expected to see txt be an array of strings since I figured
> self.original would have been split on one or more new lines. It
> turns out, though, that I get this instead:
> ['l\nvx vy z\nvx vy z']

There's no need to call str() on something that already is a string. 
Admittedly it doesn't do much harm, but it is confusing for the person 
reading, who may be fooled into thinking that perhaps the argument 
wasn't a string in the first place.

The string split method doesn't interpret its argument as a regular 
expression. r'\n+' has no special meaning here. It's just three literal 
characters backslash, the letter n, and the plus sign. split() tries to 
split on that substring, and since your data doesn't include that 
combination anywhere, returns a list containing a single item:

>>> "abcde".split("ZZZ")
['abcde']

> How is it that txt is not an array of the lines in the file, but
> instead still holds \n characters? I thought the manual said read()
> returns a string:

It does return a string. It is a string including the newline 
characters.

[...]
> I know I can use f.readline(), and I was doing that before and it all
> worked fine. However, I saw that I was reading the file twice and, in
> the interest of good practice if I ever have this sort of project
> with a huge file, I thought I would try to be more efficient and read
> it once.

You think that keeping a huge file in memory *all the time* is more 
efficient? It's the other way around -- when dealing with *small* files 
you can afford to keep it in memory. When dealing with huge files, you 
need to re-write your program to deal with the file a piece at a time. 
(This is often a good strategy for small files as well, but it is 
essential for huge ones.)

Of course, "small" and "huge" is relative to the technology of the day. 
I remember when 1MB was huge. These days, huge would mean gigabytes. 
Small would be anything under a few tens of megabytes.

--

-- 
Steven D'Aprano
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Steven D'Aprano | 1 Oct 02:06 2010

Re: Runnig a windows.exe from python

On Fri, 1 Oct 2010 04:05:08 am Susana Iraiis Delgado Rodriguez wrote:
> Hello !
>
> I apoligize for the format of my last message, I didn't realize that
> putting my text in bold format will show those asterisks.
> By the way you're Alan, for the pythom module I first had to read my
> arguments and passed them to the os.system(), it worked. Now when I
> execute this command, in my normal DOS Windows, I must change to
> another path before I run the .exe, I don't know how to tell this to
> my python module. 

os.chrdir(path)

changes to a new path.

--

-- 
Steven D'Aprano
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Steven D'Aprano | 1 Oct 02:12 2010

Re: Doubly linked list

On Thu, 30 Sep 2010 11:24:06 pm T MURPHY wrote:
> I was having trouble, where on the python site can i find steps on
> classes 

Google is your friend: googling for "python tutorial" brings up lots of 
tutorials that will help. Here's the first one:

http://docs.python.org/tutorial/

> and making a doubly linked list, is there a help option for 
> linked lists.

Python doesn't supply a doubly-linked list, but it's easy to make your 
own. Here's a singly-linked list for you:

class Node(object):
    def __init__(self, data, next=None):
        self.data = data
        self.next = next

mylist = Node("a")
mylist.next = Node("b", Node("c"))

node = mylist
while node is not None:
    print node.data
    node = node.next

--

-- 
Steven D'Aprano
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Steven D'Aprano | 1 Oct 02:16 2010

Re: Printing prime numbers

On Thu, 30 Sep 2010 11:52:49 pm Emmanuel Ruellan wrote:
> On Thu, Sep 30, 2010 at 1:57 PM, <delegbede <at> dudupay.com> wrote:
> > 1 is prime
>
> One is /not/ prime. </pedant>

There's no need to apologize for pedantry. Saying that one is prime is 
like saying that 2 is an odd number, or that glass is a type of meat.

--

-- 
Steven D'Aprano
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Chris King | 1 Oct 02:24 2010
Picon

Mutable Properties

Dear Tutors,
    I noticed that when you make a property to represent a mutable value

class Obj(object):
        def get(self):
                print 'Get'
                return self.prop
        def set(self, new):
                print 'Set'
                self.prop = new
    prop = property(get, set)

test = Obj()
test.prop = ['Hello']

and then try and use one of its methods.

test.prop.append('World')

It will treat it as a get, not a set.

Output: Get

Even thou you are basically changing its value.

Before: ['Hello']
After: ['Hello', 'World']

I know this happens because I'm not technically setting it to something else, just getting one of its properties.
I was wondering how to make it fire off set to for certain methods.

Sincerely,
    Me
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Gmane