Python Forum

Full Version: Better structure than this?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
After receiving some feedback on past posts, I have begun re-designing my code to be more object oriented. It's going great and is really fun finding the best ways to fit all the pieces, but I have come across an issue that is a nuisance, but not detrimental. I have a function that creates a 'description' for the data frame. The description is comprised of 4 sections:

Date
Item
Name
SaleID

The sections are then concatenated and inserted into the InvoiceDataFrame.

The arguments that this function calls for are plenty due to needing a variety of information to create and merge the description into the main data frame. The arguments consists of:

Date
Vin
Item
SaleID
CurHA
CC

The reason I've posted this is because the entire code looks like I could somehow consolidate/compress it more. I feel I shouldn't have to use so many break lines? Here is what it looks like with what I've described above. I will go ahead and point out that I am sure there will be other critiques of the code and if that is the case, please do give me your feedback! Thank you.

InvoiceDataFrame.loc[InvoiceDataFrame.index[len(InvoiceDataFrame) - 1],
                                     InvoiceDataFrame.columns[2]] = CreateDescription(Date=LoadedSales['LOGDATE'][inx],
                                                                                      Vin=CustomerVin,
                                                                                      Item=FindItem(Item=LoadedSales['NAME'][inx],
                                                                                                    SaleID=LoadedSales['SALEID'][inx]),
                                                                                      SaleID=LoadedSales['SALEID'][inx],
                                                                                      CurHA=HouseAccount,
                                                                                      CC=CC)
And here is the header of the function CreateDescrition():
def CreateDescription(Date, CC, Item, SaleID, CurHA, Vin):
first - Follow PEP8 recommendation 4 spaces per indentation.
(Oct-02-2018, 03:38 PM)WuchaDoin Wrote: [ -> ]After receiving some feedback on past posts, I have begun re-designing my code to be more object oriented. It's going great and is really fun finding the best ways to fit all the pieces, but I have come across an issue that is a nuisance, but not detrimental. I have a function that creates a 'description' for the data frame. The description is comprised of 4 sections:

Date
Item
Name
SaleID

The sections are then concatenated and inserted into the InvoiceDataFrame.

The arguments that this function calls for are plenty due to needing a variety of information to create and merge the description into the main data frame. The arguments consists of:

Date
Vin
Item
SaleID
CurHA
CC

The reason I've posted this is because the entire code looks like I could somehow consolidate/compress it more. I feel I shouldn't have to use so many break lines? Here is what it looks like with what I've described above. I will go ahead and point out that I am sure there will be other critiques of the code and if that is the case, please do give me your feedback! Thank you.

InvoiceDataFrame.loc[InvoiceDataFrame.index[len(InvoiceDataFrame) - 1],
                                     InvoiceDataFrame.columns[2]] = CreateDescription(Date=LoadedSales['LOGDATE'][inx],
                                                                                      Vin=CustomerVin,
                                                                                      Item=FindItem(Item=LoadedSales['NAME'][inx],
                                                                                                    SaleID=LoadedSales['SALEID'][inx]),
                                                                                      SaleID=LoadedSales['SALEID'][inx],
                                                                                      CurHA=HouseAccount,
                                                                                      CC=CC)
And here is the header of the function CreateDescrition():
def CreateDescription(Date, CC, Item, SaleID, CurHA, Vin):

Again, your names - for Pythonista - are terrible. They are too long too, and don't comply to snake-style - InvoiceDataFrame any self-respecting Pythonista will call invoices; SaleID - sale_id; CurHA (actually, too short and cryptic) - house_account; CreateDescription - create_description.

pandas is about data processing - not about looping over data. The strength of pandas is to enable you to work on DataFrame - or its slices - as on a single object.

You should look at DataFrame.apply method
(Oct-03-2018, 08:33 AM)volcano63 Wrote: [ -> ]pandas is about data processing - not about looping over data. The strength of pandas is to enable you to work on DataFrame - or its slices - as on a single object.

You sholuld look ad DataFrame.apply method

I've never looked at pandas from that perspective. It makes much more sense though. I feel rushing into Python wasn't necessarily a bad idea, but I am beginning to figure out what I had missed/skipped during tutorials.
(Oct-03-2018, 02:41 PM)WuchaDoin Wrote: [ -> ]I've never looked at pandas from that perspective. It makes much more sense though. I feel rushing into Python wasn't necessarily a bad idea, but I am beginning to figure out what I had missed/skipped during tutorials.

pandas is an amazing - but very non-trivial - package. Rushing into pandas may be painful for Python beginners. Just a friendly warning.
Just looked up DataFrame.apply. I am going to love this method. Surprised I haven't found/seen this sooner. Thanks.