Пример #1
0
def write_state_lines(start_time, end_time, fp):
    y = int(start_time[:4])
    m = int(start_time[4:6])
    d = int(start_time[6:8])

    ey = int(end_time[:4])
    em = int(end_time[4:6])
    ed = int(end_time[6:8])

    while (y < ey):
        while (m <= 12):
            num = utils.days_in_month(y, m)
            while (d <= num):
                str = "%d%02d%02d" % (y, m, d)
                fp.write(str + " unassigned" + '\n')
                d = d + 1
            d = 1
            m = m + 1
        m = 1
        y = y + 1

    while (m < em):
        num = utils.days_in_month(ey, m)
        while (d <= num):
            str = "%d%02d%02d" % (y, m, d)
            fp.write(str + " unassigned" + '\n')
            d = d + 1
        d = 1
        m = m + 1

    while (d <= ed):
        str = "%d%02d%02d" % (y, m, d)
        fp.write(str + " unassigned" + '\n')
        d = d + 1

    fp.close()
Пример #2
0
 def get_latest_month(self):
     dates = self.get_date_list()
     if dates:
         y = dates[-1][0]
         m = dates[-1][1]
         return datetime.datetime(y, m, days_in_month(m, y))
Пример #3
0
 def get_latest_month(self):
     dates = self.get_date_list()
     if dates:
         y = dates[-1][0]
         m = dates[-1][1]
         return datetime.datetime(y, m, days_in_month(m, y))
Пример #4
0
========================

You are given the following information, but you may prefer to do some
research for yourself.

  * 1 Jan 1900 was a Monday.
  * Thirty days has September,
    April, June and November.
    All the rest have thirty-one,
    Saving February alone,
    Which has twenty-eight, rain or shine.
    And on leap years, twenty-nine.
  * A leap year occurs on any year evenly divisible by 4, but not on a
    century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth
century (1 Jan 1901 to 31 Dec 2000)?
"""

from utils import MONTHS
from utils import  days_in_month

total_days = 1 # the first day, Jan 1, 1900 is a monday.
lonely_mondays = 0
for y in range(1900, 2001):
  for m in MONTHS:
    total_days += days_in_month(m, y)
    if y>1900 and total_days%7==1:
        lonely_mondays += 1

print lonely_mondays
Пример #5
0
    def read_header(self):
        """
        Read the header of a .wea file and save in self.header.
        """
        if self.header:
            return self.header

        if not hasattr(self, 'fd'):
            self._open()
        tr = self._get_short()  # Always 1 ?
        # pr = float Total minutes in this file
        # oi = short Observation Interval (in minutes)
        # ne = short Number of Elements
        # rgt = short Rain Gauge Type
        # wsh = short Wind Speed Height
        pr, oi, ne, rgt, wsh = self._do_unpack("f4h", 4 + 2 * 4)

        # BUG FIX. Recalculate pr manually
        year, month = self.yearmonth()
        pr = days_in_month(month, year) * 24 * 60

        # Read 8 shorts (unused placeholders)
        self._do_unpack("8h", 2 * 8)

        # Read pc based on ne
        # Read 3 1-byte chars per element
        pc = ''.join(self._do_unpack("%dc" % ne * 3, ne * 3))
        # Put pcodes in a tuple
        pcodes = []
        for i in range(0, len(pc), 3):
            pcodes.append(pc[i:i + 3])
        pcodes = tuple(pcodes)

        # Set observation factors
        if oi == 1440:
            fac1 = 1
            fac2 = 24
        if oi == 360:
            fac1 = 1
            fac2 = 6
        if oi == 240:
            fac1 = 1
            fac2 = 4
        if oi == 60:
            fac1 = 1
            fac2 = 1
        if oi == 30:
            fac1 = 2
            fac2 = 1
        if oi == 20:
            fac1 = 3
            fac2 = 1
        if oi == 15:
            fac1 = 4
            fac2 = 1
        if oi == 10:
            fac1 = 6
            fac2 = 1
        if oi == 5:
            fac1 = 12
            fac2 = 1
        if oi == 2:
            fac1 = 30
            fac2 = 1
        if oi == 1:
            fac1 = 60
            fac2 = 1

        self.header = {
            'tr': tr,
            'pr': pr,
            'oi': oi,
            'ne': ne,
            'rgt': rgt,
            'wsh': wsh,
            'ne': ne,
            'pcodes': pcodes,
            'fac1': fac1,
            'fac2': fac2,
        }

        return self.header
Пример #6
0
    def read_header(self):
        """
        Read the header of a .wea file and save in self.header.
        """
        if self.header:
            return self.header

        if not hasattr(self, "fd"):
            self._open()
        tr = self._get_short()  # Always 1 ?
        # pr = float Total minutes in this file
        # oi = short Observation Interval (in minutes)
        # ne = short Number of Elements
        # rgt = short Rain Gauge Type
        # wsh = short Wind Speed Height
        pr, oi, ne, rgt, wsh = self._do_unpack("f4h", 4 + 2 * 4)

        # BUG FIX. Recalculate pr manually
        year, month = self.yearmonth()
        pr = days_in_month(month, year) * 24 * 60

        # Read 8 shorts (unused placeholders)
        self._do_unpack("8h", 2 * 8)

        # Read pc based on ne
        # Read 3 1-byte chars per element
        pc = "".join(self._do_unpack("%dc" % ne * 3, ne * 3))
        # Put pcodes in a tuple
        pcodes = []
        for i in range(0, len(pc), 3):
            pcodes.append(pc[i : i + 3])
        pcodes = tuple(pcodes)

        # Set observation factors
        if oi == 1440:
            fac1 = 1
            fac2 = 24
        if oi == 360:
            fac1 = 1
            fac2 = 6
        if oi == 240:
            fac1 = 1
            fac2 = 4
        if oi == 60:
            fac1 = 1
            fac2 = 1
        if oi == 30:
            fac1 = 2
            fac2 = 1
        if oi == 20:
            fac1 = 3
            fac2 = 1
        if oi == 15:
            fac1 = 4
            fac2 = 1
        if oi == 10:
            fac1 = 6
            fac2 = 1
        if oi == 5:
            fac1 = 12
            fac2 = 1
        if oi == 2:
            fac1 = 30
            fac2 = 1
        if oi == 1:
            fac1 = 60
            fac2 = 1

        self.header = {
            "tr": tr,
            "pr": pr,
            "oi": oi,
            "ne": ne,
            "rgt": rgt,
            "wsh": wsh,
            "ne": ne,
            "pcodes": pcodes,
            "fac1": fac1,
            "fac2": fac2,
        }

        return self.header