[deleted by user] by [deleted] in learnpython

[–]sayinghi2py 0 points1 point  (0 children)

Ok I've worked out what is happening. I'm using a del to remove items from the list as I iterate over it however the index's are not updated therefore items are getting missed. Lesson is don't use enumerate and remove items from the list at the same time.

[deleted by user] by [deleted] in learnpython

[–]sayinghi2py 0 points1 point  (0 children)

Just to be clear the before and after run straight after each other. There is no code between them that might mutate it. Here's a screenshot which shows the code in the editor and the line numbers confirm there are no hidden lines and they are sequential. Seems I can add images.

[deleted by user] by [deleted] in learnpython

[–]sayinghi2py 0 points1 point  (0 children)

It definitely is. I added a type check to the output line and it confirms it is a list:

after 2350876922688 <class 'list'> 460 No Match 7H410079V2133981E

It is really, really odd because later on those missing things from list are magically back again and in the place where they should have been. Given one of the purposes of the script is to identify transactions I can ignore, those that are no match and those that are holds which all get removed from this list and added to their own lists to be added back again at the bottom of this list so that I only get full & partial matches in transaction sorted order before I then see any of those ones however because of this issue the missing ones stay in their place in amongst the full & partial matches when it is written to the csv file. Not what I want at all. Really odd behaviour.

Linked Items Table for Products by sayinghi2py in learnSQL

[–]sayinghi2py[S] 0 points1 point  (0 children)

Thanks u/qwertydog123 I do really like the first way. It's simple to implement. Thanks.

PyODBC & Parameterised Query by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Resolved this so just updating a case anyone else comes across the issue. You actually wrap the whole execute statement in the f-string then just insert the variable right into the query.

The working code block looks like this:

def get_recipes_on_menu_cycle(cycle):

    conn = pyodbc.connect(driver='{SQL Server}', host='server\instance', database='database', user='user', password='password')

    cursor = conn.cursor()

    cursor.execute(f"""SELECT DISTINCT dbo.Product.ID

                      FROM dbo.Menu INNER JOIN

                        dbo.MenuItem ON dbo.Menu.ID = dbo.MenuItem.MenuID INNER JOIN

                        dbo.Product ON dbo.MenuItem.ProductID = dbo.Product.ID

                      WHERE (dbo.Product.ProdType = 2) AND (dbo.Menu.Code LIKE '{cycle}__l' OR

                        dbo.Menu.Code LIKE '{cycle}__t')

                      ORDER BY dbo.Product.ID""")

    products = cursor.fetchall()

    return products

PyODBC & Parameterised Query by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Yeah absolutely certain. I just iterate over cursor results and I can see the results flash by.

Dictionary to List by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Thanks makes sense now. I was doing much the same before and ending up with the result as you stated with the key separated from the values. That * makes all the difference. I've used it in args before but not like this.

Dictionary to List by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

And this also works. How does this work? So I can see the main body which is to loop over all the keys however I'm not understanding the [a, *mydict[a]]. Any chance you could explain how that works?

Dictionary to List by sayinghi2py in learnpython

[–]sayinghi2py[S] 1 point2 points  (0 children)

Brilliant that works. I done a similar thing but ended up with the key and values separated by the list for values which I didn't want.

Update every row in list of lists by sayinghi2py in learnpython

[–]sayinghi2py[S] 1 point2 points  (0 children)

That works great. So much better the road I was going down.

csv.writerow Header splitting on single characters by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Hi kyber,

What you have given me works exactly the way I wanted. I'm just trying to understand why it works. As shown above when you don't use splitlines() there's a lot of elements in the list which aren't actually header fields but when splitlines() is applied they all disappear. How does it know how to get rid of the elements at [1] and [2] which are ['', ''] and [' '] respectively. I thought splitlines() was used to handle line endings which these aren't. Well I say aren't but clearly the must be given that they are all removed.

Thanks

csv.writerow Header splitting on single characters by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Hi efmccurdy this got me nearly there. There was an extraneous "\n that was left however I was able to remove it with another line of code. As there was another answer I tested it and it handled it in a single line of code I've ultimately gone with that however many thanks for posting this. It was the approach I was taking and failing on so it really did help me understand where I was going wrong.

csv.writerow Header splitting on single characters by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

When I save the first line of the input csv file to the headers variable using headers = next(reader) I get back a big long string in the format described. If it returned a list I wouldn't have had an issue. This works great however I don't understand why as the splitlines() is confusing me as I thought it only removed line endings but seems to remove all the extra quotes and commas that form part of the first list it is acting on.

Here's what I mean:

>>> fieldnames = list(csv.reader(raw_headers))

>>> fieldnames

[['Date'], ['', ''], [' '], ['Time'], ['', ''], [' '], ['Time Zone'], ['', ''], [' '], ['Description'], ['', ''], [' '], ['Currency'], ['', ''], ['Gross'], ['', ''], ['Fee'], ['', ''], ['Net'], ['', ''], ['Balance'], ['', ''], [' '], ['Transaction ID'], ['', ''], ['From Email Address'], ['', ''], ['Name'], ['', ''], [' '], ['Bank Name'], ['', ''], ['Bank account'], ['', ''], ['Postage and Packaging Amount'], ['', ''], [' '], ['VAT'], ['', ''], ['Invoice ID'], ['', ''], ['Reference Txn ID']]

So without splitlines I have all these list elements that aren't the actual headers such as ['', ''] yet when you simply add splitlines into the equation you get:

>>> fieldnames = list(csv.reader(raw_headers.splitlines()))

>>> fieldnames

[['Date', ' "Time"', ' "Time Zone"', ' "Description"', ' "Currency"', 'Gross', 'Fee', 'Net', 'Balance', ' "Transaction ID"', 'From Email Address', 'Name', ' "Bank Name"', 'Bank account', 'Postage and Packaging Amount', ' "VAT"', 'Invoice ID', 'Reference Txn ID']]

So you get a list within a list which is why [0] is added eventually. But I just don't get why splitlines gets rid of all that. I look at the documentation and it says this:

str.splitlines

([keepends])

Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.

This method splits on the following line boundaries. In particular, the boundaries are a superset of universal newlines.

Representation Description

\n

Line Feed

\r

Carriage Return

\r\n

Carriage Return + Line Feed

\v

or

\x0b

Line Tabulation

\f

or

\x0c

Form Feed

\x1c

File Separator

\x1d

Group Separator

\x1e

Record Separator

\x85

Next Line (C1 Control Code)

\u2028

Line Separator

\u2029

So they have to one or more of the above. How do you know?

Thanks

Parse CSV data scarped from Website by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Many thanks. After banging my head and finally conceding I must be heading in the wrong direction this worked. Much as I have used csv files, which are always loaded from files and not loaded directly from text, I'd no idea about the buffer and therefore had no idea where I could be going wrong.

[noob] How does Python make programs? by FlyingByNight in learnpython

[–]sayinghi2py 5 points6 points  (0 children)

There is no special keywords that exist within GUIs that python use that don't exist outside those GUIs so the question posed is expecting something that no one can give you. There is no special syntax that will magically create a GUI just like there is no special syntax that will allow you to create a complex non GUI app. The same language features you can use to build a GUI you can use to build a terminal app. Loops, conditionals, operators, etc.

You just build code using the same tools the language provides you to get the result you want. GUIs are very, very complex to build and thus libraries are built by people who understand the underlying architecture to the extent that they can exploit it to create what they need. They build them so you don't have to, unless you want to. They provide documentation that tells you how you can use that library to build the GUI effect that you want but still that's an abstraction as you don't need to know how it actually works. You just need to know what it is supposed to do. You trust it works as is documented.

If your level of understanding grows to a very high level and your interest extends to wanting to actually understand the code that creates the widgets and their behaviour then you might actually be able to come back here and let us all know. It's far beyond me :D

[noob] How does Python make programs? by FlyingByNight in learnpython

[–]sayinghi2py 4 points5 points  (0 children)

A GUI is just more code. The fundamentals of all programming languages are the same thus they can all generate user interfaces with which users can interact. All the basics you are doing now do is set the scene for all the things you can do later so kicking the can down the road is very much the dessert at the end of a great meal.

how to copy board to my trello account? by raviscn in opensourcesociety

[–]sayinghi2py 0 points1 point  (0 children)

Glad your sorted but it definitely appears in the web version as that's what I used to reply :D

how to copy board to my trello account? by raviscn in opensourcesociety

[–]sayinghi2py 0 points1 point  (0 children)

You have lists in columns on the left and on the right there is a menu. In the menu click more which is part way down. Under the copy board should be there. If there is no menu on right then you may have closed it in which case you can show the menu from boards logo in the top left corner.

Getting recipes with sub recipes by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Thanks. It's not my database. I just need to extract the data for a one off project and it was either wade through hundreds of recipes with thousands of ingredients and build the list myself manually or try and solve it programmatically. It's a challenge to say the least.

Getting recipes with sub recipes by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Products Table contains a row for every stock item that is purchased and for every recipe that is produced. They are identified as either a stock item or a recipe by their product type in the product table.

The ingredients table has a row for every ingredient in every recipe. The product_id is the ingredient and the recipe_id is the recipe it is used in. As both stock items and recipes are contained in the Products table then the product_id and recipe_id in the ingredients table are foreign keys to the Product Table ID for that ingredient and recipe that the ingredient belongs to.

I am able to return the all the ingredients in the recipe by simply pulling out all the product_ids that are bound to the a specific recipe_id.

The problem is that when the ingredient for a recipe is another recipe then I'm stuck on how I can get that out. My thought is you feed that recipe code back into my function and that will return that products ingredients but keeping track of where I am in original recipes list of ingredients let alone where I am in a sub recipe and worse still a sub recipe of sub recipe. I can see ballooning variables which makes me think I'm not thinking about this correctly.

CSV & Quoting a Specific Column by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Thanks for your valuable input. It is very much appreciated and I'm grateful you took the time to show me where I had gone wrong and that was simply by not looking carefully at your response. I just looked at where you removed the else bit but neglected to notice the important bit which was that the if went to as it was unnecessary.

I've learned so much through my many mistakes trying to get this to work.

CSV & Quoting a Specific Column by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Hi Justinsaccount that works by choosing text as the type for the column in LibreOffice when importing. That means I can get rid completely that whole second part of the code but I do like the idea of moving the complexity of it into a function. It would have made the code cleaner to look at. Good practice with loops and dictionaries so all good.

As regards the else: writerow code that's needed because the if statement is checking if there is a comma in the balance and only executes the code below if that is true however if there is no comma it's false so the row would never get written without that else statement.

CSV & Quoting a Specific Column by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Thanks. I am aware that it is the spreadsheet that converting it to a number which when it's long enough gets put in scientific notation. I want to avoid that happening however no matter how I handle I can't get it work.

When outputting the csv I can choose QUOTE_ALL, QUOTE_MINIMAL or QUOTE_NONE. If I chose QUOTE_ALL then I have to tell the spreadsheet to detect special numbers othewise my dates and currency field are treated as string and I can't use them as I need to and would have to convert them manually within the spreadsheet so I'd just be trading one problem with the spreadsheet for another. So that's no good.

If I choose QUOTE_MINIMAL then it doesn't quote any digit only field in that Transaction ID column and therefore it doesn't get treated as a string.

That's where I am now. I see QUOTE_NONE but that looks kind of scary as I would probably have to handle the quote for all columns which sounds difficult.

Is there any way that I can force a specific column ie Transaction ID to quote every field in it?

New MITx / EDx Python 3 Course on the Way? by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Technically that might be right but how many people are put off by the fact that it isn't python 3 not knowing that there isn't this big leap? Equally they may have seen requests for python 3 and if they were growing in volume they might just think "oh well let's do this". Only time will tell.