Re: [Tutor] OT: A bit of humor related to my sporadic quest to learn Python
Ditto, but the angle is steeper, I've lost my climbing equipment, and the volcano is erupting. On Sun, Jan 13, 2019 at 5:44 PM boB Stepp wrote: > My son sent me this link, which I think captures my situation with > Python quite nicely: > > https://cdn-images-1.medium.com/max/720/1*7RZKI-g4K_syDf6XQEGWKw.jpeg > > -- > boB > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Debugging a sort error.
Peter, Thanks for the code for a custom key. That will come in handy later down the track. -Original Message- From: Tutor On Behalf Of Peter Otten Sent: Sunday, 13 January 2019 10:00 PM To: tutor@python.org Subject: Re: [Tutor] Debugging a sort error. mhysnm1...@gmail.com wrote: > Issue, following error is generated after trying to sort a list of > strings. > > description.sort() > TypeError: unorderable types: float() < str() Consider >>> descriptions = ["foo", "bar", 123, 3.14, 42, 200.1, "0"] >>> sorted(descriptions) Traceback (most recent call last): File "", line 1, in TypeError: unorderable types: int() < str() If there are only numbers and strings in the list you can force the sort to succeed with the following custom key function: >>> def key(item): ... return isinstance(item, str), item ... This will move the numbers to the beginning of the list: >>> sorted(descriptions, key=key) [3.14, 42, 123, 200.1, '0', 'bar', 'foo'] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Debugging a sort error.
All, Once again thanks for all the suggestions. It was the input data after all. As I am importing three sheets into python. One of the sheets had one less column. Thus how I ended up with float points. The testing for the string helped here greatly. Now things are correct again. On and forward to start working on text pattern exercise which I always have struggled with. Last language I did this in was Perl and had all sorts of headaches. 😊 Python seems cleaner from the reading I have done thus far. Lets see what challenges wait in front of me. -Original Message- From: Stephen Nelson-Smith Sent: Monday, 14 January 2019 1:15 AM To: mhysnm1...@gmail.com Cc: Python Tutor mailing list Subject: Re: [Tutor] Debugging a sort error. Hi, On Sun, Jan 13, 2019 at 8:34 AM wrote: > description.sort() > TypeError: unorderable types: float() < str() So, fairly obviously, we can't test whether a float is less than a string. Any more than we can tell if a grapefruit is faster than a cheetah. So there must be items in description that are strings and floats. With 2000 lines, you're going to struggle to eyeball this, so try something like this: In [69]: irrational_numbers = [3.14159265, 1.606695, "pi", "Pythagoras Constant"] In [70]: from collections import Counter In [71]: dict(Counter([type(e) for e in irrational_numbers])) Out[71]: {float: 2, str: 2} If with your data, this shows only strings, I'll eat my hat. S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] OT: A bit of humor related to my sporadic quest to learn Python
My son sent me this link, which I think captures my situation with Python quite nicely: https://cdn-images-1.medium.com/max/720/1*7RZKI-g4K_syDf6XQEGWKw.jpeg -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running Python 3 on Linux Mint
On 13/01/2019 14:47, Cranky Frankie wrote: > I want to start developing an application in Python 3 on my main computer > which runs Linux Mint with Python 2.7. What is the best way to work with > Python 3 on this system? You don't say which Mint version but assuming its 17 or greater then you can just use the software manager (or Synaptic) and install the python3 packages. You need the base plus IDLE plus any other third party libraries you use. (IDLE should pull in Tkinter as a dependency). It will probably only be version 3.5 but unless you are obsessive about having the latest version it should be adequate. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Debugging a sort error.
Everyone, I did find out the issue. When looking at the output in a spreadsheet. I was inserting floats into the description dictionary from the code I was using to extract the data. Thus I have gone back to the drawing board. Rather than extracting by columns which became difficult to achieve what I want to do. I am now extracting by row and the code has greatly simplified. Sorry about the data example. AS I am dealing with personal information. I don't want to share the original data. I will look at my editor to see if it is able to change the indent. Note: I am a Vision Impaired (blind) person learning to code in Python. Thus indents don't really matter to me. 😊 But I will change the indents to make it easier to read. Thanks for the test, this will help greatly. -Original Message- From: Cameron Simpson Sent: Sunday, 13 January 2019 8:12 PM To: mhysnm1...@gmail.com Cc: Tutor@python.org Subject: Re: [Tutor] Debugging a sort error. Discussion inline below. On 13Jan2019 13:16, mhysnm1...@gmail.com wrote: >I am hoping someone can help with the below error using Python3.5 in >the Windows 10 bash environment. I found the below link which I am not >sure if this is related to the issue or not. As I don't fully understand the >answer. > >https://github.com/SethMMorton/natsort/issues/7 I'm not sure that URL is very relevant, except to the extent that it points out that Python 3 issues an error when comparing incomparable types. In Python 2 this problem could go unnoticed, and that just leads to problems later, much farther from the source of the issue. >Issue, following error is generated after trying to sort a list of strings. > >description.sort() >TypeError: unorderable types: float() < str() > >Each list items (elements) contain a mixture of alpha chars, numbers, >punctuation chars like / and are in a string type. Below is an example >extract of the data from the list. > >['Description', 'EFTPOS WOOLWORTHS 1294 ", "withdrawal Hudson >street 3219"] The error message says that some of these values are not strings. One at least is a float. My expectation is that the openpyxl module is reading a floating point value into your description array. This might be openpxyl being too clever, or it might be (more likely IMO) be Excel turning something that looked like a float into a float. Spreadsheets can be ... helpful like that. >There is over 2000 such entries. This used to work and now doesn't. You'll need to examine the values. But I see that you're trying to do this. I've snipped the data loading phase. Here: >description = data['Description'] >for i in description: > if not str(i): >print "not a string") This is not a valid check that "i" is a string. That expression: str(i) tries to convert "i" into a string (via its __str__ method). Most objects have such a method, and str() of a float is the textual representation of the float. So the if statement doesn't test what you want to test. Try this: if not isinstance(i, str): print("not a string:", i, type(i)) >description.sort() >I am suspecting it is something to do with the data but cannot track >down the cause. Any suggestions on how to debug this? Your exception is in here, but as you expect you want to inspect the description types first. If the description column does contain a float in the original data then you could convert it to a string first! Note that this may not match visually what was in the spreadsheet. (BTW, your cited code never fills out the description list, not it cannot be current.) But first, fine out what's wrong. Try the type test I suggest and see how far you get. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Running Python 3 on Linux Mint
I want to start developing an application in Python 3 on my main computer which runs Linux Mint with Python 2.7. What is the best way to work with Python 3 on this system? -- Frank L. "Cranky Frankie" Palmeri, Risible Riding Raconteur & Writer "If you have a garden and a library, you have everything you need." - Cicero ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python installtion
Hi, On Mon, Jan 7, 2019 at 11:11 AM mousumi sahu wrote: > > Dear Sir, > I am trying to install python 2.7.10 on HPC. Python 2.6 has already been > install on root. I do not have root authority. Please suggest me how can I > do this. Sorry - I replied to you directly, by accident. Take 2, with reply all: You need to do a local installation of Python, and set up your system to use that in preference to the one at the system level. Although it's possible to do this with various manual steps, there's a really handy tool you can use which will make your life easier, and allow you to manage multiple versions of Python, which might be useful, if you wanted, say, to be able to run both Python 2 and Python 3. The tool is called `pyenv`, and as long as you have a bash/zsh shell, and your system has a C compiler and associated tools already installed, you can install and use it. The simplest approach is to clone the tool it from git, modify your shell to use it, and then use it to install Python. Here's a sample way to set it up. This won't necessarily match your exact requirements, but you can try it, and please come back if you have any further questions: 1. Clone the git repo into your home directory git clone https://github.com/pyenv/pyenv.git ~/.pyenv Pyenv is very simple, conceptually. It's just a set of shell scripts to automate the process of fetching, compiling, and installing versions of Python, and then massaging your shell to make sure the versions you have installed are used in preference to anything else. So now you have the tool, you need to configure your shell to use it. I'm going to assume you're using Bash. 2. Make sure the contents of the pyenv tool is available on your path echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile Note - this might need to be .bashrc, or something else, depending on your os/distro/setup. However, in principle you're just making the pyenv tool (which itself is just a set of shell scripts) available at all times. 3. Set your shell to initialise the pyenv tool every time you start a new shell echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile Again: this might need to be .bashrc 4. Now open a new shell, and check you have pyenv available: $ pyenv pyenv 1.2.9-2-g6309aaf2 Usage: pyenv [] Some useful pyenv commands are: commandsList all available pyenv commands local Set or show the local application-specific Python version global Set or show the global Python version shell Set or show the shell-specific Python version install Install a Python version using python-build uninstall Uninstall a specific Python version rehash Rehash pyenv shims (run this after installing executables) version Show the current Python version and its origin versionsList all Python versions available to pyenv which Display the full path to an executable whence List all Python versions that contain the given executable See `pyenv help ' for information on a specific command. For full documentation, see: https://github.com/pyenv/pyenv#readme If you don't have pyenv working at this stage, come back and I'll help you troubleshoot. Assuming you do, continue: 5. Now you can install a version of Python, locally : pyenv install --list This shows you the various options of Pythons you can install. You want the latest 2.7: pyenv install 2.7.15 This will fetch the source code of Python, and compile and install it for you, and place it in your local shell environment, where you can use it. If this step doesn't work, it's probably because your system doesn't have a compiler and associated tools. I can help you troubleshoot that, but ultimately you'll need support from your system administrator at this point. Assuming it's install Python, now you just need to tell your shell that you want to use it: pyenv local 2.7.15 This will make your shell find your 2.7.15 installation ahead of the system python: $ python --version Python 2.7.15 Now you can run and use your Python. Any further questions, sing out. S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Debugging a sort error.
Hi, On Sun, Jan 13, 2019 at 8:34 AM wrote: > description.sort() > TypeError: unorderable types: float() < str() So, fairly obviously, we can't test whether a float is less than a string. Any more than we can tell if a grapefruit is faster than a cheetah. So there must be items in description that are strings and floats. With 2000 lines, you're going to struggle to eyeball this, so try something like this: In [69]: irrational_numbers = [3.14159265, 1.606695, "pi", "Pythagoras Constant"] In [70]: from collections import Counter In [71]: dict(Counter([type(e) for e in irrational_numbers])) Out[71]: {float: 2, str: 2} If with your data, this shows only strings, I'll eat my hat. S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Debugging a sort error.
mhysnm1...@gmail.com wrote: > Issue, following error is generated after trying to sort a list of > strings. > > description.sort() > TypeError: unorderable types: float() < str() Consider >>> descriptions = ["foo", "bar", 123, 3.14, 42, 200.1, "0"] >>> sorted(descriptions) Traceback (most recent call last): File "", line 1, in TypeError: unorderable types: int() < str() If there are only numbers and strings in the list you can force the sort to succeed with the following custom key function: >>> def key(item): ... return isinstance(item, str), item ... This will move the numbers to the beginning of the list: >>> sorted(descriptions, key=key) [3.14, 42, 123, 200.1, '0', 'bar', 'foo'] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Debugging a sort error.
On Sun, Jan 13, 2019 at 01:16:10PM +1100, mhysnm1...@gmail.com wrote: > Issue, following error is generated after trying to sort a list of strings. > > description.sort() > TypeError: unorderable types: float() < str() That tells you that you don't have a list of strings. You have a list of strings with at least one float mixed in. > There is over 2000 such entries. This used to work and now doesn't. Only > change to the code was modifying the data variable from a list to a > dictionary. I doubt that. Anytime people say "nothing changed except..." it invariably turns out to be "oh yeah, these other ten things changed too..." *wink* > Below is the full code: Not it isn't, because the code you supply has at least one syntax error. So it is impossible for it to be the actual code you are running. In any case, we are volunteers, not paid consultants. If you want to pay us $200 an hour to debug your code, you get to dump a big ball of mud in our laps and say "fix it". But as volunteers, we have no obligation to trawl through your code trying to debug it (unless it looks interesting, or easy, or we're bored and looking for something to do...) To maximize your chances of people actually helping, please read this: http://www.sscce.org/ Having said that, I have spotted one obvious bug: > description = data['Description'] > > for i in description: > if not str(i): > print "not a string") (See what I mean about a syntax error?) That does not do what you think it does. My *guess* is that you are trying to check that every item in description is a string. But that's not what you do: you run through the items, make a string copy of each one (regardless of what kind of object it is) and then test whether the string is not the empty string (which it never will be, unless the original was the empty string). Instead, try this: for item in description: if not isinstance(item, str): print(type(item), repr(item), "is not a string") -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Debugging a sort error.
Discussion inline below. On 13Jan2019 13:16, mhysnm1...@gmail.com wrote: I am hoping someone can help with the below error using Python3.5 in the Windows 10 bash environment. I found the below link which I am not sure if this is related to the issue or not. As I don't fully understand the answer. https://github.com/SethMMorton/natsort/issues/7 I'm not sure that URL is very relevant, except to the extent that it points out that Python 3 issues an error when comparing incomparable types. In Python 2 this problem could go unnoticed, and that just leads to problems later, much farther from the source of the issue. Issue, following error is generated after trying to sort a list of strings. description.sort() TypeError: unorderable types: float() < str() Each list items (elements) contain a mixture of alpha chars, numbers, punctuation chars like / and are in a string type. Below is an example extract of the data from the list. ['Description', 'EFTPOS WOOLWORTHS 1294 ", "withdrawal Hudson street 3219"] The error message says that some of these values are not strings. One at least is a float. My expectation is that the openpyxl module is reading a floating point value into your description array. This might be openpxyl being too clever, or it might be (more likely IMO) be Excel turning something that looked like a float into a float. Spreadsheets can be ... helpful like that. There is over 2000 such entries. This used to work and now doesn't. You'll need to examine the values. But I see that you're trying to do this. I've snipped the data loading phase. Here: description = data['Description'] for i in description: if not str(i): print "not a string") This is not a valid check that "i" is a string. That expression: str(i) tries to convert "i" into a string (via its __str__ method). Most objects have such a method, and str() of a float is the textual representation of the float. So the if statement doesn't test what you want to test. Try this: if not isinstance(i, str): print("not a string:", i, type(i)) description.sort() I am suspecting it is something to do with the data but cannot track down the cause. Any suggestions on how to debug this? Your exception is in here, but as you expect you want to inspect the description types first. If the description column does contain a float in the original data then you could convert it to a string first! Note that this may not match visually what was in the spreadsheet. (BTW, your cited code never fills out the description list, not it cannot be current.) But first, fine out what's wrong. Try the type test I suggest and see how far you get. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Debugging a sort error.
On 13/01/2019 02:16, mhysnm1...@gmail.com wrote: > Issue, following error is generated after trying to sort a list of strings. > > description.sort() > TypeError: unorderable types: float() < str() Please send the complete error message not just the last line summary. There is a lot of potentially useful information in the missing lines. > ['Description', 'EFTPOS WOOLWORTHS 1294 ", "withdrawal Hudson > street 3219"] That doesn't look right. The quotes are all mixed up and miss-matched. Can you send an actual example? > There is over 2000 such entries. This used to work and now doesn't. Only > change to the code was modifying the data variable from a list to a > dictionary. The data variable appears to only have one entry, namely "Amount". Is that what you intended? Below is the full code: > > > > import openpyxl > > from openpyxl.utils import get_column_letter > > from more_itertools import unique_everseen > > > > # Loding the workbook and names for the sheets. > > wb = openpyxl.load_workbook("test.xlsx") > > wss = wb.get_sheet_names() > > > > description = [] # all the descriptions > > data = {} > > > > for ws_name in wss: > > ws = wb.get_sheet_by_name(ws_name) > > for column in ws.columns: > >col_data = [] # column list > >for cell in column: > > value = cell.value > > col_data.append(value) > ># end for cell loop > >if ws_name == "WestPac": > > if col_data[0] == 'Credit Amount': > >num = 1 > >while num <= > (len(col_data) - 1): > > value = > col_data[num] > > if > value: > > > data['Amount'][num] = value > > else: > > > data['Amount'][num] = data['Amount'][num] * -1 > > num += > 1 > ># end while num > >break > > # end if > ># end if > >if col_data[0] in data: > > > data[col_data[0]].extend(col_data[1:-1]) > >else: > > data[col_data[0]] = col_data > > # end for column > > #end for ws_name > > > > description = data['Description'] You try to assign data["Description"] but you never populated that in the code above. You only ever used data["Amount"]. > for i in description: > if not str(i): > print "not a string") I'm not sure what you think that's doing but the print should give a syntax error if you are using Python 3.5 as you claim. You need parens around it. Can you cut n paste the actual code? Also, it is unlikely to ever print anything because most things can be converted to a string. However, given that description is supposed to contain lists of values I suspect you need another inner loop to convert the list contents. I think you wanted something like: for lst in description: for i in lst: if type(i) is not str: print(i, "is not a string") > description.sort() > > I am suspecting it is something to do with the data but cannot track down > the cause. Any suggestions on how to debug this? Send the full error and some genuine data and try the modified type test loop above. Oh, and using smaller indentation will make it more readable too. 3-4 spaces is the usual size. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Debugging a sort error.
Hello everyone. I am hoping someone can help with the below error using Python3.5 in the Windows 10 bash environment. I found the below link which I am not sure if this is related to the issue or not. As I don't fully understand the answer. https://github.com/SethMMorton/natsort/issues/7 Issue, following error is generated after trying to sort a list of strings. description.sort() TypeError: unorderable types: float() < str() Each list items (elements) contain a mixture of alpha chars, numbers, punctuation chars like / and are in a string type. Below is an example extract of the data from the list. ['Description', 'EFTPOS WOOLWORTHS 1294 ", "withdrawal Hudson street 3219"] There is over 2000 such entries. This used to work and now doesn't. Only change to the code was modifying the data variable from a list to a dictionary. Below is the full code: import openpyxl from openpyxl.utils import get_column_letter from more_itertools import unique_everseen # Loding the workbook and names for the sheets. wb = openpyxl.load_workbook("test.xlsx") wss = wb.get_sheet_names() description = [] # all the descriptions data = {} for ws_name in wss: ws = wb.get_sheet_by_name(ws_name) for column in ws.columns: col_data = [] # column list for cell in column: value = cell.value col_data.append(value) # end for cell loop if ws_name == "WestPac": if col_data[0] == 'Credit Amount': num = 1 while num <= (len(col_data) - 1): value = col_data[num] if value: data['Amount'][num] = value else: data['Amount'][num] = data['Amount'][num] * -1 num += 1 # end while num break # end if # end if if col_data[0] in data: data[col_data[0]].extend(col_data[1:-1]) else: data[col_data[0]] = col_data # end for column #end for ws_name description = data['Description'] for i in description: if not str(i): print "not a string") description.sort() I am suspecting it is something to do with the data but cannot track down the cause. Any suggestions on how to debug this? Sean ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor