Page 1 of 1

### Dictionary for C struct?

Posted: Fri Feb 19, 2016 3:44 pm
I am writing a camera logger that captures images at a constant rate within a few fixed time intervals of the day, such as between 6:30:00AM and 6:35:00AM etc. I got one interval taken care of, using some variables:

Code: Select all

``````start_hour=6
start_minute=30
start_second=0
stop_hour=6
stop_minute=35
stop_second=00
``````
I just compare the current time with the above to determine whether I'm within the interval. What I'm trying to do is to expand to several intervals. In C, I would go ahead and define a struct like the following:

Code: Select all

``````typedef struct
{
int start_hour;
int start_minute;
int start_second;
int stop_hour;
int stop_minute;
int stop_second;
} intervals;

intervals my_intervals[10];``````
In python, I learned about dictionary with keywords and values. I'm not concerned with performance or speed so I'm thinking about using dictionary for convenience. I just don't have the skills to do dictionaries yet. How do I mimic the function of a struct array with dictionaries? Thanks.

### Re: Dictionary for C struct?

Posted: Fri Feb 19, 2016 4:43 pm
You can use a class instead of a struct and a list instead of an array.

Code: Select all

``````class data:
def __init__(self,start_hour,start_min,stop_hour,stop_min):
self.start_hour = start_hour
self.start_min = start_min
self.stop_hour = stop_hour
self.stop_min = stop_min

intervals = [data(6,30,6,35),data(7,0,7,30),data(7,30,7,35)]
``````
I have simplified a bit.

x = intervals[1].start_hour

should result in x = 7

### Re: Dictionary for C struct?

Posted: Fri Feb 19, 2016 6:11 pm
Thanks RogerW. That looks exactly like what I need. I'm behind on my python reading since there are so many aspects of the language. Different packages that do fancy things are dragging me different ways, images, dropbox, serial port etc.

So is the __init__ function the equivalence of a class constructor? Does the class need explicit destructor? The making of intervals list (?) is pretty convenient short hand. I'll remember to use it in my code.

### Re: Dictionary for C struct?

Posted: Fri Feb 19, 2016 9:51 pm
Different approach

Using an unsigned long that contains the date and time, in standard form (epoch time, seconds since 1/1/1970). Then comparing with < == > is trivial.
There are library routines to convert from Gregorian time to epoch time and vice-versa.
In C and also Python.

### Re: Dictionary for C struct?

Posted: Fri Feb 19, 2016 10:05 pm
Yes __init__ is a constructor. Destructors do exist but they do not work very well so tend not to be used. It is better to implement your own function and call it explicitly.

### Re: Dictionary for C struct?

Posted: Sat Feb 20, 2016 5:38 am
stevech wrote:Different approach

Using an unsigned long that contains the date and time, in standard form (epoch time, seconds since 1/1/1970). Then comparing with < == > is trivial.
There are library routines to convert from Gregorian time to epoch time and vice-versa.
In C and also Python.
Thanks. My intervals are repeated every day so the unix time will be quite awkward to use.

### Re: Dictionary for C struct?

Posted: Sat Feb 20, 2016 5:38 am
RogerW wrote:Yes __init__ is a constructor. Destructors do exist but they do not work very well so tend not to be used. It is better to implement your own function and call it explicitly.
Great! Thanks!