Home > OS >  What would be the best approach to creating a function that does the summing of 3-day sales transact
What would be the best approach to creating a function that does the summing of 3-day sales transact

Time:11-30

with open('POS.csv') as csvfile:
    my_reader = csv.reader(csvfile, delimiter=',')
    fricnt = 0
    satcnt = 0
    suncnt = 0
    sun = 0
    door = 0
    hdr = []
    sales = 0
    frisales = 0
    satsales = 0
    sunsales = 0
    total = 0
    totcnt = 0
    for row in my_reader:
        if door == 0:
            hdr.append(row)
            door = 1
        elif row[21] == 'Friday':
            frisales = frisales   float(row[11])
            fricnt  = 1
        elif row[21] == 'Saturday':
            satsales = satsales   float(row[11])
            satcnt  = 1
        elif row[21] == 'Sunday':
            sunsales = sunsales   float(row[11])
            suncnt  = 1
        total = frisales   satsales   sunsales
        totcnt = fricnt   satcnt   suncnt
print('3-day Total Sales:', total, '3-day Average Sale:', total/totcnt)

I am trying to create a function that does the summing of 3-day sales amount transactions and counting of 3-day sales transactions. I know this should be simple but I am new to programming and could use some assistance.

CodePudding user response:

Don't compute your totals until the whole file is finished. And you can use a dictionary to track your sales by day.

with open('POS.csv') as csvfile:
    my_reader = csv.reader(csvfile)
    door = 0
    hdr = []
    sales = {
        'Friday': 0,
        'Saturday': 0,
        'Sunday': 0
    }
    counts = {
        'Friday': 0,
        'Saturday': 0,
        'Sunday': 0
    }
    for row in my_reader:
        if door == 0:
            hdr.append(row)
            door = 1
        elif row[21] in sales:
            sales[row[21]]  = float(row[11])
            counts[row[21]]  = 1
total = sales['Friday'] sales['Saturday'] sales['Sunday']
totcnt = counts['Friday'] counts['Saturday'] counts['Sunday']
print('3-day Total Sales:', total, '3-day Average Sale:', total/totcnt)

Although now that I'm thinking about it, if you don't need the day totals separately, there's no need to keep three separate subtotals. Just pile them all into one variable.

CodePudding user response:

consider using convtools library, which provides lots of data processing primitives:

from convtools import conversion as c
from convtools.contrib.tables import Table


rows = (
    Table.from_csv("POS.csv", header=False)
    .update(COLUMN_11=c.col("COLUMN_11").as_type(float))
    .filter(c.col("COLUMN_21").in_({"Friday", "Saturday", "Sunday"}))
    .into_iter_rows(list)
)

# this is where code generation happens, store for further reuse if needed
converter = c.aggregate(
    {
        "total": c.ReduceFuncs.Sum(c.item(11)),
        "average": c.ReduceFuncs.Average(c.item(11)),
    }
).gen_converter()

converter(rows)

if day by day statistics is needed, you could omit filtering by days of week and process like this:

converter = c.aggregate(
    {
        "day_to_sales": c.ReduceFuncs.DictSum(c.item(21), c.item(11)),
        "day_to_count": c.ReduceFuncs.DictCount(c.item(21), c.item(11)),
    }
).gen_converter()

Here day_to_sales would be a dict, where keys are days of week and values would be sums of sales.

Also, since you are working with money, consider using decimal.Decimal

  • Related