From sc_opensource at yahoo.com Sun Apr 1 06:57:15 2018 From: sc_opensource at yahoo.com (Simon Connah) Date: Sun, 1 Apr 2018 10:57:15 +0000 (UTC) Subject: [Tutor] Proper way to unit test the raising of exceptions? References: <594912527.351322.1522580235677.ref@mail.yahoo.com> Message-ID: <594912527.351322.1522580235677@mail.yahoo.com> Hi, I'm just wondering what the accepted way to handle unit testing exceptions is? I know you are meant to use assertRaises, but my code seems a little off. try:? ? some_func() except SomeException:? ? self.assertRaises(SomeException) Is there a better way to do this at all? The problem with the above code is that if no exception is raised the code completely skips the except block and that would mean that the unit test would pass so, I considered adding: self.fail('No exception raised') at the end outside of the except block but not sure if that is what I need to do. Any help is appreciated. From steve at pearwood.info Sun Apr 1 11:06:02 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 2 Apr 2018 01:06:02 +1000 Subject: [Tutor] Proper way to unit test the raising of exceptions? In-Reply-To: <594912527.351322.1522580235677@mail.yahoo.com> References: <594912527.351322.1522580235677.ref@mail.yahoo.com> <594912527.351322.1522580235677@mail.yahoo.com> Message-ID: <20180401150601.GD16661@ando.pearwood.info> On Sun, Apr 01, 2018 at 10:57:15AM +0000, Simon Connah via Tutor wrote: > I'm just wondering what the accepted way to handle unit testing > exceptions is? I know you are meant to use assertRaises, but my code > seems a little off. Here's a modified example from the statistics library in Python 3.4 and above, testing that statistics.mean raises appropriate errors. class TestMean(TestCase): def test_no_args(self): # Fail if given no arguments. self.assertRaises(TypeError, statistics.mean) def test_empty_data(self): # Fail when the data argument (first argument) is empty. for empty in ([], (), iter([])): self.assertRaises(statistics.StatisticsError, statistics.mean, empty) Let's dissect how they work. `test_no_args` calls self.assertRaises(TypeError, statistics.mean) which calls `statistics.mean` with no arguments. If it raises TypeError, then the test passes; otherwise the test fails. `test_empty_data` makes the following test: self.assertRaises(statistics.StatisticsError, statistics.mean, []) which calls `statistics.mean` with a single argument, the empty list [], and fails if StatisticsError is NOT raised; then it repeats the test using an empty tuple (), and a third time with an empty iterable. Only if all three tests raise does the test pass. Examples are heavily modified from here: https://github.com/python/cpython/blob/3.7/Lib/test/test_statistics.py > try:? ? some_func() > except SomeException:? ? self.assertRaises(SomeException) Re-write it as: self.assertRaises(SomeException, some_func) Note carefully that you do NOT write some_func() with parentheses. The idea is to pass the function object itself to the test case, which will then call it for you. If your function needs arguments, you add them after the function: self.assertRaises(SomeException, some_func, 1, 2, 3, "surprise") will call some_func(1, 2, 3, "surprise") and only pass if it raises SomeException. -- Steve From __peter__ at web.de Sun Apr 1 11:11:00 2018 From: __peter__ at web.de (Peter Otten) Date: Sun, 01 Apr 2018 17:11 +0200 Subject: [Tutor] Proper way to unit test the raising of exceptions? References: <594912527.351322.1522580235677.ref@mail.yahoo.com> <594912527.351322.1522580235677@mail.yahoo.com> Message-ID: Simon Connah via Tutor wrote: > Hi, > I'm just wondering what the accepted way to handle unit testing exceptions > is? I know you are meant to use assertRaises, but my code seems a little > off. > try: > some_func() > except SomeException: > self.assertRaises(SomeException) The logic is wrong here as you surmise below. If you catch the exception explicitly you have to write try: some_func() except SomeException: pass # OK else: self.fail("no exception raised") Alternatively you can write self.assertRaises(SomeException, some_func) which is already better, but becomes a bit hard to read once some_func takes arguments: self.assertRaises(SomeException, some_func, "one", two=42) Therefore I recommend using assertRaises() as a context manager: with self.assertRaises(SomeException): some_func("one", two=42) This has the advantage that some_func() appears with its args as it would in normal code, and that you can also test expressions: with self.assertRaises(ZeroDivisionError): MyNumber(1)/0 > Is there a better way to do this at all? > The problem with the above code is that if no exception is raised the code > completely skips the except block and that would mean that the unit test > would pass so, I considered adding: self.fail('No exception raised') at > the end outside of the except block but not sure if that is what I need to > do. Any help is appreciated. From mats at wichmann.us Sun Apr 1 11:31:38 2018 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 1 Apr 2018 09:31:38 -0600 Subject: [Tutor] Proper way to unit test the raising of exceptions? In-Reply-To: References: <594912527.351322.1522580235677.ref@mail.yahoo.com> <594912527.351322.1522580235677@mail.yahoo.com> Message-ID: <8713dfa1-5d12-7f99-ab24-8ef803e3f30a@wichmann.us> On 04/01/2018 09:10 AM, Peter Otten wrote: > Simon Connah via Tutor wrote: > >> Hi, >> I'm just wondering what the accepted way to handle unit testing exceptions >> is? I know you are meant to use assertRaises, but my code seems a little >> off. > >> try: >> some_func() >> except SomeException: >> self.assertRaises(SomeException) > > The logic is wrong here as you surmise below. If you catch the exception > explicitly you have to write > > try: > some_func() > except SomeException: > pass # OK > else: > self.fail("no exception raised") If you use PyTest, the procedure is pretty well documented: https://docs.pytest.org/en/latest/assert.html From sc_opensource at yahoo.com Sun Apr 1 12:14:11 2018 From: sc_opensource at yahoo.com (Simon Connah) Date: Sun, 1 Apr 2018 16:14:11 +0000 (UTC) Subject: [Tutor] Proper way to unit test the raising of exceptions? In-Reply-To: <8713dfa1-5d12-7f99-ab24-8ef803e3f30a@wichmann.us> References: <594912527.351322.1522580235677.ref@mail.yahoo.com> <594912527.351322.1522580235677@mail.yahoo.com> <8713dfa1-5d12-7f99-ab24-8ef803e3f30a@wichmann.us> Message-ID: <1416219610.408455.1522599251743@mail.yahoo.com> Awesome. Thank you all. Your solutions are great and should make the whole process a lot more simple. The only problem is that some_func() on my end is Django model with about 8 named arguments so it might be a bit of a pain passing all of those arguments. The context manager example seems like a perfect fit for that particular problem. Thanks again. All of your help is much appreciated. On Sunday, 1 April 2018, 16:32:11 BST, Mats Wichmann wrote: On 04/01/2018 09:10 AM, Peter Otten wrote: > Simon Connah via Tutor wrote: > >> Hi, >> I'm just wondering what the accepted way to handle unit testing exceptions >> is? I know you are meant to use assertRaises, but my code seems a little >> off. > >> try: >>? ? some_func() >> except SomeException:? >>? ? self.assertRaises(SomeException) > > The logic is wrong here as you surmise below. If you catch the exception > explicitly you have to write > > try: >? ? some_func() > except SomeException: >? ? pass? # OK > else: >? ? self.fail("no exception raised") If you use PyTest, the procedure is pretty well documented: https://docs.pytest.org/en/latest/assert.html _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From sjeik_appie at hotmail.com Sun Apr 1 15:20:10 2018 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Sun, 1 Apr 2018 19:20:10 +0000 Subject: [Tutor] pythonic Message-ID: On Mar 30, 2018 10:39, Alan Gauld via Tutor wrote: > > On 30/03/18 03:48, Pat Martin wrote: > > > the "right" way to do it in python? > > More or less, a couple of comments below... > > > def Main(): > > Python function names begin with a lowercase letter by convention. > > > """Run if run as a program.""" > > parser = argparse.ArgumentParser() > ... > > > > > now = datetime.datetime.now() > > slug = args.title.replace(" ", "-").lower() > > > > with open("{}.md".format(slug), 'w') as f: > > f.write("Title: {}\n".format(args.title)) > > f.write("Date: {}-{}-{} {}:{}\n".format(now.year, > > now.month, > > now.day, > > now.hour, > > now.minute)) > > Formatting of dates and times is usually done using > the time.strftime function which is specifically > designed for that. It might be worth taking a peek > at the docs on that one. You can call it directly > on a datetime object - 'now' in your case: > > fmt="%Y-%m-%d %H:%M\n" > f.write(now.strftime(fmt)) Lately I've been using format(), which uses __format__, because I find it slightly more readable: format(datetime.now(), "%Y-%m-%d %H:%M") From alan.gauld at yahoo.co.uk Sun Apr 1 17:58:51 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 1 Apr 2018 22:58:51 +0100 Subject: [Tutor] pythonic In-Reply-To: References: Message-ID: <26d8178a-1a53-92b1-0084-df2efad79463@yahoo.co.uk> On01/04/18 20:20, Albert-Jan Roskam wrote: > fmt="%Y-%m-%d %H:%M\n" > f.write(now.strftime(fmt)) > Lately I've been using format(), which uses __format__, because I find it slightly more readable: > format(datetime.now(), "%Y-%m-%d %H:%M") Interesting, I didn't know that format() recognised the datetime format codes. I assumed it would just use the printf codes. Time to do some more reading I see. -- 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 From steve at pearwood.info Sun Apr 1 23:19:55 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 2 Apr 2018 13:19:55 +1000 Subject: [Tutor] pythonic In-Reply-To: <26d8178a-1a53-92b1-0084-df2efad79463@yahoo.co.uk> References: <26d8178a-1a53-92b1-0084-df2efad79463@yahoo.co.uk> Message-ID: <20180402031954.GE16661@ando.pearwood.info> On Sun, Apr 01, 2018 at 10:58:51PM +0100, Alan Gauld via Tutor wrote: > On01/04/18 20:20, Albert-Jan Roskam wrote: > > fmt="%Y-%m-%d %H:%M\n" > > f.write(now.strftime(fmt)) > > Lately I've been using format(), which uses __format__, because I find it slightly more readable: > > format(datetime.now(), "%Y-%m-%d %H:%M") > Interesting, > I didn't know that format() recognised the datetime format codes. It doesn't. It is the datetime object that recognises them. format() merely passes the format string to the datetime.__format__ method, which is what recognises the codes. It doesn't care what it is. py> class Spam: ... def __format__(self, template): ... return template.replace("^DD^", " surprise ") ... py> format(Spam(), "some^DD^string") 'some surprise string' -- Steve From sunnlotus at aol.com Sun Apr 1 20:46:05 2018 From: sunnlotus at aol.com (Rex Florian) Date: Sun, 1 Apr 2018 20:46:05 -0400 Subject: [Tutor] running a .py file from the comand line Message-ID: <16283d175ea-c8a-7e56@webjas-vac039.srv.aolmail.net> Hello, I am running Python 3.6 in a Window 7 environment. I have a python script that I am trying to run from the command line. The script is from a Learning to Program file demonstrating event driven programming. I have copied it to a file named Ascii Keys.py into my user directory c:\Users\Rex I try to execute the file by typing python Ascii Keys.py at the command line and receive the following message: python: can't open file 'Ascii': [errno2] no such file or directory I expected the python .py file to open and run. I check with the path command and receive among other paths these to paths to Python. c:\Users\Rex/AppData\Local\Programs\Python\Python36-32\Scripts c:\Users\Rex/AppData\Local\Programs\Python\Python36-32 I also load python from c:\Users\Rex and try running Ascii Keys from >> and get the following error: File "", line 1 Ascii Keys SyntaxError: invalid syntax Why doesn't python open my file? Also, in windows explorer, I tried to find the above paths and could not see AppData under c:\Users\Rex What am I missing here? import msvcrt import sys # First the event handlers def doKeyEvent(key): if key == '\x00' or key == '\xe0': key = msvcrt.getch() print ( ord(key), ' ', end='') sys.stdout.flush() # make sure it appears on screen def doQuit(key): print() # force a new line raise SystemExit # first clear some screen space lines = 25 for n in range(lines): print() # Now the main event-loop while True: ky = msvcrt.getch() if len(str(ky)) != 0: # we have a real event if " " in str(ky): doQuit(ky) else: doKeyEvent(ky) From alan.gauld at yahoo.co.uk Mon Apr 2 04:53:27 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 2 Apr 2018 09:53:27 +0100 Subject: [Tutor] running a .py file from the comand line In-Reply-To: <16283d175ea-c8a-7e56@webjas-vac039.srv.aolmail.net> References: <16283d175ea-c8a-7e56@webjas-vac039.srv.aolmail.net> Message-ID: On 02/04/18 01:46, Rex Florian via Tutor wrote: > Hello, > > I am running Python 3.6 in a Window 7 environment. > I have a python script that I am trying to run from the command line. > I have copied it to a file named Ascii Keys.py into my user directory c:\Users\Rex > > I try to execute the file by typing python Ascii Keys.py Try python c:\Users\Rex\"ascii keys.py" Note the quotes to cater for the space. > python: can't open file 'Ascii': [errno2] no such file or directory The space confuses windows CMD, so it thinks you have two files called 'Ascii' and 'keys.py' By quoting the name it should see it as a single file name. -- 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 From alan.gauld at btinternet.com Mon Apr 2 04:56:16 2018 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 2 Apr 2018 09:56:16 +0100 Subject: [Tutor] pythonic In-Reply-To: <20180402031954.GE16661@ando.pearwood.info> References: <26d8178a-1a53-92b1-0084-df2efad79463@yahoo.co.uk> <20180402031954.GE16661@ando.pearwood.info> Message-ID: On 02/04/18 04:19, Steven D'Aprano wrote: > On Sun, Apr 01, 2018 at 10:58:51PM +0100, Alan Gauld via Tutor wrote: >> On01/04/18 20:20, Albert-Jan Roskam wrote: >>> fmt="%Y-%m-%d %H:%M\n" >>> f.write(now.strftime(fmt)) >>> Lately I've been using format(), which uses __format__, because I find it slightly more readable: >>> format(datetime.now(), "%Y-%m-%d %H:%M") >> Interesting, >> I didn't know that format() recognised the datetime format codes. > It doesn't. It is the datetime object that recognises them. format() > merely passes the format string to the datetime.__format__ method, which > is what recognises the codes. It doesn't care what it is. Aha! That makes sense. I've never really used format() so have never bothered to find out how it works. To the point that until this thread I hadn't realized we even had a __format__() operator. As I said, I need to do some reading. Obviously a gap in my python education. -- 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 From mats at wichmann.us Mon Apr 2 08:49:52 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 2 Apr 2018 06:49:52 -0600 Subject: [Tutor] pythonic In-Reply-To: References: <26d8178a-1a53-92b1-0084-df2efad79463@yahoo.co.uk> <20180402031954.GE16661@ando.pearwood.info> Message-ID: <247a8b0b-95ab-7bb9-9e3d-18846a22fdd8@wichmann.us> On 04/02/2018 02:56 AM, Alan Gauld via Tutor wrote: > On 02/04/18 04:19, Steven D'Aprano wrote: >> On Sun, Apr 01, 2018 at 10:58:51PM +0100, Alan Gauld via Tutor wrote: >>> On01/04/18 20:20, Albert-Jan Roskam wrote: >>>> fmt="%Y-%m-%d %H:%M\n" >>>> f.write(now.strftime(fmt)) >>>> Lately I've been using format(), which uses __format__, because I find it slightly more readable: >>>> format(datetime.now(), "%Y-%m-%d %H:%M") >>> Interesting, >>> I didn't know that format() recognised the datetime format codes. >> It doesn't. It is the datetime object that recognises them. format() >> merely passes the format string to the datetime.__format__ method, which >> is what recognises the codes. It doesn't care what it is. > Aha! That makes sense. I've never really used format() so have never > bothered to find out how it works. To the point that until this thread I > hadn't realized we even had a __format__() operator. > > As I said, I need to do some reading. Obviously a gap in my python > education. > so since we're all learning things here, how would this play out with the new f-strings? From david at graniteweb.com Mon Apr 2 09:01:37 2018 From: david at graniteweb.com (David Rock) Date: Mon, 2 Apr 2018 08:01:37 -0500 Subject: [Tutor] pythonic In-Reply-To: References: Message-ID: <82E72766-F1C7-42DB-BE54-E2C1E4294FDB@graniteweb.com> > On Mar 30, 2018, at 04:15, George Fischhof wrote: > > 2.) > argparse > > it is good, but you can write more Pythonic code using click > https://pypi.python.org/pypi/click/ > it is also Pythonic to use / know the Python ecosystem (the packages) It?s just as (if not more) pythonic to use the standard libraries. It?s very common in a professional environment to not have access to outside (i.e., internet) resources. I wouldn?t venture into Pypi unless there?s something you can?t do well with what?s already provided by default. ? David Rock david at graniteweb.com From leamhall at gmail.com Mon Apr 2 09:08:45 2018 From: leamhall at gmail.com (leam hall) Date: Mon, 2 Apr 2018 09:08:45 -0400 Subject: [Tutor] pythonic In-Reply-To: <82E72766-F1C7-42DB-BE54-E2C1E4294FDB@graniteweb.com> References: <82E72766-F1C7-42DB-BE54-E2C1E4294FDB@graniteweb.com> Message-ID: On Mon, Apr 2, 2018 at 9:01 AM, David Rock wrote: > It?s just as (if not more) pythonic to use the standard libraries. It?s very common in a professional environment to not have access to outside (i.e., internet) resources. I wouldn?t venture into Pypi unless there?s something you can?t do well with what?s already provided by default. +1. Use standard libraries; most places I've worked didn't allow a lot of non-standard library code at all. If something is in the standard library you have some level of assurance that the maintenance is moderated and monitored. From steve at pearwood.info Mon Apr 2 09:28:59 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 2 Apr 2018 23:28:59 +1000 Subject: [Tutor] pythonic In-Reply-To: <247a8b0b-95ab-7bb9-9e3d-18846a22fdd8@wichmann.us> References: <26d8178a-1a53-92b1-0084-df2efad79463@yahoo.co.uk> <20180402031954.GE16661@ando.pearwood.info> <247a8b0b-95ab-7bb9-9e3d-18846a22fdd8@wichmann.us> Message-ID: <20180402132859.GI16661@ando.pearwood.info> On Mon, Apr 02, 2018 at 06:49:52AM -0600, Mats Wichmann wrote: > On 04/02/2018 02:56 AM, Alan Gauld via Tutor wrote: > > On 02/04/18 04:19, Steven D'Aprano wrote: > >> On Sun, Apr 01, 2018 at 10:58:51PM +0100, Alan Gauld via Tutor wrote: > >>> On01/04/18 20:20, Albert-Jan Roskam wrote: > >>>> fmt="%Y-%m-%d %H:%M\n" > >>>> f.write(now.strftime(fmt)) > >>>> Lately I've been using format(), which uses __format__, because I find it slightly more readable: > >>>> format(datetime.now(), "%Y-%m-%d %H:%M") > >>> Interesting, > >>> I didn't know that format() recognised the datetime format codes. > >> It doesn't. It is the datetime object that recognises them. format() > >> merely passes the format string to the datetime.__format__ method, which > >> is what recognises the codes. It doesn't care what it is. > > Aha! That makes sense. I've never really used format() so have never > > bothered to find out how it works. To the point that until this thread I > > hadn't realized we even had a __format__() operator. > > > > As I said, I need to do some reading. Obviously a gap in my python > > education. > > > > so since we're all learning things here, how would this play out with > the new f-strings? I don't think f-strings are even a bit Pythonic. They look like string constants, but they're actually a hidden call to eval(). py> x = 2 py> f'hello {3*x}' 'hello 6' So they can execute arbitrary code that has arbitrary side-effects: py> L = [] py> f'hello {L.append(1) or 99}' 'hello 99' py> L [1] By my count, they violate at least three of the Zen of Python: Explicit is better than implicit. Simple is better than complex. Special cases aren't special enough to break the rules. They can only be used once, and are not re-usable (unlike proper templates): py> x = 2 py> template = f'x = {x}' # gets the value of x now py> print(template) x = 2 py> x = 99 py> print(template) # still has the old value of x x = 2 However, they do use the same mechanism as format(): py> from datetime import datetime py> t = datetime.now() py> format(t, '%H:%M') '23:22' py> f'{t:%H:%M}' '23:22' -- Steve From __peter__ at web.de Mon Apr 2 10:28:10 2018 From: __peter__ at web.de (Peter Otten) Date: Mon, 02 Apr 2018 16:28:10 +0200 Subject: [Tutor] pythonic References: <26d8178a-1a53-92b1-0084-df2efad79463@yahoo.co.uk> <20180402031954.GE16661@ando.pearwood.info> <247a8b0b-95ab-7bb9-9e3d-18846a22fdd8@wichmann.us> <20180402132859.GI16661@ando.pearwood.info> Message-ID: Steven D'Aprano wrote: > On Mon, Apr 02, 2018 at 06:49:52AM -0600, Mats Wichmann wrote: >> so since we're all learning things here, how would this play out with >> the new f-strings? > > I don't think f-strings are even a bit Pythonic. > > They look like string constants, but they're actually a hidden call to > eval(). But because you cannot f-ify a string variable (without an additional eval() call) you aren't tempted to feed them user-provided data. > They can only be used once, and are not re-usable (unlike proper > templates): You can't eat your cake an have it. As "proper templates" they would indeed be as dangerous as eval(). > By my count, they violate at least three of the Zen of Python: > > Explicit is better than implicit. > Simple is better than complex. > Special cases aren't special enough to break the rules. As I'm getting tired of writing "...{foo}...{bar}...".format(foo=foo, bar=bar, ...) lately I'd say they win big in the "practicality beats you-name-it" area. From mats at wichmann.us Mon Apr 2 12:31:55 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 2 Apr 2018 10:31:55 -0600 Subject: [Tutor] pythonic In-Reply-To: References: <26d8178a-1a53-92b1-0084-df2efad79463@yahoo.co.uk> <20180402031954.GE16661@ando.pearwood.info> <247a8b0b-95ab-7bb9-9e3d-18846a22fdd8@wichmann.us> <20180402132859.GI16661@ando.pearwood.info> Message-ID: <3ab5801f-d6ed-7d7a-78e9-da9c3bd21479@wichmann.us> On 04/02/2018 08:28 AM, Peter Otten wrote: > Steven D'Aprano wrote: > >> On Mon, Apr 02, 2018 at 06:49:52AM -0600, Mats Wichmann wrote: > >>> so since we're all learning things here, how would this play out with >>> the new f-strings? >> >> I don't think f-strings are even a bit Pythonic. >> >> They look like string constants, but they're actually a hidden call to >> eval(). > > But because you cannot f-ify a string variable (without an additional eval() > call) you aren't tempted to feed them user-provided data. > >> They can only be used once, and are not re-usable (unlike proper >> templates): > > You can't eat your cake an have it. As "proper templates" they would indeed > be as dangerous as eval(). > >> By my count, they violate at least three of the Zen of Python: >> >> Explicit is better than implicit. >> Simple is better than complex. >> Special cases aren't special enough to break the rules. > > As I'm getting tired of writing > > "...{foo}...{bar}...".format(foo=foo, bar=bar, ...) > > lately I'd say they win big in the "practicality beats you-name-it" area. so swooping back to the origial code, it seems there are several options for expressing the output. Picking one of the lines (and replacing the writes to a file with prints for simplicity): print("Date: {}-{}-{} {}:{}\n".format(now.year, now.month, now.day, now.hour, now.minute)) print("Date:", format(now, "%Y-%m-%d %H:%M")) print(f"Date: {now:%Y-%m-%d %H:%M}") those aren't exactly identical, since the using %m-%d form causes zero-padding: Date: 2018-4-2 10:23 Date: 2018-04-02 10:23 Date: 2018-04-02 10:23 For me, I don't know if the f-string version is the most Pythonic, but it has an appealing conciseness in this particular context :) From eryksun at gmail.com Mon Apr 2 13:41:05 2018 From: eryksun at gmail.com (eryk sun) Date: Mon, 2 Apr 2018 17:41:05 +0000 Subject: [Tutor] running a .py file from the comand line In-Reply-To: References: <16283d175ea-c8a-7e56@webjas-vac039.srv.aolmail.net> Message-ID: On Mon, Apr 2, 2018 at 8:53 AM, Alan Gauld via Tutor wrote: > > Try > > python c:\Users\Rex\"ascii keys.py" > > Note the quotes to cater for the space. > >> python: can't open file 'Ascii': [errno2] no such file or directory > > The space confuses windows CMD, so it thinks you have > two files called 'Ascii' and 'keys.py' Unlike Unix, this is not due to the shell in Windows. A process is started with a raw command line string. For a C/C++ application, the default process entry point is provided by the C runtime and does the setup work to call the application entry point (e.g. [w]main). This includes parsing the command line into an argv array according to documented rules [1]. An application can also call GetCommandLineW [2] and CommandLineToArgvW [3]. [1]: https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments [2]: https://msdn.microsoft.com/en-us/library/ms683156 [3]: https://msdn.microsoft.com/en-us/library/bb776391 CPython is written in C and uses the standard Windows C/C++ wmain and wWinMain application entry points. If you run "python C:\Users\Rex\Ascii Keys.py", the C runtime parses this into an argv array with 3 items: "python", "C:\Users\Rex\Ascii", and "Keys.py". Thus Python tries to open a script named "C:\Users\Rex\Ascii". From mats at wichmann.us Mon Apr 2 14:01:15 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 2 Apr 2018 12:01:15 -0600 Subject: [Tutor] running a .py file from the comand line In-Reply-To: References: <16283d175ea-c8a-7e56@webjas-vac039.srv.aolmail.net> Message-ID: <5ccfae7d-a7d5-71cf-36a0-08bf3ac872d7@wichmann.us> On 04/02/2018 11:41 AM, eryk sun wrote: > On Mon, Apr 2, 2018 at 8:53 AM, Alan Gauld via Tutor wrote: >> >> Try >> >> python c:\Users\Rex\"ascii keys.py" >> >> Note the quotes to cater for the space. >> >>> python: can't open file 'Ascii': [errno2] no such file or directory >> >> The space confuses windows CMD, so it thinks you have >> two files called 'Ascii' and 'keys.py' > > Unlike Unix, this is not due to the shell in Windows. A process is > started with a raw command line string. For a C/C++ application, the > default process entry point is provided by the C runtime and does the > setup work to call the application entry point (e.g. [w]main). This > includes parsing the command line into an argv array according to > documented rules [1]. An application can also call GetCommandLineW [2] > and CommandLineToArgvW [3]. > > [1]: https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments > [2]: https://msdn.microsoft.com/en-us/library/ms683156 > [3]: https://msdn.microsoft.com/en-us/library/bb776391 > > CPython is written in C and uses the standard Windows C/C++ wmain and > wWinMain application entry points. If you run "python > C:\Users\Rex\Ascii Keys.py", the C runtime parses this into an argv > array with 3 items: "python", "C:\Users\Rex\Ascii", and "Keys.py". > Thus Python tries to open a script named "C:\Users\Rex\Ascii". so in summary... if you have things set up so you can click-to-launch, filenames with spaces in them will work, but you'll be better off avoiding them, esp. if you intend to launch from a command line. From shannonev101 at googlemail.com Mon Apr 2 18:44:39 2018 From: shannonev101 at googlemail.com (Shannon Evans) Date: Mon, 2 Apr 2018 23:44:39 +0100 Subject: [Tutor] Python help Message-ID: Hi, I am trying to write a code with if statements but the code keeps just repeating and not carrying on. I am trying to get user to input a grade, either A, B, C, D, E or F and trying to have an error message if anything but these values are inputted. This is what i've wrote so far: while True: try: Grade = int(raw_input("Please enter your Grade: ")) except ValueError: print("Error, Please enter A, B, C, D, E or F") continue if Grade <> 'A','B','C','D','E','F': print ("Error, Please enter A, B, C, D, E or F") continue else:#Grade succesfully completed, and we're happy with its value. #ready to exit the loop. break When trying to run it just writes the error message for any letter you input. Any hep would be appreciated thank you! From steve at pearwood.info Mon Apr 2 19:48:18 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 3 Apr 2018 09:48:18 +1000 Subject: [Tutor] pythonic In-Reply-To: References: <26d8178a-1a53-92b1-0084-df2efad79463@yahoo.co.uk> <20180402031954.GE16661@ando.pearwood.info> <247a8b0b-95ab-7bb9-9e3d-18846a22fdd8@wichmann.us> <20180402132859.GI16661@ando.pearwood.info> Message-ID: <20180402234818.GJ16661@ando.pearwood.info> On Mon, Apr 02, 2018 at 04:28:10PM +0200, Peter Otten wrote: > > They look like string constants, but they're actually a hidden call to > > eval(). > > But because you cannot f-ify a string variable (without an additional eval() > call) you aren't tempted to feed them user-provided data. If only that were the case... https://mail.python.org/pipermail/python-list/2018-March/731967.html He reads f-strings from user-supplied data, then evals them. But its okay, he's only doing it within his own organisation, and we all know that "insiders" are always 100% trusted. "Insider attack" is just a pair of words. Right? > As I'm getting tired of writing > > "...{foo}...{bar}...".format(foo=foo, bar=bar, ...) You can write: template.format(**(locals())) or possibly nicer still: template.format_map(locals()) -- Steve From steve at pearwood.info Mon Apr 2 19:56:03 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 3 Apr 2018 09:56:03 +1000 Subject: [Tutor] Python help In-Reply-To: References: Message-ID: <20180402235603.GK16661@ando.pearwood.info> On Mon, Apr 02, 2018 at 11:44:39PM +0100, Shannon Evans via Tutor wrote: > Hi, I am trying to write a code with if statements but the code keeps just > repeating and not carrying on. > I am trying to get user to input a grade, either A, B, C, D, E or F and > trying to have an error message if anything but these values are inputted. > This is what i've wrote so far: > > while True: > try: > Grade = int(raw_input("Please enter your Grade: ")) Here you convert the user's input into an integer, a number such as 1, 2, 57, 92746 etc. If that *fails*, you run this block: > except ValueError: > print("Error, Please enter A, B, C, D, E or F") > continue and start again. If it succeeds, because the user entered (let's say) 99, then you run another test: > if Grade <> 'A','B','C','D','E','F': > print ("Error, Please enter A, B, C, D, E or F") > continue That will ALWAYS fail, because you are trying to compare the integer 99 (for example) with the tuple of six characters: 99 <> ('A', 'B','C','D','E','F') which is always false. So that block will ALWAYS fail, and you always go back to the start. -- Steve From alan.gauld at btinternet.com Mon Apr 2 20:00:59 2018 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 3 Apr 2018 01:00:59 +0100 Subject: [Tutor] Python help In-Reply-To: References: Message-ID: <56ca4479-1ec5-b84b-ee60-8ffd5ea2cc7a@btinternet.com> On 02/04/18 23:44, Shannon Evans via Tutor wrote: > Hi, I am trying to write a code with if statements but the code keeps just > repeating and not carrying on. There are quite a few problems here, see comments below. > while True: > try: > Grade = int(raw_input("Please enter your Grade: ")) You are trying to convert the input to an integer. But A-F will not convert so thats the first problem right there. > except ValueError: > print("Error, Please enter A, B, C, D, E or F") > continue And here you prompt for an A-F input not an integer. > if Grade <> 'A','B','C','D','E','F': This doesn't do what you think. It tests to see if Grade is not equal to a tuple of values ('A','B','C','D','E','F') You need to use 'in' instead. That will check whether or not Grade is *one* of the values in the tuple. if Grade not in 'A','B','C','D','E','F': > print ("Error, Please enter A, B, C, D, E or F") > continue > else:#Grade succesfully completed, and we're happy with its value. > #ready to exit the loop. > break Taking out the conversion to int() would be a good start. Then change the if test to use 'in'. -- 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 From steve at pearwood.info Mon Apr 2 20:19:38 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 3 Apr 2018 10:19:38 +1000 Subject: [Tutor] Python help In-Reply-To: <56ca4479-1ec5-b84b-ee60-8ffd5ea2cc7a@btinternet.com> References: <56ca4479-1ec5-b84b-ee60-8ffd5ea2cc7a@btinternet.com> Message-ID: <20180403001938.GM16661@ando.pearwood.info> On Tue, Apr 03, 2018 at 01:00:59AM +0100, Alan Gauld via Tutor wrote: > You need to use 'in' instead. That will check whether > or not Grade is *one* of the values in the tuple. > > if Grade not in 'A','B','C','D','E','F': Actually, that returns a tuple consisting of a flag plus five more strings: py> 'A' in 'A','B','C', 'D', 'E', 'F' (True, 'B','C', 'D', 'E', 'F') We need to put round brackets (parentheses) around the scores: if Grade not in ('A','B','C','D','E','F'): Don't be tempted to take a short-cut: # WRONG! if Grade not in 'ABCDEF': as that would accepted grades like "CDEF" and "BC". -- Steve From alan.gauld at btinternet.com Mon Apr 2 20:34:24 2018 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 3 Apr 2018 01:34:24 +0100 Subject: [Tutor] Python help In-Reply-To: <20180403001938.GM16661@ando.pearwood.info> References: <56ca4479-1ec5-b84b-ee60-8ffd5ea2cc7a@btinternet.com> <20180403001938.GM16661@ando.pearwood.info> Message-ID: <093e6c37-193b-98f0-127e-7495a7fe7cc3@btinternet.com> On 03/04/18 01:19, Steven D'Aprano wrote: > On Tue, Apr 03, 2018 at 01:00:59AM +0100, Alan Gauld via Tutor wrote: > >> You need to use 'in' instead. That will check whether >> or not Grade is *one* of the values in the tuple. >> >> if Grade not in 'A','B','C','D','E','F': > Actually, that returns a tuple consisting of a flag plus five more > strings: > > py> 'A' in 'A','B','C', 'D', 'E', 'F' > (True, 'B','C', 'D', 'E', 'F') > > We need to put round brackets (parentheses) around the scores: > > if Grade not in ('A','B','C','D','E','F'): Oops, yes, a cut n' paste error, I forgot to add the parens. mea culpa! -- 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 From alan.gauld at btinternet.com Mon Apr 2 20:41:53 2018 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 3 Apr 2018 01:41:53 +0100 Subject: [Tutor] Python help In-Reply-To: <20180403001938.GM16661@ando.pearwood.info> References: <56ca4479-1ec5-b84b-ee60-8ffd5ea2cc7a@btinternet.com> <20180403001938.GM16661@ando.pearwood.info> Message-ID: On 03/04/18 01:19, Steven D'Aprano wrote: > >> if Grade not in 'A','B','C','D','E','F': > Actually, that returns a tuple consisting of a flag plus five more > strings: > > py> 'A' in 'A','B','C', 'D', 'E', 'F' > (True, 'B','C', 'D', 'E', 'F') Although in the context of the program the colon at the end ensures we get a syntax error so the mistake is found pretty quickly... -- 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 From Sunnlotus at aol.com Mon Apr 2 21:15:00 2018 From: Sunnlotus at aol.com (Rex) Date: Mon, 2 Apr 2018 21:15:00 -0400 Subject: [Tutor] import tkinter as tk Message-ID: <6F36CD09-08F5-4049-BE83-580BE4AB2180@aol.com> At the Python command prompt, I write: import tkinter as tk top = Tk() Traceback (most recent call last): File ??, line 1, in module NameError: name ?Tk? is not defined I see under my Python36 directories for both the Lib and libs directories both of which have a directory or file referencing tkinter. Why is a blank window not loaded? Sent from my iPhone From alan.gauld at yahoo.co.uk Tue Apr 3 03:34:13 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 3 Apr 2018 08:34:13 +0100 Subject: [Tutor] import tkinter as tk In-Reply-To: <6F36CD09-08F5-4049-BE83-580BE4AB2180@aol.com> References: <6F36CD09-08F5-4049-BE83-580BE4AB2180@aol.com> Message-ID: On 03/04/18 02:15, Rex via Tutor wrote: > At the Python command prompt, I write: > > import tkinter as tk > top = Tk() You forgot the module prefix. top = tk.Tk() -- 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 From evapcoop.evapcoop at dana.com Wed Apr 4 11:10:18 2018 From: evapcoop.evapcoop at dana.com (Evapcoop, Evapcoop) Date: Wed, 4 Apr 2018 15:10:18 +0000 Subject: [Tutor] Using the Nimblenet Library in Python 3.6 Message-ID: Hello, I am running into some trouble when trying to import certain modules; I wanted to know if it is possible to use Nimblenet and all it's associated packages with Python3.6. From what I have read, that library is compatible with Python2.7. Some how I was able to successfully import Nimblenet on through the Conda prompt. But when I try to import some associated packages, it errors out. Here is an example: from nimblenet.activation_functions import tanh_function from nimblenet.learning_algorithms import scaled_conjugate_gradient from nimblenet.cost_functions import sum_squared_error from nimblenet.data_structures import Instance from nimblenet.neuralnet import NeuralNet import numpy as np import time import cPickle as pickle import logging from tabulate import tabulate import database_operations as dbo --------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) in () 3 from __future__ import print_function 4 from nimblenet.activation_functions import tanh_function ----> 5 from nimblenet.learning_algorithms import scaled_conjugate_gradient 6 from nimblenet.cost_functions import sum_squared_error 7 from nimblenet.data_structures import Instance C:\Anaconda3\lib\site-packages\nimblenet\learning_algorithms\__init__.py in () 1 #from generalized_hebbian import * ----> 2 from scaled_conjugate_gradient import scaled_conjugate_gradient 3 from resilient_backpropagation import resilient_backpropagation 4 from scipyoptimize import scipyoptimize 5 ModuleNotFoundError: No module named 'scaled_conjugate_gradient' I would appreciate any help I can get! Thank you, Christine ________________________________ This e-mail, and any attachments, is intended solely for use by the addressee(s) named above. It may contain the confidential or proprietary information of Dana Incorporated, its subsidiaries, affiliates or business partners. If you are not the intended recipient of this e-mail or are an unauthorized recipient of the information, you are hereby notified that any dissemination, distribution or copying of this e-mail or any attachments, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by reply e-mail and permanently delete the original and any copies or printouts. Computer viruses can be transmitted via email. The recipient should check this e-mail and any attachments for the presence of viruses. Dana Incorporated accepts no liability for any damage caused by any virus transmitted by this e-mail. English, Fran?ais, Espa?ol, Deutsch, Italiano, Portugu?s: http://www.dana.com/corporate-pages/Email ________________________________ From sjeik_appie at hotmail.com Wed Apr 4 15:34:18 2018 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Wed, 4 Apr 2018 19:34:18 +0000 Subject: [Tutor] pythonic Message-ID: Op 2 apr. 2018 15:31 schreef Steven D'Aprano : > > On Mon, Apr 02, 2018 at 06:49:52AM -0600, Mats Wichmann wrote: > > On 04/02/2018 02:56 AM, Alan Gauld via Tutor wrote: > > > On 02/04/18 04:19, Steven D'Aprano wrote: > > >> On Sun, Apr 01, 2018 at 10:58:51PM +0100, Alan Gauld via Tutor wrote: > > >>> On01/04/18 20:20, Albert-Jan Roskam wrote: > > >>>> fmt="%Y-%m-%d %H:%M\n" > > >>>> f.write(now.strftime(fmt)) > > >>>> Lately I've been using format(), which uses __format__, because I find it slightly more readable: > > >>>> format(datetime.now(), "%Y-%m-%d %H:%M") > > >>> Interesting, > > >>> I didn't know that format() recognised the datetime format codes. > > >> It doesn't. It is the datetime object that recognises them. format() > > >> merely passes the format string to the datetime.__format__ method, which > > >> is what recognises the codes. It doesn't care what it is. > > > Aha! That makes sense. I've never really used format() so have never > > > bothered to find out how it works. To the point that until this thread I > > > hadn't realized we even had a __format__() operator. > > > > > > As I said, I need to do some reading. Obviously a gap in my python > > > education. > > > > > > > so since we're all learning things here, how would this play out with > > the new f-strings? > > I don't think f-strings are even a bit Pythonic. "There should be one-- and preferably only one --obvious way to do it.": 1-str.format 2-% interpolation 3-string.Template 4-f strings -... I think that at least a few of these methods should become deprecated. Maybe 2 and 3? (though I use 2 all the time!). Not sure about 4. A proper templating language like Jinja might be more desirable to have in the standard library, addition to a simple string substitution mechanism From alan.gauld at yahoo.co.uk Wed Apr 4 19:07:47 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 5 Apr 2018 00:07:47 +0100 Subject: [Tutor] Using the Nimblenet Library in Python 3.6 In-Reply-To: References: Message-ID: On 04/04/18 16:10, Evapcoop, Evapcoop wrote: > I wanted to know if it is possible to use Nimblenet and all it's > associated packages with Python3.6.> From what I have read, that library is compatible with Python2.7. I think you just answered your own question. If the package is designed for v2.7 its extremely unlikely to work in 3.6 even if you did succeed in importing it. The differences between 2.7 and 3.6 are significant. But this forum is the wrong place to ask. We are focused on the Python language and standard library. For detailed info you really need to ask the nimblenet developers/support fora. -- 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 From mats at wichmann.us Wed Apr 4 19:54:06 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 4 Apr 2018 17:54:06 -0600 Subject: [Tutor] Using the Nimblenet Library in Python 3.6 In-Reply-To: References: Message-ID: On 04/04/2018 05:07 PM, Alan Gauld via Tutor wrote: > On 04/04/18 16:10, Evapcoop, Evapcoop wrote: >> I wanted to know if it is possible to use Nimblenet and all it's >> associated packages with Python3.6.> From what I have read, that library is compatible with Python2.7. > > I think you just answered your own question. If the package is > designed for v2.7 its extremely unlikely to work in 3.6 even if > you did succeed in importing it. The differences between 2.7 > and 3.6 are significant. > > But this forum is the wrong place to ask. We are focused on the > Python language and standard library. For detailed info you > really need to ask the nimblenet developers/support fora. Having no idea what nimblenet is I do see from a lightning search that it's tied into numpy somehow. So you may get some joy from asking in the Anaconda forums as well - the whole reason for existence of the Anaconda distribution is to make it reasonable to install things to do with Numerical and Scientific Python without some of the headaches sometimes associated with doing so from scratch - you seem to be trying the conda route, so maybe someone has blazed the trail before you? Also this is more supposition, but the fact that the line 3 from __future__ import print_function appears in the traceback means it's at least _possible_ Python 3 compatibility of the package has been considered - that's one of the most obvious and visible changes, that print becomes a function instead of a statement, and that was added to the future module somewhere along the line in the Py2 series. Best of luck in your searches! From breamoreboy at gmail.com Wed Apr 4 23:02:20 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Thu, 5 Apr 2018 04:02:20 +0100 Subject: [Tutor] Using the Nimblenet Library in Python 3.6 In-Reply-To: References: Message-ID: On 05/04/18 00:07, Alan Gauld via Tutor wrote: > On 04/04/18 16:10, Evapcoop, Evapcoop wrote: >> I wanted to know if it is possible to use Nimblenet and all it's >> associated packages with Python3.6.> From what I have read, that library is compatible with Python2.7. > > I think you just answered your own question. If the package is > designed for v2.7 its extremely unlikely to work in 3.6 even if > you did succeed in importing it. The differences between 2.7 > and 3.6 are significant. > Python 3.6 has more functionality than 2.7 by definition, but your comment implies, at least to me, that 2.7 and 3.6 are chalk and cheese. Nothing could be further from the truth and has regrettably been one of the reasons for the dreadfully slow uptake of Python 3. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at yahoo.co.uk Thu Apr 5 04:39:08 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 5 Apr 2018 09:39:08 +0100 Subject: [Tutor] Using the Nimblenet Library in Python 3.6 In-Reply-To: References: Message-ID: On 05/04/18 04:02, Mark Lawrence wrote: > Python 3.6 has more functionality than 2.7 by definition, but your > comment implies, at least to me, that 2.7 and 3.6 are chalk and cheese. > Nothing could be further from the truth and has regrettably been one of > the reasons for the dreadfully slow uptake of Python 3. I disagree. Apart from the superficial language compatibility issues, which 2.7 can partially address by importing from future, the libraries are dramatically different. Any non trivial program runs into issues the minute it starts importing modules. module names are different, function names within those modules are different, return values and parameter types are different. Given Python programming relies hugely on the modules in the standard library I find it impossible to produce code that works across 2.7 and 3.x without significant effort to force compatibility. That's why tools like six etc exist. You may have been luckier than me but in my experience the gap between the two versions is significant and not to be underestimated. -- 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 From breamoreboy at gmail.com Thu Apr 5 04:50:41 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Thu, 5 Apr 2018 09:50:41 +0100 Subject: [Tutor] Using the Nimblenet Library in Python 3.6 In-Reply-To: References: Message-ID: On 05/04/18 09:39, Alan Gauld via Tutor wrote: > On 05/04/18 04:02, Mark Lawrence wrote: > >> Python 3.6 has more functionality than 2.7 by definition, but your >> comment implies, at least to me, that 2.7 and 3.6 are chalk and cheese. >> Nothing could be further from the truth and has regrettably been one of >> the reasons for the dreadfully slow uptake of Python 3. > > I disagree. Apart from the superficial language compatibility issues, > which 2.7 can partially address by importing from future, the libraries > are dramatically different. Any non trivial program runs into issues the > minute it starts importing modules. module names are different, function > names within those modules are different, return values and parameter > types are different. > > Given Python programming relies hugely on the modules in the standard > library I find it impossible to produce code that works across 2.7 > and 3.x without significant effort to force compatibility. That's why > tools like six etc exist. > > You may have been luckier than me but in my experience the gap > between the two versions is significant and not to be underestimated. > Mountains out of molehills. I suggest that you give up programming. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From zachary.ware+pytut at gmail.com Thu Apr 5 10:28:52 2018 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Thu, 5 Apr 2018 09:28:52 -0500 Subject: [Tutor] Using the Nimblenet Library in Python 3.6 In-Reply-To: References: Message-ID: On Thu, Apr 5, 2018 at 3:39 AM, Alan Gauld via Tutor wrote: > On 05/04/18 04:02, Mark Lawrence wrote: > >> Python 3.6 has more functionality than 2.7 by definition, but your >> comment implies, at least to me, that 2.7 and 3.6 are chalk and cheese. >> Nothing could be further from the truth and has regrettably been one of >> the reasons for the dreadfully slow uptake of Python 3. > > I disagree. Apart from the superficial language compatibility issues, > which 2.7 can partially address by importing from future, the libraries > are dramatically different. Any non trivial program runs into issues the > minute it starts importing modules. module names are different, function > names within those modules are different, return values and parameter > types are different. > > Given Python programming relies hugely on the modules in the standard > library I find it impossible to produce code that works across 2.7 > and 3.x without significant effort to force compatibility. That's why > tools like six etc exist. > > You may have been luckier than me but in my experience the gap > between the two versions is significant and not to be underestimated. I would appreciate keeping the FUD about the differences to a minimum :). The differences are there and they are significant, but far from insurmountable; in my experience, well-written Python 3 code is fairly trivial to port to Python 2/3. Depending on the quality of the initial Python 2 code, it's not quite as easy to port from 2 to 2/3 or directly to 3 due to Unicode/bytes confusion (made worse if the Python 2 code relies on bad Unicode practices), but they're still very much the same language. However, it does take some effort to port an established Python 2 codebase to Python 3, and to bring the thread back to the original topic, it looks like the author of the package in question is not particularly open to doing the work for their package (but may accept a pull request!). Christine (OP), you may have some luck with opening an issue at https://github.com/jorgenkg/python-neural-network and requesting Python 3 support. I don't have enough experience in the field of machine learning to competently suggest an alternative, but I've heard good things about TensorFlow. -- Zach From alan.gauld at yahoo.co.uk Thu Apr 5 13:12:33 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 5 Apr 2018 18:12:33 +0100 Subject: [Tutor] Using the Nimblenet Library in Python 3.6 In-Reply-To: References: Message-ID: On 05/04/18 15:28, Zachary Ware wrote: > I would appreciate keeping the FUD about the differences to a minimum > :). The differences are there and they are significant, but far from > insurmountable; in my experience, well-written Python 3 code is fairly > trivial to port to Python 2/3. I agree but we were speaking in the context of the OP who wanted to use a module written for 2.7 within 3.6. I stated that was unlikely to work because the differences in versions were significant. It is non trivial but not difficult to port code from one version to another. It is very much more effort to produce a single code base that works with both v2 and v3. And that was the context in which my comments should be taken. Speaking personally I write almost all my new code for v3 and keep my old code at v2. I have ported a few projects to v3. I have only written 2 projects that work on both versions and they were painful to do. That was what I was talking about in my response to Mark. It's not a reasonable thing to expect a module written for v2 to work in v3, it's too much work for most authors. They might consider porting it to v3 if they are not busy with other things but trying to build for dual versions is no fun at all. -- 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 From rls4jc at gmail.com Fri Apr 6 20:50:35 2018 From: rls4jc at gmail.com (Roger Lea Scherer) Date: Fri, 6 Apr 2018 17:50:35 -0700 Subject: [Tutor] Return problems Message-ID: So I've examined my educational material, I've perused the python documentation and for the life of me I can't figure out why return doesn't print a result in the IDE. I use print, the program acts as expected. What am I missing? import math from decimal import Decimal def findHypot(a, b): c = math.sqrt((a ** 2) + (b ** 2)) if c == (round(c)): return round(c) else: return round(c, 4) findHypot(3, 4) from the IDE: ===== RESTART: C:\Users\Roger\Documents\GitHub\LaunchCode\hypotenuse.py ===== >>> Thank you as always. -- Roger Lea Scherer 623.255.7719 From alan.gauld at yahoo.co.uk Sat Apr 7 03:58:38 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 7 Apr 2018 08:58:38 +0100 Subject: [Tutor] Return problems In-Reply-To: References: Message-ID: On 07/04/18 01:50, Roger Lea Scherer wrote: > So I've examined my educational material, I've perused the python > documentation and for the life of me I can't figure out why return doesn't > print a result in the IDE. I use print, the program acts as expected. What > am I missing? The fact that return doesn't print anything it just returns a value to the caller. I suspect that you are getting confused by the behaviour of the interactive prompt (>>>) which automatically prints results. But that is purely a feature of the >>> prompt designed to make developing/debugging code easier. In regular Python code execution values are only printed when you ask for them to be by using print. And if you think about it, that's a good thing, otherwise the screen would quickly fill with a mess of intermediate values the user doesn't need. -- 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 From steve at pearwood.info Sat Apr 7 04:36:50 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 7 Apr 2018 18:36:50 +1000 Subject: [Tutor] Return problems In-Reply-To: References: Message-ID: <20180407083647.GC16661@ando.pearwood.info> On Fri, Apr 06, 2018 at 05:50:35PM -0700, Roger Lea Scherer wrote: > So I've examined my educational material, I've perused the python > documentation and for the life of me I can't figure out why return doesn't > print a result in the IDE. I use print, the program acts as expected. What > am I missing? What IDE are you using? In the standard Python interactive interpreter, calling findHypot(3, 4) ought to print 5. The same applies in IDLE, or iPython. That's a convenience feature for interactive use. But if you use IDLE to run the file as a script, using the Run Module command, then you're running in "batch mode", so to speak, and results aren't printed unless you explicitly use the print command. So try changing your script to: print(findHypot(3, 4)) for Python 3, or print findHypot(3, 4) for Python 2. -- Steve From mauryasunilkumar98 at gmail.com Sun Apr 8 01:16:24 2018 From: mauryasunilkumar98 at gmail.com (SUNIL KUMAR MAURYA) Date: Sun, 08 Apr 2018 05:16:24 +0000 Subject: [Tutor] Problem regarding NZEC error Message-ID: NZEC error occurs often in Python 3.5. How can it be resolved? From alan.gauld at yahoo.co.uk Sun Apr 8 04:00:47 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 8 Apr 2018 09:00:47 +0100 Subject: [Tutor] Problem regarding NZEC error In-Reply-To: References: Message-ID: On 08/04/18 06:16, SUNIL KUMAR MAURYA wrote: > NZEC error occurs often in Python 3.5. How can it be resolved? A Non Zero Error Code occurs when the interpreter exits with an error. Those are very rarely errors in the interpreter they are due to errors in the code which it is executing. So, to stop getting NZEC errors, you must stop writing code that produces them. What kind of code would that be? It could be memory consumption issues (too much data for your RAM?, arithmetic overflows etc (although most of them get caught by Python's error handling - do you get error messages?) Memory access issues - are you using third party modules written in C? Do you have some code that generates an NZEC that you can share? We can then consider the cause. Also each NZEC error has an error code which tells you the underlying problem type. Which error codes are you seeing. -- 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 From media at 4barapp.com Tue Apr 10 03:55:02 2018 From: media at 4barapp.com (4bar) Date: Tue, 10 Apr 2018 03:55:02 -0400 Subject: [Tutor] Pip is giving me issues Message-ID: Hello! I keep getting the error message below. Any suggestions? Joshs-MacBook-Pro:~ Yoshi$ cd Desktop Joshs-MacBook-Pro:Desktop Yoshi$ cd instabot.py-master Joshs-MacBook-Pro:instabot.py-master Yoshi$ ls ISSUE_TEMPLATE.md README.md requirements.txt LICENSE example.py src Joshs-MacBook-Pro:instabot.py-master Yoshi$ pip install -r requirements Could not open requirements file: [Errno 2] No such file or directory: 'requirements' You are using pip version 9.0.1, however version 9.0.3 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Joshs-MacBook-Pro:instabot.py-master Yoshi$ ///// Pip version I have: Joshs-MacBook-Pro:instabot.py-master Yoshi$ pip --version pip 9.0.1 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (python 2.7) Joshs-MacBook-Pro:instabot.py-master Yoshi$ Thank you for your time, Josh From __peter__ at web.de Tue Apr 10 07:16:14 2018 From: __peter__ at web.de (Peter Otten) Date: Tue, 10 Apr 2018 13:16:14 +0200 Subject: [Tutor] Pip is giving me issues References: Message-ID: 4bar wrote: > I keep getting the error message below. Any suggestions? > Joshs-MacBook-Pro:instabot.py-master Yoshi$ ls > > ISSUE_TEMPLATE.md README.md requirements.txt > Joshs-MacBook-Pro:instabot.py-master Yoshi$ pip install -r requirements > > Could not open requirements file: [Errno 2] No such file or directory: > 'requirements' The error message seems pretty clear; looks like you must provide the file extension: $ pip install -r requirements.txt From adityamukherjee1999 at gmail.com Thu Apr 12 06:54:52 2018 From: adityamukherjee1999 at gmail.com (Aditya Mukherjee) Date: Thu, 12 Apr 2018 20:54:52 +1000 Subject: [Tutor] Help with a task Message-ID: Hello, I'm relatively new to python and have been given a task for university, and would greatly appreciate if anyone could help me basically just get started because as of now I am completely clueless and have no idea what to do. I don't really know who to ask, but anything would be of assistance. I feel like I understand the basics of functions such as lists, if/elif etc. but can't really comprehend what to do with anything involving more than the use of simple functions. Helping me getting started and just understanding what I actually need to do would be vastly helpful, as I've heard from other students that they have over 200 lines. :) I've attached the premise of what the task is asking for, due in 2 days. Thank you in advance Aditya Mukherjee. From david at graniteweb.com Thu Apr 12 11:48:50 2018 From: david at graniteweb.com (David Rock) Date: Thu, 12 Apr 2018 10:48:50 -0500 Subject: [Tutor] Help with a task In-Reply-To: References: Message-ID: > On Apr 12, 2018, at 05:54, Aditya Mukherjee wrote: > > Helping me getting started and just understanding what I actually need to > do would be vastly helpful, as I've heard from other students that they > have over 200 lines. :) > > I've attached the premise of what the task is asking for, due in 2 days. This list does not support attachments, so we can?t see what your requirements are. Also, we won?t do the work for you, so you need to help us help you by telling us what you have tried so far. We can give guidance, but only if we can see where you have tried to go. I would also recommend talking to your classmates that are having more success as a first attempt to get some direction. 2 days is not much time for a mailing list to help you understand an unknown assignment. ? David Rock david at graniteweb.com From alan.gauld at yahoo.co.uk Thu Apr 12 12:01:48 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 12 Apr 2018 17:01:48 +0100 Subject: [Tutor] Help with a task In-Reply-To: References: Message-ID: On 12/04/18 11:54, Aditya Mukherjee wrote: > Hello, I'm relatively new to python and have been given a task for > university, and would greatly appreciate if anyone could help me basically > just get started because as of now I am completely clueless and have no > idea what to do. I don't really know who to ask, but anything would be of > assistance. OK, First you need to tell us what the problem is, and as this is a text based email service non-text attachments get stripped by the server. You need to send a new mail explaining the problem, what you have tried, or think you should try. If you have any code, even if it doesn't work send it. That way we can clearly see where you are getting things wrong or are mis-understanding how things work. From your comments below it is obvious that you have not fully grasped the meaning of much of the programming jargon and terminology. Getting these details right helps discuss solutions without confusion arising. > I feel like I understand the basics of functions such as lists, > if/elif etc. These are not functions. Lists are data structures and if/else are control structures. Functions are something entirely different (and possibly you haven't covered them yet). > but can't really comprehend what to do with anything involving > more than the use of simple functions. Can you write a description in English (or your native tongue if not English) of what you want the computer to do to solve the problem? You need to understand how you would do it, without a computer, to be able to make the computer do it for you. If you can do that include it as requested above. > Helping me getting started and just understanding what I actually need to > do would be vastly helpful, as I've heard from other students that they > have over 200 lines. :) 1 good line is often better than 100 bad lines. Don't worry about volume, worry about results. > I've attached the premise of what the task is asking for, due in 2 days. We can't see the attachment, just send us the description in text (or a URL link if that's easier). And 2 days is maybe too late for this one - remember email can take 24 hours to be delivered, so by the time everyone receives/reads/responds and you get the answer 2 days could be up! -- 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 From shannonev101 at googlemail.com Thu Apr 12 12:44:09 2018 From: shannonev101 at googlemail.com (Shannon Evans) Date: Thu, 12 Apr 2018 17:44:09 +0100 Subject: [Tutor] Json files Message-ID: Hi, I have managed to get my two json files that need to be imported into python and then i need to write a program on who gets fruit and who doesn't and then need to get the final stock at the end. I was just wondering how i import the files into python and how i use them after that? [ ["James Bruce", "Bananas"], ["Katherine Newton", "Bananas"], ["Deborah Garcia", "Pears"], ["Marguerite Kozlowski", "Pineapples"], ["Kenneth Fitzgerald", "Pineapples"], ["Ronald Crawford", "Bananas"], ["Donald Haar", "Apples"], ["Al Whittenberg", "Bananas"], ["Max Bergevin", "Bananas"], ["Carlos Doby", "Pears"], ["Barry Hayes", "Pineapples"], ["Donald Haar", "Bananas"] ] this is one of them { "Apples": 14, "Bananas": 14, "Pineapples": 0, "Pears": 8 } and this another. Thanks From mats at wichmann.us Thu Apr 12 13:54:20 2018 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 12 Apr 2018 11:54:20 -0600 Subject: [Tutor] Json files In-Reply-To: References: Message-ID: On 04/12/2018 10:44 AM, Shannon Evans via Tutor wrote: > Hi, > I have managed to get my two json files that need to be imported into > python and then i need to write a program on who gets fruit and who doesn't > and then need to get the final stock at the end. I was just wondering how i > import the files into python and how i use them after that? You should start by looking at the json encoder/decoder, part of the standard Python library: https://docs.python.org/3/library/json.html From alan.gauld at yahoo.co.uk Thu Apr 12 16:50:58 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 12 Apr 2018 21:50:58 +0100 Subject: [Tutor] Json files In-Reply-To: References: Message-ID: On 12/04/18 17:44, Shannon Evans via Tutor wrote: > Hi, > I have managed to get my two json files that need to be imported into > python and then i need to write a program on who gets fruit and who doesn't > and then need to get the final stock at the end. I was just wondering how i > import the files into python and how i use them after that? Do you know how to read standard text files? And are you comfortable with dictionaries? If so the json module documentation should be easy to understand Have a look and come back with any specific questions. -- 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 From bscharrer at luc.edu Thu Apr 12 13:24:18 2018 From: bscharrer at luc.edu (Scharrer, Brianna) Date: Thu, 12 Apr 2018 17:24:18 +0000 Subject: [Tutor] PLEASE HELP Message-ID: Applications of basic language syntax Date/time string parsing Time stamps on data are often recorded in the standard ISO date and time format as shown below yyyy-mm-ddThh:mm:ss 2018-01-02T18:31:16 ----> 6:31pm on January 2, 2018 1999-02-14T21:02:37 ----> 9:02pm on February 14, 1999 Write code that when given a datetime string and outputs a human readable version exactly in the format specified below. [Morning, Noon, Afternoon, Evening, or Night], X:XX[am or pm] on [Month as a word] [day], [year] E.g. 1999-02-14T21:02:37 would be ?Night, 9:02pm on February 14, 1999? Do not use any datetime string function, though depending on your language they would be useful to do this in practice. Hint: you?ll have to cast strings to integers in order to perform the if statements which place [Morning, Noon?], [am or pm] and [Month as a word] appropriately. From alan.gauld at yahoo.co.uk Thu Apr 12 18:13:11 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 12 Apr 2018 23:13:11 +0100 Subject: [Tutor] PLEASE HELP In-Reply-To: References: Message-ID: On 12/04/18 18:24, Scharrer, Brianna wrote: What appears below seems to be an assignment of some kind. We do not do your work for you although we will offer help. But it is best if you show us your work so far, or at least describe how you intend to tackle it. In this case you are being given a string and need to convert it to a different format. It is not clear how you should expect to "be given" the input data but I assume you can read it from a user? In Python you have many string methods to help you slice/split the input into its parts. Look at the slice operations and the split method. Given the fixed lengths involved here slicing is probably easiest. The assignment suggests "casting" the string to integers but I think this is poor terminology and what is meant is that you should convert the substrings into integers (casting and conversion are very different things!). Get back to us when you have something to show us or a specific query. > Applications of basic language syntax > > Date/time string parsing > > Time stamps on data are often recorded in the standard ISO date and time format as shown below > > yyyy-mm-ddThh:mm:ss > > 2018-01-02T18:31:16 ----> 6:31pm on January 2, 2018 > > 1999-02-14T21:02:37 ----> 9:02pm on February 14, 1999 > > > Write code that when given a datetime string and outputs a human readable version exactly in the format specified below. > > [Morning, Noon, Afternoon, Evening, or Night], X:XX[am or pm] on [Month as a word] [day], [year] > > E.g. 1999-02-14T21:02:37 would be ?Night, 9:02pm on February 14, 1999? > > Do not use any datetime string function, though depending on your language they would be useful to do this in practice. > > Hint: you?ll have to cast strings to integers in order to perform the if statements which place [Morning, Noon?], [am or pm] and [Month as a word] appropriately. -- 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 From neilc at norwich.edu Fri Apr 13 10:24:33 2018 From: neilc at norwich.edu (Neil Cerutti) Date: Fri, 13 Apr 2018 14:24:33 +0000 (UTC) Subject: [Tutor] PLEASE HELP References: Message-ID: On 2018-04-12, Scharrer, Brianna wrote: > Applications of basic language syntax > > Date/time string parsing > > Time stamps on data are often recorded in the standard ISO date > and time format as shown below > > yyyy-mm-ddThh:mm:ss > > 2018-01-02T18:31:16 ----> 6:31pm on January 2, 2018 > > 1999-02-14T21:02:37 ----> 9:02pm on February 14, 1999 > > Write code that when given a datetime string and outputs a > human readable version exactly in the format specified below. I disagree that the first version isn't human readable. It is both human readable and stores the date/time in lexicographic order, which is extremly useful for both humans and machines. > [Morning, Noon, Afternoon, Evening, or Night], X:XX[am or pm] on [Month as a word] [day], [year] > > E.g. 1999-02-14T21:02:37 would be ?Night, 9:02pm on February 14, 1999? You will need to ask for clarification from your teacher for the deliniation between morning, noon, afternoon, evening and night. There's no international standard I'm aware of. > > Do not use any datetime string function, though depending on > your language they would be useful to do this in practice. They certainly would! > Hint: you?ll have to cast strings to integers in order to > perform the if statements which place [Morning, Noon?], [am or > pm] and [Month as a word] appropriately. With the exception of converting string to integer (as Alan discussed) I recommend using a dictionaries for conversion, rather than if statements, if you're allowed. -- Neil Cerutti From david at graniteweb.com Fri Apr 13 10:52:16 2018 From: david at graniteweb.com (David Rock) Date: Fri, 13 Apr 2018 09:52:16 -0500 Subject: [Tutor] PLEASE HELP In-Reply-To: References: Message-ID: <0513A17F-6B11-474F-AC3A-6E89C7DD1205@graniteweb.com> > On Apr 13, 2018, at 09:24, Neil Cerutti wrote: > > On 2018-04-12, Scharrer, Brianna wrote: >> Applications of basic language syntax >> >> Date/time string parsing >> >> Time stamps on data are often recorded in the standard ISO date >> and time format as shown below >> 1999-02-14T21:02:37 ----> 9:02pm on February 14, 1999 >> >> Write code that when given a datetime string and outputs a >> human readable version exactly in the format specified below. > > I disagree that the first version isn't human readable. It is > both human readable and stores the date/time in lexicographic > order, which is extremly useful for both humans and machines. Don?t nitpick the definition of ?human readable;? it isn?t relevant to the assignment and just serves to confuse the student. Using the phrase ?human readable? is just a poor choice for describing the assignment parameters: changing from one format to another (ISO -> ?standard English? (for lack of a better description of the target format). That?s the only thing that matters in this context. For the assignment, think about the following: How to separate the date from the time How to separate the YYYY-MM-DD into discreet variables How to convert the digit month into a Full name (e.g, convert 2 -> February) How to convert a 24-hour time into a 12-hour am/pm time How to print the bits into a specific format Tackle each part separately, and it should be fairly straightforward. Look up methods for parsing formatted strings as a place to start. ? David Rock david at graniteweb.com From neilc at norwich.edu Fri Apr 13 11:38:53 2018 From: neilc at norwich.edu (Neil Cerutti) Date: Fri, 13 Apr 2018 15:38:53 +0000 (UTC) Subject: [Tutor] PLEASE HELP References: <0513A17F-6B11-474F-AC3A-6E89C7DD1205@graniteweb.com> Message-ID: On 2018-04-13, David Rock wrote: > >> On Apr 13, 2018, at 09:24, Neil Cerutti wrote: >> >> On 2018-04-12, Scharrer, Brianna wrote: >>> Applications of basic language syntax >>> >>> Date/time string parsing >>> >>> Time stamps on data are often recorded in the standard ISO date >>> and time format as shown below >>> 1999-02-14T21:02:37 ----> 9:02pm on February 14, 1999 >>> >>> Write code that when given a datetime string and outputs a >>> human readable version exactly in the format specified below. >> >> I disagree that the first version isn't human readable. It is >> both human readable and stores the date/time in lexicographic >> order, which is extremly useful for both humans and machines. > > Don???t nitpick the definition of ???human readable;??? it > isn???t relevant to the assignment and just serves to confuse > the student. Using the phrase ???human readable??? is just a > poor choice for describing the assignment parameters: changing > from one format to another (ISO -> ???standard English??? (for > lack of a better description of the target format). That???s > the only thing that matters in this context. It is relevant to the assignment if the student hadn't noticed that the date was human readable. I was hoping to correct this possible misapprehension resulting from the poor assignment language. -- Neil Cerutti From mayoadams at gmail.com Fri Apr 13 14:53:54 2018 From: mayoadams at gmail.com (Mayo Adams) Date: Fri, 13 Apr 2018 14:53:54 -0400 Subject: [Tutor] PLEASE HELP In-Reply-To: References: <0513A17F-6B11-474F-AC3A-6E89C7DD1205@graniteweb.com> Message-ID: discrete, not discreet. On Fri, Apr 13, 2018 at 11:38 AM, Neil Cerutti wrote: > On 2018-04-13, David Rock wrote: > > > >> On Apr 13, 2018, at 09:24, Neil Cerutti wrote: > >> > >> On 2018-04-12, Scharrer, Brianna wrote: > >>> Applications of basic language syntax > >>> > >>> Date/time string parsing > >>> > >>> Time stamps on data are often recorded in the standard ISO date > >>> and time format as shown below > >>> 1999-02-14T21:02:37 ----> 9:02pm on February 14, 1999 > >>> > >>> Write code that when given a datetime string and outputs a > >>> human readable version exactly in the format specified below. > >> > >> I disagree that the first version isn't human readable. It is > >> both human readable and stores the date/time in lexicographic > >> order, which is extremly useful for both humans and machines. > > > > Don???t nitpick the definition of ???human readable;??? it > > isn???t relevant to the assignment and just serves to confuse > > the student. Using the phrase ???human readable??? is just a > > poor choice for describing the assignment parameters: changing > > from one format to another (ISO -> ???standard English??? (for > > lack of a better description of the target format). That???s > > the only thing that matters in this context. > > It is relevant to the assignment if the student hadn't noticed > that the date was human readable. I was hoping to correct this > possible misapprehension resulting from the poor assignment > language. > > -- > Neil Cerutti > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Mayo Adams 287 Erwin Rd. Chapel Hill, NC 27514 (919)-780-3917 mayoadams at gmail.com From pcpanchal123 at gmail.com Fri Apr 13 23:32:38 2018 From: pcpanchal123 at gmail.com (Pareshkumar Panchal) Date: Fri, 13 Apr 2018 23:32:38 -0400 Subject: [Tutor] csv merge with different column title Message-ID: Hi, I am trying to merge two csv files with following condition. filea.csv column( a1,a2,a3) fileb.csv column( b1,b2,b3) i wanted to merge if a1 & b1 reference (as the title is different but contents are valid for comparison). also the result i need is a1,a2,a3,b1,b2 (not b3). i am using openpyxl,csv modules at this moment. any help appreciated. Thank you, P From alan.gauld at yahoo.co.uk Sat Apr 14 04:40:41 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 14 Apr 2018 09:40:41 +0100 Subject: [Tutor] csv merge with different column title In-Reply-To: References: Message-ID: On 14/04/18 04:32, Pareshkumar Panchal wrote: > I am trying to merge two csv files with following condition. > > filea.csv column( a1,a2,a3) > fileb.csv column( b1,b2,b3) > > i wanted to merge if a1 & b1 reference (as the title is different but > contents are valid for comparison). also the result i need is > a1,a2,a3,b1,b2 (not b3). This doesn't have much to do with the data being CSV but applies to merging any data structures. Lets start by assuming the data is small enough to read both filers into memory as two lists. (If not the same principles apply but you have to do it from disk which will be slower) First read your two lists A and B. Sort both lists based on the first field. Start at the top of list A and search for a corresponding item in B. step through B processing each item until the field no longer matches step onto the next item in A and repeat the above process When you reach the end of either list you are done. There are some other (cleverer) ways to do this but that simple approach is how batch data processing has worked for the last 60 years. It's simple to write and debug. Try it, if it doesn't work for you show us what you've tried and any errors. -- 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 From __peter__ at web.de Sat Apr 14 06:32:22 2018 From: __peter__ at web.de (Peter Otten) Date: Sat, 14 Apr 2018 12:32:22 +0200 Subject: [Tutor] csv merge with different column title References: Message-ID: Pareshkumar Panchal wrote: > I am trying to merge two csv files with following condition. > > filea.csv column( a1,a2,a3) > fileb.csv column( b1,b2,b3) > > i wanted to merge if a1 & b1 reference (as the title is different but > contents are valid for comparison). If the column title is the problem you can rename it. > also the result i need is > a1,a2,a3,b1,b2 (not b3). Then delete that column either before or after the merge. > i am using openpyxl,csv modules at this moment. > > any help appreciated. Here's a demo using pandas: >>> import pandas as pd Read the original files: >>> left = pd.read_csv("left.csv") >>> left alpha beta gamma 0 foo one two 1 bar three four 2 baz five six 3 ham seven eight [4 rows x 3 columns] >>> right = pd.read_csv("right.csv") >>> right delta epsilon zeta 0 foo the quick 1 bar brown fox 2 baz jumps over 3 spam the lazy [4 rows x 3 columns] Merge by alpha/delta column: >>> left.merge(right, left_on="alpha", right_on="delta", how="outer") alpha beta gamma delta epsilon zeta 0 foo one two foo the quick 1 bar three four bar brown fox 2 baz five six baz jumps over 3 ham seven eight NaN NaN NaN 4 NaN NaN NaN spam the lazy [5 rows x 6 columns] >>> both = _ >>> import sys Write to file (or stream as I did for demonstration purposes): >>> both.to_csv(sys.stdout) ,alpha,beta,gamma,delta,epsilon,zeta 0,foo,one,two,foo,the,quick 1,bar,three,four,bar,brown,fox 2,baz,five,six,baz,jumps,over 3,ham,seven,eight,,, 4,,,,spam,the,lazy Delete a column you don't need (could have done that earlier): >>> del both["zeta"] >>> both.to_csv(sys.stdout) ,alpha,beta,gamma,delta,epsilon 0,foo,one,two,foo,the 1,bar,three,four,bar,brown 2,baz,five,six,baz,jumps 3,ham,seven,eight,, 4,,,,spam,the From chris_roysmith at internode.on.net Sat Apr 14 22:57:22 2018 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Sun, 15 Apr 2018 12:57:22 +1000 Subject: [Tutor] tkinter code executes before function returned Message-ID: <183547fe-0ad3-136b-c544-44af239411eb@internode.on.net> Hi, System: Python 3.6, Ubuntu Linux 17.10 I am trying to get tkinter to return a number from a window, which then sets how many times to print a sign. The code does not wait till the function returns a value, resulting in the signcount variable in having a None value, giving an output like below. Note that the output "line 64 ###?? The required number of signs is 5?? ###" only occurs after a number is input. I can use an input statement to get the desired output, but that's not good having to go to the terminal to enter the response. How can I get the printSign code to wait till getcount() returns it's value? Any help greatly appreciated. Regards, Chris Roy-Smith chris at chris-X451MA:~/Scripts/python3/dvms$ ./debug1.py line 27 ###?? required sign count for D is None?? ### Exception in Tkinter callback Traceback (most recent call last): ? File "/usr/lib/python3.6/tkinter/__init__.py", line 1702, in __call__ ??? return self.func(*args) ? File "./debug1.py", line 28, in printSign ??? for x in range(signcount): TypeError: 'NoneType' object cannot be interpreted as an integer line 64 ###?? The required number of signs is 5?? ### Code: #!/usr/bin/python3 from tkinter import * import os from reportlab.lib.units import cm from reportlab.lib.pagesizes import A4 from reportlab.pdfgen import canvas from reportlab.lib.utils import ImageReader def printSign(): ??? global gc, packages, rows ??? myCanvas = canvas.Canvas("Signs.pdf", pagesize=A4) ??? width, height = A4 #keep for ??? myCanvas.rotate(90) ??? myCanvas.setFillColorRGB(0,0,0) ??? myCanvas.setFont("Helvetica-Bold", 400) ??? TopMargin=-20 ??? LeftMargin=1 ??? Width=14 ??? Height=19 ??? VertPos=-15 ??? Bottom=-1 ??? a=[" " for i in range(rows)] ??? i=0 ??? sign=0 ??? for line in packages: ??????? signcount=getcount(line[1]) ??????? print('line 27 ###?? required sign count for {} is {} ###'.format(line[1], str(signcount))) ??????? for x in range(signcount): ??????????? #draw rectangle ??????????? myCanvas.rect(LeftMargin*cm+Width*sign*cm, TopMargin*cm, Width*cm, Height*cm, stroke=0, fill=1) myCanvas.drawCentredString((LeftMargin+(0.5*Width))*cm+(Width*sign)*cm, VertPos*cm, line[0]) ??????????? if sign==1: ??????????????? myCanvas.showPage() ??????????????? sign=0 ??????????????? myCanvas.rotate(90) ??????????????? i+=1 ??????????? else: ??????????????? sign+=1 ??????????????? i+=1 ??? myCanvas.showPage() ??? myCanvas.save() ??? if os.name == "posix": ??????? os.popen("evince %s" % ("Signs.pdf")) ??? if os.name == "nt": ??????? os.startfile('Signs.pdf') def getcount(SignText): ??? global gc,e ??? gc=Toplevel(master) ??? MsgText='How many copies of {} do you want to print?'.format(SignText) ??? Label(gc, text=MsgText).grid(row=0, column=0, sticky=(W,E)) ??? e = Entry(gc) ??? e.grid(row=0, column=1) ??? Button(gc, text='Okay', command=ReturnCount).grid(row=1, column=0, sticky=(W,E)) ??? Button(gc, text='Cancel', command=gc.destroy).grid(row=1, column=1, sticky=(W,E)) def ReturnCount(): ??? global gc,e ??? b0=e.get() ??? if b0 == None: ??????? b0=0 ??? gc.destroy() ??? print('line 64 ###?? The required number of signs is {} ###'.format(b0)) ??? return b0 master = Tk() master.title("Testing") packages = [[0,'D','drill'],[1,'J','Jointer'],[2,'B','Bandsaw']] rows = 3 b2 = Button(master, text="Print Signs", command=printSign).grid(row=4, column=0, sticky=(W,E), padx=5, pady=5) b3 = Button(master, text="Quit", command=master.destroy).grid(row=4, column=3, sticky=(W,E)) master.mainloop() From alan.gauld at yahoo.co.uk Sun Apr 15 04:10:27 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 15 Apr 2018 09:10:27 +0100 Subject: [Tutor] tkinter code executes before function returned In-Reply-To: <183547fe-0ad3-136b-c544-44af239411eb@internode.on.net> References: <183547fe-0ad3-136b-c544-44af239411eb@internode.on.net> Message-ID: On 15/04/18 03:57, Chris Roy-Smith wrote: > I am trying to get tkinter to return a number from a window, which then > sets how many times to print a sign. I don;t jhave time to look at this in detail just now, maybe later. But first impressions is that you have a very unorthodox style of Tkinter programming. Its more traditional to build the entire GUI up front rather than creating and destroying widgets each time you execute an event handler. Its less disturbing to the user than having things appear/disappear etc, as you seem to be doing. You can make widget hide/show/deactivate themselves without destroying them just by withdrawing/unpacking them etc or changing their status, if that's really what you want to do. > The code does not wait till the function returns a value, resulting in > the signcount variable in having a None value, giving an output like > below. I'll look at this a bit more closely later if nobody else answers by then... > Code: > > #!/usr/bin/python3 > from tkinter import * > import os > from reportlab.lib.units import cm > from reportlab.lib.pagesizes import A4 > from reportlab.pdfgen import canvas > from reportlab.lib.utils import ImageReader > > def printSign(): > ??? global gc, packages, rows > ??? myCanvas = canvas.Canvas("Signs.pdf", pagesize=A4) > ??? width, height = A4 #keep for > ??? myCanvas.rotate(90) > ??? myCanvas.setFillColorRGB(0,0,0) > ??? myCanvas.setFont("Helvetica-Bold", 400) > ??? TopMargin=-20 > ??? LeftMargin=1 > ??? Width=14 > ??? Height=19 > ??? VertPos=-15 > ??? Bottom=-1 All of the above could be done as part of your GUI initialisation. Its not needed in the event handler where it gets done every time the function is called. > ??? a=[" " for i in range(rows)] > ??? i=0 > ??? sign=0 > ??? for line in packages: > ??????? signcount=getcount(line[1]) This is where you call your function. Looking at it quickly I think you would be as well using the standard Tkinter simpledialogs/messagebox modules to get user input. Have you looked at the simpledialogs? Or better still having a static entry field on your GUI and just reading that? > ??????? print('line 27 ###?? required sign count for {} is {} > ###'.format(line[1], str(signcount))) > ??????? for x in range(signcount): > ??????????? #draw rectangle > ??????????? myCanvas.rect(LeftMargin*cm+Width*sign*cm, TopMargin*cm, > Width*cm, Height*cm, stroke=0, fill=1) > myCanvas.drawCentredString((LeftMargin+(0.5*Width))*cm+(Width*sign)*cm, > VertPos*cm, line[0]) > ??????????? if sign==1: > ??????????????? myCanvas.showPage() > ??????????????? sign=0 > ??????????????? myCanvas.rotate(90) > ??????????????? i+=1 > ??????????? else: > ??????????????? sign+=1 > ??????????????? i+=1 > > ??? myCanvas.showPage() > ??? myCanvas.save() > ??? if os.name == "posix": > ??????? os.popen("evince %s" % ("Signs.pdf")) > ??? if os.name == "nt": > ??????? os.startfile('Signs.pdf') > > def getcount(SignText): > ??? global gc,e > ??? gc=Toplevel(master) > ??? MsgText='How many copies of {} do you want to print?'.format(SignText) > ??? Label(gc, text=MsgText).grid(row=0, column=0, sticky=(W,E)) > ??? e = Entry(gc) > ??? e.grid(row=0, column=1) > ??? Button(gc, text='Okay', command=ReturnCount).grid(row=1, column=0, > sticky=(W,E)) > ??? Button(gc, text='Cancel', command=gc.destroy).grid(row=1, column=1, > sticky=(W,E)) I suspect the problem is because you have three callbacks each inside the other the second returns to your first before the third has a chance to run. This is all compounded by your dynamic GUI creation stuff. You need to restructure the code. More later. > def ReturnCount(): > ??? global gc,e > ??? b0=e.get() > ??? if b0 == None: > ??????? b0=0 > ??? gc.destroy() > ??? print('line 64 ###?? The required number of signs is {} > ###'.format(b0)) > ??? return b0 > > master = Tk() > master.title("Testing") > packages = [[0,'D','drill'],[1,'J','Jointer'],[2,'B','Bandsaw']] > rows = 3 > b2 = Button(master, text="Print Signs", command=printSign).grid(row=4, > column=0, sticky=(W,E), padx=5, pady=5) > b3 = Button(master, text="Quit", command=master.destroy).grid(row=4, > column=3, sticky=(W,E)) > > master.mainloop() -- 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 From alan.gauld at yahoo.co.uk Sun Apr 15 09:24:34 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 15 Apr 2018 14:24:34 +0100 Subject: [Tutor] tkinter code executes before function returned In-Reply-To: <183547fe-0ad3-136b-c544-44af239411eb@internode.on.net> References: <183547fe-0ad3-136b-c544-44af239411eb@internode.on.net> Message-ID: On 15/04/18 03:57, Chris Roy-Smith wrote: > The code does not wait till the function returns a value, OK, I've had a closet look now and can confirm the problem lies in your code structure. Its not event based. You need to understand event-driven programming better. In event driven code you initialise your program (including the GUI) and then wait for events - usually from the user. When you receive an event you handle that event - and only that specific event - before returning to the wait state until the user makes the next move. The problem with your code is that you build a basic GUI then wait. But when the user presses a button you create a second set of GUI elements and then *without waiting for the user to do anything* try to process the data in the new elements. That is why you get nothing back, the user hasn't done anything yet. The other, related, problem is that event handlers do NOT return values. You can't assign the value of an event callback method to a variable you have to use a global variable inside the event function. (Note: This is one reason GUIs are often built using objects because you can assign to an window object's attribute rather than a global variable, which makes for slightly cleaner code, easier to maintain.) So what to do? You need to break your data processing code out to a separate function(*) then call that only when you know there is data to be processed. That wont happen until you get the submit action from the user. So you need to call the data processing in your final callback - ReturnCount in your case. (*)Technically, you could just move the code into ReturnCount but that makes the callback code overly complex, better to keep reading and displaying as distinct operations. So the flow of action should be: initialise GUI and wait. on clock -> create extended GUI and wait on click -> create input dialog and wait on click -> read data and if valid -> call data processing function delete input dialog wait else -> reset fields and/or generate error message wait Note how the end of each step waits for the user to initiate the next action? That's the event driven bit. How do we wait? We do nothing, the GUI mainloop handles all that for us. > #!/usr/bin/python3 > from tkinter import * > import os > from reportlab.lib.units import cm > from reportlab.lib.pagesizes import A4 > from reportlab.pdfgen import canvas > from reportlab.lib.utils import ImageReader > > def printSign(): > ??? global gc, packages, rows > ??? myCanvas = canvas.Canvas("Signs.pdf", pagesize=A4) > ??? width, height = A4 #keep for > ??? myCanvas.rotate(90) > ??? myCanvas.setFillColorRGB(0,0,0) > ??? myCanvas.setFont("Helvetica-Bold", 400) > ??? TopMargin=-20 > ??? LeftMargin=1 > ??? Width=14 > ??? Height=19 > ??? VertPos=-15 > ??? Bottom=-1 > ??? a=[" " for i in range(rows)] > ??? i=0 > ??? sign=0 All the stuff below here should go in a function called something like display??? where ??? is whatever your data represents. > ??? for line in packages: > ??????? signcount=getcount(line[1]) > ??????? print('line 27 ###?? required sign count for {} is {} > ###'.format(line[1], str(signcount))) > ??????? for x in range(signcount): > ??????????? #draw rectangle > ??????????? myCanvas.rect(LeftMargin*cm+Width*sign*cm, TopMargin*cm, > Width*cm, Height*cm, stroke=0, fill=1) > myCanvas.drawCentredString((LeftMargin+(0.5*Width))*cm+(Width*sign)*cm, > VertPos*cm, line[0]) > ??????????? if sign==1: > ??????????????? myCanvas.showPage() > ??????????????? sign=0 > ??????????????? myCanvas.rotate(90) > ??????????????? i+=1 > ??????????? else: > ??????????????? sign+=1 > ??????????????? i+=1 > > ??? myCanvas.showPage() > ??? myCanvas.save() > ??? if os.name == "posix": > ??????? os.popen("evince %s" % ("Signs.pdf")) > ??? if os.name == "nt": > ??????? os.startfile('Signs.pdf') > > def getcount(SignText): > ??? global gc,e > ??? gc=Toplevel(master) > ??? MsgText='How many copies of {} do you want to print?'.format(SignText) > ??? Label(gc, text=MsgText).grid(row=0, column=0, sticky=(W,E)) > ??? e = Entry(gc) > ??? e.grid(row=0, column=1) > ??? Button(gc, text='Okay', command=ReturnCount).grid(row=1, column=0, > sticky=(W,E)) > ??? Button(gc, text='Cancel', command=gc.destroy).grid(row=1, column=1, > sticky=(W,E)) > > def ReturnCount(): > ??? global gc,e > ??? b0=e.get() > ??? if b0 == None: > ??????? b0=0 call display??? here > ??? gc.destroy() > ??? print('line 64 ###?? The required number of signs is {} > ###'.format(b0)) > ??? return b0 You need to make b0 (can't you think of a more informative name? count say?) Then you can access b0 from your display??? code > > master = Tk() > master.title("Testing") > packages = [[0,'D','drill'],[1,'J','Jointer'],[2,'B','Bandsaw']] > rows = 3 > b2 = Button(master, text="Print Signs", command=printSign).grid(row=4, > column=0, sticky=(W,E), padx=5, pady=5) > b3 = Button(master, text="Quit", command=master.destroy).grid(row=4, > column=3, sticky=(W,E)) > > master.mainloop() Finally, I still think you would be better creating all of the initial GUI window at the beginning (ie in the code just above this paragraph) And then your printSign() code will be much smaller, basically just a call to getcount() In fact you can probably eliminate all of printSign and make getcount the direct callback function of Button b2. (Please use better names in future it makes it much easier to read and comment on your code! Including for yourself in 6 months if you have to modify it!) For more on event driven programming principles read the event-driven programming and GUO topics in my web tutorial. HTH -- 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 From alan.gauld at yahoo.co.uk Sun Apr 15 09:36:57 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 15 Apr 2018 14:36:57 +0100 Subject: [Tutor] tkinter code executes before function returned In-Reply-To: References: <183547fe-0ad3-136b-c544-44af239411eb@internode.on.net> Message-ID: On 15/04/18 14:24, Alan Gauld via Tutor wrote: > OK, I've had a closet look now and can confirm the A closer look! Not a closet look. Ooops! :-/ -- 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 From chris_roysmith at internode.on.net Sun Apr 15 17:13:21 2018 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Mon, 16 Apr 2018 07:13:21 +1000 Subject: [Tutor] tkinter code executes before function returned In-Reply-To: References: <183547fe-0ad3-136b-c544-44af239411eb@internode.on.net> Message-ID: <5cf35055-287c-0cbb-7ebb-5eede873acd6@internode.on.net> On 15/04/18 23:36, Alan Gauld via Tutor wrote: > On 15/04/18 14:24, Alan Gauld via Tutor wrote: > >> OK, I've had a closet look now and can confirm the > A closer look! Not a closet look. Ooops! :-/ > > Thank you Alan, I have even more to learn than I thought. I have been bashing away at several OOP tutorials, but the penny still hasn't dropped on making that work yet, I get the concept, but don't seem to understand how to make it work for methods. Events just add to my lack of understanding. I'll have to try your tutorial. Hopefully I won't have too many questions after that. Regards, Chris Roy-Smith From mats at wichmann.us Sun Apr 15 18:18:16 2018 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 15 Apr 2018 16:18:16 -0600 Subject: [Tutor] tkinter code executes before function returned In-Reply-To: <5cf35055-287c-0cbb-7ebb-5eede873acd6@internode.on.net> References: <183547fe-0ad3-136b-c544-44af239411eb@internode.on.net> <5cf35055-287c-0cbb-7ebb-5eede873acd6@internode.on.net> Message-ID: <1bf5405d-9b10-74ae-ceb2-fa9adc0b5e9c@wichmann.us> On 04/15/2018 03:13 PM, Chris Roy-Smith wrote: > On 15/04/18 23:36, Alan Gauld via Tutor wrote: >> On 15/04/18 14:24, Alan Gauld via Tutor wrote: >> >>> OK, I've had a closet look now and can confirm the >> A closer look! Not a closet look. Ooops! :-/ >> >> > Thank you Alan, I have even more to learn than I thought. I have been > bashing away at several OOP tutorials, but the penny still hasn't > dropped on making that work yet, I get the concept, but don't seem to > understand how to make it work for methods. Events just add to my lack > of understanding. I'll have to try your tutorial. Hopefully I won't have > too many questions after that. By all means work through a tutorial that appeals to you! If there are OOP examples that aren't working for you, the people at this list can help: just like you did with the tkinter example, if you post the code, people have something to explain against. There are plenty of "lectures" on the Internet, we don't need to reproduce that role :) Object-oriented features help make Python more powerful, but you don't have to use them unless they help solve the problem at hand, unlike languages like C# and Java, where everything is object-oriented by design. From sandrasherif at yahoo.com Sun Apr 15 22:30:37 2018 From: sandrasherif at yahoo.com (Sandra Sherif) Date: Sun, 15 Apr 2018 19:30:37 -0700 Subject: [Tutor] Need help please Message-ID: Dear Python Tutor, I am in desperate need for help with programming on python. I am new to using python and I?m trying to write a program called ?turtle tag?. I?m trying to do these things in the program: a. Asks how many turtles are playing tag b. Creates a turtle, assigns it a random color, and assigns it a random starting position in the graphical window c. Randomly assigns one of the turtles to be ?it?, and marking that turtle in some way (like making it a special color no other turtle is allowed to be) d. Figures out which turtle is the closest to the turtle that is ?it? e. Moves the turtle that is it to a point where it is touching the closest other turtle Can you please show me how it should be written please? Thank you so much. ~ Sandra Loza From alan.gauld at yahoo.co.uk Mon Apr 16 03:38:48 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 16 Apr 2018 08:38:48 +0100 Subject: [Tutor] Need help please In-Reply-To: References: Message-ID: On 16/04/18 03:30, Sandra Sherif via Tutor wrote: > Dear Python Tutor, > > I am in desperate need for help with programming on python. I am new to using python and I?m trying to write a program called ?turtle tag?. I?m trying to do these things in the program: > a. Asks how many turtles are playing tag > b. Creates a turtle, assigns it a random color, and assigns it a random starting > position in the graphical window > c. Randomly assigns one of the turtles to be ?it?, and marking that turtle in some > way (like making it a special color no other turtle is allowed to be) > d. Figures out which turtle is the closest to the turtle that is ?it? > e. Moves the turtle that is it to a point where it is touching the closest other turtle > > Can you please show me how it should be written please? Thank you so much. When writing almost any program it helps to divide it into chunks that you know how to do. You can then build each chunk separate and gradually combine them to form the final complete program. Your problem has already been divided into chunks: a-e. So which bits of that can you do? And which bits puzzle you? a) Do you know how to get a number from a user? b1) Can you create a turtle? b2) Can you choose a random color value? b3) Can you set a turtles color? b4) Can you choose a random starting position? b5) Can you set a turtles position? c1) Can you create several turtles? c2) Can you store them in a list? c3) Can you choose a random list element? c4) can you store a reference to a list element? c5) Can you choose a color that no other turtle can take? (This may mean changing the way b2 works) d1) Can you work out the distance between two turtles? d2) Can you repeat that for each turtle in your list? e) Can you move a turtle to a new location? For each item that you know how to do write a small function (do you know how to write functions?) named after the action it performs. For example: getNumber(), createTurtle(), getRandomColor(), setColor(turtle, color) etc... Test each function. Remember that functions that get a value will need to have that value stored in a variable. Write another function that uses these function to complete each major step (a-e) above. eg initializeTurtle() could cover steps a and b. For the steps you don't know how to do, or those that don't work, come back here for help. Show us your code plus any error messages(in full) Hint: It may make life easier if you forget about the random aspects for now and get it working with fixed values - easier to debug... Once it works with fixed values introduce randomness bit by bit - say colors first then position. Divide and conquer is the key. HTH -- 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 From glenuk at gmail.com Mon Apr 16 07:10:30 2018 From: glenuk at gmail.com (Glen) Date: Mon, 16 Apr 2018 12:10:30 +0100 Subject: [Tutor] XML Programs Message-ID: Hey guys, I'm writing a save-game editor for a game I play (just a project to learn). But I am struggling on how to structure the code, how to store the xml data in data structure etc, Can anyone recommend some source I can review that reads and writes data from an xml file. Thanks, From leamhall at gmail.com Mon Apr 16 08:54:30 2018 From: leamhall at gmail.com (leam hall) Date: Mon, 16 Apr 2018 08:54:30 -0400 Subject: [Tutor] XML Programs In-Reply-To: References: Message-ID: On Mon, Apr 16, 2018 at 7:10 AM, Glen wrote: > Hey guys, > > I'm writing a save-game editor for a game I play (just a project to learn). > But I am struggling on how to structure the code, how to store the xml data > in data structure etc, > > Can anyone recommend some source I can review that reads and writes data > from an xml file. A friend's comment was "life is too short for XML". I like that. Have you considered JSON? Taking it a step further, MongoDB (JSON) or SQLite (SQL)? Both are pretty common and standard. While Python has its own stuff, like Pickle, that means you can only use Python. Using something like JSON or SQL means others can use the data and you get a chace to develop in a shared environment. :) From glenuk at gmail.com Mon Apr 16 09:07:45 2018 From: glenuk at gmail.com (Glen) Date: Mon, 16 Apr 2018 14:07:45 +0100 Subject: [Tutor] XML Programs In-Reply-To: References: Message-ID: I understand, I'd love to use something else but the save game files are in XML so I have no choice :'( On 16 April 2018 at 13:54, leam hall wrote: > On Mon, Apr 16, 2018 at 7:10 AM, Glen wrote: > > Hey guys, > > > > I'm writing a save-game editor for a game I play (just a project to > learn). > > But I am struggling on how to structure the code, how to store the xml > data > > in data structure etc, > > > > Can anyone recommend some source I can review that reads and writes data > > from an xml file. > > A friend's comment was "life is too short for XML". I like that. Have > you considered JSON? Taking it a step further, MongoDB (JSON) or > SQLite (SQL)? Both are pretty common and standard. > > While Python has its own stuff, like Pickle, that means you can only > use Python. Using something like JSON or SQL means others can use the > data and you get a chace to develop in a shared environment. :) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From leamhall at gmail.com Mon Apr 16 09:13:17 2018 From: leamhall at gmail.com (leam hall) Date: Mon, 16 Apr 2018 09:13:17 -0400 Subject: [Tutor] XML Programs In-Reply-To: References: Message-ID: Yeah, understood. Okay, knowing that others are smarter about python, and ElementTree, here's some code I was using to parse XML. Took a while to recover from. :) Leam ##### import xml.etree.ElementTree as ET import os import argparse import fnmatch def show_info(file, element): action = "" net_proto = "" trans_proto = "" r_port = "" l_port = "" direction = "" name = "" has_info = False f_name = "" id = element.attrib['name'] f_name = os.path.splitext(file)[0] for setting in element.iter('Setting'): if setting.attrib['name'] == 'Action': action = setting.attrib['value'] has_info = True elif setting.attrib['name'] == '+NetworkProtocol#0': net_proto = setting.attrib['value'] has_info = True elif setting.attrib['name'] == '+TransportProtocol#0': trans_proto = setting.attrib['value'] has_info = True elif setting.attrib['name'] == '+RemotePort#0': r_port = setting.attrib['value'] has_info = True elif setting.attrib['name'] == '+LocalPort#0': l_port = setting.attrib['value'] has_info = True elif setting.attrib['name'] == 'Direction': direction = setting.attrib['value'] has_info = True elif setting.attrib['name'] == 'Name': name = setting.attrib['value'] has_info = True if has_info: outfile.write("%s ; %s ; %s ; %s ; %s ; %s ; %s ; %s ; %s\n" % (f_name, id, name, action, net_proto, trans_proto, l_port, r_port, direction)) ## Main parser = argparse.ArgumentParser() parser.add_argument("-o", "--outfile", default = "new_out.csv", help="File to write to.") parser.add_argument("-d", "--dir", default = ".", help="Directory of the XML files.") args = parser.parse_args() indir = args.dir outfile = open(args.outfile, 'w') outfile.write("File ;Rule ID ;Name ;Action ; Network Protocol; Transport Protocol; Local Port; Remote Port; Direction\n") for file in os.listdir(indir): if fnmatch.fnmatch(file, '*.xml'): full_file = indir + "\\" + file tree = ET.parse(full_file) root = tree.getroot() for element in root.iter('PolicySettings'): show_info(file, element) outfile.close() #### On Mon, Apr 16, 2018 at 9:07 AM, Glen wrote: > I understand, I'd love to use something else but the save game files are in > XML so I have no choice :'( > > On 16 April 2018 at 13:54, leam hall wrote: >> >> On Mon, Apr 16, 2018 at 7:10 AM, Glen wrote: >> > Hey guys, >> > >> > I'm writing a save-game editor for a game I play (just a project to >> > learn). >> > But I am struggling on how to structure the code, how to store the xml >> > data >> > in data structure etc, >> > >> > Can anyone recommend some source I can review that reads and writes data >> > from an xml file. >> >> A friend's comment was "life is too short for XML". I like that. Have >> you considered JSON? Taking it a step further, MongoDB (JSON) or >> SQLite (SQL)? Both are pretty common and standard. >> >> While Python has its own stuff, like Pickle, that means you can only >> use Python. Using something like JSON or SQL means others can use the >> data and you get a chace to develop in a shared environment. :) >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor > > From george at fischhof.hu Mon Apr 16 12:27:38 2018 From: george at fischhof.hu (George Fischhof) Date: Mon, 16 Apr 2018 16:27:38 +0000 Subject: [Tutor] XML Programs In-Reply-To: References: Message-ID: Hi, Maybe you should give a try to xmltodict package George leam hall ezt ?rta (id?pont: 2018. ?pr. 16., H 15:14): > Yeah, understood. > > Okay, knowing that others are smarter about python, and ElementTree, > here's some code I was using to parse XML. Took a while to recover > from. :) > > Leam > > ##### > > import xml.etree.ElementTree as ET > import os > import argparse > import fnmatch > > def show_info(file, element): > action = "" > net_proto = "" > trans_proto = "" > r_port = "" > l_port = "" > direction = "" > name = "" > has_info = False > f_name = "" > > id = element.attrib['name'] > f_name = os.path.splitext(file)[0] > > for setting in element.iter('Setting'): > if setting.attrib['name'] == 'Action': > action = setting.attrib['value'] > has_info = True > elif setting.attrib['name'] == '+NetworkProtocol#0': > net_proto = setting.attrib['value'] > has_info = True > elif setting.attrib['name'] == '+TransportProtocol#0': > trans_proto = setting.attrib['value'] > has_info = True > elif setting.attrib['name'] == '+RemotePort#0': > r_port = setting.attrib['value'] > has_info = True > elif setting.attrib['name'] == '+LocalPort#0': > l_port = setting.attrib['value'] > has_info = True > elif setting.attrib['name'] == 'Direction': > direction = setting.attrib['value'] > has_info = True > elif setting.attrib['name'] == 'Name': > name = setting.attrib['value'] > has_info = True > > if has_info: > outfile.write("%s ; %s ; %s ; %s ; %s ; %s ; %s ; %s ; %s\n" % > (f_name, id, name, action, net_proto, trans_proto, l_port, r_port, > direction)) > > > > ## Main > parser = argparse.ArgumentParser() > parser.add_argument("-o", "--outfile", default = "new_out.csv", > help="File to write to.") > parser.add_argument("-d", "--dir", default = ".", help="Directory of > the XML files.") > args = parser.parse_args() > > indir = args.dir > outfile = open(args.outfile, 'w') > outfile.write("File ;Rule ID ;Name ;Action ; Network Protocol; > Transport Protocol; Local Port; Remote Port; Direction\n") > > for file in os.listdir(indir): > if fnmatch.fnmatch(file, '*.xml'): > full_file = indir + "\\" + file > tree = ET.parse(full_file) > root = tree.getroot() > for element in root.iter('PolicySettings'): > show_info(file, element) > > outfile.close() > > #### > > On Mon, Apr 16, 2018 at 9:07 AM, Glen wrote: > > I understand, I'd love to use something else but the save game files are > in > > XML so I have no choice :'( > > > > On 16 April 2018 at 13:54, leam hall wrote: > >> > >> On Mon, Apr 16, 2018 at 7:10 AM, Glen wrote: > >> > Hey guys, > >> > > >> > I'm writing a save-game editor for a game I play (just a project to > >> > learn). > >> > But I am struggling on how to structure the code, how to store the xml > >> > data > >> > in data structure etc, > >> > > >> > Can anyone recommend some source I can review that reads and writes > data > >> > from an xml file. > >> > >> A friend's comment was "life is too short for XML". I like that. Have > >> you considered JSON? Taking it a step further, MongoDB (JSON) or > >> SQLite (SQL)? Both are pretty common and standard. > >> > >> While Python has its own stuff, like Pickle, that means you can only > >> use Python. Using something like JSON or SQL means others can use the > >> data and you get a chace to develop in a shared environment. :) > >> _______________________________________________ > >> Tutor maillist - Tutor at python.org > >> To unsubscribe or change subscription options: > >> https://mail.python.org/mailman/listinfo/tutor > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From chris_roysmith at internode.on.net Mon Apr 16 05:09:23 2018 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Mon, 16 Apr 2018 19:09:23 +1000 Subject: [Tutor] tkinter code executes before function returned In-Reply-To: References: <183547fe-0ad3-136b-c544-44af239411eb@internode.on.net> Message-ID: <6cca6b00-183c-9fd4-f508-5ac22bbfc1a6@internode.on.net> On 15/04/18 18:10, Alan Gauld via Tutor wrote: > On 15/04/18 03:57, Chris Roy-Smith wrote: > >> I am trying to get tkinter to return a number from a window, which then >> sets how many times to print a sign. > I don;t jhave time to look at this in detail just now, maybe later. > > But first impressions is that you have a very unorthodox style of > Tkinter programming. Its more traditional to build the entire GUI > up front rather than creating and destroying widgets each time you > execute an event handler. Its less disturbing to the user than > having things appear/disappear etc, as you seem to be doing. > > You can make widget hide/show/deactivate themselves without > destroying them just by withdrawing/unpacking them etc or > changing their status, if that's really what you want to do. > >> The code does not wait till the function returns a value, resulting in >> the signcount variable in having a None value, giving an output like >> below. > I'll look at this a bit more closely later if nobody else > answers by then... > > This is where you call your function. Looking at it quickly > I think you would be as well using the standard Tkinter > simpledialogs/messagebox modules to get user input. > Have you looked at the simpledialogs? Thank you Alan, I didn't know of simpledialogs. That was all I needed to search documentation for these. I have now achieved what I was trying to do, with user interface as I was planning. > Or better still having a static entry field on your GUI > and just reading that? > I'll have to figure out how to achieve that! Perhaps my intended design is not in line with modern styles? Regards, Chris Roy-Smith From stefan_ml at behnel.de Tue Apr 17 14:42:14 2018 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 17 Apr 2018 20:42:14 +0200 Subject: [Tutor] XML Programs In-Reply-To: References: Message-ID: leam hall schrieb am 16.04.2018 um 14:54: > On Mon, Apr 16, 2018 at 7:10 AM, Glen wrote: >> I'm writing a save-game editor for a game I play (just a project to learn). >> But I am struggling on how to structure the code, how to store the xml data >> in data structure etc, >> >> Can anyone recommend some source I can review that reads and writes data >> from an xml file. > > A friend's comment was "life is too short for XML". I like that. Have > you considered JSON? Taking it a step further, MongoDB (JSON) or > SQLite (SQL)? Both are pretty common and standard. Actually, XML is pretty common and standard. But life's definitely too short for Mongo. Stefan From stefan_ml at behnel.de Tue Apr 17 14:56:03 2018 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 17 Apr 2018 20:56:03 +0200 Subject: [Tutor] XML Programs In-Reply-To: References: Message-ID: Glen schrieb am 16.04.2018 um 13:10: > I'm writing a save-game editor for a game I play (just a project to learn). > But I am struggling on how to structure the code, how to store the xml data > in data structure etc, > > Can anyone recommend some source I can review that reads and writes data > from an xml file. Here's a tutorial for the lxml package: http://lxml.de/tutorial.html However, I'd first check if there really is no Python library yet that handles your "game files", whatever format they may have. One of the most important things to learn about software engineering is to know when *not* to write code to solve a problem. If you end up having (or wanting) to deal with the bare XML format yourself, you may consider implementing your own XML API for your format, so that you can nicely assign functionality to certain tags in the document tree. See the section on "Implementing Namespaces" here: http://lxml.de/element_classes.html Stefan From glenuk at gmail.com Wed Apr 18 03:39:27 2018 From: glenuk at gmail.com (Glen) Date: Wed, 18 Apr 2018 08:39:27 +0100 Subject: [Tutor] XML Programs In-Reply-To: References: Message-ID: Hello Stefan, Thank you for this. That's actually quite helpful! Regards, On 17 April 2018 at 19:56, Stefan Behnel wrote: > Glen schrieb am 16.04.2018 um 13:10: > > I'm writing a save-game editor for a game I play (just a project to > learn). > > But I am struggling on how to structure the code, how to store the xml > data > > in data structure etc, > > > > Can anyone recommend some source I can review that reads and writes data > > from an xml file. > > Here's a tutorial for the lxml package: > > http://lxml.de/tutorial.html > > However, I'd first check if there really is no Python library yet that > handles your "game files", whatever format they may have. One of the most > important things to learn about software engineering is to know when *not* > to write code to solve a problem. > > If you end up having (or wanting) to deal with the bare XML format > yourself, you may consider implementing your own XML API for your format, > so that you can nicely assign functionality to certain tags in the document > tree. See the section on "Implementing Namespaces" here: > > http://lxml.de/element_classes.html > > Stefan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From Joshua2022064 at outlook.com Wed Apr 18 21:15:59 2018 From: Joshua2022064 at outlook.com (Joshua Nghe) Date: Thu, 19 Apr 2018 01:15:59 +0000 Subject: [Tutor] Help! Message-ID: Hi, This is Joshua N from Campus Middle School. In my science class, we are learning topics of our choice. I chose coding as my project, and I am contacting you to ask about certain projects which would be appropriate for a programmer at a beginner level. I only have 15 hours on Python, however I expect to get to at least double that when I start to work on my project. What projects would be appropriate for someone of my level. From Joshua2022064 at outlook.com Wed Apr 18 21:34:14 2018 From: Joshua2022064 at outlook.com (Joshua Nghe) Date: Thu, 19 Apr 2018 01:34:14 +0000 Subject: [Tutor] Beginner Level Projects Message-ID: Hi, This is Joshua N from Campus Middle School in CO. Our science class is collectively participating in a project that consumes 20% of our classtime every week. For my project, I chose to learn Python, and create something from what I learned. I currently have around 10 hours spent learning Python, and predict to have at least 20 by the time I start my project. According to my level of experience, what would be an interesting project that would exihbit all that I've learned. Thanks, Joshua From niharika1883 at gmail.com Thu Apr 19 04:45:07 2018 From: niharika1883 at gmail.com (Niharika Jakhar) Date: Thu, 19 Apr 2018 10:45:07 +0200 Subject: [Tutor] File handling Tab separated files Message-ID: Hi I want to store a file from BioGRID database (tab separated file, big data) into a data structure(I prefer lists, please let me know if another would be better) and I am trying to print the objects. Here?s my code: class BioGRIDReader: def __init__(self, filename): with open('filename', 'r') as file_: read_data = f.read() for i in file_ : read_data = (i.split('\t')) return (objects[:100]) a = BioGRIDReader print (a.__init__(test_biogrid.txt)) Here's what the terminal says: Traceback (most recent call last): File "./BioGRIDReader.py", line 23, in print (a.__init__(test_biogrid.txt)) NameError: name 'test_biogrid' is not defined The file named test_biogrid.txt do exist in the same folder as this program. I am unable to go further with this code. Kindly help me out. Thanks and regards NIHARIKA From rls4jc at gmail.com Wed Apr 18 19:39:27 2018 From: rls4jc at gmail.com (Roger Lea Scherer) Date: Wed, 18 Apr 2018 16:39:27 -0700 Subject: [Tutor] beginning encryption Message-ID: I am absolutely stumped. I've tried a number of different scenarios and copied the answer more than I like, but I still can't figure this out. I don't want to copy the answer verbatim because then I won't learn. I'm doing the beginning cipher, mix up the letters routine. I get the entire Gettysburg address with no alterations in this form of the code (and a few others I've tried). I do not receive any error, but I expect the character in the Gettysburg address to change to the index position in the encryption variable. What am I not getting? Thank you as always. address = """Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.""" alphabet = "abcdefghijklmnopqrstuvwxyz" encryption = "nopqrstuvwxyzabcdefghijklm" def encryptor(address): encrypted = "" for char in address: if char != alphabet: encrypted += char else: pos = alphabet.index(char) encrypted += encryption[pos] print(encrypted) encryptor(address) -- Roger Lea Scherer 623.255.7719 From steve at pearwood.info Thu Apr 19 08:46:46 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 19 Apr 2018 22:46:46 +1000 Subject: [Tutor] beginning encryption In-Reply-To: References: Message-ID: <20180419124646.GS11616@ando.pearwood.info> Hi Roger, and welcome. See my comments below. On Wed, Apr 18, 2018 at 04:39:27PM -0700, Roger Lea Scherer wrote: > def encryptor(address): > encrypted = "" > for char in address: > if char != alphabet: > encrypted += char > else: > pos = alphabet.index(char) > encrypted += encryption[pos] > print(encrypted) For each character in the Gettysburg address, you compare the individual single character "F", "o", "u", "r", etc against the ENTIRE alphabet "abcde...xyz". Since a single letter is never equal to 26 letters, you always get char != alphabet # they are never equal and so you always add the char unchanged to the encrypted text. Try this instead: if char not in alphabet: encrypted += char Then once you have that working, write back (with a shorter extract of the Gettysburgh address please!) and we'll see how else we can improve your code. -- Steve From steve at pearwood.info Thu Apr 19 09:29:16 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 19 Apr 2018 23:29:16 +1000 Subject: [Tutor] Help! In-Reply-To: References: Message-ID: <20180419132916.GT11616@ando.pearwood.info> Hello Joshua, and welcome! My comments below. On Thu, Apr 19, 2018 at 01:15:59AM +0000, Joshua Nghe wrote: > Hi, > This is Joshua N from Campus Middle School. You are talking to people from all over the world, and some of us are not familiar with what you mean by "Middle School". What is it? About what age group are you in? > In my science class, we > are learning topics of our choice. I chose coding as my project, and I > am contacting you to ask about certain projects which would be > appropriate for a programmer at a beginner level. I only have 15 hours > on Python, however I expect to get to at least double that when I > start to work on my project. What projects would be appropriate for > someone of my level. Very simple projects! 30 hours is not a lot of time to learn a programming language if your aim is a complex project. Python is a great language and I love it, but unfortunately the graphics capabilities are very primitive, and even when you can get them working, it might take many more hours to become productive with them. So unless you have a LOT of help from your class, I recommand you stick to a text-based program. Does your project have to be oriented towards science, or can it be anything at all? Some program ideas... Encrypt and decrypt text using a Caesar Cipher. https://en.wikipedia.org/wiki/Caesar_cipher or one of many other simple ciphers. Calculate the prime numbers up to a certain limit. https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes Play "Guess the number" games. Play 20 Questions. (Advanced) Given the formula for a (simple) molecule like H20, print the names of the elements in the molecule, their atomic weights, and the total molecular weight. Convert between temperature scales (degrees Celsius, Fahrenheit, and Kelvin). Try reading here: http://www.programmingforbeginnersbook.com/blog/what_should_i_make_beginner_programming_project_ideas/ Hope this helps! -- Steve From steve at pearwood.info Thu Apr 19 09:37:17 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 19 Apr 2018 23:37:17 +1000 Subject: [Tutor] File handling Tab separated files In-Reply-To: References: Message-ID: <20180419133717.GU11616@ando.pearwood.info> On Thu, Apr 19, 2018 at 10:45:07AM +0200, Niharika Jakhar wrote: > Hi > I want to store a file from BioGRID database (tab separated file, big data) > into a data structure(I prefer lists, please let me know if another would > be better) and I am trying to print the objects. You should probably look at using the csv module. It can handle tab-separated files too. https://docs.python.org/3/library/csv.html https://docs.python.org/2/library/csv.html > Here?s my code: > class BioGRIDReader: > def __init__(self, filename): > with open('filename', 'r') as file_: > read_data = f.read() > for i in file_ : > read_data = (i.split('\t')) > return (objects[:100]) > > a = BioGRIDReader > print (a.__init__(test_biogrid.txt)) You have two errors here. Firstly, you should not call a.__init__ directly. You never call methods with two leading and trailing underscores directly: always let Python call them for you. Instead, you call: a = BioGRIDReader(filename) Your second error is the file name. You write: test_biogrid.txt but to Python, that looks like a variable called test_biogrid, which does not exist. That is why you get the error: > Here's what the terminal says: > Traceback (most recent call last): > File "./BioGRIDReader.py", line 23, in > print (a.__init__(test_biogrid.txt)) > NameError: name 'test_biogrid' is not defined The solution is to quote the name of the file, so Python treats it as a string: a = BioGRIDReader("test_biogrid.txt") By the way, THANK YOU for quoting the whole error message! That is much better than people who just say "it doesn't work" and leave us to guess what happens. -- Steve From wolfgang.maier at biologie.uni-freiburg.de Thu Apr 19 09:57:40 2018 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Thu, 19 Apr 2018 15:57:40 +0200 Subject: [Tutor] File handling Tab separated files In-Reply-To: References: Message-ID: <741113f1-cb9f-af6c-5884-7153c1c47b43@biologie.uni-freiburg.de> On 04/19/2018 10:45 AM, Niharika Jakhar wrote: > Hi > I want to store a file from BioGRID database (tab separated file, big data) > into a data structure(I prefer lists, please let me know if another would > be better) and I am trying to print the objects. > Here?s my code: > class BioGRIDReader: > def __init__(self, filename): > with open('filename', 'r') as file_: > read_data = f.read() > for i in file_ : > read_data = (i.split('\t')) > return (objects[:100]) > > a = BioGRIDReader > print (a.__init__(test_biogrid.txt)) > In addition to your immediate problem, which Steven explained already, you will run into more issues with the posted code: 1) in your open() call you have filename quoted This is kind of the opposite of the mistake Steven points out. Here, filename really is an identifier known to Python, but by quoting it it, you will always try to open a file literally named 'filename' 2) wrong indentation after the "with open( ..." line with starts a block of indented code, in which you will have access to the opened file 3) file_ is the identifier under which you will have access to the input file's content, but on the very next line you're trying to use f.read(). f won't have any meaning for Python at that point 4) Even if you used file_.read() at that step, it would be wrong because in the subsequent for loop you are trying to consume file_ line by line. However, if you read() all of file_ before, there won't be anything left to loop over. 5) You are reading the data into a list called read_data, but you are trying to return a slice of an identifier objects, which Python will not know about when it gets there 6) As Steven said, you shouldn't call __init__() directly, but that also means that you should not return data from it. Instead you might want to only parse the file contents in the __init__ method and store that data as an attribute in self (e.g., use self.data for this). Then you could use things like this: a = BioGRIDReader('test_biogrid.txt') print(a.data[:100]) Best, Wolfgang From glenuk at gmail.com Thu Apr 19 10:32:19 2018 From: glenuk at gmail.com (Glen) Date: Thu, 19 Apr 2018 15:32:19 +0100 Subject: [Tutor] XML Programs In-Reply-To: References: Message-ID: Hey guys, I have the following code: https://repl.it/@glendog/HurtfulPunctualInterface Using the function I have define I can print to screen a list of books. However, how can I search for records within the xml using an ID or the title of the book etc? I've tried reading the tutorial but the penny is not dropping. On 18 April 2018 at 08:39, Glen wrote: > Hello Stefan, > > Thank you for this. That's actually quite helpful! > > Regards, > > On 17 April 2018 at 19:56, Stefan Behnel wrote: > >> Glen schrieb am 16.04.2018 um 13:10: >> > I'm writing a save-game editor for a game I play (just a project to >> learn). >> > But I am struggling on how to structure the code, how to store the xml >> data >> > in data structure etc, >> > >> > Can anyone recommend some source I can review that reads and writes data >> > from an xml file. >> >> Here's a tutorial for the lxml package: >> >> http://lxml.de/tutorial.html >> >> However, I'd first check if there really is no Python library yet that >> handles your "game files", whatever format they may have. One of the most >> important things to learn about software engineering is to know when *not* >> to write code to solve a problem. >> >> If you end up having (or wanting) to deal with the bare XML format >> yourself, you may consider implementing your own XML API for your format, >> so that you can nicely assign functionality to certain tags in the >> document >> tree. See the section on "Implementing Namespaces" here: >> >> http://lxml.de/element_classes.html >> >> Stefan >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > From mats at wichmann.us Thu Apr 19 10:50:19 2018 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 19 Apr 2018 08:50:19 -0600 Subject: [Tutor] File handling Tab separated files In-Reply-To: <741113f1-cb9f-af6c-5884-7153c1c47b43@biologie.uni-freiburg.de> References: <741113f1-cb9f-af6c-5884-7153c1c47b43@biologie.uni-freiburg.de> Message-ID: On 04/19/2018 07:57 AM, Wolfgang Maier wrote: > On 04/19/2018 10:45 AM, Niharika Jakhar wrote: >> Hi >> I want to store a file from BioGRID database (tab separated file, big >> data) >> into a data structure(I prefer lists, please let me know if another would >> be better) and I am trying to print the objects. >> Here?s my code: >> class BioGRIDReader: >> ???? def __init__(self, filename): >> ???????????? with open('filename', 'r') as file_: >> ???????????? read_data = f.read() >> ???????????? for i in file_ : >> ???????????????? read_data = (i.split('\t')) >> ???????????????? return (objects[:100]) >> >> a = BioGRIDReader >> print (a.__init__(test_biogrid.txt)) >> > > In addition to your immediate problem, which Steven explained already, > you will run into more issues with the posted code: In addition to this low level advice, let me observe that whenever the term "big data" is tossed into the discussion, you want to consider whether reading it all in to Python's memory into a "simple" data structure in one go is what you want to do. You may want to look into the Pandas project (possibly after spending a little more time becoming comfortable with Python itself first): https://pandas.pydata.org/ Pandas has its own file handling code (particularly, a read_csv function) which might end up being useful. Also quite by chance, I happen to know there's an existing project to interact with the BioGRID web service, have no idea if that would be a match for any of your needs. A quick google to refind it: https://github.com/arvkevi/biogridpy From wrw at mac.com Thu Apr 19 10:51:41 2018 From: wrw at mac.com (William Ray Wing) Date: Thu, 19 Apr 2018 10:51:41 -0400 Subject: [Tutor] Beginner Level Projects In-Reply-To: References: Message-ID: > On Apr 18, 2018, at 9:34 PM, Joshua Nghe wrote: > > Hi, > > This is Joshua N from Campus Middle School in CO. Our science class is collectively participating in a project that consumes 20% of our classtime every week. For my project, I chose to learn Python, and create something from what I learned. I currently have around 10 hours spent learning Python, and predict to have at least 20 by the time I start my project. According to my level of experience, what would be an interesting project that would exihbit all that I've learned. > Since we don?t know your interests, or how fast your are progressing, it is a bit hard to make suggestions. But if you were to Google ?simple python projects? you will get a page full of sites with lists and suggestions of fun starter projects. Bill > > Thanks, > > Joshua > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From david at graniteweb.com Thu Apr 19 12:49:59 2018 From: david at graniteweb.com (David Rock) Date: Thu, 19 Apr 2018 11:49:59 -0500 Subject: [Tutor] Beginner Level Projects In-Reply-To: References: Message-ID: <7F3A70AE-7C69-4FA0-B158-D76A23AE7ED0@graniteweb.com> > On Apr 18, 2018, at 20:34, Joshua Nghe wrote: > > Hi, > > This is Joshua N from Campus Middle School in CO. Our science class is collectively participating in a project that consumes 20% of our classtime every week. For my project, I chose to learn Python, and create something from what I learned. I currently have around 10 hours spent learning Python, and predict to have at least 20 by the time I start my project. According to my level of experience, what would be an interesting project that would exihbit all that I've learned. Start by thinking about problems you would like to solve. Is there something that you wish you could do easier? Are you in 6th grade, or 8th? What other kinds of classes are you in where python might help? For example, have you done any graphing in a Math class? making a graph of a function using text is a pretty complete exercise that might help you visualize problems (and help with you other classes, too). Are you interested in making things move or turning stuff on/off? Using python on a Raspberry Pi is a great way to start with using a computer to control the ?real world.? ? David Rock david at graniteweb.com From __peter__ at web.de Thu Apr 19 12:57:02 2018 From: __peter__ at web.de (Peter Otten) Date: Thu, 19 Apr 2018 18:57:02 +0200 Subject: [Tutor] XML Programs References: Message-ID: Glen wrote: > Hey guys, > > I have the following code: > > https://repl.it/@glendog/HurtfulPunctualInterface from lxml import etree catalog = etree.parse("example.xml") def getbooks(xmldata): books = xmldata.xpath("//catalog")[0] for item in books: print(item.findtext("title")) getbooks(catalog) > Using the function I have define I can print to screen a list of books. > However, how can I search for records within the xml using an ID or the > title of the book etc? I've tried reading the tutorial but the penny is > not dropping. As a rule of thumb do not print() anything in functions that process your data. Have them return something instead, like a list of titles that may be searched, sorted, or filtered later. The final result of this can then be printed or displayed in a GUI or webpage, written to a file, or saved in a database. In that spirit I would rewrite your getbooks() function as def get_book_titles(xmldata): books = xmldata.xpath("//catalog")[0] return [book.findtext("title") for book in books] Now you have something to work with. You provide an xml tree and get a list of book titles. As long as you don't change that you can rewrite the implementation without breaking the rest of your script. Now let's suppose we want to find all titles containing some words the user can provide. We break the task into tiny subtasks: - break a string into words - search a string for words - filter a list of titles def get_words(title): return title.split() def has_words(words, title): title_words = get_words(title) return all(word in title_words for word in words) def find_matching_titles(titles, words): return [title for title in titles if has_words(title, words)] We can check if the above works with a few lines of code: catalog = etree.parse("example.xml") titles = get_book_titles(catalog) print(find_matching_titles(titles, ["Guide"])) Seems to work. What we really should be doing is to write unit tests for every function. If turns out that our program sometimes doesn't work as we would like it to we can identify the lacking function and improve only that. Let's say you want to make the search case-insensitive. That should be possible by having get_words() return case-folded strings. Finally we can add a simple user interface: def lookup_title(titles): while True: try: words = get_words(input("Print titles containing all words: ")) except EOFError: break matching_titles = find_matching_titles(titles, words) if matching_titles: for i, title in enumerate(matching_titles): print(i, title) else: print("no matches") print() print("That's all, folks") if __name__ == "__main__": catalog = etree.parse("example.xml") titles = get_book_titles(catalog) lookup_title(titles) If instead of just titles you want to process book objects class Book: def __init__(self, title, author): self.title = title self.author = author you can only reuse some of the functions, but you can keep the structure of the script. For example get_book_titles() could be replaced with def get_books(xmldata): books = xmldata.xpath("//catalog")[0] return [ Book( book.findtext("title"), book.findtext("author") ) for book in books ] and the filter function could be modified def find_matching_titles(books, words): return [book for book in book if has_words(book.title, words)] You may even change the above to search words in an arbitrary attribute of the book instance def find_matching_books(books, words, attribute): return [ book for book in books if has_words(getattr(book, attribute), words) ] From neilc at norwich.edu Thu Apr 19 14:25:12 2018 From: neilc at norwich.edu (Neil Cerutti) Date: Thu, 19 Apr 2018 18:25:12 +0000 (UTC) Subject: [Tutor] Help! References: <20180419132916.GT11616@ando.pearwood.info> Message-ID: On 2018-04-19, Steven D'Aprano wrote: > Some program ideas... > > Encrypt and decrypt text using a Caesar Cipher. > > https://en.wikipedia.org/wiki/Caesar_cipher > > or one of many other simple ciphers. > > Calculate the prime numbers up to a certain limit. > > https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes > > Play "Guess the number" games. > > Play 20 Questions. > > (Advanced) > Given the formula for a (simple) molecule like H20, print the names of > the elements in the molecule, their atomic weights, and the total > molecular weight. > > Convert between temperature scales (degrees Celsius, Fahrenheit, and > Kelvin). Excellent suggestions. It could also be fun to build a text-based solitaire program of your choice. -- Neil Cerutti From niharika1883 at gmail.com Thu Apr 19 12:50:25 2018 From: niharika1883 at gmail.com (Niharika Jakhar) Date: Thu, 19 Apr 2018 18:50:25 +0200 Subject: [Tutor] File handling Tab separated files In-Reply-To: References: <741113f1-cb9f-af6c-5884-7153c1c47b43@biologie.uni-freiburg.de> Message-ID: Hi again I tried re-writing the code with all your advices(i assume to cover all of them). I have extended the code a little bit to store the data in the form of lists and am trying to access it. I also changed the file name to BioGRID.txt Here's what I wrote(Please ignore the identation, there was no such error, it's just the e-mail thingy.): import csv class BioGRIDReader: def __init__(self, filename): with open('filename', 'rb') as f: self.reader = csv.reader(f, delimiter='\t') self.object_ = self.reader.split['\n'] for row in range(len(object_)): for r in range(len(row)): if (r[-1] == r[-2]): return (r[2], r[3]) #returns pair of taxon ids a = BioGRIDReader('BioGRID.txt') print(a.object[:100]) here's what the compiler says: Traceback (most recent call last): File "./BioGRIDReader.py", line 17, in a = BioGRIDReader('BioGRID.txt') File "./BioGRIDReader.py", line 4, in __init__ with open('filename', 'rb') as f: IOError: [Errno 2] No such file or directory: 'filename' I am extremely sorry if I have repeated a same mistake again, but I did what I understood. Thanks again for the links and I am not allowed to use different packages and have to strictly use the standard python library. But thanks anyway, it will be of great help in future. :) On Thu, Apr 19, 2018 at 4:50 PM, Mats Wichmann wrote: > On 04/19/2018 07:57 AM, Wolfgang Maier wrote: > > On 04/19/2018 10:45 AM, Niharika Jakhar wrote: > >> Hi > >> I want to store a file from BioGRID database (tab separated file, big > >> data) > >> into a data structure(I prefer lists, please let me know if another > would > >> be better) and I am trying to print the objects. > >> Here?s my code: > >> class BioGRIDReader: > >> def __init__(self, filename): > >> with open('filename', 'r') as file_: > >> read_data = f.read() > >> for i in file_ : > >> read_data = (i.split('\t')) > >> return (objects[:100]) > >> > >> a = BioGRIDReader > >> print (a.__init__(test_biogrid.txt)) > >> > > > > In addition to your immediate problem, which Steven explained already, > > you will run into more issues with the posted code: > > In addition to this low level advice, let me observe that whenever the > term "big data" is tossed into the discussion, you want to consider > whether reading it all in to Python's memory into a "simple" data > structure in one go is what you want to do. You may want to look into > the Pandas project (possibly after spending a little more time becoming > comfortable with Python itself first): > > https://pandas.pydata.org/ > > Pandas has its own file handling code (particularly, a read_csv > function) which might end up being useful. > > > Also quite by chance, I happen to know there's an existing project to > interact with the BioGRID web service, have no idea if that would be a > match for any of your needs. A quick google to refind it: > > https://github.com/arvkevi/biogridpy > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From shlyoko at gmail.com Thu Apr 19 08:27:32 2018 From: shlyoko at gmail.com (Emil Natan) Date: Thu, 19 Apr 2018 15:27:32 +0300 Subject: [Tutor] beginning encryption In-Reply-To: References: Message-ID: if char != alphabet: should be if char not in alphabet: Otherwise you are comparing char with alphabet. What you want to do if to check if char is in alphabet. Emil On Thu, Apr 19, 2018 at 2:39 AM, Roger Lea Scherer wrote: > I am absolutely stumped. I've tried a number of different scenarios and > copied the answer more than I like, but I still can't figure this out. I > don't want to copy the answer verbatim because then I won't learn. > > I'm doing the beginning cipher, mix up the letters routine. I get the > entire Gettysburg address with no alterations in this form of the code (and > a few others I've tried). I do not receive any error, but I expect the > character in the Gettysburg address to change to the index position in the > encryption variable. > > What am I not getting? > > Thank you as always. > > > > address = """Four score and seven years ago our fathers brought forth on > this continent, a new nation, > conceived in Liberty, and dedicated to the proposition that all men are > created equal. > Now we are engaged in a great civil war, testing whether that nation, or > any nation so conceived > and so dedicated, can long endure. We are met on a great battle-field of > that war. We have come > to dedicate a portion of that field, as a final resting place for those who > here gave their lives that > that nation might live. It is altogether fitting and proper that we should > do this. > But, in a larger sense, we can not dedicate -- we can not consecrate -- we > can not hallow -- this ground. > The brave men, living and dead, who struggled here, have consecrated it, > far above our poor power > to add or detract. The world will little note, nor long remember what we > say here, but it can never > forget what they did here. It is for us the living, rather, to be dedicated > here to the unfinished work > which they who fought here have thus far so nobly advanced. It is rather > for us to be here dedicated > to the great task remaining before us -- that from these honored dead we > take increased devotion > to that cause for which they gave the last full measure of devotion -- that > we here highly resolve > that these dead shall not have died in vain -- that this nation, under God, > shall have a new birth > of freedom -- and that government of the people, by the people, for the > people, shall not perish > from the earth.""" > > alphabet = "abcdefghijklmnopqrstuvwxyz" > encryption = "nopqrstuvwxyzabcdefghijklm" > > > def encryptor(address): > encrypted = "" > for char in address: > if char != alphabet: > encrypted += char > else: > pos = alphabet.index(char) > encrypted += encryption[pos] > print(encrypted) > > encryptor(address) > > > -- > Roger Lea Scherer > 623.255.7719 > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From t100ss at gmail.com Thu Apr 19 09:01:23 2018 From: t100ss at gmail.com (Brian Lockwood) Date: Thu, 19 Apr 2018 14:01:23 +0100 Subject: [Tutor] Fwd: File handling Tab separated files References: Message-ID: >> >> Here are some fixes >> >> filename is a variable and hence should not be in quotes. >> file_ is then called ?f? on the next line. >> The indenting is a bit wrong but this may just be your email. >> >> the line read_data ? should be followed by something that appends the read_data to ?object? which should be declared earlier. >> >>> On 19 Apr 2018, at 09:45, Niharika Jakhar > wrote: >>> >>> Hi >>> I want to store a file from BioGRID database (tab separated file, big data) >>> into a data structure(I prefer lists, please let me know if another would >>> be better) and I am trying to print the objects. >>> Here?s my code: >>> class BioGRIDReader: >>> def __init__(self, filename): >>> with open('filename', 'r') as file_: >>> read_data = f.read() >>> for i in file_ : >>> read_data = (i.split('\t')) >>> return (objects[:100]) >>> >>> a = BioGRIDReader >>> print (a.__init__(test_biogrid.txt)) >>> >>> >>> >>> >>> Here's what the terminal says: >>> Traceback (most recent call last): >>> File "./BioGRIDReader.py", line 23, in >>> print (a.__init__(test_biogrid.txt)) >>> NameError: name 'test_biogrid' is not defined >>> >>> The file named test_biogrid.txt do exist in the same folder as this program. >>> >>> I am unable to go further with this code. Kindly help me out. >>> >>> >>> Thanks and regards >>> NIHARIKA >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> https://mail.python.org/mailman/listinfo/tutor >> > From alan.gauld at yahoo.co.uk Thu Apr 19 16:59:52 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 19 Apr 2018 21:59:52 +0100 Subject: [Tutor] File handling Tab separated files In-Reply-To: References: <741113f1-cb9f-af6c-5884-7153c1c47b43@biologie.uni-freiburg.de> Message-ID: On 19/04/18 17:50, Niharika Jakhar wrote: > Hi again > I tried re-writing the code with all your advices(i assume to cover all of > them). I have extended the code a little bit to store the data in the form > of lists and am trying to access it. > I also changed the file name to BioGRID.txt > > Here's what I wrote(Please ignore the identation, there was no such error, > it's just the e-mail thingy.): > > import csv > class BioGRIDReader: > def __init__(self, filename): > with open('filename', 'rb') as f: Notice 'filename' is in quotes which means its a literal string so Python looks for a file called 'filename'. It can't find one. You really want to use the file name stored in the variable called filename so remove the quotes. You need to review the difference between variable names and string literals. > self.reader = csv.reader(f, delimiter='\t') > self.object_ = self.reader.split['\n'] > for row in range(len(object_)): > for r in range(len(row)): This is almost certainly wrong. The first for sets row to be a number in the range(len...) The second for line tries to iterate over the len(row) which will be a very small number - and hence have no len() value. > if (r[-1] == r[-2]): And this is probably wrong too even if r from the line above was to be a small integer. You can't index an integer. Now can you express what you really want to do? I suspect something like: for row in self.object: # or self.object.split() maybe? if row[-1] == row[-2] # last two chars are the same return row[2],row[3] # 3rd and 4th chars HTH -- 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 From dvnsarma at gmail.com Fri Apr 20 00:53:37 2018 From: dvnsarma at gmail.com (=?UTF-8?B?RC5WLk4uU2FybWEg4LCh4LC/LuCwteCwvy7gsI7gsKjgsY0u4LC24LCw4LGN4LCu?=) Date: Fri, 20 Apr 2018 10:23:37 +0530 Subject: [Tutor] anomaly Message-ID: I have a situation in which the same code gives an error in idle but works in qtconsole regards, *?in idle* v = np.zeros(len(x)) for i in range(len(x)): if x[i] < 1.0: v[i] = 0 else: v[i] = V print v? ======== RESTART: C:\Users\SHARMA\Documents\Python Scripts\sqwell.py ======== Traceback (most recent call last): File "C:\Users\SHARMA\Documents\Python Scripts\sqwell.py", line 45, in if x[i] < 1.0: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() ?*in qtconsole?* v = np.zeros(len(x)) for i in range(len(x)): if x[i] < 1.0: v[i] = 0 else: v[i] = V print v [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.] Sarma. From steve at pearwood.info Fri Apr 20 02:17:12 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 20 Apr 2018 16:17:12 +1000 Subject: [Tutor] anomaly In-Reply-To: References: Message-ID: <20180420061711.GV11616@ando.pearwood.info> On Fri, Apr 20, 2018 at 10:23:37AM +0530, D.V.N.Sarma ??.??.???.???? wrote: > I have a situation in which the same code gives an error in idle but works > in qtconsole Probably different versions of Python running. What does this say in each of them? import sys print (sys.version) print (sys.executable) -- Steve From alan.gauld at yahoo.co.uk Fri Apr 20 03:04:13 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 20 Apr 2018 08:04:13 +0100 Subject: [Tutor] anomaly In-Reply-To: References: Message-ID: On 20/04/18 05:53, D.V.N.Sarma ??.??.???.???? wrote: > I have a situation in which the same code gives an error in idle but works > in qtconsole > regards, > > *?in idle* > v = np.zeros(len(x)) > > for i in range(len(x)): > if x[i] < 1.0: > v[i] = 0 You don't give us any clue to what x (or V) is, so its hard to know what is going on. Also how are you running the code in IDLE? Are you running the same file using the Run menu option? Or are you importing your module? Also which Python version are you using? -- 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 From __peter__ at web.de Fri Apr 20 03:29:30 2018 From: __peter__ at web.de (Peter Otten) Date: Fri, 20 Apr 2018 09:29:30 +0200 Subject: [Tutor] anomaly References: Message-ID: Alan Gauld via Tutor wrote: > On 20/04/18 05:53, D.V.N.Sarma ??.??.???.???? wrote: >> I have a situation in which the same code gives an error in idle but >> works in qtconsole >> regards, >> >> *?in idle* >> v = np.zeros(len(x)) >> >> for i in range(len(x)): >> if x[i] < 1.0: >> v[i] = 0 > > You don't give us any clue to what x (or V) is, > so its hard to know what is going on. The code may be the same, but it's probably not fed the same data. I'm guessing that x is a numpy array. The code above produces an error if the array has more than one dimension: $ python3 Python 3.4.3 (default, Nov 28 2017, 16:41:13) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import numpy >>> x = numpy.array([1,2,3]) >>> for i in range(len(x)): ... if x[i] < 1.0: pass ... >>> x = numpy.array([[1,2,3], [4,5,6]]) >>> for i in range(len(x)): ... if x[i] < 1.0: pass ... Traceback (most recent call last): File "", line 2, in ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() Comparing an array with a scalar uses broadcasting to build another array >>> numpy.array([1,2,3]) < 3 array([ True, True, False], dtype=bool) and the numpy developers forbid using an array in a boolean context because it's not clear what bool(some_array) is supposed to return. Instead you have to make explicit if you want all values or at least one value to be truthy. > Also how are you running the code in IDLE? > Are you running the same file using the Run > menu option? Or are you importing your module? > > Also which Python version are you using? From alan.gauld at yahoo.co.uk Fri Apr 20 05:06:56 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 20 Apr 2018 10:06:56 +0100 Subject: [Tutor] File handling Tab separated files In-Reply-To: References: <741113f1-cb9f-af6c-5884-7153c1c47b43@biologie.uni-freiburg.de> Message-ID: Use Reply-All or Reply-List to include the mailing list in replies. On 20/04/18 09:10, Niharika Jakhar wrote: > Hi > > I want to store the data of file into a data structure which has 11 > objects per line , something like this: > 2354?? ???? somethin2???? 23nothing?? ??? 23214..... > > > so I was trying to split the lines using \n and storer each line in a > list so I have a list of 11 objects, then I need to retrieve the last > two position, You are using the csv module so you don't need to split the lines, the csv reader has already done that for you. It generates a sequence of tuples, one per line. So you only need to do something like: results = [] with open(filename) as f: ???? for line in csv.reader(f, delimiter='\t'): ???????? if line[-1] == line[-2]: ??????????? results.append(line[2],line[3]) Let the library do the work. You can see what the reader is doing by inserting a print(line) call instead of the if statement. When using a module for the first time don't be afraid to use print to check the input/output values. Its better than guessing. -- 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 From klu at talize.com Fri Apr 20 16:58:09 2018 From: klu at talize.com (Kuan Lu) Date: Fri, 20 Apr 2018 20:58:09 +0000 Subject: [Tutor] Code to download credit card statement and add to DB Message-ID: <4f33007726354c2da02bcbc8e0a79fa7@TM1900EX2013B.talize.net> Hello Mr. Tutor: I constructed a pieced of code utilizing requests and pyodbc to download creditcard statement transactions and insert them into a DB table based on the code published by Louis Millette here: https://github.com/louismillette/Banking/tree/master. Now I am a total newbie to Python even though I had some experience writing in other languages. The code works like a charm until where it comes to inserting the transactions into the table. As you can see in my code, in order to avoid duplicates being inserted into the table I?m trying to compared the downloaded statement line to each existing line in the table through nested loop and insert only when there?s no match. I kind of intuitively expect that there is another much simpler way to compare each downloaded line to all the inserted lines and find out if it is already in there. Obviously the approach I?m using doesn?t really work as I want it to. Can you please give me some hints as to how to get the loops to work as I explained or, if there is another better way to do it, what would that look like? Thanks a lot for your guidance. Kaun KUAN LU, CPA, CGA | FINANCE TEAM LEAD ________________________________ Email: klu at talize.com | Tel.:416-757-7008,212 | Fax.:416-757-9656 67 Alexdon Rd. Unit 1, North York, M3J 2B5 [cid:image8b62b4.PNG at 56bf8aa6.478ff844] [cid:image60c6be.PNG at 4e534b25.42997eac] [cid:imagecf6195.PNG at 85b08044.4f853faf] [cid:image5aee5c.PNG at df0af238.4cb43c7f] [cid:image53396f.PNG at 356ad215.46ac24a4] [cid:imagede763d.PNG at 0b16b53d.47948094] The information transmitted, including attachments, is intended only for the person(s) or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error please contact the sender and destroy any copies of this information. From klu at talize.com Fri Apr 20 16:59:52 2018 From: klu at talize.com (Kuan Lu) Date: Fri, 20 Apr 2018 20:59:52 +0000 Subject: [Tutor] Code to download credit card statement and add to DB In-Reply-To: <4f33007726354c2da02bcbc8e0a79fa7@TM1900EX2013B.talize.net> References: <4f33007726354c2da02bcbc8e0a79fa7@TM1900EX2013B.talize.net> Message-ID: Hello Mr. Tutor: I constructed a pieced of code utilizing requests and pyodbc to download creditcard statement transactions and insert them into a DB table based on the code published by Louis Millette here: https://github.com/louismillette/Banking/tree/master. Now I am a total newbie to Python even though I had some experience writing in other languages. The code works like a charm until where it comes to inserting the transactions into the table. As you can see in my code, in order to avoid duplicates being inserted into the table I?m trying to compared the downloaded statement line to each existing line in the table through nested loop and insert only when there?s no match. I kind of intuitively expect that there is another much simpler way to compare each downloaded line to all the inserted lines and find out if it is already in there. Obviously the approach I?m using doesn?t really work as I want it to. Can you please give me some hints as to how to get the loops to work as I explained or, if there is another better way to do it, what would that look like? Thanks a lot for your guidance. Kuan The code is listed as below: import os import requests import datetime import re import json import pyodbc from datetime import date, timedelta pyodbc.lowercase = False #connect to the server through pyodbc server = 'XXXX\\XXX' database = 'XXXXXX' username = 'XXX' password = 'XXXXX' cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=XXXX\\XXX;DATABASE=XXXXXXX;UID=XXX;PWD=XXXXX', autocommit=True) cursor = cnxn.cursor() #connect to the Utilities DB and determin the latest date of statement line(s) imported cursor.execute("SELECT MAX(TransDate) AS TransDate FROM Visa.CreditCardStatement") Latest_Statement_Date=cursor.fetchone() #login and date range parameters _username = "XXXXXXXXXXXXX" _password = "XXXXXX" dateFrom = Latest_Statement_Date.TransDate - timedelta(2) dateUntil = date.today() #Firstly, you'll need to create an authentication request to acquire an X Auth Token. # The headers are pulled from a request on my computer, feel free to change them to headers acquired from yours or another header. # Note that the URL is an encrypted HTTPS request, as are all calls made to the API. authenticate_request = requests.post( url="https://www.cibconline.cibc.com/ebm-anp/api/v1/json/sessions", json={"card": {"value": "{}".format(_username), "description": "", "encrypted": False, "encrypt": True}, "password": "{}".format(_password)}, headers={ "Host": "www.cibconline.cibc.com", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0", "Accept": "application/vnd.api+json", "Accept-Language": "en", "Accept-Encoding": "gzip, deflate, br", "Referer":"https://www.cibconline.cibc.com/ebm-resources/public/banking/cibc/client/web/index.html", "Content-Type": "application/vnd.api+json", "Client-Type": "default_web", "X-Auth-Token": "", "brand": "cibc", "WWW-Authenticate": "CardAndPassword", "X-Requested-With": "XMLHttpRequest", "Content-Length": "112", "Connection": "keep-alive", "Pragma": "no-cache", "Cache-Control": "no-cache" } ) #Next you'll need to save the cookies, response header, and X Auth Token cookies = dict(authenticate_request.cookies) #self.cookies = cookies authenticate_response_headers = authenticate_request.headers X_Auth_Token = authenticate_response_headers['X-Auth-Token'] #self.X_Auth_Token = X_Auth_Token #Make a login request like below. Again the headers are not rigid, however, the "X-Auth-Token" filed must be the X Auth Token from earlier. login_request = requests.get( url="https://www.cibconline.cibc.com/ebm-anp/api/v1/profile/json/userPreferences", headers={ "Host": "www.cibconline.cibc.com", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0", "Accept": "application/vnd.api+json", "Accept-Language": "en", "Accept-Encoding": "gzip, deflate, br", "Referer":"https://www.cibconline.cibc.com/ebm-resources/public/banking/cibc/client/web/index.html", "Content-Type": "application/vnd.api+json", "Client-Type": "default_web", "brand": "cibc", "X-Auth-Token": X_Auth_Token, "X-Requested-With": "XMLHttpRequest", "Connection": "keep-alive", }, cookies=cookies ) #after logging in as yourself, go ahead and pull your default account id from the response login_request_response = login_request.json() #The Visa Account ID defaultAccountId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" #the dateFrom and dateUntil arguments are python datetimes representing from when until when you want to pull credit and debit entries account_requests = requests.get( url="https://www.cibconline.cibc.com/ebm-ai/api/v1/json/transactions?accountId={}&filterBy=range&fromDate={}&lastFilterBy=range&limit=150&lowerLimitAmount=&offset=0&sortAsc=true&sortByField=date&toDate={}&transactionLocation=&transactionType=&upperLimitAmount=".format( defaultAccountId, dateFrom.strftime("%Y-%m-%d"), dateUntil.strftime("%Y-%m-%d") ), headers={ "Host": "www.cibconline.cibc.com", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0", "Accept": "application/vnd.api+json", "Accept-Language": "en", "Accept-Encoding": "gzip, deflate, br", "Referer":"https://www.cibconline.cibc.com/ebm-resources/public/banking/cibc/client/web/index.html", "Content-Type": "application/vnd.api+json", "Client-Type": "default_web", "brand": "cibc", "X-Auth-Token": X_Auth_Token, "X-Requested-With": "XMLHttpRequest", "Connection": "keep-alive", }, cookies=cookies ) transactions = account_requests.json()['transactions'] #for T in transactions: # ListOfRows=[r for r in T] # print(ListOfRows[0],ListOfRows[1],ListOfRows[2],ListOfRows[3]) #The function to load the statement dataset def Generate_List(): #There are a couple of ways to do this one, I set it up as an iterator. This is just to format the output as a nice dictionary. for transaction in transactions: transaction_type = 'Debit' if transaction['debit'] else 'Credit' date_datetime = datetime.datetime.strptime(transaction['date'].split('T')[0],"%Y-%m-%d") amount = transaction['debit'] if transaction_type == 'Debit' else -transaction['credit'] yield { 'transaction': transaction_type, # 'Debit' or 'Credit' 'TransDate': date_datetime, 'TransDescription': transaction['transactionDescription'], 'CreditCardNumber': transaction['creditCardNumber'], 'Amount': amount, 'balance': transaction['runningBalance'] } #call the function to populate the generator Statement=Generate_List() #read the credit card list from the DB Card_Dict = dict(cursor.execute("SELECT * FROM dbo.CardNameLookup")) #read the transactions already loaded from the latest date existing in the statement table Loaded_Trans = cursor.execute("SELECT * FROM Visa.CreditCardStatement WHERE TransDate=(SELECT MAX(TransDate) FROM Visa.CreditCardStatement)") #gather the column name info for the dictionary columns = [column[0] for column in Loaded_Trans.description] #create the empty dictionary to hold the loaded transactions Card_Trans = [] #iterate through the cursor and load the dictionary with transactions for line in Loaded_Trans.fetchall(): Card_Trans.append(dict(zip(columns,line))) Trans_Count = 0 #loop through the generator and insert the new visa statement lines for Statement_Line in Statement: for trans in Card_Trans: Trans_Count += 1 print(Statement_Line['TransDate'],Statement_Line['TransDescription'],Card_Dict.get(Statement_Line['CreditCardNumber']),\ Statement_Line['Amount'],Statement_Line['balance']) if trans['TransDate'] == Statement_Line['TransDate'].date() and trans['TransDescription'] == Statement_Line['TransDescription'] and \ trans['CreditCardID'] == Card_Dict.get(Statement_Line['CreditCardNumber']) and float(trans.Amount) == float(Statement_Line['Amount']): pass elif Trans_Count==len(Card_Trans): cursor.execute('INSERT INTO Visa.CreditCardStatement (TransDate, TransDescription, CreditCardID, Amount) VALUES (?, ?, ?, ?)', \ Statement_Line['TransDate'], Statement_Line['TransDescription'], Card_Dict.get(Statement_Line['CreditCardNumber']), Statement_Line['Amount']) KUAN LU, CPA, CGA | FINANCE TEAM LEAD ________________________________ Email: klu at talize.com | Tel.:416-757-7008,212 | Fax.:416-757-9656 67 Alexdon Rd. Unit 1, North York, M3J 2B5 [cid:image9ae555.PNG at 558c93ca.4a9482d2] [cid:image0b1cd4.PNG at 048ae104.4a9e50c4] [cid:image65149d.PNG at b0ad0da3.40867d32] [cid:image7e7083.PNG at 0fcf5a63.4cb71f2c] [cid:imagea0af02.PNG at 3584bf2f.4697ad6a] [cid:imagef55df3.PNG at ff566e87.4281f912] The information transmitted, including attachments, is intended only for the person(s) or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error please contact the sender and destroy any copies of this information. From __peter__ at web.de Sat Apr 21 03:04:15 2018 From: __peter__ at web.de (Peter Otten) Date: Sat, 21 Apr 2018 09:04:15 +0200 Subject: [Tutor] XML Programs References: Message-ID: Glen wrote: > Thank you for your comprehensive reply. It was very helpful! > Just to clarify one point, is it not possible to search for a node > directly in the element / elementTree or do you first need to pull the > data into a class/dict? You certainly have other options >>> catalog.xpath("//catalog/book[contains(title,'Guide')]/title/text()") ["XML Developer's Guide", 'MSXML3: A Comprehensive Guide', 'Visual Studio 7: A Comprehensive Guide'] >>> catalog.xpath("//catalog/book[starts- with(author,'Corets')]/title/text()") ['Maeve Ascendant', "Oberon's Legacy", 'The Sundered Grail'] but then the question is whether you want to learn Python or XPath. From sjeik_appie at hotmail.com Sat Apr 21 05:50:52 2018 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Sat, 21 Apr 2018 09:50:52 +0000 Subject: [Tutor] XML Programs Message-ID: On Apr 21, 2018 09:06, Peter Otten <__peter__ at web.de> wrote: > > Glen wrote: > > > Thank you for your comprehensive reply. It was very helpful! > > Just to clarify one point, is it not possible to search for a node > > directly in the element / elementTree or do you first need to pull the > > data into a class/dict? > > You certainly have other options > > >>> catalog.xpath("//catalog/book[contains(title,'Guide')]/title/text()") > ["XML Developer's Guide", 'MSXML3: A Comprehensive Guide', 'Visual Studio 7: > A Comprehensive Guide'] > >>> catalog.xpath("//catalog/book[starts- > with(author,'Corets')]/title/text()") > ['Maeve Ascendant', "Oberon's Legacy", 'The Sundered Grail'] > > but then the question is whether you want to learn Python or XPath. Neat! Can you recommend a good resource (book, webpage) for learning Xpath? From drtraceyjones at hotmail.com Sat Apr 21 07:50:24 2018 From: drtraceyjones at hotmail.com (tracey jones-Francis) Date: Sat, 21 Apr 2018 11:50:24 +0000 Subject: [Tutor] Theory of computation non-emptiness Message-ID: Hi there, I've been working on code that takes a text file that represents a specific Deterministic Finite Automata. The text files are set up in a specific way so that line 1 specifies the number of states. Line 2 specifies the states (i.e., just a list of the names of the states, separated by spaces). Line 3 specifies the size of the alphabet. Line 4 specifies the alphabet. Lines 5-7 give the transition function, each row corresponding to a state (in order specified on line 2) and each column corresponding to a symbol from the alphabet (in order specified on line 4). Line 8 specifies the start state. Line 9 specifies the number of final/accept states. Line 10 specifies the final states. I have already constructed some functions but next I want to construct a function determining, for any DFA M, whether or not L(M) = 0 i.e., whether or not there exists at least one string that is accepted by M. if the language If L(M) = 0 then I want ?language empty? printed. If L(M) != 0; then i need to print ?language non-empty - xxxx accepted?, where xxxx is replaced by some string that is accepted by M. I wanted to use breath first search in order to determine the existence of a path. Code below is all I've done so far but i'm struggling to find a way in which to link the states with the transitions in the text file. hope you can give me some direction and advice. def NonEmptiness(dic): dic = openTextFile(dic) #print(dic['states'].split()) theString = "" visited = [] queue = dic['states'].split() newStates = dic['states'] newStatesFinal = re.sub(' ','', newStates) newAplhabet = re.sub(' ','', dic['alphabet']) #print(dic['finalStates'][2]) print(newStatesFinal) #print(newStatesFinal[0]) while queue: currentState = queue.pop(0) visited.append(currentState) #print(visited) for a in newAplhabet: if (currentState, a) == (newStatesFinal[0], newAplhabet[0]): if dic['transitionStates'][0][0] != dic['finalStates'][0] or dic['finalStates'][2]: theString + a else: From alan.gauld at yahoo.co.uk Sat Apr 21 18:24:05 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 21 Apr 2018 23:24:05 +0100 Subject: [Tutor] Theory of computation non-emptiness In-Reply-To: References: Message-ID: On 21/04/18 12:50, tracey jones-Francis wrote: > Hi there, I've been working on code I've only glanced at this but one thing jumped out at me: > while queue: > currentState = queue.pop(0) > visited.append(currentState) > #print(visited) > for a in newAplhabet: > if (currentState, a) == (newStatesFinal[0], newAplhabet[0]): > if dic['transitionStates'][0][0] != dic['finalStates'][0] or dic['finalStates'][2]: This reads to Python like if X != A or B which it parses as if (X != A) or B Is that what you intended? It may be the case but, if so, it's a rather cryptic way of expressing it. HTH -- 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 From 01patrickliu at gmail.com Sun Apr 22 09:46:42 2018 From: 01patrickliu at gmail.com (=?UTF-8?B?5YqJ5qyK6Zme?=) Date: Sun, 22 Apr 2018 21:46:42 +0800 Subject: [Tutor] about tree input Message-ID: To deposit a nodes in the tree (adding a new node), for example: put 3: {} in the Tree branch at branch tuple (2, 4, 1, 5) . If don't use : def f(T,k,v): m=len(T) if m==0: Tree[k]=v elif m==1: Tree[T[0]][k]=v elif m==2: Tree[T[0]][T[1]][k]=v elif m==3: Tree[T[0]][T[1]][T[2]][k]=v elif m==4: Tree[T[0]][T[1]][T[2]][T[3]][k]=v elif m==5: Tree[T[0]][T[1]][T[2]][T[3]][T[4]][k]=v ... f( (2, 4, 1, 5),3,{}) Is there a dynamic approach(the node tuple can be any length) I want to create a function like f(branch_tuple,key,value) Tree = {1: {2: {3: {...}, 4: {...}, ...}, 3: {2: {...}, 4: {...}, ...}, 4: {2: {...}, 3: {...}, ...}, ...}, 2: {1: {...}, 3: {...}, 4: {...}, ...}, 3: {...}, 4: {...}, ...} *(... indicates omission) * (Node tuple represent one of the branches) From zachary.ware+pytut at gmail.com Sun Apr 22 12:42:04 2018 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Sun, 22 Apr 2018 16:42:04 +0000 Subject: [Tutor] about tree input In-Reply-To: References: Message-ID: Basically what you want to do here is give Tree a local name (d), loop through T reassigning d to d[k_from_T], and then d[k] = v after the loop. T can be arbitraily long, and Tree can actually be and contain any indexable type. If this pointer isn't enough to send you in the right direction, I'll try to actually write an example when I have a real keyboard in front of me :) -- Zach (Top-posted in HTML from a phone) On Sun, Apr 22, 2018, 08:54 ??? <01patrickliu at gmail.com> wrote: > To deposit a nodes in the tree (adding a new node), for example: put 3: {} > in the Tree branch at branch tuple (2, 4, 1, 5) . > If don't use : > def f(T,k,v): > m=len(T) > if m==0: > Tree[k]=v > elif m==1: > Tree[T[0]][k]=v > elif m==2: > Tree[T[0]][T[1]][k]=v > elif m==3: > Tree[T[0]][T[1]][T[2]][k]=v > elif m==4: > Tree[T[0]][T[1]][T[2]][T[3]][k]=v > elif m==5: > Tree[T[0]][T[1]][T[2]][T[3]][T[4]][k]=v > ... > > f( (2, 4, 1, 5),3,{}) > > Is there a dynamic approach(the node tuple can be any length) > I want to create a function like f(branch_tuple,key,value) > > Tree = {1: {2: {3: {...}, 4: {...}, ...}, 3: {2: {...}, 4: {...}, ...}, 4: > {2: {...}, 3: {...}, ...}, ...}, 2: {1: {...}, 3: {...}, 4: {...}, ...}, 3: > {...}, 4: {...}, ...} > > *(... indicates omission) > * (Node tuple represent one of the branches) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From hemendrarathod at gmail.com Mon Apr 23 03:00:29 2018 From: hemendrarathod at gmail.com (Hemendra Singh Rathod) Date: Mon, 23 Apr 2018 09:00:29 +0200 Subject: [Tutor] Query: How to fetch data to Python from InfluxDB database? Message-ID: Hello Tutor Members, I want to import the big Data?s from InfluxDB database. Could anyone please guide me on how can I do it in Python 3.x? Please help me on this. Thank you in advance. Regards Hemendra Rathod From mats at wichmann.us Mon Apr 23 10:11:51 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 23 Apr 2018 08:11:51 -0600 Subject: [Tutor] Query: How to fetch data to Python from InfluxDB database? In-Reply-To: References: Message-ID: <2dd36cd0-a653-1b3b-d896-8a0e5527c6f1@wichmann.us> On 04/23/2018 01:00 AM, Hemendra Singh Rathod wrote: > Hello Tutor Members, > > > I want to import the big Data?s from InfluxDB database. Could anyone please > guide me on how can I do it in Python 3.x? > > Please help me on this. Thank you in advance. There is a project listed on PyPi which might help: https://pypi.org/project/influxdb/ From giorgio030898 at gmail.com Mon Apr 23 06:29:16 2018 From: giorgio030898 at gmail.com (Giorgio De Angelis) Date: Mon, 23 Apr 2018 12:29:16 +0200 Subject: [Tutor] Python 3.6.5 for MAC Message-ID: <7CB576E9-AD12-4CE3-89AF-E0568BE4D937@gmail.com> Hello guys, I have a problem with my MacBook Pro ?13, version 10.13.4, because when I try to open the shell it says that it couldn?t make any connection because it wasn?t able to make a subprocess. Can you help me? Giorgio From wrw at mac.com Mon Apr 23 11:37:03 2018 From: wrw at mac.com (William Ray Wing) Date: Mon, 23 Apr 2018 11:37:03 -0400 Subject: [Tutor] Python 3.6.5 for MAC In-Reply-To: <7CB576E9-AD12-4CE3-89AF-E0568BE4D937@gmail.com> References: <7CB576E9-AD12-4CE3-89AF-E0568BE4D937@gmail.com> Message-ID: <7555C681-4F2D-4C23-9F83-38DE7389AD0A@mac.com> > On Apr 23, 2018, at 6:29 AM, Giorgio De Angelis wrote: > > Hello guys, > > I have a problem with my MacBook Pro ?13, version 10.13.4, because when I try to open the shell it says that it couldn?t make any connection because it wasn?t able to make a subprocess. Can you help me? > Sorry, do you mean open a shell in the Terminal application, or open a shell in a python subprocess? Bill > Giorgio > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From mats at wichmann.us Mon Apr 23 13:04:54 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 23 Apr 2018 11:04:54 -0600 Subject: [Tutor] Python 3.6.5 for MAC In-Reply-To: <7555C681-4F2D-4C23-9F83-38DE7389AD0A@mac.com> References: <7CB576E9-AD12-4CE3-89AF-E0568BE4D937@gmail.com> <7555C681-4F2D-4C23-9F83-38DE7389AD0A@mac.com> Message-ID: On 04/23/2018 09:37 AM, William Ray Wing wrote: > >> On Apr 23, 2018, at 6:29 AM, Giorgio De Angelis wrote: >> >> Hello guys, >> >> I have a problem with my MacBook Pro ?13, version 10.13.4, because when I try to open the shell it says that it couldn?t make any connection because it wasn?t able to make a subprocess. Can you help me? >> > > Sorry, do you mean open a shell in the Terminal application, or open a shell in a python subprocess? > > Bill > Indeed, more details, please. This *sounds* like the old IDLE problem with Tcl/Tk on the Mac, Python itself shouldn't need to create a subprocess in the way that causes a problem. Did you read the "macOS users" section of https://www.python.org/downloads/release/python-365/ and did you, in fact, get the second installer listed, which is supposed to get around that particular problem? From 01patrickliu at gmail.com Mon Apr 23 12:28:55 2018 From: 01patrickliu at gmail.com (=?UTF-8?B?5YqJ5qyK6Zme?=) Date: Tue, 24 Apr 2018 00:28:55 +0800 Subject: [Tutor] about tree input In-Reply-To: References: Message-ID: I still have no idea about how to write the code. Can you make a demonstration? 2018-04-23 0:42 GMT+08:00 Zachary Ware : > Basically what you want to do here is give Tree a local name (d), loop > through T reassigning d to d[k_from_T], and then d[k] = v after the loop. T > can be arbitraily long, and Tree can actually be and contain any indexable > type. > > If this pointer isn't enough to send you in the right direction, I'll try > to actually write an example when I have a real keyboard in front of me :) > > -- > Zach > (Top-posted in HTML from a phone) > > On Sun, Apr 22, 2018, 08:54 ??? <01patrickliu at gmail.com> wrote: > >> To deposit a nodes in the tree (adding a new node), for example: put 3: {} >> in the Tree branch at branch tuple (2, 4, 1, 5) . >> If don't use : >> def f(T,k,v): >> m=len(T) >> if m==0: >> Tree[k]=v >> elif m==1: >> Tree[T[0]][k]=v >> elif m==2: >> Tree[T[0]][T[1]][k]=v >> elif m==3: >> Tree[T[0]][T[1]][T[2]][k]=v >> elif m==4: >> Tree[T[0]][T[1]][T[2]][T[3]][k]=v >> elif m==5: >> Tree[T[0]][T[1]][T[2]][T[3]][T[4]][k]=v >> ... >> >> f( (2, 4, 1, 5),3,{}) >> >> Is there a dynamic approach(the node tuple can be any length) >> I want to create a function like f(branch_tuple,key,value) >> >> Tree = {1: {2: {3: {...}, 4: {...}, ...}, 3: {2: {...}, 4: {...}, ...}, 4: >> {2: {...}, 3: {...}, ...}, ...}, 2: {1: {...}, 3: {...}, 4: {...}, ...}, >> 3: >> {...}, 4: {...}, ...} >> >> *(... indicates omission) >> * (Node tuple represent one of the branches) >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > From zachary.ware+pytut at gmail.com Mon Apr 23 17:50:21 2018 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Mon, 23 Apr 2018 16:50:21 -0500 Subject: [Tutor] about tree input In-Reply-To: References: Message-ID: On Mon, Apr 23, 2018 at 11:28 AM, ??? <01patrickliu at gmail.com> wrote: > I still have no idea about how to write the code. Can you make a > demonstration? Something like this: def set(tree, path, key, value): for p in path: tree = tree[p] tree[key] = value -- Zach From kentaro0919 at gmail.com Mon Apr 23 22:34:58 2018 From: kentaro0919 at gmail.com (Kentaro Hori) Date: Tue, 24 Apr 2018 11:34:58 +0900 Subject: [Tutor] Python 3.6.5 for MAC In-Reply-To: <7CB576E9-AD12-4CE3-89AF-E0568BE4D937@gmail.com> References: <7CB576E9-AD12-4CE3-89AF-E0568BE4D937@gmail.com> Message-ID: Hi can you take a screenshot after rebooting and trying one aging? 2018-04-23 19:29 GMT+09:00 Giorgio De Angelis : > Hello guys, > > I have a problem with my MacBook Pro ?13, version 10.13.4, because when I > try to open the shell it says that it couldn?t make any connection because > it wasn?t able to make a subprocess. Can you help me? > > Giorgio > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Tue Apr 24 03:19:41 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 24 Apr 2018 08:19:41 +0100 Subject: [Tutor] Python 3.6.5 for MAC In-Reply-To: References: <7CB576E9-AD12-4CE3-89AF-E0568BE4D937@gmail.com> Message-ID: On 24/04/18 03:34, Kentaro Hori wrote: > Hi > > can you take a screenshot after rebooting and trying one aging? Although you will need to post a link to the image since the list server will not send binary attachments. -- 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 From f32 at inbox.ru Tue Apr 24 07:59:20 2018 From: f32 at inbox.ru (=?UTF-8?B?QVM=?=) Date: Tue, 24 Apr 2018 14:59:20 +0300 Subject: [Tutor] =?utf-8?q?Python_Debugger_shortcut_to_go_to_the_last_fra?= =?utf-8?q?me?= Message-ID: <1524571160.857042129@f106.i.mail.ru> Hi Please consider the following script: #!/usr/bin/env python3.6 #pdb_last_frame.py import pdb def recursive_function(n=5, output='default print'): ??? if n > 0: ??????? recursive_function(n - 1) ??? else: ??????? pdb.set_trace() ??????? print(output) ??? return if __name__ == '__main__': ??? recursive_function("space ham") For instance we run it as python3.6 -m pdb pdb_last_frame.py The following command sequence applied once the script stopped on bp: u u The question is there any command to go directly to the last frame instead of typing d d ? Thank you in advance. From evuraan at gmail.com Tue Apr 24 22:26:47 2018 From: evuraan at gmail.com (Evuraan) Date: Tue, 24 Apr 2018 19:26:47 -0700 Subject: [Tutor] threading for each line in a large file, and doing it right Message-ID: Greetings! Please consider this situation : Each line in "massive_input.txt" need to be churned by the "time_intensive_stuff" function, so I am trying to background it. import threading def time_intensive_stuff(arg): # some code, some_conditional return (some_conditional) with open("massive_input.txt") as fobj: for i in fobj: thread_thingy = thread.Threading(target=time_intensive_stuff, args=(i,) ) thread_thingy.start() With above code, it still does not feel like it is backgrounding at scale, I am sure there is a better pythonic way. How do I achieve something like this bash snippet below in python: time_intensive_stuff_in_bash(){ # some code : } for i in $(< massive_input.file); do time_intensive_stuff_in_bash i & disown : done Many thanks in advance, Thanks! From dyoo at hashcollision.org Tue Apr 24 23:20:23 2018 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 24 Apr 2018 20:20:23 -0700 Subject: [Tutor] threading for each line in a large file, and doing it right In-Reply-To: References: Message-ID: > Please consider this situation : > Each line in "massive_input.txt" need to be churned by the > "time_intensive_stuff" function, so I am trying to background it. > > import threading > > def time_intensive_stuff(arg): > # some code, some_conditional > return (some_conditional) > > with open("massive_input.txt") as fobj: > for i in fobj: > thread_thingy = thread.Threading(target=time_intensive_stuff, args=(i,) ) > thread_thingy.start() > > > With above code, it still does not feel like it is backgrounding at > scale, I am sure there is a better pythonic way. You might be looking for the multiprocessing library: https://docs.python.org/3.6/library/multiprocessing.html. Can you say more about the nature of the "time_intensive_stuff" part though? More context may help. From alan.gauld at yahoo.co.uk Wed Apr 25 04:27:08 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 25 Apr 2018 09:27:08 +0100 Subject: [Tutor] threading for each line in a large file, and doing it right In-Reply-To: References: Message-ID: On 25/04/18 03:26, Evuraan wrote: > Please consider this situation : > Each line in "massive_input.txt" need to be churned by the > "time_intensive_stuff" function, so I am trying to background it. What kind of "churning" is involved? If its compute intensive threading may not be the right answer, but if its I/O bound then threading is probably ok. > import threading > > def time_intensive_stuff(arg): > # some code, some_conditional > return (some_conditional) What exactly do you mean by some_conditional? Is it some kind of big decision tree? Or if/else network? Or is it dependent on external data (from where? a database? network?) And you return it - but what is returned? - an expression, a boolean result? Its not clear what the nature of the task is but that makes a big difference to how best to parallelise the work. > with open("massive_input.txt") as fobj: > for i in fobj: > thread_thingy = thread.Threading(target=time_intensive_stuff, args=(i,) ) > thread_thingy.start() > > With above code, it still does not feel like it is backgrounding at > scale, Can you say why you feel that way? What measurements have you done? What system observations(CPU, Memory, Network etc)? What did you expect to see and what did you see. Also consider that processing a huge number of lines will generate a huge number of subprocesses or threads. There is an overhead to each thread and your computer may not have enough resources to run them all efficiently. It may be better to batch the lines so each subprocess handles 10, or 50 or 100 lines (whatever makes sense). Put a loop into your time intensive function to process the list of input values and return a list of outputs. And your external loop needs an inner loop to create the batches. The number of entries in the batch can be parametrized so that you can experiment to find the most cost effective size.. > I am sure there is a better pythonic way. I suspect the issues are not Python specific but are more generally about paralleling large jobs. > How do I achieve something like this bash snippet below in python: > > time_intensive_stuff_in_bash(){ > # some code > : > } > > for i in $(< massive_input.file); do > time_intensive_stuff_in_bash i & disown > : > done Its the same except in bash you start a whole new process so instead of using threading you use concurrent. But did you try this in bash? Was it faster than using Python? I would expect the same issues of too many processes to arise in bash. HTH -- 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 From alan.gauld at yahoo.co.uk Wed Apr 25 05:43:08 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 25 Apr 2018 10:43:08 +0100 Subject: [Tutor] threading for each line in a large file, and doing it right In-Reply-To: References: Message-ID: On 25/04/18 09:27, Alan Gauld via Tutor wrote: >> for i in $(< massive_input.file); do >> time_intensive_stuff_in_bash i & disown >> : >> done > > Its the same except in bash you start a whole > new process so instead of using threading you > use concurrent. concurrent -> multiprocessing doh! sorry -- 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 From micsolan at gmail.com Wed Apr 25 06:29:23 2018 From: micsolan at gmail.com (Michael Solan) Date: Wed, 25 Apr 2018 11:29:23 +0100 Subject: [Tutor] I'm attempting to code the barbershop problem in OS except with 3 barbers instead of one. Can anyone help rewrite my Barber1 and Barber2 classes so it just uses the functions already defined in the original Barber class. Message-ID: class Barber: barberWorkingEvent = Event() def sleep(self): self.barberWorkingEvent.wait() def wakeUp(self): self.barberWorkingEvent.set() def cutHair(self, customer): #Set barber as busy self.barberWorkingEvent.clear() print '{0} is having a haircut from barber\n'.format(customer.name) HairCuttingTime = random.randrange(0, 5) time.sleep(HairCuttingTime) print '{0} is done\n'.format(customer.name) class Barber1: barberWorkingEvent = Event() def sleep(self): self.barberWorkingEvent.wait() def wakeUp(self): self.barberWorkingEvent.set() def cutHair(self, customer): #Set barber as busy self.barberWorkingEvent.clear() print '{0} is having a haircut from barber1\n'.format(customer.name) HairCuttingTime = random.randrange(0, 5) time.sleep(HairCuttingTime) print '{0} is done\n'.format(customer.name) class Barber2: barberWorkingEvent = Event() def sleep(self): self.barberWorkingEvent.wait() def wakeUp(self): self.barberWorkingEvent.set() def cutHair(self, customer): #Set barber as busy self.barberWorkingEvent.clear() print '{0} is having a haircut from barber1\n'.format(customer.name) HairCuttingTime = random.randrange(0, 5) time.sleep(HairCuttingTime) print '{0} is done\n'.format(customer.name) From vagp21 at gmail.com Wed Apr 25 06:27:53 2018 From: vagp21 at gmail.com (Vagelis) Date: Wed, 25 Apr 2018 13:27:53 +0300 Subject: [Tutor] Calling the same thread multiple times Message-ID: <35f2ef88-f66b-0a88-69f3-5b15077e0a8c@gmail.com> Hello, Im creating a progress bar for applications that can keep track of a download in progress. The progress bar will be on a separate thread and will communicate with the main thread using delegates. Ive made the download and the progress bar part, all that remains is the connection between the two of them. For this purpose i tried to simplify the problem, but i cant seem to make it right. Here's what i got so far... import threading def test(): ??? print(threading.current_thread()) for i in range(5): ??? print(threading.current_thread()) ??? t1 = threading.Thread(target = test) ??? t1.start() ??? t1.join() This gives me the output: <_MainThread(MainThread, started 139983023449408)> <_MainThread(MainThread, started 139983023449408)> <_MainThread(MainThread, started 139983023449408)> <_MainThread(MainThread, started 139983023449408)> <_MainThread(MainThread, started 139983023449408)> What i need to do is to call the same thread (Thread-1) multiple times, and the call (of the test function) must be IN the for loop. Ive also tried something like that: import threading import queue def test(): ??? print(threading.current_thread()) ??? i = q.get() ??? print(i) q = queue.Queue() t1 = threading.Thread(target = test) t1.start() for i in range(5): ??? print(threading.current_thread()) ??? q.put(i) t1.join() The result im getting is : <_MainThread(MainThread, started 140383183029568)> <_MainThread(MainThread, started 140383183029568)> 0 <_MainThread(MainThread, started 140383183029568)> <_MainThread(MainThread, started 140383183029568)> <_MainThread(MainThread, started 140383183029568)> Any ideas on how to solve this? From steve at pearwood.info Wed Apr 25 08:14:18 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 25 Apr 2018 22:14:18 +1000 Subject: [Tutor] I'm attempting to code the barbershop problem... In-Reply-To: References: Message-ID: <20180425121418.GT11616@ando.pearwood.info> Hi Michael and welcome. In future, please leave the Subject line as a BRIEF summary, and put the description of your problem in the body of the email. You said: > I'm attempting to code the barbershop problem in OS except > with 3 barbers instead of one. Can anyone help rewrite my Barber1 and > Barber2 classes so it just uses the functions already defined in the > original Barber class. What's the barbershop problem? Why do you need three classes? On Wed, Apr 25, 2018 at 11:29:23AM +0100, Michael Solan wrote: > class Barber: barberWorkingEvent = Event() def sleep(self): > self.barberWorkingEvent.wait() def wakeUp(self): > self.barberWorkingEvent.set() def cutHair(self, customer): #Set barber as > busy self.barberWorkingEvent.clear() print '{0} is having a haircut from > barber\n'.format(customer.name) HairCuttingTime = random.randrange(0, 5) > time.sleep(HairCuttingTime) print '{0} is done\n'.format(customer.name) The formatting here is completely messed up. If you are posting using Gmail, you need to ensure that your email uses no formatting (no bold, no colours, no automatic indentation etc), or else Gmail will mangle the indentation of your code, as it appears to have done above. My wild guess is that what you probably want is something like this: import random import time class Barber(object): def __init__(self, name): self.workingEvent = Event() # What is Event? self.name = name def sleep(self): self.workingEvent.wait() def wakeUp(self): self.workingEvent.set() def cutHair(self, customer): # Set this barber as busy. self.workingEvent.clear() template = '{0} is having a haircut from barber {1}\n' print template.format(customer.name, self.name) HairCuttingTime = random.randrange(0, 5) time.sleep(HairCuttingTime) print '{0} is done\n'.format(customer.name) tony = Barber('Tony') fred = Barber('Fred') george = Barber('George') # and then what? Notice that we have *one* class and three instances of that class. I've given them individual names so they're easier to distinguish. Please ensure you reply on the mailing list. -- Steve From mats at wichmann.us Wed Apr 25 09:41:47 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 25 Apr 2018 07:41:47 -0600 Subject: [Tutor] I'm attempting to code the barbershop problem... In-Reply-To: <20180425121418.GT11616@ando.pearwood.info> References: <20180425121418.GT11616@ando.pearwood.info> Message-ID: <590f6deb-0905-9b26-3c03-66790e50bbcd@wichmann.us> > What's the barbershop problem? a classic computer science puzzle which is essentially a process synchronization problem. it does help to spell out the problem you are trying to solve, however - we don't have the context the original poster is operating in. From kb at kbojens.de Wed Apr 25 09:22:32 2018 From: kb at kbojens.de (Kai Bojens) Date: Wed, 25 Apr 2018 15:22:32 +0200 Subject: [Tutor] Dict of Dict with lists Message-ID: <20180425132232.GA1277@mail.kbojens.de> Hello everybody, I'm coming from a Perl background and try to parse some Exim Logfiles into a data structure of dictionaries. The regex and geoip part works fine and I'd like to save the email adress, the countries (from logins) and the count of logins. The structure I'd like to have: result = { 'foo at bar.de': { 'Countries': [DE,DK,UK] 'IP': ['192.168.1.1','172.10.10.10'] 'Count': [12] } 'bar at foo.de': { 'Countries': [DE,SE,US] 'IP': ['192.168.1.2','172.10.10.11'] 'Count': [23] } } I don't have a problem when I do these three seperately like this with a one dimensonial dict (snippet): result = defaultdict(list) with open('/var/log/exim4/mainlog',encoding="latin-1") as logfile: for line in logfile: result = pattern.search(line) if (result): login_ip = result.group("login_ip") login_auth = result.group("login_auth") response = reader.city(login_ip) login_country = response.country.iso_code if login_auth in result and login_country in result[login_auth]: continue else: result[login_auth].append(login_country) else: continue This checks if the login_country exists within the list of the specific login_auth key, adds them if they don't exist and gives me the results I want. This also works for the ip addresses and the number of logins without any problems. As I don't want to repeat these loops three times with three different data structures I'd like to do this in one step. There are two main problems I don't understand right now: 1. How do I check if a value exists within a list which is the value of a key which is again a value of a key in my understanding exists? What I like to do: if login_auth in result and (login_country in result[login_auth][Countries]) continue This obviously does not work and I am not quite sure how to address the values of 'Countries' in the right way. I'd like to check 'Key1:Key2:List' and don't know how to address this 2. How do I append new values to these lists within the nested dict? From alan.gauld at yahoo.co.uk Wed Apr 25 13:35:30 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 25 Apr 2018 18:35:30 +0100 Subject: [Tutor] Dict of Dict with lists In-Reply-To: <20180425132232.GA1277@mail.kbojens.de> References: <20180425132232.GA1277@mail.kbojens.de> Message-ID: On 25/04/18 14:22, Kai Bojens wrote: > The structure I'd like to have: > > result = { > 'foo at bar.de': { > 'Countries': [DE,DK,UK] > 'IP': ['192.168.1.1','172.10.10.10'] > 'Count': [12] > } > } > ... > for line in logfile: > result = pattern.search(line) Doesn't this overwrite your data structure? I would strongly advise using another name. > if (result): > login_ip = result.group("login_ip") > login_auth = result.group("login_auth") > response = reader.city(login_ip) > login_country = response.country.iso_code > if login_auth in result and login_country in result[login_auth]: > continue > else: > result[login_auth].append(login_country) > else: > continue > 1. How do I check if a value exists within a list which is the value of a key > which is again a value of a key in my understanding exists? What I like to do: dic = {'key1':{'key2':[...]}} if my_value in dic[key1][key2]: > if login_auth in result and (login_country in result[login_auth][Countries]) > continue Should work. > This obviously does not work and I am not quite sure how to address the values > of 'Countries' in the right way. I'd like to check 'Key1:Key2:List' and don't > know how to address this It should worjk as you expect. However personally I'd use a class to define tyour data structure and just have a top leveldictionary holding instances of the class. Something like: class Login: def __init__(self, countries, IPs, count): self.countries = countries self.IPs = IPs self.count = count results = {'foo at bar.de': Login([DE,DK,UK], ['192.168.1.1','172.10.10.10'], [12]) } if auth in results and (myvalue in results[auth].Countries): ... BTW should count really be a list? > 2. How do I append new values to these lists within the nested dict? Same as any other list, just use the append() method: dic[key1][key2].append(value) or with a class: results[auth].IPs.append(value) HTH -- 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 From niharika1883 at gmail.com Wed Apr 25 13:34:43 2018 From: niharika1883 at gmail.com (Niharika Jakhar) Date: Wed, 25 Apr 2018 19:34:43 +0200 Subject: [Tutor] File handling Tab separated files In-Reply-To: References: <741113f1-cb9f-af6c-5884-7153c1c47b43@biologie.uni-freiburg.de> Message-ID: hi again when I #print (self.organismA) under the for x in self.results: , it results in what it is supposed to be. But when i print it in the below function, it gives some garbage value. Kindly let me know what is wrong. :) import functools import csv import time start =time.time() class BioGRIDReader: def __init__(self, filename): self.results = [] self.organisms = {} i = 0 with open(filename) as f: for line in csv.reader(f, delimiter = '\t'): i += 1 if i>35: self.results.append(line) #print (self.results) for x in self.results: self.organismA = x[2] self.organismB = x[3] self.temp = (x[2],) self.keys = self.temp self.values = [x[:]] self.organisms = dict(zip(self.keys, self.values)) #print (self.organismA) #print (self.results[0:34]) #omitted region def getMostAbundantTaxonIDs(self,n): #print (self.organismA) self.temp_ = 0 self.number_of_interactions = [] self.interaction_dict = {} for x in self.organismA: for value in self.organisms: if (x in value): self.temp_ += 1 self.number_of_interactions.append(self.temp_) self.interaction_dict = dict(zip(self.organismA, self.number_of_interactions)) a = BioGRIDReader("BIOGRID-ALL-3.4.159.tab.txt") a.getMostAbundantTaxonIDs(5) end = time.time() #print(end - start) Thanking you in advance Best Regards NIHARIKA On Fri, Apr 20, 2018 at 11:06 AM, Alan Gauld wrote: > > Use Reply-All or Reply-List to include the mailing list in replies. > > On 20/04/18 09:10, Niharika Jakhar wrote: > > Hi > > > > I want to store the data of file into a data structure which has 11 > > objects per line , something like this: > > 2354 somethin2 23nothing 23214..... > > > > > > so I was trying to split the lines using \n and storer each line in a > > list so I have a list of 11 objects, then I need to retrieve the last > > two position, > > You are using the csv module so you don't need to split the lines, the > csv reader has already done that for you. It generates a sequence of > tuples, one per line. > > So you only need to do something like: > > results = [] > with open(filename) as f: > for line in csv.reader(f, delimiter='\t'): > if line[-1] == line[-2]: > results.append(line[2],line[3]) > > Let the library do the work. > > You can see what the reader is doing by inserting a print(line) call > instead of the if statement. When using a module for the first time > don't be afraid to use print to check the input/output values. > Its better than guessing. > > > -- > 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 > > From mats at wichmann.us Wed Apr 25 14:19:28 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 25 Apr 2018 12:19:28 -0600 Subject: [Tutor] (no subject) In-Reply-To: <20180425132232.GA1277@mail.kbojens.de> References: <20180425132232.GA1277@mail.kbojens.de> Message-ID: On 12/31/1969 05:00 PM, wrote: > Hello everybody, > I'm coming from a Perl background and try to parse some Exim Logfiles into a > data structure of dictionaries. The regex and geoip part works fine and I'd > like to save the email adress, the countries (from logins) and the count of > logins. > > The structure I'd like to have: > > result = { > 'foo at bar.de': { > 'Countries': [DE,DK,UK] > 'IP': ['192.168.1.1','172.10.10.10'] > 'Count': [12] > } > 'bar at foo.de': { > 'Countries': [DE,SE,US] > 'IP': ['192.168.1.2','172.10.10.11'] > 'Count': [23] > } > } I presume that's pseudo-code, since it's missing punctuation (commas between elements) and the country codes are not quoted.... > > I don't have a problem when I do these three seperately like this with a one > dimensonial dict (snippet): > > result = defaultdict(list) > > with open('/var/log/exim4/mainlog',encoding="latin-1") as logfile: > for line in logfile: > result = pattern.search(line) > if (result): > login_ip = result.group("login_ip") > login_auth = result.group("login_auth") > response = reader.city(login_ip) > login_country = response.country.iso_code > if login_auth in result and login_country in result[login_auth]: > continue > else: > result[login_auth].append(login_country) > else: > continue > > This checks if the login_country exists within the list of the specific > login_auth key, adds them if they don't exist and gives me the results I want. > This also works for the ip addresses and the number of logins without any problems. > > As I don't want to repeat these loops three times with three different data > structures I'd like to do this in one step. There are two main problems I > don't understand right now: > > 1. How do I check if a value exists within a list which is the value of a key > which is again a value of a key in my understanding exists? What I like to do: > > if login_auth in result and (login_country in result[login_auth][Countries]) > continue you don't actually need to check (there's a Python aphorism that goes something like "It's better to ask forgiveness than permission"). You can do: try: result[login_auth]['Countries'].append(login_country) except KeyError: # means there was no entry for login_auth # so add one here that will happily add another instance of a country if it's already there, but there's no problem with going and cleaning the 'Countries' value later (one trick is to take that list, convert it to a set, then (if you want) convert it back to a list if you need unique values. you're overloading the name result here so this won't work literally - you default it outside the loop, then also set it to the regex answer... I assume you can figure out how to fix that up. From kb at kbojens.de Wed Apr 25 14:46:12 2018 From: kb at kbojens.de (Kai Bojens) Date: Wed, 25 Apr 2018 20:46:12 +0200 Subject: [Tutor] Dict of Dict with lists In-Reply-To: References: <20180425132232.GA1277@mail.kbojens.de> Message-ID: <20180425184612.GA9850@mail.kbojens.de> On 25/04/2018 ?? 18:35:30PM +0100, Alan Gauld via Tutor wrote: > > ... > > for line in logfile: > > result = pattern.search(line) > Doesn't this overwrite your data structure? > I would strongly advise using another name. You are of course right. I accidentally shortened this name as I was trying to fit my code into 80 characters width of this mail. That was sloppy ;) > However personally I'd use a class to define tyour data structure and > just have a top leveldictionary holding instances of the class. You are right (again). I haven't thougt of using classes, but that's exactly what they were invented for. Thanks for pointing that out. Thanks for the help! From kb at kbojens.de Wed Apr 25 14:50:21 2018 From: kb at kbojens.de (Kai Bojens) Date: Wed, 25 Apr 2018 20:50:21 +0200 Subject: [Tutor] (no subject) In-Reply-To: References: <20180425132232.GA1277@mail.kbojens.de> Message-ID: <20180425185021.GB9850@mail.kbojens.de> On 25/04/2018 ?? 12:19:28PM -0600, Mats Wichmann wrote: > I presume that's pseudo-code, since it's missing punctuation (commas > between elements) and the country codes are not quoted.... Yes, that was just a short pseudo code example of what I wanted to achieve. > you don't actually need to check (there's a Python aphorism that goes > something like "It's better to ask forgiveness than permission"). > You can do: > try: > result[login_auth]['Countries'].append(login_country) > except KeyError: > # means there was no entry for login_auth > # so add one here I see. That'd be better indeed. The try/except concept is still rather new to me and I still have to get used to it. Thanks for your hints! I'm sure that I can work with these suggestions ;) From scopensource at gmail.com Wed Apr 25 17:14:17 2018 From: scopensource at gmail.com (Simon Connah) Date: Wed, 25 Apr 2018 22:14:17 +0100 Subject: [Tutor] Async TCP Server Message-ID: Hi, I've come up with an idea for a new protocol I want to implement in Python using 3.6 (or maybe 3.7 when that comes out), but I'm somewhat confused about how to do it in an async way. The way I understand it is that you have a loop that waits for an incoming request and then calls a function/method asynchronously which handles the incoming request. While that is happening the main event loop is still listening for incoming connections. Is that roughly correct? The idea is to have a chat application that can at least handle a few hundred clients if not more in the future. I'm planning on using Python because I am pretty up-to-date with it, but I've never written a network server before. Also another quick question. Does Python support async database operations? I'm thinking of the psycopg2-binary database driver. That way I can offload the storage in the database while still handling incoming connections. If I have misunderstood anything, any clarification would be much appreciated. Simon. From leamhall at gmail.com Wed Apr 25 19:37:53 2018 From: leamhall at gmail.com (Leam Hall) Date: Wed, 25 Apr 2018 19:37:53 -0400 Subject: [Tutor] Async TCP Server In-Reply-To: References: Message-ID: On 04/25/2018 05:14 PM, Simon Connah wrote: > Hi, > > I've come up with an idea for a new protocol I want to implement in > Python using 3.6 (or maybe 3.7 when that comes out), but I'm somewhat > confused about how to do it in an async way. > > The way I understand it is that you have a loop that waits for an > incoming request and then calls a function/method asynchronously which > handles the incoming request. While that is happening the main event > loop is still listening for incoming connections. > > Is that roughly correct? > > The idea is to have a chat application that can at least handle a few > hundred clients if not more in the future. I'm planning on using > Python because I am pretty up-to-date with it, but I've never written > a network server before. > > Also another quick question. Does Python support async database > operations? I'm thinking of the psycopg2-binary database driver. That > way I can offload the storage in the database while still handling > incoming connections. > > If I have misunderstood anything, any clarification would be much appreciated. > > Simon. How does your idea differ from Twisted? From mats at wichmann.us Thu Apr 26 09:48:49 2018 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 26 Apr 2018 07:48:49 -0600 Subject: [Tutor] Dict of Dict with lists In-Reply-To: <20180425184612.GA9850@mail.kbojens.de> References: <20180425132232.GA1277@mail.kbojens.de> <20180425184612.GA9850@mail.kbojens.de> Message-ID: <1df6c94a-0389-1895-1282-190b1b6f9e1b@wichmann.us> On 04/25/2018 12:46 PM, Kai Bojens wrote: > On 25/04/2018 ?? 18:35:30PM +0100, Alan Gauld via Tutor wrote: >>> ... >>> for line in logfile: >>> result = pattern.search(line) > >> Doesn't this overwrite your data structure? >> I would strongly advise using another name. > > You are of course right. I accidentally shortened this name as I was trying to > fit my code into 80 characters width of this mail. That was sloppy ;) > >> However personally I'd use a class to define tyour data structure and >> just have a top leveldictionary holding instances of the class. > > You are right (again). I haven't thougt of using classes, but that's exactly > what they were invented for. Thanks for pointing that out. Opinion follows - there's not a right-or-wrong on this, or, I think, even a "most Pythonic": Using a bunch of class instances stuffed into one of the built-in iterable data structures will certainly work fine for this problem. When I first started learning how to use the "OOP features", not coming to Python from an OOP background, this is what I did - essentially, if I would have coded a struct in C, I wrote a class in Python. I don't do that any more if there's not any clear benefit to using classes, and I don't see one for this problem. As a learning excercise, you can try writing it both ways and see how it feels to you. In this case the dividing line might be... is this a building block for some larger complex system where the clarity you get from the layout - it's all there in the class definition - is important, or is this more of a "throwaway" single purpose script. From alan.gauld at yahoo.co.uk Thu Apr 26 12:13:02 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 26 Apr 2018 17:13:02 +0100 Subject: [Tutor] Dict of Dict with lists In-Reply-To: <1df6c94a-0389-1895-1282-190b1b6f9e1b@wichmann.us> References: <20180425132232.GA1277@mail.kbojens.de> <20180425184612.GA9850@mail.kbojens.de> <1df6c94a-0389-1895-1282-190b1b6f9e1b@wichmann.us> Message-ID: On 26/04/18 14:48, Mats Wichmann wrote: >>> However personally I'd use a class to define your data structure and >>> just have a top level dictionary holding instances of the class. >> >> You are right (again). I haven't thougt of using classes, but that's exactly >> what they were invented for. Thanks for pointing that out. > > Opinion follows - there's not a right-or-wrong on this, or, I think, > even a "most Pythonic": That's very true, its about personal preference mostly. > that any more if there's not any clear benefit to using classes, and I > don't see one for this problem. The advantage of using classes as defined data structures is reliability. Instead of using strings as names of keys in a dict we get the advantages of properly defined python attribute names so if we spell it wrong we get a name error, and we don't need to surround everything with ['']. It's not infallible of course because Python allows us to create attributes on instances at runtime so spelling errors in assignments still result in invalid fields being created. But at least we get a measure of extra checking done by the interpreter and save some typing. OTOH its definitely not good OOP, ther are no methods and we are just using the class as a record. (A named tuple might actually be a better option on reflection.) > In this case the dividing line might be... is this a building block for > some larger complex system where the clarity you get from the layout - > it's all there in the class definition - is important, or is this more > of a "throwaway" single purpose script. I'd also add the question of whether there might in fact be some behaviour that these login records might acquire within the system. For example formatted printing, comparisons between records etc etc. -- 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 From neilc at norwich.edu Thu Apr 26 13:09:38 2018 From: neilc at norwich.edu (Neil Cerutti) Date: Thu, 26 Apr 2018 17:09:38 +0000 (UTC) Subject: [Tutor] Dict of Dict with lists References: <20180425132232.GA1277@mail.kbojens.de> <20180425184612.GA9850@mail.kbojens.de> <1df6c94a-0389-1895-1282-190b1b6f9e1b@wichmann.us> Message-ID: On 2018-04-26, Alan Gauld via Tutor wrote: > OTOH its definitely not good OOP, ther are no methods and we > are just using the class as a record. (A named tuple might > actually be a better option on reflection.) namedtuple is great in lots of cases, but sometimes it transpires I wish to make alterations in the data while I'm working. Rebuilding tuples on the fly is possible, but painful. With a class instance it is easy to use __slots__ to help prevent the accidental creation of new attributes. >> In this case the dividing line might be... is this a building >> block for some larger complex system where the clarity you get >> from the layout - it's all there in the class definition - is >> important, or is this more of a "throwaway" single purpose >> script. > > I'd also add the question of whether there might in fact be > some behaviour that these login records might acquire within > the system. For example formatted printing, comparisons between > records etc etc. Excellent point! At the very least, such classes usually gain the ability to build themselves with an __init__ method, which can be a convenient place to put any logic they require. -- Neil Cerutti From jf_byrnes at comcast.net Thu Apr 26 16:18:39 2018 From: jf_byrnes at comcast.net (Jim) Date: Thu, 26 Apr 2018 15:18:39 -0500 Subject: [Tutor] Need help with FileNotFoundError Message-ID: <19212500-95e8-a20e-fcda-2c5947f16023@comcast.net> Been working my way through an online Python course. Up until now I have had no problems writing and running the programs using Python 3.6 in a virtual environment and then pasting them into the courses editor. When I run this program on my system I get the following error. # file_io.py def copy(file, new_file): with open(file) as data: text = data.read() with open(new_file, 'w') as new_text: new_text.write(text) copy('~/Documents/Courses/ModernBootcamp/story.txt', '~/Documents/Courses/ModernBootcamp/story_copy.txt') (env36) jfb at jims-mint18 ~ $ python '/home/jfb/Documents/Courses/ModernBootcamp/file_io.py' Traceback (most recent call last): File "/home/jfb/Documents/Courses/ModernBootcamp/file_io.py", line 11, in copy('~/Documents/Courses/ModernBootcamp/story.txt', '~/Documents/Courses/ModernBootcamp/story_copy.txt') File "/home/jfb/Documents/Courses/ModernBootcamp/file_io.py", line 4, in copy with open(file) as data: FileNotFoundError: [Errno 2] No such file or directory: '~/Documents/Courses/ModernBootcamp/story.txt' The file is there. jfb at jims-mint18 ~/Documents/Courses/ModernBootcamp $ ls adding_to_lists.py errors.py functionsII.py helpers.py modules.py stop_copying.py animals exercise.py functions.py iteration.py oop.py story.txt decorators.py file_io.py generators.py list_comps.py __pycache__ unlucky_numbers.py dictionarys.py FirstProgram guessing_game.py list_methods.py smiley_faces.py while_loop.py I must be doing something wrong path-wise, but I can't seem to figure it out. Any help appreciated. Regards, Jim From dyoo at hashcollision.org Thu Apr 26 16:27:28 2018 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 26 Apr 2018 20:27:28 +0000 Subject: [Tutor] Need help with FileNotFoundError In-Reply-To: <19212500-95e8-a20e-fcda-2c5947f16023@comcast.net> References: <19212500-95e8-a20e-fcda-2c5947f16023@comcast.net> Message-ID: > copy('~/Documents/Courses/ModernBootcamp/story.txt', > '~/Documents/Courses/ModernBootcamp/story_copy.txt') Hi Jim, You may need to use os.path.expanduser, as "tilde expansion" isn't something that's done automatically. This is referenced in the docs when they say: "Unlike a unix shell, Python does not do any automatic path expansions. Functions such as expanduser() and expandvars() can be invoked explicitly when an application desires shell-like path expansion. (See also the glob module.)" https://docs.python.org/3/library/os.path.html#module-os.path Try adding a call to os.path.expanduser() https://docs.python.org/3/library/os.path.html#os.path.expanduser on that tilde-prefixed path string. Hope this helps! From jf_byrnes at comcast.net Thu Apr 26 16:41:05 2018 From: jf_byrnes at comcast.net (Jim) Date: Thu, 26 Apr 2018 15:41:05 -0500 Subject: [Tutor] Need help with FileNotFoundError In-Reply-To: References: <19212500-95e8-a20e-fcda-2c5947f16023@comcast.net> Message-ID: On 04/26/2018 03:27 PM, Danny Yoo wrote: >> copy('~/Documents/Courses/ModernBootcamp/story.txt', >> '~/Documents/Courses/ModernBootcamp/story_copy.txt') > > > Hi Jim, > > You may need to use os.path.expanduser, as "tilde expansion" isn't > something that's done automatically. > > This is referenced in the docs when they say: "Unlike a unix shell, Python > does not do any automatic path expansions. Functions such as expanduser() > and expandvars() can be invoked explicitly when an application desires > shell-like path expansion. (See also the glob module.)" > > https://docs.python.org/3/library/os.path.html#module-os.path > > Try adding a call to os.path.expanduser() > https://docs.python.org/3/library/os.path.html#os.path.expanduser on that > tilde-prefixed path string. > > Hope this helps! Danny, Thanks for pointing me in the right direction. I had tried replacing the ~ with home/jfb/... now I realize it should have been /home/jfb/... Working now. Thanks, Jim From skahraman at cherrycreekschools.org Thu Apr 26 16:00:10 2018 From: skahraman at cherrycreekschools.org (Kahraman, Sam K.) Date: Thu, 26 Apr 2018 20:00:10 +0000 Subject: [Tutor] Sam Kahraman Campus Middle School Advice Message-ID: Hello, I am a 8th grade student at Campus Middle School. We have a project on coding and I need a expert to interview. Currently I'm learning Python and thought I should ask someone who works their to Interview. To contact me and ask any questions my email is skahraman at cherrycreekschools.org. From zachary.ware+pytut at gmail.com Fri Apr 27 00:12:24 2018 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Thu, 26 Apr 2018 23:12:24 -0500 Subject: [Tutor] Sam Kahraman Campus Middle School Advice In-Reply-To: References: Message-ID: Hi Sam, On Thu, Apr 26, 2018 at 3:00 PM, Kahraman, Sam K. wrote: > Hello, > > I am a 8th grade student at Campus Middle School. We have a project on coding and I need a expert to interview. Currently I'm learning Python and thought I should ask someone who works their to Interview. To contact me and ask any questions my email is skahraman at cherrycreekschools.org. Can you give some more details on what exactly you're looking for? This is a fairly unusual request for this mailing list, but I'm sure we can at least point you in the right direction. It might be easiest to just ask your questions here, and you're likely to get more answers than you bargained for :) Regards, Zach From dkwolfe at gmail.com Thu Apr 26 17:56:00 2018 From: dkwolfe at gmail.com (David Wolfe) Date: Thu, 26 Apr 2018 17:56:00 -0400 Subject: [Tutor] Sam Kahraman Campus Middle School Advice In-Reply-To: References: Message-ID: Sam; Were I you, I would look for a local MeetUp that deals with Python, attend one of their meetings, and talk to one of the folks there. I've learned a lot from the MeetUp in my area, and there are Python experts in several fields usually in attendance. They'll be able to help you with your questions and interview, as well as to give you pointers on next steps in learning and proficiency. Good Luck, David On Thu, Apr 26, 2018 at 4:00 PM, Kahraman, Sam K. < skahraman at cherrycreekschools.org> wrote: > Hello, > > I am a 8th grade student at Campus Middle School. We have a project on > coding and I need a expert to interview. Currently I'm learning Python and > thought I should ask someone who works their to Interview. To contact me > and ask any questions my email is skahraman at cherrycreekschools.org. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From skahraman at cherrycreekschools.org Thu Apr 26 16:11:25 2018 From: skahraman at cherrycreekschools.org (Kahraman, Sam K.) Date: Thu, 26 Apr 2018 20:11:25 +0000 Subject: [Tutor] Do you have any Ideas for a project. Message-ID: Sorry Forgot to explain what my project is about. I chose to learn to code in 30 hours in a span of 10 weeks. I'm ok at coding so I was wondering if you had any suggests for a project. I'm 14 and I'm ok at coding but probably a beginner. If you could help me that would be great. To contact me use my email, skahraman at cherrycreekschools.org From glennmschultz at me.com Fri Apr 27 16:52:22 2018 From: glennmschultz at me.com (Glenn Schultz) Date: Fri, 27 Apr 2018 20:52:22 +0000 (GMT) Subject: [Tutor] pandas read sql query advice Message-ID: All, I have the following set-up (below) which will be used to call data from multiple sectors. ?There is a sql query (transact sql) and connection. ?This works fine. ?However, I would like to parametrize the query so I can enter different sectors. ?I have checked through pyodbc and several SO posts. ?However, I am stuck and cannot get the parametrized version to work. ?Most examples I have found are either sqlite or postgres. sql ="""select foo from mutable where model sector = 'mysector'""" conn = myconn def modeldata(modelquery, conn) modeldata = pandas.read_sql_query(sql, conn) return modeldata Here is what I have tried (various combinations) - what am I doing wrong? sql ="""select foo from mutable where model sector = ?""", [params] conn = myconn def modeldata(modelquery, conn, params) modeldata = pandas.read_sql_query(sql, conn, params) return modeldata Thanks in advance, Glenn From sjeik_appie at hotmail.com Sat Apr 28 13:12:02 2018 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Sat, 28 Apr 2018 17:12:02 +0000 Subject: [Tutor] pandas read sql query advice Message-ID: On Apr 28, 2018 06:54, Glenn Schultz wrote: > > All, > > I have the following set-up (below) which will be used to call data from multiple sectors. There is a sql query (transact sql) and connection. This works fine. However, I would like to parametrize the query so I can enter different sectors. I have checked through pyodbc and several SO posts. However, I am stuck and cannot get the parametrized version to work. Most examples I have found are either sqlite or postgres. > > sql ="""select foo from mutable where model sector = 'mysector'""" > conn = myconn > > def modeldata(modelquery, conn) > modeldata = pandas.read_sql_query(sql, conn) > return modeldata > > Here is what I have tried (various combinations) - what am I doing wrong? > > sql ="""select foo from mutable where model sector = ?""", [params] > conn = myconn > > def modeldata(modelquery, conn, params) > modeldata = pandas.read_sql_query(sql, conn, params) > return modeldata pandas.read_sql_query(sql, conn, params=params) The third positional arg is actually index_col, so you need to use a keyword arg. https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql_query.html#pandas.read_sql_query BTW, I think you can use a context manager to automatically close the connection: with pyodbc.connect(your_conn_string) as conn: df = pd.read_sql_query(sql, conn, params=params) From shannonev101 at googlemail.com Mon Apr 30 09:35:01 2018 From: shannonev101 at googlemail.com (Shannon Evans) Date: Mon, 30 Apr 2018 14:35:01 +0100 Subject: [Tutor] Help with loops Message-ID: Hi, is there any way that i can add a loop or iteration or something so that i dont have to write out every person who has fruit. This information is coming from the following json files: *queue.json* file [ ["James Bruce", "Bananas"], ["Katherine Newton", "Bananas"], ["Deborah Garcia", "Pears"], ["Marguerite Kozlowski", "Pineapples"], ["Kenneth Fitzgerald", "Pineapples"], ["Ronald Crawford", "Bananas"], ["Donald Haar", "Apples"], ["Al Whittenberg", "Bananas"], ["Max Bergevin", "Bananas"], ["Carlos Doby", "Pears"], ["Barry Hayes", "Pineapples"], ["Donald Haar", "Bananas"] ] *stock.json* file { "Apples": 14, "Bananas": 14, "Pineapples": 0, "Pears": 8 } This is what i've done so far: import json #Load the stock and queue files queue=json.load(open("queue.json")) stock=json.load(open("stock.json")) if (stock[queue[0][1]]>0): #in stock print "Gave Bananas to James Bruce" else: print "Could not give Bananas to James Bruce" if (stock[queue[1][1]]>0): #in stock print "Gave Bananas to "+ queue[1][0] else: print "Could not give Bananas to "+ queue[1][0] if (stock[queue[2][1]]>0): #in stock print "Gave Pears to "+ queue[2][0] else: print "Could not give Pears to "+ queue[2][0] if (stock[queue[3][1]]>0): #in stock print "Gave Pineapples to "+ queue[3][0] else: print "Could not give Pineapples to "+ queue[3][0] Thanks Shannon From alan.gauld at yahoo.co.uk Mon Apr 30 14:42:42 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 30 Apr 2018 19:42:42 +0100 Subject: [Tutor] Help with loops In-Reply-To: References: Message-ID: On 30/04/18 14:35, Shannon Evans via Tutor wrote: > Hi, is there any way that i can add a loop or iteration or something so > that i dont have to write out every person who has fruit. Yes that's what loops are for. You have two options in Python: a 'for' loop or a 'while' loop In your case I suspect a 'for' loop is most appropriate. This information > is coming from the following json files: > *queue.json* file > > [ > > ["James Bruce", "Bananas"], > ... > ] > > > > *stock.json* file > > { > > "Apples": 14, > > "Bananas": 14, > > "Pineapples": 0, > > "Pears": 8 > > } > import json > > #Load the stock and queue files > queue=json.load(open("queue.json")) > stock=json.load(open("stock.json")) > You need to start the loop here then indent everything below. The syntax is like for data in queue: name, product = data Now change the queue access items to use name and product. > if (stock[queue[1][1]]>0): > #in stock > print "Gave Bananas to "+ queue[1][0] > else: > print "Could not give Bananas to "+ queue[1][0] > > if (stock[queue[2][1]]>0): > #in stock > print "Gave Pears to "+ queue[2][0] > else: > print "Could not give Pears to "+ queue[2][0] > > if (stock[queue[3][1]]>0): > #in stock > print "Gave Pineapples to "+ queue[3][0] > else: > print "Could not give Pineapples to "+ queue[3][0] Try it, if you get stuck come back and show us what you did. You will find more about Looping in my tutorial(see below) -- 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 From dbosah at buffalo.edu Mon Apr 30 11:57:16 2018 From: dbosah at buffalo.edu (Daniel Bosah) Date: Mon, 30 Apr 2018 11:57:16 -0400 Subject: [Tutor] Matplotlib scatterplot help Message-ID: I have a function in which returns scatterplot of a Isomap function, which takes output from a TF-IDF function, which calculated TF-IDF values of certain articles online. I used four articles and I want to show the 4 articles by a 3D scatterplot. Below is the function to turn my Isomap values to a 3D scatterplot : def Isomap(tfidf): jon = pd.read_csv(tfidf) le = preprocessing.LabelEncoder() tims = jon.apply(le.fit_transform) iso = manifold.Isomap(n_neighbors=2, n_components=3) john = iso.fit_transform(tims) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') use_colors = 'rybg' ax.scatter(john[:,0], john[:,1],john[:,2],color=use_colors,alpha=.5) # x,y,z coord. jon 1-3 plt.title('Isomap of candiates') plt.xlabel('x') plt.ylabel('y') plt.show() plt.savefig('isomap.png') The problem is that I usually only get one color returned. And even if I get the code to print out 4 colors, I'm not sure how to get those colors to correspond to the four web articles. Thanks for the help in advance. From shannonev101 at googlemail.com Mon Apr 30 18:15:46 2018 From: shannonev101 at googlemail.com (Shannon Evans) Date: Mon, 30 Apr 2018 23:15:46 +0100 Subject: [Tutor] updating stock list Message-ID: Hi, i'm wanting to update the stock list at the end so that the fruit that's given is taken subtracted from the original stock list. The code i've written isn't updating it it's just staying the same. Any idea what i can do to fix this? import json stock_json= json.load(open("stock.json")) queue_json= json.load(open("queue.json")) queue=[ ["James Bruce", "Bananas"], ["Katherine Newton", "Bananas"], ["Deborah Garcia", "Pears"], ["Marguerite Kozlowski", "Pineapples"], ["Kenneth Fitzgerald", "Pineapples"], ["Ronald Crawford", "Bananas"], ["Donald Haar", "Apples"], ["Al Whittenberg", "Bananas"], ["Max Bergevin", "Bananas"], ["Carlos Doby", "Pears"], ["Barry Hayes", "Pineapples"], ["Donald Haar", "Bananas"] ] stock={ "Apples": 14, "Bananas": 14, "Pineapples": 0, "Pears": 8 } for i in queue: if stock[i[1]]>0: print("Gave {} to {}".format(i[1],i[0])) else: print("Could not give {} to {}".format(i[1],i[0])) def total_stock(fruit): total=0 for i in fruit: if stock[i]>0: stock[i]=stock[i]-1 return total print stock From alan.gauld at yahoo.co.uk Mon Apr 30 19:51:13 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 1 May 2018 00:51:13 +0100 Subject: [Tutor] updating stock list In-Reply-To: References: Message-ID: On 30/04/18 23:15, Shannon Evans via Tutor wrote: > Hi, i'm wanting to update the stock list at the end so that the fruit > that's given is taken subtracted from the original stock list. The code > i've written isn't updating it it's just staying the same. Any idea what i > can do to fix this? > > > import json > > stock_json= json.load(open("stock.json")) > queue_json= json.load(open("queue.json")) > > queue=[ > ["James Bruce", "Bananas"], > ["Katherine Newton", "Bananas"], ... > ] > > stock={ > "Apples": 14, > "Bananas": 14, > "Pineapples": 0, > "Pears": 8 > } > > for i in queue: This should work OK but by convention programmers use single letter names like i to indicate indeces or other temporary integers or characters. It might be better to use a more descriptive name than i for your data. > if stock[i[1]]>0: > print("Gave {} to {}".format(i[1],i[0])) > else: > print("Could not give {} to {}".format(i[1],i[0])) But the loop should work and display the appropriate messages. > def total_stock(fruit): > total=0 > for i in fruit: > if stock[i]>0: > stock[i]=stock[i]-1 > return total Notice that this defines a function but... > print stock You never call it. Instead you print stock which is just your original list. Oncwe you have defined the function you need to explicitly call it for it to do anything. Notice too that in your function you use i as a key into stock. That implies that fruit is a list of strings(ie names of fruit) - is that going to be true? Or will it be the pairs from the queue data?In which case you need to extract the fruit name. HTH -- 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 From alan.gauld at yahoo.co.uk Mon Apr 30 19:58:32 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 1 May 2018 00:58:32 +0100 Subject: [Tutor] Matplotlib scatterplot help In-Reply-To: References: Message-ID: On 30/04/18 16:57, Daniel Bosah wrote: > I have a function in which returns scatterplot of a Isomap Below you show a function that creates a scatterplot but it does not return anything. Sorry to nitpick but correct terminology is quite important in understanding what you are trying to achieve. > Below is the function to turn my Isomap values to a 3D scatterplot : > > def Isomap(tfidf): > jon = pd.read_csv(tfidf) > le = preprocessing.LabelEncoder() > tims = jon.apply(le.fit_transform) > iso = manifold.Isomap(n_neighbors=2, n_components=3) > john = iso.fit_transform(tims) > fig = plt.figure() > ax = fig.add_subplot(111, projection='3d') > use_colors = 'rybg' > ax.scatter(john[:,0], john[:,1],john[:,2],color=use_colors,alpha=.5) # > x,y,z coord. jon 1-3 > plt.title('Isomap of candiates') > plt.xlabel('x') > plt.ylabel('y') > plt.show() > plt.savefig('isomap.png') > > The problem is that I usually only get one color returned. Do you mean displayed? Nothing is returned (or to be pedantic, None is returned) > get the code to print out 4 colors, Again nothing is printed. Are you talking about the displayed scatterplot? > I'm not sure how to get those colors > to correspond to the four web articles. Sadly neither do I, you need a mmatplotlib user to answer that bit. The only reference to colors I can see is the line use_colors - 'rybg' Which I assume stands for red, yellow, blue,green? I further assume that the order is significant? But I'm only guessing... -- 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 From itetteh34 at hotmail.com Mon Apr 30 12:03:52 2018 From: itetteh34 at hotmail.com (Isaac Tetteh) Date: Mon, 30 Apr 2018 16:03:52 +0000 Subject: [Tutor] Help with loops In-Reply-To: References: Message-ID: Hi Shannon, Yes there is a way. First in my opinion I think queue.json is not really a JSON data structure but just a list of list. If you are able to read it then I that?s great. One way to solve this is doing something like below: queue = [ ["James Bruce", "Bananas"], ["Katherine Newton", "Bananas"], ["Deborah Garcia", "Pears"], ["Marguerite Kozlowski", "Pineapples"], ["Kenneth Fitzgerald", "Pineapples"], ["Ronald Crawford", "Bananas"], ["Donald Haar", "Apples"], ["Al Whittenberg", "Bananas"], ["Max Bergevin", "Bananas"], ["Carlos Doby", "Pears"], ["Barry Hayes", "Pineapples"], ["Donald Haar", "Bananas"] ] stock = {"Apples": 14, "Bananas": 14, "Pineapples": 0, "Pears": 8} for i in queue: if stock[i[1]] > 0 : print("Gave {} to {}".format(i[1],i[0])) else: print("Could not give {} to {}".format(i[1],i[0])) Let us know if you have any other questions. Isaac, Sent from Mail for Windows 10 ________________________________ From: Tutor on behalf of Shannon Evans via Tutor Sent: Monday, April 30, 2018 8:35:01 AM To: tutor at python.org Subject: [Tutor] Help with loops Hi, is there any way that i can add a loop or iteration or something so that i dont have to write out every person who has fruit. This information is coming from the following json files: *queue.json* file [ ["James Bruce", "Bananas"], ["Katherine Newton", "Bananas"], ["Deborah Garcia", "Pears"], ["Marguerite Kozlowski", "Pineapples"], ["Kenneth Fitzgerald", "Pineapples"], ["Ronald Crawford", "Bananas"], ["Donald Haar", "Apples"], ["Al Whittenberg", "Bananas"], ["Max Bergevin", "Bananas"], ["Carlos Doby", "Pears"], ["Barry Hayes", "Pineapples"], ["Donald Haar", "Bananas"] ] *stock.json* file { "Apples": 14, "Bananas": 14, "Pineapples": 0, "Pears": 8 } This is what i've done so far: import json #Load the stock and queue files queue=json.load(open("queue.json")) stock=json.load(open("stock.json")) if (stock[queue[0][1]]>0): #in stock print "Gave Bananas to James Bruce" else: print "Could not give Bananas to James Bruce" if (stock[queue[1][1]]>0): #in stock print "Gave Bananas to "+ queue[1][0] else: print "Could not give Bananas to "+ queue[1][0] if (stock[queue[2][1]]>0): #in stock print "Gave Pears to "+ queue[2][0] else: print "Could not give Pears to "+ queue[2][0] if (stock[queue[3][1]]>0): #in stock print "Gave Pineapples to "+ queue[3][0] else: print "Could not give Pineapples to "+ queue[3][0] Thanks Shannon _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor