Python Forum
Total electric construction cost project
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Total electric construction cost project
#1
Hey guys! I've started learning Python on my own and am looking to start building real world projects in order to boost my understanding of how things tie together. I currently work at an electric coop where we provide electric service to residential and commercial customers. The first project I'm currently working on is a program that will contain a GUI where construction materials will be selected/input and a total price will be calculated.

I brainstormed ways on how to create a price sheet from which the individual components will be taken and a total price generated. Creating and importing a module with a function for each component of the construction project along with the price per unit made the most sense to me.

My train of though was that I should start with creating the price sheet as a module, then move on to creating a GUI that will selectivly use only the appropriate functions within the module, then creating some kind of an equation at the end and tying everything together to spit out a grand total. I'm very new to Python but extremely excited to learn! I need to know if I'm on the right track or if anybody has ideas on how to go about this a better more efficient way! Any help would be greatly appreciated!!!!

Below you'll find my incomplete module so you guys can see what I have so far. Thanks again!!!!!

#thirty foot pole
def thirtyftpole(treiftpl):
    return 30*treiftpl

#forty foot pole
def fortyftpole(quarftpole):
    return 40*quarftpole

#fifty foot pole
def fiftyftpole(cinqftpole):
    return 50*cinqftpole

#sidewalk guy wire assembly
def ve12SWG(sg):
    return 35*sg

#overhead guy wire assembly
def ve22(ohg):
    return 25*ohg

#security light assembly, add if new construction then no charge, system imp will result in  charge
def vm265(scl):
    return 60*scl

#made up code, LED security light 
def vm777(lscl):
    return 100*lscl

#made up code, wood cross arm
def vc7w(wxa):
    return 250*wxa

#fiberglass cross arm
def vc8fg(fgxa):
    return 400*fgxa

#arrestors
def vm56(arts):
    return 4*arts

#cutout
def vm50(cut):
    return 30*cut

#I210+ meter
def vm8(srm):
    return 125*srm

#cogen meter, made up code
def qm8(cgm):
    return 320*cgm

#overhead conductor - primary - in $/ft where ohcp = 1 foot
def overheadcp(ohcp):
    return 1.5*ohcp

#overhead conductur - secondary
def overheadcs(ohcs):
    return 1*ohcs

#underground conductor - primary
def undergroundcp(urdcp):
    return 30*urdcp

#underground conductor - secondary - max 175ft, what happens after this? must be primary or gets more expensive???
def undergroundcs(urdcs):
    return 15*urdcs

#riser, only if urd srv
def riser(riz):
    45*riz

#10KVA xfmr
def tenKVA(dzkva):
    250*dzkva
#15KVA xfmr
def fifteenKVA(qckva):
    return 350*qckva

#25KVA xfmr
def twentyfiveKVA(twfkva):
    return 450*twfkva
Python Version: 3.9.5
Larz60+ write Jul-20-2021, 10:37 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Please avoid images of code, rather use bbcode tags.
Reply
#2
Making a function for each item is not an efficient way of doing it.
It would be better to create a new object with the necessary attributes to represent a component.
The component attributes can be loaded from/to your choice of storage ie database, file etc, into the object that represents them.
This would create a list of available components that can be displayed in the GUI.
Sorry, I'm out of time for explaining now, here is some example code.
from collections import Counter
from dataclasses import dataclass, field
from decimal import Decimal


def decimal_2_palces(decimal: Decimal) -> Decimal:
    """Returns the given decimal to 2 decimal places"""
    return decimal.quantize(Decimal('.01'))


@dataclass(frozen=True)
class Component:
    """Basic representaion of a component"""

    name: str
    price: Decimal


components = [Component('some item', Decimal(1.4)),
              Component('another item', Decimal(3.2))]


@dataclass
class PriceSheet:
    """Keeps a count of components"""

    component_counter: Counter = field(default_factory=Counter, init=False)

    def add_component(self, componet: Component, component_qty: int) -> None:
        """Add a component to the price sheet"""
        self.component_counter[componet] += component_qty

    def remove_component(self, componet: Component, component_qty: int) -> None:
        """Remove a component from the price sheet"""
        self.component_counter[componet] -= component_qty
        if self.component_counter[componet] <= 0:
            del self.component_counter[componet]

    @property
    def total_price(self) -> Decimal:
        """Calculates the total price of all the components"""
        result = sum(component.price * quantity for component, quantity in
                     self.component_counter.items())
        return decimal_2_palces(result)

    def component_totals(self) -> str:
        """Returns an itemized price sheet"""
        bill_lines = []
        for component, component_qty in self.component_counter.items():
            price = decimal_2_palces(component.price)
            total_price = decimal_2_palces(component.price * component_qty)
            bill_lines.append(
                f'{component.name:20} {price} * {component_qty} = {total_price}')
        bill_lines.append(f'Total = {self.total_price}')
        return '\n'.join(bill_lines)


price_sheet = PriceSheet()
price_sheet.add_component(components[0], 3)
price_sheet.add_component(components[1], 3)
price_sheet.add_component(components[1], 2)
price_sheet.remove_component(components[0], 1)
print(price_sheet.component_totals())
Output:
some item 1.40 * 2 = 2.80 another item 3.20 * 5 = 16.00 Total = 18.80
Larz60+ likes this post
Reply
#3
Since this is your first project I suggest starting with the user interface and then adding the business logic. To start with don't worry about making the controls do anything. Just lay them out and use the application as an interactive storyboard to find out if it supports working the way you want to work.

Once you have a the controls you need to do your work, figure out what you want to happen "behind the scenes" when each control is activated. Start out writing this in pseudo-code. Look for ways where you can introduce errors. If your pseudo-code reveals a required order of operations, how are you going to enforce that order? Or maybe you can rethink how the task is done to remove the order restriction.

When you think your pseudo-code will produce the desired results, translate the pseudo-code to Python. For experienced programmers this is the simplest part. For you it will be the hardest.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculate and print the total cost isha_dx 7 11,607 Feb-10-2021, 04:56 PM
Last Post: nilamo
  Electric Car battery size updating ???? ridgerunnersjw 1 1,794 Sep-06-2019, 02:03 AM
Last Post: metulburr
  Reproducing assignment with min cost flow in networkx mntfr 0 2,128 Jun-13-2019, 04:06 PM
Last Post: mntfr
  Writing device driver to stop electric supply to any I/O port sumandas89 0 1,793 May-02-2019, 10:22 AM
Last Post: sumandas89
  I'm blocked in the construction of my program [Novice] abcd 1 2,625 May-22-2018, 06:02 AM
Last Post: buran
  Class construction in python Alberto 3 4,400 Jul-18-2017, 03:37 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020