Python Forum
Better structure than this?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Better structure than this?
#1
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):
Reply
#2
first - Follow PEP8 recommendation 4 spaces per indentation.
Reply
#3
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
(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.
Reply
#5
(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.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
Just looked up DataFrame.apply. I am going to love this method. Surprised I haven't found/seen this sooner. Thanks.
Reply


Forum Jump:

User Panel Messages

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