Run process with timeout

2005-10-17 Thread Natan
Hi.

I have a python script under linux where I poll many hundreds of
interfaces with mrtg every 5 minutes. Today I create some threads and
use os.system(command) to run the process, but some of them just hang.
I would like to terminate the process after 15 seconds if it doesn't
finish, but os.system() doesn't have any timeout parameter.

Can anyone help me on what can I use to do this?

Thank you

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


Feature suggestion: math.zod for painlessly avoiding ZeroDivisionError

2011-04-11 Thread Natan Yellin
Hey everyone,
This is my first posting to python-list, so be gentle.

I propose the following function for the math module (which can, of course,
be rewritten in C):

>  zod = lambda a, b: b and a / b


zod, the zero or divide function, is useful for division where the
denominator can be 0. For example, here's one line of code

> stat = x / y


If y can be zero, that one-liner needs to be rewritten as:

> if y != 0:
> stat = x / y
> else:
> stat = 0

...which is 4 lines of code for simple division!


Using zod, the zero or divide function, we can write:

> stat = zod(x,y)


I've encountered this issue before, but I don't know how common it is. Let
me know!
Would you use zod? Or do you need ood (one or divide), in which case lets
forget this altogether!

Natan

--
blog: http://natanyellin.com
<http://natanyellin.com/>twitter: @aantn <http://twitter.com/aantn>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Feature suggestion: math.zod for painlessly avoiding ZeroDivisionError

2011-04-11 Thread Natan Yellin
On Mon, Apr 11, 2011 at 5:20 PM, Chris Angelico  wrote:

> On Tue, Apr 12, 2011 at 12:10 AM, Natan Yellin  wrote:
> > Hey everyone,
> > This is my first posting to python-list, so be gentle.
> > I propose the following function for the math module (which can, of
> course,
> > be rewritten in C):
> >>
> >>  zod = lambda a, b: b and a / b
> >
> > If y can be zero, that one-liner needs to be rewritten as:
> >>
> >> if y != 0:
> >> stat = x / y
> >> else:
> >> stat = 0
>
> You can optimize that the same way as your zod function:
> stat = y and x/y

I'm dealing with long-named variables inside of dictionaries, which makes
that impractical.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: CodeAcademy Python Tip Calculator

2017-09-10 Thread Emil Natan
To see output you should use function that prints to the output, for
example print(). You also do not calculate correctly the tax and tip, it is
percentage from the meal cost, so the tax to be added to the total meal
cost is meal * tax / 100.

meal = 44.50
tax = 6.75
tip = 15.0

tax_amount = meal * tax / 100
tip_amount = meal * tip / 100

print('Meal total: %.2f' % (meal + tax_amount + tip_amount))

If I remember correctly, you receive the above values as input to your
script. Use the input() and float() functions to accept value as input and
convert it to float.


On Mon, Sep 11, 2017 at 7:51 AM, Cai Gengyang  wrote:

> So, I’m on section (3. The Tip) …
>
> Instructions
>
> 1.
> Set the variable tip to decimal value of 15% on line 5.
>
> This was my input:
> You’re almost there! Assign the tip variable on line 5.
> meal = 44.50
> tax = 6.75 / 100
> tip = 15.0
>
> But, when I tried to run the program, I don’t get any output at all. Nada,
> nothing zilch. Nothing happens, how come ?
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: 2.6.7: Does socket.gethostbyaddr truncate?

2018-02-04 Thread Emil Natan
On Sat, Feb 3, 2018 at 1:11 PM, Peter J. Holzer  wrote:

> On 2018-01-30 08:56:16 -0800, Dan Stromberg wrote:
> > dig -x should return a single PTR in all cases, shouldn't it?
>
> No. dig -x should return *all* PTR records. There is usually at most one
> of them, but there may be several. (46 seems a bit much, but there
> really isn't any limit).
>
> > What IP are you using?
>
> Yup. I want to see an address with 46 PTR records, too ;-).
>
> > On Tue, Jan 30, 2018 at 4:05 AM, Antoon Pardon 
> wrote:
> > > I am using python 2.6.7 to do a little network programming, but it
> seems I don't
> > > get all the results.
> > >
> > > When I call socket.gethostbyaddr(IP) entry [1] of the result is a list
> of 34 addresses.
>
> gethostbyaddr just calls the underlying C library function. It is
> possibly that this has a limit (either on the number of names or more
> likely on the packet size).
>
> hp
>
> --
>_  | Peter J. Holzer| we build much bigger, better disasters now
> |_|_) || because we have much more sophisticated
> | |   | [email protected] | management tools.
> __/   | http://www.hjp.at/ | -- Ross Anderson 
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>
I would guess it's the packet size limitation. Using EDNS0 you can have
packet size of up 4096 bytes, but it's the underlying IP protocol which
limits further the amount of data in a single packet. Given the 20 bytes
IPv4 header and 8 bytes DNS header, that leaves 1472 bytes for data. Bigger
payload leads to IP fragmentation which in many cases is blocked by
firewalls or other network devices on the way.

In your case dig -x runs on the same machine where the Python code runs? If
yes, then it's not network issue.

What's the size of the response containing 34 addresses? What's the size of
the response from dig when all 46 addresses are returned?

Emil
-- 
https://mail.python.org/mailman/listinfo/python-list


return from function

2015-12-21 Thread Emil Natan
I'm completely new to Python.
I have the following function to find the parent for domain. It removes the
left most label from the name and then checks if SOA record exists for the
reminder, if not it calls itself recursively removing another label and
checking again for SOA record. It works well for 'co.uk' and 'amazon.co.uk'
for example. It does not return the expected value 'uk' when invoked for '
amazon1.co1.uk', though the print command before the return prints what is
expected. Can someone explain why? Thanks.

>>> find_parent_domain('amazon.co1.uk.')
Test for parent domain co1.uk.
NXDOMAIN: invoke find_parent_domain recursively
Test for parent domain uk.
the parent domain we use is: uk.
>>>

import dns.resolver
from dns.exception import DNSException

def find_parent_domain(domainname):

if domainname == '.':
parent_domain = None
return parent_domain

parent_domain = domainname.partition('.')[2]
try:
print('Test for parent domain %s' % parent_domain)
z = dns.resolver.query(parent_domain, 'SOA')
print('the parent domain we use is: %s' % parent_domain)
return parent_domain
except dns.resolver.NXDOMAIN:
print('NXDOMAIN: invoke find_parent_domain recursively')
find_parent_domain(parent_domain)
except dns.resolver.NoAnswer:
print('NoAnswer: invoke find_parent_domain recursively')
find_parent_domain(parent_domain)
-- 
https://mail.python.org/mailman/listinfo/python-list


Retreiving objects from other processes

2008-02-06 Thread Natan Yellin
Hello,
Sorry if this is a stupid question... I have some experience with C
but very little with Python.
I'd like to have one python program retrieve a reference or copy of an
object from another python process on the same computer. I know I can
use RPyC, but that seems like overkill. Is there simpler way of doing
it?
Thanks in advance,
Natan
-- 
http://mail.python.org/mailman/listinfo/python-list