Skip to main content

Posts

Showing posts from 2015

Iterator

In Python, an iterator is an object that allows you to traverse through a collection of elements, such as lists, tuples, or dictionaries, one at a time. Iterators provide a way to access elements sequentially without needing to know the underlying implementation of the data structure. An iterator must implement two methods: 1. __iter__(): This method returns the iterator object itself. It is called when you use the `iter()` function on the iterator. 2. __next__() : This method returns the next item in the iterator. It is called repeatedly to fetch the next element in the sequence. When there are no more elements to return, it raises the ' StopIteration ' exception. Here's an example to illustrate how iterators work: class MyIterator:     def __init__(self, data):         self.data = data         self.index = 0     def __iter__(self):         return self     def __next__(self):         if self.index < len(self.data):             item = self.data[self.index]            

Date Time

Unix Timestamp Date Format Handling dateString = "UNIX_TIMESTAMP_DATE_FORMAT" datetime.datetime.fromtimestamp(int(str(dateString)[:10])).strftime('%Y-%m-%d') The  datetime  module is used for manipulating dates and times. If we want to use DateTime we need to first import in our file. Example: import datetime dt = datetime.datetime.now() print(dt) Output:  2024-03-27 21:41:12.717541 # get the current date currDt = datetime.date.today() print( currDt ) Output: 2024-03-27 # get the current year currDt = datetime.date.today() print( currDt.year ) Output: 2024 # get the current month print( currDt.month ) Output: 3 # get the current date only print( currDt.day ) Output: 27 # get the current day print( currDt .strftime("%A")) Output:  Wednesday