~mrp/time_computer

24174ab80205f277753e64a64379ec87e976b4a5 — Mark Penner 3 years ago 4b77491
start rewriting tc as tc.py, add test_tc.py, remove tc.cpp because it's unnecessary
3 files changed, 161 insertions(+), 266 deletions(-)

D tc.cpp
R tc => tc.py
A test_tc.py
D tc.cpp => tc.cpp +0 -135
@@ 1,135 0,0 @@
/*
  Name: time-clock
  Copyright: Public Domain
  Author: Mark Penner
  Date: apr-8-2010 16:02 cdt
  Description: figures hours
  Version: 0.0.1
  Warranty: none
*/

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

#define size 1024

struct jbs
       {
       char job[16];
       float hrs;
       };
       
struct rec
    {
     char job[16];
     float inh1;
     float inm1;
     float outh1;
     float outm1;
     float inh2;
     float inm2;
     float outh2;
     float outm2;
    };
    
int main(int argc, char *argv[])
{
    cout<<setiosflags(ios::showpoint|ios::fixed)<<setprecision(3);
    int lc1=0, lc2=0;
    rec Data[size];
    jbs jobs[16];
    for(lc1=0;lc1<16;lc1++) jobs[lc1].job[0]='\0';
    for(lc1=0;lc1<size;lc1++){Data[lc1].job[0]='\0';}
    char wst='\0';
 	char FName[128];
    ifstream inFile;
  	ofstream outFile;
 	float tothrs=0;
  	// end declarations
  	// begin filetorec
     cout<<"File Name:\n";
     cin>>FName;
     
     inFile.open(FName);
     if (inFile.fail())
       {
   	   cerr<<"Error opening input file\n";
	   system("PAUSE");
	   return EXIT_SUCCESS;
	   }
     else
 	{
      lc1=0;
	 while(inFile.peek()!=EOF&&lc1<=size-1)//loop until end of file or array, whichever comes first
	 { cout<<"file line "<<lc1+1<<'\n';
	  lc2=0;
  	  while (inFile.peek()!=',') inFile>>Data[lc1].job[lc2++]; 
  	  if(inFile.peek()==',') inFile>>wst;
      while (inFile.peek()!=',') inFile>>wst;//move file pointer past date
	  if(inFile.peek()==',') inFile>>wst;
       
  	  inFile>>Data[lc1].inh1>>wst>>Data[lc1].inm1>>
              Data[lc1].outh1>>wst>>Data[lc1].outm1>>
              Data[lc1].inh2>>wst>>Data[lc1].inm2>>
              Data[lc1].outh2>>wst>>Data[lc1].outm2;
              
       if(inFile.peek()=='\'')//for comments
		{while (inFile.peek()!=';')inFile>>wst;}// move file pointer to end of comment

       inFile>>wst;// move past end of line character (;)
       if(inFile.eof())break;
	  lc1++;
      } 
      inFile.close();
      cout<<"input file closed\n";
     }
 	//end filetorec
 	// begin dada
 	lc1=0;
 	while(Data[lc1].inh1!=0)
 	{ cout<<"fig "<<lc1+1<<'\n';
 	lc2=0;
 	while(strcmp(Data[lc1].job,jobs[lc2].job)&&jobs[lc2].job[0]!='\0'&&lc2<15)
           lc2++;
    if(strcmp(Data[lc1].job,jobs[lc2].job))
          strcpy(jobs[lc2].job,Data[lc1].job);
    jobs[lc2].hrs+=(Data[lc1].outh2+Data[lc1].outm2/60)-
    (Data[lc1].inh2+Data[lc1].inm2/60)+
    (Data[lc1].outh1+Data[lc1].outm1/60)-
    (Data[lc1].inh1+Data[lc1].inm1/60);
    lc1++;
    }
    lc1=0;
    while(Data[lc1].inh1!=0)
      {
      tothrs+=(Data[lc1].outh2+Data[lc1].outm2/60)-
      (Data[lc1].inh2+Data[lc1].inm2/60)+
      (Data[lc1].outh1+Data[lc1].outm1/60)-
      (Data[lc1].inh1+Data[lc1].inm1/60);
      lc1++;
      }
    cout<<"done figuring\n";
	// end dada
	//begin output
	strcat(FName,".tcd");
  	cout<<"saving "<<FName<<" . . .\n";
  	outFile.open(FName);
  	if (outFile.fail())
  	cerr<<"Error making output file\n";
  	else
 	{
  	 outFile<<"total hrs. = "<<tothrs<<endl;
  	 lc1=0;
  	 while(jobs[lc1].job[0]!='\0')
  	     {outFile<<jobs[lc1].job<<" hrs. = "<<jobs[lc1++].hrs<<endl;}
    }
    cout<<FName<<" saved.\n";
    // end output
    system("PAUSE");
    return EXIT_SUCCESS;
}

R tc => tc.py +142 -131
@@ 9,137 9,148 @@
import sys
import datetime

print("time computer")
print("version 2020-03")
print("        ____")
print("       /    \\")
print("      /  |   \\")
print("     |   |    |")
print("      \   \  /")
print("       \____/")

try:
    FName = sys.argv[1]
except IndexError:
    FName = input("File Name: ")
infile = open(FName, "r")

minutes = [[None, 0]]
weeks = [[None, 0]]
days = [[None, 0]]
dates = []
totalminutes = 0
linenum = 1
try:
    while True:
        linebuffer = []
        try:
            year = int(infile.read(4))
        except ValueError:
            break
        month = int(infile.read(2))
        day = int(infile.read(3))
        d = datetime.date(year, month, day)

        # read job name (random length)
        name = ""
        while True:
            x = infile.read(1)
            if x == " ":
                break
            name = name + x
        linebuffer.append(name)

        while True:
            x = int(infile.read(2))
            linebuffer.append(x)
            if infile.read(1) == "\n":
                break

        # figure hours for one clock-in/out
        x = 0
        timeIndex = 1
if __name__ == '__main__':
    print("time computer")
    print("version 2020-03")
    print("        ____")
    print("       /    \\")
    print("      /  |   \\")
    print("     |   |    |")
    print("      \\   \\  /")
    print("       \\____/")
    
    try:
        FName = sys.argv[1]
    except IndexError:
        FName = input("File Name: ")
    try:
        infile = open(FName, "r")
    except FileNotFoundError:
        print("file not found, exiting")
        quit()
    
    minutes = [[None, 0]]
    weeks = [[None, 0]]
    days = [[None, 0]]
    dates = []
    totalminutes = 0
    linenum = 1
    try:
        while True:
            linebuffer = []
            try:
                x += (linebuffer[timeIndex + 2] * 60 + linebuffer[timeIndex + 3]) - (
                    linebuffer[timeIndex] * 60 + linebuffer[timeIndex + 1]
                )
            except IndexError:
                year = int(infile.read(4))
            except ValueError:
                break
            timeIndex += 4  # go to next in/out

        # add minutes to right job
        jobIndex = 0
        while (
            linebuffer[0] != minutes[jobIndex][0] and minutes[jobIndex][0]
        ):  # check if current job matches previous
            jobIndex += 1

        if linebuffer[0] != minutes[jobIndex][0]:  # if no match append jobname
            minutes.append([None, 0])
            minutes[jobIndex][0] = linebuffer[0]
        minutes[jobIndex][1] += x

        # add minutes to right week
        weekIndex = 0
        ic = d.isocalendar()
        while ic[1] != weeks[weekIndex][0] and weeks[weekIndex][0]:
            weekIndex += 1
        if ic[1] != weeks[weekIndex][0]:
            weeks.append([None, 0])
            weeks[weekIndex][0] = ic[1]
        weeks[weekIndex][1] += x

        # add minutes to right day
        dayIndex = 0
        while d != days[dayIndex][0] and days[dayIndex][0]:
            dayIndex += 1
        if d != days[dayIndex][0]:
            days.append([None, 0])
            days[dayIndex][0] = d
        days[dayIndex][1] += x

        totalminutes += x
        linenum += 1
except:
    print("error on line", linenum)
finally:
    infile.close()

print("\nhours by day")
x = 0
while days[x][0]:
    print(str(days[x][0]) + ":", round(days[x][1] / 60, 2))
    x += 1

print("\nhours by job")
x = 0
while minutes[x][0]:
    print(minutes[x][0] + ":", round(minutes[x][1] / 60, 2))
    x += 1

print("\nhours by week")
stdweek = 40
regular_time = 0
overtime = 0
x = 0
while weeks[x][0]:
    if weeks[x][1] / 60 <= stdweek:
        print("week", str(weeks[x][0]) + ":", round(weeks[x][1] / 60, 2))
        regular_time += weeks[x][1] / 60
    else:
        print(
            "week",
            str(weeks[x][0]) + ":",
            stdweek,
            "overtime:",
            round(weeks[x][1] / 60 - stdweek, 2),
        )
        regular_time += stdweek
        overtime += weeks[x][1] / 60 - stdweek
    x += 1

print("\nregular hours:", round(regular_time, 2))
print("overtime:", round(overtime, 2))

print("\ntotal hours:", round(totalminutes / 60, 2), "\n")
            month = int(infile.read(2))
            day = int(infile.read(3))
            d = datetime.date(year, month, day)
    
            # read job name (random length)
            name = ""
            while True:
                x = infile.read(1)
                if x == " ":
                    break
                name = name + x
            linebuffer.append(name)
    
            while True:
                x = int(infile.read(2))
                linebuffer.append(x)
                if infile.read(1) == "\n":
                    break
    
            # figure hours for one clock-in/out
            x = 0
            timeIndex = 1
            while True:
                try:
                    x += (linebuffer[timeIndex + 2] * 60 + linebuffer[timeIndex + 3]) - (
                        linebuffer[timeIndex] * 60 + linebuffer[timeIndex + 1]
                    )
                except IndexError:
                    break
                timeIndex += 4  # go to next in/out
    
            # add minutes to right job
            jobIndex = 0
            while (
                linebuffer[0] != minutes[jobIndex][0] and minutes[jobIndex][0]
            ):  # check if current job matches previous
                jobIndex += 1
    
            if linebuffer[0] != minutes[jobIndex][0]:  # if no match append jobname
                minutes.append([None, 0])
                minutes[jobIndex][0] = linebuffer[0]
            minutes[jobIndex][1] += x
    
            # add minutes to right week
            weekIndex = 0
            ic = d.isocalendar()
            while ic[1] != weeks[weekIndex][0] and weeks[weekIndex][0]:
                weekIndex += 1
            if ic[1] != weeks[weekIndex][0]:
                weeks.append([None, 0])
                weeks[weekIndex][0] = ic[1]
            weeks[weekIndex][1] += x
    
            # add minutes to right day
            dayIndex = 0
            while d != days[dayIndex][0] and days[dayIndex][0]:
                dayIndex += 1
            if d != days[dayIndex][0]:
                days.append([None, 0])
                days[dayIndex][0] = d
            days[dayIndex][1] += x
    
            totalminutes += x
            linenum += 1
    except:
        print("error on line", linenum)
    finally:
        infile.close()
    
    print("\nhours by day")
    x = 0
    while days[x][0]:
        print(str(days[x][0]) + ":", round(days[x][1] / 60, 2))
        x += 1
    
    print("\nhours by job")
    x = 0
    while minutes[x][0]:
        print(minutes[x][0] + ":", round(minutes[x][1] / 60, 2))
        x += 1
    
    print("\nhours by week")
    stdweek = 40
    regular_time = 0
    overtime = 0
    x = 0
    while weeks[x][0]:
        if weeks[x][1] / 60 <= stdweek:
            print("week", str(weeks[x][0]) + ":", round(weeks[x][1] / 60, 2))
            regular_time += weeks[x][1] / 60
        else:
            print(
                "week",
                str(weeks[x][0]) + ":",
                stdweek,
                "overtime:",
                round(weeks[x][1] / 60 - stdweek, 2),
            )
            regular_time += stdweek
            overtime += weeks[x][1] / 60 - stdweek
        x += 1
    
    print("\nregular hours:", round(regular_time, 2))
    print("overtime:", round(overtime, 2))
    
    print("\ntotal hours:", round(totalminutes / 60, 2), "\n")

def parse(line):
    """
    parse a line in the input file
    """
    return []

A test_tc.py => test_tc.py +19 -0
@@ 0,0 1,19 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
tests for Time Computer

MIT License
mrpenner
"""

import unittest
tc = __import__("tc")

class Tests(unittest.TestCase):
    
    def test_parse(self):
        self.assertEqual(tc.parse([]), [], "parse a line in file")
        
if __name__ == '__main__':
    unittest.main()