473,500 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python rrdtool try catch statement

20 New Member
Hi

I have a Python script that updates an rrd file with data it pulls from a database. Here is a snippet of the Python code
Expand|Select|Wrap|Line Numbers
  1. for i in range(numrowed):
  2.                 row = curred.fetchone()
  3.                 time_stamp = row[0]
  4.                 rx_max = row[1]
  5.  
  6.                 ret = rrdtool.update(ds_file.rrd,'%s:%s' %(time_stamp,rx_max));
  7.  
  8.                 if ret:
  9.                        print rrdtool.error()
  10.  
  11.           i = i + 1
My problem is that the script crashes anytime there is a duplicate time stamp because rrdtool allows only one value per timestamp. Thus i get this error

Traceback (most recent call last):
File "rrdfile_update.py", line 40, in <module>
ret = rrdtool.update(ds_file.rrd,'%s:%s' %(time_stamp,rx_max));
rrdtool.error: ds_file: illegal attempt to update using time 1363358441 when last update time is 1363358441 (minimum one second step)

I will appreciate very much if you can help me with a Python try catch statement to help skip this error and continue to update the rrd file with the next timestamp and value so the script does not stop.

Help very much appreciated. Thanks
Mar 18 '13 #1
3 3267
bvdet
2,851 Recognized Expert Moderator Specialist
Try/except blocks can catch any error by doing this:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     ...code that may raise an exception...
  3. except Exception, e:
  4.     print e
Generally it's better to catch specific errors such as your by checking for it:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     ...code that may raise an exception...
  3. except rrdtool.error, e:
  4.     print e
You can also issue a pass statement instead of printing the error
Mar 18 '13 #2
kaf3773
20 New Member
hi bvdet

Thanks a lot for your answer it worked and this is my final code
hope it helps others too
Expand|Select|Wrap|Line Numbers
  1. for i in range(numrowed):
  2.                 row = curred.fetchone()
  3.                 time_stamp = row[0]
  4.                 rx_max = row[1]
  5.                 try:
  6.                     ret = rrdtool.update(ds_file.rrd,'%s:%s' %(time_stamp,rx_max));
  7.                 except rrdtool.error, e:
  8.                     print e
  9.           i = i + 1
  10.  
  11.  
  12.  
Mar 18 '13 #3
dwblas
626 Recognized Expert Contributor
Assuming you want to use the first time stamp only, construct a set to contain time sets already processed and check against it.
Expand|Select|Wrap|Line Numbers
  1. all_times = set()
  2. for i in range(numrowed):
  3.     row = curred.fetchone()
  4.     time_stamp = row[0]
  5.     rx_max = row[1]
  6.  
  7.     if time_stamp not in all_times:
  8.         ret = rrdtool.update(ds_file.rrd,'%s:%s' %(time_stamp,rx_max))
  9.         all_times.add(time_stamp) 
Mar 18 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1815
by: John Max Skaller | last post by:
please reply to skaller@ozemail.com.au (I don't read this newsgroup, and the reply-to address is fake) For some time now -- over 5 years -- I have been using the interscript literate programming...
7
1387
by: Xah Lee | last post by:
# here's a while statement in python. a,b = 0,1 while b < 20: print b a,b = b,a+b --------------- # here's the same code in perl
1
1299
by: Xah Lee | last post by:
.. # here's an example of if statement in python. .. .. x=-1 .. if x<0: .. print 'neg' .. elif x==0: .. print 'zero' .. elif x==1: .. print 'one' .. else:
5
1576
by: Xah Lee | last post by:
© # this is an example of for statement © # the % symbol calculates the remainder © # of division. © # the range(m,n) function © # gives a list from m to n-1. © © a = range(1,51) © for x in a:...
2
6940
by: VM | last post by:
Is it possible to intercept all exception errors into one Try...Catch? I've tried wrapping Application.Run() in a Try...Catch, but it doesn't seem to work all the time. Thanks.
4
1349
by: Steve | last post by:
Hi All In VB6 I used to catch exceptions in a goto errtrap call then resume if I could handle the problem and continue within the routine or function I am not sure how to do this in VB 2005...
5
1822
by: Back 9 | last post by:
I try to insert try/catch statement in every single method of my c# code to prevent the app from crashing not nicely. Do you think it is good idea? I remember that an artical says " do not overuse...
2
1997
by: Stimp | last post by:
I have the following Try, Catch, Finally block... Dim objConn As SqlConnection = New SqlConnection(ConfigurationSettings. AppSettings("conn")) objConn.Open() Try Dim strSQL As String =...
2
2190
by: AnnetteB | last post by:
I have an action performed on the click of a button which basically opens up a text file based on the textfield input of the user. I would like to use an if statement to say if the variable already...
3
1842
by: Bernie | last post by:
Hi is it possible to receive information like type of exception (division by zero/null pointer/...), memory adress or source line number in a catch(...) statement? main() { try {
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.