From __peter__ at web.de Fri Mar 1 10:17:57 2019 From: __peter__ at web.de (Peter Otten) Date: Fri, 01 Mar 2019 16:17:57 +0100 Subject: [Tutor] Remove soft line break References: <431086936.29.1549300383777.JavaMail.zimbra@pbds.eu> <800104116.88.1551355527069.JavaMail.zimbra@pbds.eu> Message-ID: Valerio Pachera wrote: [Me:] >> def merge_lines(lines): >> lines = (line.rstrip("\n") for line in lines) >> accu = [next(lines)] >> for line in lines: >> if line.startswith(" "): >> accu.append(line[1:]) >> else: >> yield "".join(accu) + "\n" >> accu = [line] >> yield "".join(accu) + "\n" > I think it could be solved in a much easier way. > content=f.read().replace('\n ', '') That is indeed much simpler than my suggestion. > What am I missing? Nothing. My attempt to keep memory usage low led to a solution which is more complex than your slurp-it-in, fix it, spit-it-out. From alan.gauld at yahoo.co.uk Fri Mar 1 11:16:21 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 01 Mar 2019 16:16:21 +0000 Subject: [Tutor] Exception not working as expected? In-Reply-To: Message-ID: <351d2da2-ff89-4841-aca3-1aefd75f1a93@email.android.com> I don't have python2 on my win10 PC and it works as expected on python 3. I suspect study of Eryk's post is your best bet. When it comes to the innards of Windows he is our local guru. On 1 Mar 2019 2:24 pm, Chip Wachob wrote: Alan, Thanks.** Once again, a case of not finding a tree in the forest...** One of these days I'll remember to KISS and try some SIMPLE experimentation... So I did your experiment with just the single line raw_input('>'), ran it, and used Ctrl-C to exit.** Traceback shown below.** C:\Temp_Python\Exception>python rawsimple.py >Traceback (most recent call last): ** File "rawsimple.py", line 1, in ****** raw_input('>') C:\Temp_Python\Exception> Note that I ran the same code on a Linux machine and did get the name of the exception as you expected (KeyboardInterrupt).** And I was able to make a slightly longer script (again, as suggested) and got the desired results.. Open to suggestions on how to capture the exception who's name isn't displayed... I'm still attempting to get my head around Eryk's post and perhaps that explains why there's no exception listed. On Thu, Feb 28, 2019 at 8:34 PM Alan Gauld via Tutor <[1]tutor at python.org> wrote: On 28/02/2019 21:03, Chip Wachob wrote: > it does work properly in Linux.** So I'm guessing I need to test for a > different exception along with the KeyboardInterrupt?? Don't guess, test. Write a single line script raw_input('> ') Run it in a console. Hit Ctrl-C while it waits for input. See what the stack trace says is the exception Edit your script try: ** raw_input('> ') except : ** print "I got it that time!" run the new script. Did it work? Hooray! Transfer to the main script. If not come back here armed with tracebacks... -- Alan G Author of the Learn to Program web site [2]http://www.alan-g.me.uk/ [3]http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: [4]http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist** -** [5]Tutor at python.org To unsubscribe or change subscription options: [6]https://mail.python.org/mailman/listinfo/tutor References Visible links 1. mailto:tutor at python.org 2. http://www.alan-g.me.uk/ 3. http://www.amazon.com/author/alan_gauld 4. http://www.flickr.com/photos/alangauldphotos 5. mailto:Tutor at python.org 6. https://mail.python.org/mailman/listinfo/tutor From wachobc at gmail.com Fri Mar 1 09:24:05 2019 From: wachobc at gmail.com (Chip Wachob) Date: Fri, 1 Mar 2019 09:24:05 -0500 Subject: [Tutor] Exception not working as expected? In-Reply-To: References: Message-ID: Alan, Thanks. Once again, a case of not finding a tree in the forest... One of these days I'll remember to KISS and try some SIMPLE experimentation... So I did your experiment with just the single line raw_input('>'), ran it, and used Ctrl-C to exit. Traceback shown below. C:\Temp_Python\Exception>python rawsimple.py >Traceback (most recent call last): File "rawsimple.py", line 1, in raw_input('>') C:\Temp_Python\Exception> Note that I ran the same code on a Linux machine and did get the name of the exception as you expected (KeyboardInterrupt). And I was able to make a slightly longer script (again, as suggested) and got the desired results.. Open to suggestions on how to capture the exception who's name isn't displayed... I'm still attempting to get my head around Eryk's post and perhaps that explains why there's no exception listed. On Thu, Feb 28, 2019 at 8:34 PM Alan Gauld via Tutor wrote: > On 28/02/2019 21:03, Chip Wachob wrote: > > > it does work properly in Linux. So I'm guessing I need to test for a > > different exception along with the KeyboardInterrupt?? > > Don't guess, test. > > Write a single line script > > raw_input('> ') > > Run it in a console. > Hit Ctrl-C while it waits for input. > See what the stack trace says is the exception > Edit your script > > try: > raw_input('> ') > except : > print "I got it that time!" > > run the new script. > Did it work? Hooray! Transfer to the main script. > If not come back here armed with tracebacks... > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From wachobc at gmail.com Fri Mar 1 14:23:50 2019 From: wachobc at gmail.com (Chip Wachob) Date: Fri, 1 Mar 2019 14:23:50 -0500 Subject: [Tutor] Exception not working as expected? In-Reply-To: References: Message-ID: Eryk Thank you for the in-depth explanation and code sample. I was able to use the sample code to create a functional script. Now, I'm still not completely following what you mean in the last couple of paragraphs about the except ValueError. I realized that I am never actually trapping on the else statement in my original code. When I enter inappropriate values, the exception is always trapping, not the else statement. Which seems sort of odd to me. I'm not sure what you mean by putting the except close to the int(). Generally, I feel like I'm getting a handle on this, but that actually leads me to a related question. As you can tell, this is just the top of a whole bunch of menu tree paths that the user can take. Below is a very rough textual representation of the layout of the menus. I understand how the sys.exit() works in the case of the initial menu, but I don't follow how I can use that same approach to navigate 'back' up the menu tree. Each major Test (Type A, Type B, etc) is a .py file unto itself. Something about this is feeling like maybe it should be a class, or some other function that I call throughout my program. How can I best handle this situation and, how do I trap those invalid entries and, as you say, "continue" the try statements? Main Menu | |-- Test Type A Items | |-- 1. Test A1 | |-- 2. Test A2 | |-- 3. Test A3 | |-- 0. Back to Main | |-- Test Type B Items | |-- 1. Test B1 | |-- 2. Test B2 | |-- 3. Test B3 | |-- 0. Back to Main | |-- Test Individual Item | |-- 1. Test I1 | |-- 2. Test I2 | |-- 3. Test I3 | |-- 0. Back to Main | |-- Quit Thank you, On Thu, Feb 28, 2019 at 9:31 PM eryk sun wrote: > On 2/28/19, Chip Wachob wrote: > > > > Python 2.7 & Windows and also Linux are the platforms being used. > Running > > the code from the command line / terminal as python except.py. Note > that > > it does work properly in Linux. So I'm guessing I need to test for a > > different exception along with the KeyboardInterrupt?? > > When reading from the console, we depend on a particular error being > set in order to distinguish an interrupted read from end-of-file > (EOF), but this error doesn't get set in Windows 8+ due to a bug in > the Windows API. I created an issue with Microsoft's console team, but > thus far they've ignored it. > > Due to this bug, Python 2 code that calls raw_input() or input() also > needs to handle EOFError, which should probably be handled anyway. > However, it's not enough to separately handle KeyboardInterrupt and > EOFError, since the KeyboardInterrupt may get raised while handling > EOFError. This can happen because Windows calls the console control > handler asynchronously in a new thread. We need a bit of a kludge that > checks for a delayed KeyboardInterrupt. For example, the following > shows a case that uses a common handler for EOF and Ctrl+C: > > import os > import sys > import time > > def main(): > try: > os.system('cls' if os.name == 'nt' else 'clear') > print "\n\n\n\n Welcome to Test.\n" > time.sleep(DISPLAY_TIME) > start_menu() > except (KeyboardInterrupt, EOFError): > try: > time.sleep(0.1) > except KeyboardInterrupt: > pass > # provides a clean exit > print "\n\n Keyboard Interrupt or EOF - Exiting \n\n" > time.sleep(5) > print "End of Script\n" > return 0 > > if __name__ == "__main__": > sys.exit(main()) > > > # trap no choice, just Enter key cases > > except ValueError: > > # handles non-entry selections like 'Enter' > > print "Invalid selection please try again" > > time.sleep(DISPLAY_TIME / 2) > > start_menu() > > ValueError should be handled near the int() conversion that raises it, > and the exception handler should `continue` the loop instead of > recursively calling start_menu(). > _______________________________________________ > 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 Fri Mar 1 19:05:38 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 2 Mar 2019 00:05:38 +0000 Subject: [Tutor] Exception not working as expected? In-Reply-To: References: Message-ID: On 01/03/2019 19:23, Chip Wachob wrote: > I'm not sure what you mean by putting the except close to the int(). He means you should have a try/except for the int conversion, typically something like: # wait for it... try: num_items = int(raw_input(": ")) except ValueError: # try a second time. if num_items == 0: # individual testing print "\nIndividual testing requested" That means the user has a chance of continuing without errors and you don;t need to use recursion to restart the menu system. Part of the problem is that your design does not cleanly separate the presentation of the menus and collection of user input from the logic behind processing the values. That usually leads to complicated code that's hard to debug(and test) You want to catch the error as soon as possibly after it happens not at the end of the while loop. Put the try inside the loop where you read the input and then when things go wrong you can catch it, fix it and continue with the program.and it simplifies the overall structure. > the layout of the menus. I understand how the sys.exit() works in the case > of the initial menu, but I don't follow how I can use that same approach to > navigate 'back' up the menu tree. Each major Test (Type A, Type B, etc) is > a .py file unto itself. So you make them modules and import them into your main program. That way you can call any of them as needed. And put the menu code in a separate function(or set of functions) that simply displays the menu and gets a valid response. Use the return value from that function(s) to drive an if/elif ladder to determine how to process the menu selections. Or you could construct a dictionary that maps menu choice to a function but, while that's a more flexibly and extensible approach its also much harder to code up correctly initially. if/elif is usually good enough. > Something about this is feeling like maybe it should be a class, or some > other function that I call throughout my program. You could create a menu class but I doubt if its needed here. A simple function (possibly taking a menu string as input, or just storing the menu internally) that returns some kind of unique identifies for the users choice is all you should need. > How can I best handle this situation and, how do I trap those invalid > entries and, as you say, "continue" the try statements? continue the while loop not the try. Put the try inside the loop and use "continue" once you fix the error. "continue" jumps you back to the top of the loop. So your code might look something like: while True: choice = get_menu_option() if choice == 0; # do something elif choice == 1: # etc else: break # exit the while loop And inside get_menu_option() you have something like def get_menu_option(menu_string) while True: print menu_string try: val = int(raw_input(': ')) except .... # fix error here if value_is_valid(val) #is it in range, non negative etc? return val # implicit else returns to top of loop That way your top level code always gets a valid user choice to process and the error handling is located beside the place it occurred. With a multi tiered menu structure you might have two menu functions, one per level. You might also have some way of mapping the menu values to a single unique command ID. But those are just details. The structure should be broadly the same. -- 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 cs at cskk.id.au Fri Mar 1 22:31:16 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 2 Mar 2019 14:31:16 +1100 Subject: [Tutor] Exception not working as expected? In-Reply-To: References: Message-ID: <20190302033116.GA5391@cskk.homeip.net> On 02Mar2019 00:05, Alan Gauld wrote: >On 01/03/2019 19:23, Chip Wachob wrote: >> I'm not sure what you mean by putting the except close to the int(). > >He means you should have a try/except for the int conversion, >typically something like: > > > # wait for it... > try: num_items = int(raw_input(": ")) > except ValueError: > # try a second time. > > if num_items == 0: # individual testing > print "\nIndividual testing requested" > >That means the user has a chance of continuing without errors >and you don;t need to use recursion to restart the menu system. Just further to this remark of Alan's: you should make try/except clauses as small as is reasonable i.e. containing as little code as possible between "try" and "except". Not only does this have the advantage Alan mentioned, of making it possible to offset the user the chance to continue after an error, it _also_ means you have more confidence about what caused the exception. In you programme, you have a try/except around the entire main body of your loop. This means that the "except ValueError" clause will intercept _any_ ValueError issued by the code, not just the one from the int() call, so it needn't indicate bad user input, it could as easily indicate some subsequent bug in your code, as many functions can raise ValueError if they are handed something unsatisfactory. Cheers, Cameron Simpson From ingoogni at gmail.com Sat Mar 2 06:07:28 2019 From: ingoogni at gmail.com (ingo) Date: Sat, 2 Mar 2019 12:07:28 +0100 Subject: [Tutor] type hint woes regex Message-ID: <03f0f32c-ba98-d20c-3fcc-d769ded2def8@gmail.com> In a first attempt to add types I ran into trouble. First a code snippet ( full code at https://gist.github.com/ingoogni/460676b11e5a04ed3a3ac93ae0e1fddd ) _matchline: Pattern[str] = re.compile(r"(?P^\s*$)|(?P[\t]*)" "((?P[A-Z]+):)?([ \t]*)(?P.*)") def proc_matchlns(momfile: TextIO) -> Iterator[Dict[str, Union[str, int]]]: for i, ln in enumerate(momfile, 1): match: Match[str] = _matchline.match(ln) #error 1 m: Dict[str, str] = match.groupdict() #error 2 m['ln'] = ln.rstrip() m['ln#'] = i #error 3 yield m #error 4 Depending on how I add types mypy nags on various aspects: error 1: match seems to want type Optional[Match[str]], but why? The regex always has a result (I hope). error 2: when I give in to the above, the result is that it errors on not being able to de a groupdict on a null type. error 3: groupdict results in a type dict[str, str], but I want to add an int. Can I change the type? (In this specific case could change i to str(i) as it has no influence on the program) error 4: result of 3 and output type, type of m and specified output type don't match. Ingo From robertvstepp at gmail.com Sat Mar 2 16:16:15 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 2 Mar 2019 15:16:15 -0600 Subject: [Tutor] Is this the preferred way to change terminal screen color using curses? Message-ID: I wanted to be able to change the background screen color of a terminal window using curses. My weak Google-Foo did not turn up a clear example of how to do this despite much searching. The two _obvious_curses methods to attempt this seemed to be window.bkgdset(ch, attr) to initially set a window's background attributes and window.bkgd(ch, attr) to change them to new values. The thing that has puzzled me is that "ch" is a mandatory parameter to supply. So after a variety of experimental efforts I came up with the following approach which seems to do what I want to do -- just change the terminal's background color at will. But since I did *not* locate any clear examples online on how to do this, I cannot help but wonder if there is an easier approach to do what I want to do? My code follows. As always I am open to any constructive criticism even if it is off this email's topic, though this code is not meant to be a polished product. #!/usr/bin/env python3 import curses def start_cli(stdscr): max_height, max_width = stdscr.getmaxyx() curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_CYAN) PAIR_BLACK_ON_CYAN = curses.color_pair(1) stdscr.clear() stdscr.bkgdset(' ', PAIR_BLACK_ON_CYAN) # Fill screen with spaces to color screen background: for y in range(max_height): try: stdscr.addstr(y, 0, ' ' * max_width) except curses.error as error: if y == max_height - 1: # Ignore error from writing to lower right-hand screen corner: pass else: raise error # Make cursor invisible to ensure *entire* screen is colored: curses.curs_set(0) stdscr.refresh() # Pause program until user presses key: stdscr.getkey() # Change background screen color: curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_RED) PAIR_BLACK_ON_RED = curses.color_pair(2) change_bkgd_color(stdscr, PAIR_BLACK_ON_RED) stdscr.getkey() def change_bkgd_color(window_obj, color_pair): window_obj.bkgd(' ', color_pair) if __name__ == '__main__': input("Press ENTER to change screen to first color, then press" " any key for next color change until the program exits.") curses.wrapper(start_cli) -- boB From cs at cskk.id.au Sat Mar 2 17:27:29 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 3 Mar 2019 09:27:29 +1100 Subject: [Tutor] Is this the preferred way to change terminal screen color using curses? In-Reply-To: References: Message-ID: <20190302222729.GA34793@cskk.homeip.net> On 02Mar2019 15:16, boB Stepp wrote: >I wanted to be able to change the background screen color of a >terminal window using curses. My weak Google-Foo did not turn up a >clear example of how to do this despite much searching. The two >_obvious_curses methods to attempt this seemed to be >window.bkgdset(ch, attr) to initially set a window's background >attributes and window.bkgd(ch, attr) to change them to new values. >The thing that has puzzled me is that "ch" is a mandatory parameter to >supply. Remember that the Python curses module is a shim for the curses (or ncurses) C library. So "man 3 curses" gets you the main library page, and there are distinct manual entries for the various functions. On my machine (a Mac) the curses(3) manual entry has a section named "Routine Name Index" thus: Routine Name Index The following table lists each curses routine and the name of the man? ual page on which it is described. Routines flagged with ?*? are ncurses-specific, not described by XPG4 or present in SVr4. and then there's a long table of functions and the match manual entry name. bkgdset is listed as in the curs_bkgd(3) entry, so run "man curs_bkgd" to view that manual entry. It describes the bkgdset, wbkgdset, bkgd, wbkgd, getbkgd functions in some detail. Also, the curs_color manual entry talks about terminal colours in detail. So "man curs_color". >So after a variety of experimental efforts I came up with the >following approach which seems to do what I want to do -- just change >the terminal's background color at will. But since I did *not* locate >any clear examples online on how to do this, I cannot help but wonder >if there is an easier approach to do what I want to do? I would be inclined to call getbkgd() and then modify what it gives you as desired, then call bkgdset(). Untested. >My code follows. As always I am open to any constructive criticism >even if it is off this email's topic, though this code is not meant to >be a polished product. [...] > # Fill screen with spaces to color screen background: > for y in range(max_height): > try: > stdscr.addstr(y, 0, ' ' * max_width) > except curses.error as error: > if y == max_height - 1: > # Ignore error from writing to lower right-hand screen corner: > pass > else: > raise error Is the painting of the screen with spaces actually required? I would have thought not (again, untested). The main window (stdscr) should start filled with spaces. [Reads more closely...] probably you want to call bkgd() or wbkgd() instead. "man curs_bkgd" says: bkgd The bkgd and wbkgd functions set the background property of the current or specified window and then apply this setting to every character position in that window: ? The rendition of every character on the screen is changed to the new background rendition. ? Wherever the former background character appears, it is changed to the new background character. Cheers, Cameron Simpson From robertvstepp at gmail.com Sat Mar 2 23:32:08 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 2 Mar 2019 22:32:08 -0600 Subject: [Tutor] Is this the preferred way to change terminal screen color using curses? In-Reply-To: <20190302222729.GA34793@cskk.homeip.net> References: <20190302222729.GA34793@cskk.homeip.net> Message-ID: On Sat, Mar 2, 2019 at 4:28 PM Cameron Simpson wrote: > Is the painting of the screen with spaces actually required? I would > have thought not (again, untested). The main window (stdscr) should > start filled with spaces. I had read this along the way, but had forgotten it. > [Reads more closely...] probably you want to call bkgd() or wbkgd() > instead. "man curs_bkgd" says: > > bkgd > The bkgd and wbkgd functions set the background property of the > current or specified window and then apply this setting to every > character position in that window: > ? The rendition of every character on the screen is changed to > the new background rendition. > ? Wherever the former background character appears, it is changed > to the new background character. I had seen this, but I have been giving way too much credence to the names given to these methods. This is the second time I have been badly burned by the names used. I had ASSumed that if there is a bkgdset() method, that the window attributes need to be initialized first if one is not satisfied with the default behavior. And I *did* try using bkgdset() by itself without manually populating spaces, but it did not change the color of anything but the window border I had used in my original trial code. I tried your suggestion with bkgd() and it worked beautifully. BTW, my Linux Mint installation did *not* have the man pages for ncurses, even though it was installed. I had to manually fetch the man pages myself. -- boB From cs at cskk.id.au Sat Mar 2 23:47:45 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 3 Mar 2019 15:47:45 +1100 Subject: [Tutor] Is this the preferred way to change terminal screen color using curses? In-Reply-To: References: Message-ID: <20190303044745.GA67257@cskk.homeip.net> On 02Mar2019 22:32, boB Stepp wrote: >BTW, my Linux Mint installation did *not* have the man pages for >ncurses, even though it was installed. I had to manually fetch the >man pages myself. Maybe they're in a separate -dev or -doc package? Sometimes the base package (eg "libncurses") contains only the library, not header files or doco wanted for development. Cheers, Cameron Simpson From guettliml at thomas-guettler.de Sun Mar 3 07:41:17 2019 From: guettliml at thomas-guettler.de (=?UTF-8?Q?Thomas_G=c3=bcttler_Lists?=) Date: Sun, 3 Mar 2019 13:41:17 +0100 Subject: [Tutor] Next steps after creating virtualenv (new github project)? Message-ID: Hello, I want to execute: print('Hello world.') in a file called hello.py After creating a virtual-env (via PyCharm) I? have these directories: ./foo ./foo/venv ./foo/venv/lib ./foo/venv/lib/python3.6 ./foo/venv/lib/python3.6/site-packages ./foo/venv/include ./foo/venv/bin I want to store my small project in git and upload it to github later. What is the best practice for the directory layout? Are there (official) docs how to start a new project after creating the virtualenv? Regards, ? Thomas G?ttler -- Thomas Guettler http://www.thomas-guettler.de/ I am looking for feedback: https://github.com/guettli/programming-guidelines From dave at the-hills.org.uk Sun Mar 3 09:46:10 2019 From: dave at the-hills.org.uk (Dave Hill) Date: Sun, 3 Mar 2019 14:46:10 +0000 Subject: [Tutor] systemd Message-ID: <2b1e2b5c-9a27-7cc7-24c6-4b8d675887fe@the-hills.org.uk> Hi, I am nearly ready for the new season on the Heritage railway I volunteer on and I am putting the final touches to a revamped 'runaway train' attraction in the museum. The last act is to get the code to auto-start on power up, and I thought that I would use 'systemd'. The code runs on a Raspberry Pi; via the GPIO: sensing an input, reed switch, and controlling three outputs; smoke machine, fan and vibrating floor. on activation a video is played, at 8 times real speed, which is viewed through a mock-up of a loco cab. I can run a program using: [Unit] Description=TestVideo Service After=multi-user.target [Service] Type=idle User=pi ExecStart=/bin/bash /home/pi/Code/testVideo.sh Restart=no RestartSec=0 [Install] WantedBy=multi-user.target where 'testVideo.sh' ## Dave Hill, 28/02/2019 # 2 = HDMI, 1 = headphones, 0 = Auto amixer cset numid=3 1 python3 /home/pi/Code/TestVideo#6.py CLIP0026.MP4 20 DEBUG exit 0 as I want to control the audio output, in this case direct audio to the Raspberry Pi stereo output. The program runs an infinite loop that can be exited by Cntrl-C I would like to take advantage of the 'restart' functionality, but I still wish to have a method of exiting i.e. Restart=always Is there a way in which I can have automatic restart but also have a way of exiting? Thanks in advance, Dave From alan.gauld at yahoo.co.uk Sun Mar 3 11:01:08 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 3 Mar 2019 16:01:08 +0000 Subject: [Tutor] systemd In-Reply-To: <2b1e2b5c-9a27-7cc7-24c6-4b8d675887fe@the-hills.org.uk> References: <2b1e2b5c-9a27-7cc7-24c6-4b8d675887fe@the-hills.org.uk> Message-ID: On 03/03/2019 14:46, Dave Hill wrote: > on power up, and I thought that I would use 'systemd'. This is really a Linux question not Python so you might like to try the Mint forums too. They are usually pretty responsive. > [Service] > Type=idle > > User=pi > ExecStart=/bin/bash /home/pi/Code/testVideo.sh > > Restart=no > RestartSec=0 > where 'testVideo.sh' > > ## Dave Hill, 28/02/2019 > > # 2 = HDMI, 1 = headphones, 0 = Auto > amixer cset numid=3 1 > > python3 /home/pi/Code/TestVideo#6.py CLIP0026.MP4 20 DEBUG To be honest I'm surprised that works. I'd expected bash to interpret the # as a comment delimiter! But I tried it and it seems ok, you live and learn... > Is there a way in which I can have automatic restart but also have a way > of exiting? That's the Linux bit... :-) -- 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 dave at the-hills.org.uk Sun Mar 3 11:16:29 2019 From: dave at the-hills.org.uk (Dave Hill) Date: Sun, 3 Mar 2019 16:16:29 +0000 Subject: [Tutor] systemd In-Reply-To: References: <2b1e2b5c-9a27-7cc7-24c6-4b8d675887fe@the-hills.org.uk> Message-ID: <79ec0633-64f2-81fc-b0c7-472e616f4688@the-hills.org.uk> Thank you - registering with Linux Mint On 03/03/2019 16:01, Alan Gauld via Tutor wrote: > On 03/03/2019 14:46, Dave Hill wrote: > >> on power up, and I thought that I would use 'systemd'. > This is really a Linux question not Python so you might like to try the > Mint forums too. They are usually pretty responsive. > >> [Service] >> Type=idle >> >> User=pi >> ExecStart=/bin/bash /home/pi/Code/testVideo.sh >> >> Restart=no >> RestartSec=0 >> where 'testVideo.sh' >> >> ## Dave Hill, 28/02/2019 >> >> # 2 = HDMI, 1 = headphones, 0 = Auto >> amixer cset numid=3 1 >> >> python3 /home/pi/Code/TestVideo#6.py CLIP0026.MP4 20 DEBUG > To be honest I'm surprised that works. > I'd expected bash to interpret the # as a comment delimiter! > But I tried it and it seems ok, you live and learn... > >> Is there a way in which I can have automatic restart but also have a way >> of exiting? > That's the Linux bit... :-) > From cs at cskk.id.au Sun Mar 3 15:59:21 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 4 Mar 2019 07:59:21 +1100 Subject: [Tutor] systemd In-Reply-To: References: Message-ID: <20190303205921.GA11534@cskk.homeip.net> On 03Mar2019 16:01, Alan Gauld wrote: >On 03/03/2019 14:46, Dave Hill wrote: >> python3 /home/pi/Code/TestVideo#6.py CLIP0026.MP4 20 DEBUG > >To be honest I'm surprised that works. >I'd expected bash to interpret the # as a comment delimiter! >But I tried it and it seems ok, you live and learn... The octothorpe is only a comment marker at the start of a word. Cheers, Cameron Simpson From david at graniteweb.com Sun Mar 3 19:59:57 2019 From: david at graniteweb.com (David Rock) Date: Sun, 3 Mar 2019 18:59:57 -0600 Subject: [Tutor] systemd In-Reply-To: <20190303205921.GA11534@cskk.homeip.net> References: <20190303205921.GA11534@cskk.homeip.net> Message-ID: > On Mar 3, 2019, at 14:59, Cameron Simpson wrote: > > On 03Mar2019 16:01, Alan Gauld wrote: >> On 03/03/2019 14:46, Dave Hill wrote: >>> python3 /home/pi/Code/TestVideo#6.py CLIP0026.MP4 20 DEBUG >> >> To be honest I'm surprised that works. >> I'd expected bash to interpret the # as a comment delimiter! >> But I tried it and it seems ok, you live and learn... > > The octothorpe is only a comment marker at the start of a word. While it works, it?s usually a bad idea to use non alphanumeric characters for filenames. Naming it TestVideo6.py would be less prone to ?weird things? happening down the road. ? David Rock david at graniteweb.com From harry_oneill_52 at hotmail.co.uk Sun Mar 3 18:01:33 2019 From: harry_oneill_52 at hotmail.co.uk (Harry Oneill) Date: Sun, 3 Mar 2019 23:01:33 +0000 Subject: [Tutor] Variable change within library depending on user input. Message-ID: Hey there everybody hope your doing great. I was here a few months ago and got pointed in the right direction very kindly by one of the tutors. Im a little stuck again now and have been researching for a while and can't come up with a solution to my problem. The below program is designed to take two user inputs, item purchase location and sale location and then to compare some dictionaries of those locations and calculate the most profitable item to buy. Initially i designed this to work with just two locations and am now struggling to add a third. #### Author : Harry O'Neill #### Program : Find most profitable sale between two trade hubs within the star citizen game client. #### Waste has been remove as it's literally garbage anyway. #### taking user information user_purchase_location = input("Were are you planning to buy your goods?").lower() user_sale_destination = input("Where are you planning to sell your goods?").lower() #### dicts of items available to buy and sell at locations along with UEC price. buying_olisar = {'Medical Supply': 17.01,} selling_olisar = {'Agricium': 25.60, 'Aluminum': 1.25, 'Beryl': 4.26, 'Chlorine': 1.57, 'Corundum': 2.53, 'Diamond': 6.90, 'Distilled Spirits': 4.95, 'Fluorine': 2.80, 'Gold': 6.07, 'Hydrogen': 1.02, 'Iodine': 0.41, 'Laranite': 28.91, 'Processed Food': 1.39, 'Quartz': 1.44, 'Scrap': 1.67, 'Stims': 3.40, 'Titanium': 8.27, 'Tungsten': 3.90,} buying_levski = {'Agricultural Supply': 1.11, 'Aluminum': 1.20, 'Hydrogen': 0.98, 'Iodine': 0.38, 'Quartz': 1.37,} selling_levski = {'Agricium': 25.60, 'Altruciatoxine': 11.63, 'Beryl': 4.25, 'Chlorine': 1.56, 'Corundum': 2.53, 'Diamond': 6.07, 'Distilled Spirits': 4.95, 'Fluorine': 2.80, 'Gold': 6.07, 'Laranite': 28.25, 'Medical Supply': 18.00, 'Processed Food': 1.38, 'Scrap': 1.68, 'Stims': 3.40, 'Titanium': 8.27, 'Tungsten': 3.90, 'Widdow': 24.00,} buying_arc_corp_mining_area_157 = {'Chlorine': 1.44, 'Flurine': 2.63, 'Hydrogen': 0.97, 'Iodine': 0.34} selling_arc_corp_mining_area_157 = {'Altruciatoxine': 11.80, 'distilled spirits': 4.95, 'medical suppies': 18.05, 'processed food': 1.36, 'stims': 3.40, 'Widdow': 24.0} #### Sorting the dict into ascending value instead of alphabetical sort_buying_olisar = sorted(buying_olisar, key=lambda tup: tup[1]) sort_selling_olisar = sorted(selling_olisar, key=lambda tup:(tup[1], tup[0])) sort_buying_levski = sorted(buying_levski, key=lambda tup: tup[1]) sort_selling_levski = sorted(selling_levski, key=lambda tup:(tup[1], tup[0])) sort_buying_arc_corp_mining_area_157 = sorted(buying_arc_corp_mining_area_157, key=lambda tup: tup[1]) sort_selling_arc_corp_mining_area_157 = sorted(selling_arc_corp_mining_area_157, key=lambda tup:(tup[1], tup[0])) #### picking two dicts (buy/sell) and filtering them to print only common elements. def common(curr, other): return set(curr.keys()).intersection(set(other.keys())) tradable_items = common(buying_levski, selling_olisar) #### calculating the prices between possible sales. prices = {k: selling_olisar[k] - buying_levski[k] for k in tradable_items} #### finding the most profitable material import operator most_profitable_resourse = max(prices.items(), key=operator.itemgetter(1))[0] profit_ammount = prices profit_sorted = (f'{profit_ammount[most_profitable_resourse]:.2f}') print("The most profitable item to buy at " + user_purchase_location + " is " + most_profitable_resourse + " and will make a net profit of " + profit_sorted + " per unit when sold at " + user_sale_destination + ".") The two areas i am struggling to alter to allow this to work with more than two locations are below tradable_items = common(buying_levski, selling_olisar) and prices = {k: selling_olisar[k] - buying_levski[k] for k in tradable_items} i need to have the variables to alter based on the user input where as currently they are fixed as buying/selling levski/olisar. I have tried to create a second variable as follows: user_purchase_location_1 = 'buying_'+user_purchase_location+'' user_sale_destination_1 = 'selling_'+user_sale_destination+'' these produce the correct information that could be used in the above two lines however when altered like this: def common(curr, other): return set(curr.keys()).intersection(set(other.keys())) tradable_items = common(user_purchase_location_1, user_sale_destination_1) #### calculating the prices between possible sales. prices = {k: user_sale_destination_1[k] - user_purchase_location_1[k] for k in tradable_items} However they produce this error that i am unsure how to resolve. Traceback (most recent call last): File "/home/floppypoppy/PycharmProjects/Star Citizen Trading Program/Star Citizen trading NEW.py", line 33, in tradable_items = common(user_purchase_location_1, user_sale_destination_1) File "/home/floppypoppy/PycharmProjects/Star Citizen Trading Program/Star Citizen trading NEW.py", line 32, in common return set(curr.keys()).intersection(set(other.keys())) AttributeError: 'str' object has no attribute 'keys' I hope i have offered enough information here if any more is required please ask, Thank you for any guidance with this. kindest regards, Harry O'Neill From david at graniteweb.com Sun Mar 3 20:16:04 2019 From: david at graniteweb.com (David Rock) Date: Sun, 3 Mar 2019 19:16:04 -0600 Subject: [Tutor] systemd In-Reply-To: <2b1e2b5c-9a27-7cc7-24c6-4b8d675887fe@the-hills.org.uk> References: <2b1e2b5c-9a27-7cc7-24c6-4b8d675887fe@the-hills.org.uk> Message-ID: <25BF5DE6-D25E-4E52-8643-D8D133822B81@graniteweb.com> > On Mar 3, 2019, at 08:46, Dave Hill wrote: > > > I would like to take advantage of the 'restart' functionality, but I still wish to have a method of exiting > > i.e. Restart=always > > Is there a way in which I can have automatic restart but also have a way of exiting? > Maybe ?always? is not what you want. If you use ?on-failure" instead, it will restart in all cases except when it cleanly exits. From systemd.service manpage: https://manpages.debian.org/jessie/systemd/systemd.service.5.en.html "Setting this to on-failure is the recommended choice for long-running services, in order to increase reliability by attempting automatic recovery from errors. For services that shall be able to terminate on their own choice (and avoid immediate restarting), on-abnormal is an alternative choice.? You will also see a table of what each setting does, to help you pick the best option for your needs. You might also want to set the RestartSec to something other than zero. If you set it to something like 20 seconds, it would also give you time to manually disable the service if you needed to. ? David Rock david at graniteweb.com From __peter__ at web.de Mon Mar 4 03:29:35 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 04 Mar 2019 09:29:35 +0100 Subject: [Tutor] Variable change within library depending on user input. References: Message-ID: Harry Oneill wrote: > Hey there everybody hope your doing great. > > I was here a few months ago and got pointed in the right direction very > kindly by one of the tutors. Im a little stuck again now and have been > researching for a while and can't come up with a solution to my problem. > > The below program is designed to take two user inputs, item purchase > location and sale location and then to compare some dictionaries of those > locations and calculate the most profitable item to buy. > > Initially i designed this to work with just two locations and am now > struggling to add a third. Do you still have the version that works with two locations? Then start from that with the following steps: (1) Extract the code that uses a buying and a selling, and finds the most profitable good into a function def find_most_profitable(buying_dict, selling_dict): ... # code adapted from the version that works with two locations (2) Write the main script that picks a buying dict and a selling dict using user input. The simplest approach is to put the buying dicts into another dict: buying_dicts = {"olisar": buying_olisar, "levski": buying_levski, "156": buying_arc_corp_mining_area_157} selling_dicts = ... def pick_dict(choices, prompt): print(prompt) while True: print("Pick one of", ", ".join(sorted(choices))) choice = input("> ") if choice in choices: return choices[choice] buying = pick_dict( buying_dicts, "Were are you planning to buy your goods?" ) selling = pick_dict( selling_dicts, "Where are you planning to sell your goods?" ) find_most_profitable(buying, selling) As a bonus the pick_dict() function checks the validity of your choice and lets you repeat your input if you made a spelling error. > However they produce this error that i am unsure how to resolve. > > Traceback (most recent call last): > File "/home/floppypoppy/PycharmProjects/Star Citizen Trading > Program/Star Citizen trading NEW.py", line 33, in > tradable_items = common(user_purchase_location_1, > user_sale_destination_1) > File "/home/floppypoppy/PycharmProjects/Star Citizen Trading > Program/Star Citizen trading NEW.py", line 32, in common > return set(curr.keys()).intersection(set(other.keys())) > AttributeError: 'str' object has no attribute 'keys' Now why did you see the traceback? > #### Sorting the dict into ascending value instead of alphabetical > sort_buying_olisar = sorted(buying_olisar, key=lambda tup: tup[1]) Sorting a dict implies iteration, and iterating over a dict produces the keys: >>> d = {"az": 2, "by": 1, "cx": 3} >>> list(d) ['cx', 'by', 'az'] Therefore >>> sorted(d, key=lambda k: k[1]) ['cx', 'by', 'az'] sorts the keys by the second character. To sort the (key, value) pairs by value you need to be explicit: >>> sorted(d.items(), key=lambda kv: kv[1]) [('by', 1), ('az', 2), ('cx', 3)] If you are using Python 3.7 (and inofficially 3.6 I think) you can convert this back into a dict with items = sorted(d.items(), key=lambda kv: kv[1]) ordered = dict(items) In older Pythons you may need ordered = collections.OrderedDict(items) instead. From edwardkanja254 at gmail.com Mon Mar 4 03:04:15 2019 From: edwardkanja254 at gmail.com (Edward Kanja) Date: Mon, 4 Mar 2019 11:04:15 +0300 Subject: [Tutor] Python Regular Expressions (Re.sub) Function Message-ID: Hi there, Hope this finds you well, I'm working on a simple project to analyse data using regular expressions. I have successfully worked on a regular expression pattern that extracts data from my txt file. Unfortunately after i extract the data my output has too much of square brackets and by so doing the output cant be well exported in a csv file. I have worked on an idea of Re.sub function where i'm trying to replace all square brackets in my output with an empty string. Below are images of what i have been working on. Explanations of the attachments and images named as follows: *1. mydata in txtfile. *This images shows how my data looks in the txt file although i have also attached the real txt file with the actual data below. *2. code for the output. *This image shows the regular expression that is well extracting the data from my txt file. 3. *my output. *This image shows the out after i ran my code . it shows the square brackets that are in my output and ones i need to do away with. 4.*tryone code. *This image shows my first try using re.sub to eliminate the square brackets. *5. tryone output.*This images shows the output i had after running the code in the image named tryone code. *6. try two code:*This image shows my second try using re.sub to eliminate the square brackets. *7.trytwo output.*This images shows the output i had after running the code in the image named trytwo code. *8.try four code.*This image shows my fourth try using re.sub to eliminate the square brackets. *9.try four output.* This images shows the output i had after running the code in the image named tryfour code. *10.try five code.*This image shows my number five try using re.sub to eliminate the square brackets *11.* *try five output.*This images shows the output i had after running the code in the image named tryfive code. *12.line five name code regex.*This image shows the full code for the name regular expression with the re.sub function for the code in the image named try five code. *13.try six code.*This image shows my sixth try using re.sub to eliminate the square brackets *14. try six output.*This images shows the output i had after running the code in the image named tryfsix code *15.try six full name code.*This image shows the full code for the name regular expression with the re.sub function for the code in the image named try six code. *16. unon.TXT. *This is the txt file that has the data i am extracting. Kindly assist and kindly copy my work email ( edward.mwangi at un.org ) when replying . Thanks alot. Regards, Kanja Edward. P.O.BOX 1203-00300, NAIROBI. *+254720724793* www.linkedin.com/in/edward-kanja-bba16a106 -------------- next part -------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |Rawzeea NLKPP | VE11-Nairobi | 20002254-MADIZ | 00 | 00 |Regular Scheme B | 15-JAN-2019 To 31-DEC-2019 | No | |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 11113648 | 18-FEB-2020 | Yes | 03 | 0000.00 USD | 0.00 USD | Leased Commercially | 000000.00 KES | No | |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | R-9-01 | 00.00% |0023.28 KES |000.18 USD | | 1.00000 | 0.00 | 0.00 | No | 10-JAN-2019 To 31-DEC-2019 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Valid From: 17-Jan-2017 | Warwass Center | | Warwass Center | UK-8 | | | UK Avenue | | Nairobi---Kenya | | | | Nairobi--00100-Kenya | | | 0207690606 | | | housert at weekly.com | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |Pattly MUNIIZ | TX00-Nairobi | 20000006-KZAU | 00 | 00 |Regular Scheme B | 05-JAN-2010 To 01-JAN-2019 | No | |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 10000007 | 07-DEC-2019 | No | 00 | 0000.00 USD | 0.00 USD | Leased Commercially | 000000.00 KES | No | |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | K-3-10 | 00.00% |0000.25 KES |0000.60 USD | | 1.00000 | 0.00 | 0.00 | No | 01-JAN-2019 To 01-JAN-2000 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Valid From: 65-Feb-2010 | Kuthat Skuh | | 98 Runka road | 98 | | | Runka Road | | Nairobi---Kenya | | | | Nairobi--00200-Kenya | | | | | | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- From steve at pearwood.info Mon Mar 4 04:57:07 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 4 Mar 2019 20:57:07 +1100 Subject: [Tutor] Python Regular Expressions (Re.sub) Function In-Reply-To: References: Message-ID: <20190304095705.GR4465@ando.pearwood.info> Hi Edward, and welcome. Please remember that we're volunteers, doing this for free. Unless your problem is really interesting, you're not likely to get people volunteering to spend a long time slogging through multiple attachments, screenshots, at least five seperate attempts, etc. By the way, most of your attachments have been deleted by the mailing list. Only things which it considers text files will get through. Please try to simplify the question you are asking, and keep the code you show as short and simple as practical. You might find it helpful to read this: http://www.sscce.org/ In the meantime, this might help you: py> import re py> text = "abcd[][]...[x]yz" py> re.sub(r'\[|\]', '@', text) 'abcd@@@@... at x@yz' py> re.sub(r'\[|\]', '', text) 'abcd...xyz' On Mon, Mar 04, 2019 at 11:04:15AM +0300, Edward Kanja wrote: > Hi there, > Hope this finds you well, I'm working on a simple project to analyse data > using > regular expressions. I have successfully worked on a regular expression > pattern > that extracts data from my txt file. Unfortunately after i extract the data > my output has too much of square brackets and by so doing the output cant > be well exported in a csv file. I have worked on an idea of Re.sub function > where i'm trying to replace all square brackets in my output with an empty > string. > Below are images of what i have been working on. > > Explanations of the attachments and images named as follows: > *1. mydata in txtfile. *This images shows how my data looks in the txt file > although > i have also attached the real txt file with the actual data below. > *2. code for the output. *This image shows the regular expression that is > well extracting the data from my txt file. > 3. *my output. *This image shows the out after i ran my code . it shows the > square brackets that are in my output and ones i need to do away with. > 4.*tryone code. *This image shows my first try using re.sub to eliminate > the square brackets. > *5. tryone output.*This images shows the output i had after running the > code in the image named tryone code. > *6. try two code:*This image shows my second try using re.sub to eliminate > the square brackets. > *7.trytwo output.*This images shows the output i had after running the code > in the image named trytwo code. > *8.try four code.*This image shows my fourth try using re.sub to eliminate > the square brackets. > *9.try four output.* This images shows the output i had after running the > code in the image named tryfour code. > *10.try five code.*This image shows my number five try using re.sub to > eliminate the square brackets > *11.* *try five output.*This images shows the output i had after running > the code in the image named tryfive code. > *12.line five name code regex.*This image shows the full code for the name > regular expression with the re.sub function for the code > in the image named try five code. > *13.try six code.*This image shows my sixth try using re.sub to eliminate > the square brackets > *14. try six output.*This images shows the output i had after running the > code in the image named tryfsix code > *15.try six full name code.*This image shows the full code for the name > regular expression with the re.sub function for the code > in the image named try six code. > *16. unon.TXT. *This is the txt file that has the data i am extracting. > > Kindly assist and kindly copy my work email ( edward.mwangi at un.org ) when > replying . > Thanks alot. > > Regards, > Kanja Edward. > P.O.BOX 1203-00300, > NAIROBI. > *+254720724793* > www.linkedin.com/in/edward-kanja-bba16a106 > > ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > |Rawzeea NLKPP | VE11-Nairobi | 20002254-MADIZ | 00 | 00 |Regular Scheme B | 15-JAN-2019 To 31-DEC-2019 | No | > |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| > | 11113648 | 18-FEB-2020 | Yes | 03 | 0000.00 USD | 0.00 USD | Leased Commercially | 000000.00 KES | No | > |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| > | R-9-01 | 00.00% |0023.28 KES |000.18 USD | | 1.00000 | 0.00 | 0.00 | No | 10-JAN-2019 To 31-DEC-2019 | > ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| > | Valid From: 17-Jan-2017 | Warwass Center | > | Warwass Center | UK-8 | > | | UK Avenue | > | Nairobi---Kenya | | > | | Nairobi--00100-Kenya | > | | 0207690606 | > | | housert at weekly.com | > ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > |Pattly MUNIIZ | TX00-Nairobi | 20000006-KZAU | 00 | 00 |Regular Scheme B | 05-JAN-2010 To 01-JAN-2019 | No | > |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| > | 10000007 | 07-DEC-2019 | No | 00 | 0000.00 USD | 0.00 USD | Leased Commercially | 000000.00 KES | No | > |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| > | K-3-10 | 00.00% |0000.25 KES |0000.60 USD | | 1.00000 | 0.00 | 0.00 | No | 01-JAN-2019 To 01-JAN-2000 | > ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| > | Valid From: 65-Feb-2010 | Kuthat Skuh | > | 98 Runka road | 98 | > | | Runka Road | > | Nairobi---Kenya | | > | | Nairobi--00200-Kenya | > | | | > | | | > ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > > > _______________________________________________ > 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 Mon Mar 4 05:21:46 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 4 Mar 2019 10:21:46 +0000 Subject: [Tutor] Python Regular Expressions (Re.sub) Function In-Reply-To: References: Message-ID: On 04/03/2019 08:04, Edward Kanja wrote: > ... Unfortunately after i extract the data > my output has too much of square brackets and by so doing the output cant > be well exported in a csv file. I can't see the data so can't be definitive but the number of square brackets shouldn't affect a CSV file (except that it will be messy to read). Are you using the csv module to produce the csv file? It is much safer to do that than to try to write a csv file yourself. > I have worked on an idea of Re.sub function > where i'm trying to replace all square brackets in my output with an empty > string. See Steve's reply. > Below are images of what i have been working on. Images don't work on this (and many other) mailing list. The server sees them as potential security threats and throws them away. Please put sample code directly in the mail message or, if it is very long ( >100 lines?) paste it on a pastebin type web site and include a link. > 4.*try one code. > *6. try two code: > *8.try four code. > *10.try five code. > *13.try six code. Multiple versions of the code just results in confusion as different people respond to different versions. Pick one version that's closest to what you want and use that as the basis of discussion. Ideally remove all code that's not directly relevant to the problem, eg the code that loads your data in the first place. Show a small sample of input data and the output data. Show us any error messages in full. -- 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 edwardkanja254 at gmail.com Mon Mar 4 06:44:05 2019 From: edwardkanja254 at gmail.com (Edward Kanja) Date: Mon, 4 Mar 2019 14:44:05 +0300 Subject: [Tutor] My problem in simple terms Message-ID: Hi there , Earlier i had sent an email on how to use re.sub function to eliminate square brackets. I have simplified the statements. Attached txt file named unon.Txt has the data im extracting from. The file named code.txt has the codes I'm using to extract the data.The regular expression works fine but my output has too many square brackets. How do i do away with them thanks. Regards, Kanja Edward. P.O.BOX 1203-00300, NAIROBI. *+254720724793* www.linkedin.com/in/edward-kanja-bba16a106 -------------- next part -------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |Rawzeea NLKPP | VE11-Nairobi | 20002254-MADIZ | 00 | 00 |Regular Scheme B | 15-JAN-2019 To 31-DEC-2019 | No | |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 11113648 | 18-FEB-2020 | Yes | 03 | 0000.00 USD | 0.00 USD | Leased Commercially | 000000.00 KES | No | |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | R-9-01 | 00.00% |0023.28 KES |000.18 USD | | 1.00000 | 0.00 | 0.00 | No | 10-JAN-2019 To 31-DEC-2019 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Valid From: 17-Jan-2017 | Warwass Center | | Warwass Center | UK-8 | | | UK Avenue | | Nairobi---Kenya | | | | Nairobi--00100-Kenya | | | 0207690606 | | | housert at weekly.com | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |Pattly MUNIIZ | TX00-Nairobi | 20000006-KZAU | 00 | 00 |Regular Scheme B | 05-JAN-2010 To 01-JAN-2019 | No | |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 10000007 | 07-DEC-2019 | No | 00 | 0000.00 USD | 0.00 USD | Leased Commercially | 000000.00 KES | No | |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | K-3-10 | 00.00% |0000.25 KES |0000.60 USD | | 1.00000 | 0.00 | 0.00 | No | 01-JAN-2019 To 01-JAN-2000 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Valid From: 65-Feb-2010 | Kuthat Skuh | | 98 Runka road | 98 | | | Runka Road | | Nairobi---Kenya | | | | Nairobi--00200-Kenya | | | | | | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------- next part -------------- import pandas as pd from pandas import DataFrame #creating my dataframes manually. import re #import textfile with open ('unon.txt') as csvfile: mydata=pd.read_csv('unon.txt') for line in csvfile: index_no=re.findall(r'(\|\s\d{5,8}\s)',line) names=re.findall(r'(\|[A-Za-z]\w*\s\w*\s\w*\s\w*\s)',line) #Address=re.findall(r'\|\s([A-Z0-9-,/]\w*\s\w*\s)',line) duty_station=re.findall(r'\|\s[A-Z]*\d{2}\-\w\w\w\w\w\w\w\s',line) print((index_no),(names),(duty_station)) From alan.gauld at yahoo.co.uk Mon Mar 4 08:17:21 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 4 Mar 2019 13:17:21 +0000 Subject: [Tutor] My problem in simple terms In-Reply-To: References: Message-ID: On 04/03/2019 11:44, Edward Kanja wrote: > Hi there , > Earlier i had sent an email on how to use re.sub function to eliminate > square brackets. I have simplified the statements. Attached txt file named > unon.Txt has the data im extracting from. Thankyou, that's much better. Although the code is now short enough to just paste into the mail: ####################### import pandas as pd from pandas import DataFrame #creating my dataframes manually. import re #import textfile with open ('unon.txt') as csvfile: mydata=pd.read_csv('unon.txt') for line in csvfile: index_no=re.findall(r'(\|\s\d{5,8}\s)',line) names=re.findall(r'(\|[A-Za-z]\w*\s\w*\s\w*\s\w*\s)',line) #Address=re.findall(r'\|\s([A-Z0-9-,/]\w*\s\w*\s)',line) duty_station=re.findall(r'\|\s[A-Z]*\d{2}\-\w\w\w\w\w\w\w\s',line) print((index_no),(names),(duty_station)) ############ You don't need the pandas line since you don't do anything with mydata. Also your data is not really a csv file so I'm not sure how Pandas will cope with it. > codes I'm using to extract the data.The regular expression works fine but > my output has too many square brackets. How do i do away with them thanks. Can you show us the output? I can't see how you can have any square brackets since your data has none. I'm guessing you maybe mean the vertical bar symbols? I suspect the best way to remove them is to improve the regexes used for extraction. There are web sites that allow you to paste sample data and then create different regex and see the output, that may be a useful way forward. Here is one such: https://regex101.com/ Assuming that you have multiple input records like the one shown I'd suggest you put the code to read one record into a function and then call that repeatedly until the file is processed. You can also create a function to write one record at a time to a file(using whatever format you prefer(csv, json, yaml etc). I suspect csv may not be ideal for this data, but you may want to process it elsewhere that requires csv, I don;t know. The functions will probably want to use the file as an iterable since it needs to process multiple lines per record. Finally, the parens in the last print line are not needed, just use: print(index_no, names, duty_station) Although I assume this is just debug information so not really too important. -- 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 Mon Mar 4 09:07:09 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 04 Mar 2019 15:07:09 +0100 Subject: [Tutor] My problem in simple terms References: Message-ID: Edward Kanja wrote: > Hi there , > Earlier i had sent an email on how to use re.sub function to eliminate > square brackets. I have simplified the statements. Attached txt file named > unon.Txt has the data im extracting from. The file named code.txt has the > codes I'm using to extract the data.The regular expression works fine but > my output has too many square brackets. How do i do away with them thanks. The square brackets appear because re.findall() returns a list. If you know that there is only one match or if you are only interested in the first match you can extract it with first = re.findall(...)[1] This will of course fail if there is no match at all, so you have to check the length first. You can also use the length check to skip the lines with no match at all, i. e. the line appearing as [] [] [] in your script's output. Now looking at your data -- at least from the sample it seems to be rather uniform. There are records separated by "---..." and fields separated by "|". I'd forego regular expressions for that: $ cat code.py from itertools import groupby def is_record_sep(line): return not line.rstrip().strip("-") with open("unon.txt") as instream: for sep, group in groupby(instream, key=is_record_sep): if not sep: record = [ [field.strip() for field in line.split("|")] for line in group if line.strip().strip("|-") ] # select field by their position in the record names = record[0][1] station = record[0][2] index = record[1][1] print(index, names, station, sep=", ") $ python3 code.py 11113648, Rawzeea NLKPP, VE11-Nairobi 10000007, Pattly MUNIIZ, TX00-Nairobi From johnf at jfcomputer.com Mon Mar 4 13:54:03 2019 From: johnf at jfcomputer.com (john fabiani) Date: Mon, 4 Mar 2019 10:54:03 -0800 Subject: [Tutor] how to get the weeks of a month Message-ID: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> Hi everyone, I'm not exactly a newbie but I can't seem to solve this problem. I need to print out the weeks of the month - given any month and any year. For example this month would have: 3/1/2019 - 3/3/2019?? # notice that this a short week 3/4/2019 - 3/10/2019 3/11/2019 - 3/17/2019 3/18/2019 - 3/24/2019 3/25/2019 - 3/31/2019? # also this can be a short week as in April 2019 last week would be 4/29/2019 - 4/30-2019 I have tried using isocalendar, dateutil, and just plain datetime. I get close but no real solution. Google wasn't much help either.? So I thought I'd ask here - you have been helpful in the past.? And no at my age I am not attending school - so this not my homework. Johnf From breamoreboy at gmail.com Mon Mar 4 14:12:23 2019 From: breamoreboy at gmail.com (Mark Lawrence) Date: Mon, 4 Mar 2019 19:12:23 +0000 Subject: [Tutor] how to get the weeks of a month In-Reply-To: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> References: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> Message-ID: On 04/03/2019 18:54, john fabiani wrote: > Hi everyone, > > I'm not exactly a newbie but I can't seem to solve this problem. > > I need to print out the weeks of the month - given any month and any year. > > For example this month would have: > > 3/1/2019 - 3/3/2019?? # notice that this a short week > 3/4/2019 - 3/10/2019 > 3/11/2019 - 3/17/2019 > 3/18/2019 - 3/24/2019 > 3/25/2019 - 3/31/2019? # also this can be a short week as in April 2019 > last week would be 4/29/2019 - 4/30-2019 > > I have tried using isocalendar, dateutil, and just plain datetime. > > I get close but no real solution. Google wasn't much help either.? So I > thought I'd ask here - you have been helpful in the past.? And no at my > age I am not attending school - so this not my homework. > > Johnf > Have you tried any of monthdatescalendar, monthdays2calendar or monthdayscalendar from https://docs.python.org/3/library/calendar.html? -- 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 Mon Mar 4 14:19:50 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 4 Mar 2019 19:19:50 +0000 Subject: [Tutor] how to get the weeks of a month In-Reply-To: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> References: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> Message-ID: On 04/03/2019 18:54, john fabiani wrote: > I need to print out the weeks of the month - given any month and any year. > I'm not totally clear how you define a week. In your example the first week starts on Friday and ends on Sunday. Subsequent weeks start on Monday and run to Sunday. So, is a week essentially a variable number of days prior to a Sunday? What do you do if a month ends on a Thursday, say? How is the last week printed? EDIT: OK I see the comment at the end now. > For example this month would have: > > 3/1/2019 - 3/3/2019?? # notice that this a short week > 3/4/2019 - 3/10/2019 > 3/11/2019 - 3/17/2019 > 3/18/2019 - 3/24/2019 > 3/25/2019 - 3/31/2019? # also this can be a short week as in April 2019 > last week would be 4/29/2019 - 4/30-2019 So what I think you want is to start with the first day and print each day up to Sunday. Newline print the current date up to sunday newline repeat until you run out of days in the month. datetime has functions for extracting the date, the day and the month It also has functions for formatting the date string. Or you could use calendar to matrix representation of the month. >>> import calendar as cal >>> cal.monthcalendar(2019,3) [[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]] >>> That looks close to what you want? -- 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 david at graniteweb.com Mon Mar 4 16:15:22 2019 From: david at graniteweb.com (David Rock) Date: Mon, 4 Mar 2019 15:15:22 -0600 Subject: [Tutor] how to get the weeks of a month In-Reply-To: References: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> Message-ID: > On Mar 4, 2019, at 13:19, Alan Gauld via Tutor wrote: > > On 04/03/2019 18:54, john fabiani wrote: > >> I need to print out the weeks of the month - given any month and any year. > > I'm not totally clear how you define a week. > > EDIT: OK I see the comment at the end now. > >> For example this month would have: >> >> 3/1/2019 - 3/3/2019 # notice that this a short week >> 3/4/2019 - 3/10/2019 >> 3/11/2019 - 3/17/2019 >> 3/18/2019 - 3/24/2019 >> 3/25/2019 - 3/31/2019 # also this can be a short week as in April 2019 >> last week would be 4/29/2019 - 4/30-2019 What I think he?s shooting for is something similar to cal output $ cal March 2019 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 > So what I think you want is to > > start with the first day and print each day up to Sunday. > Newline > print the current date up to sunday > newline > repeat until you run out of days in the month. > > >>>> import calendar as cal >>>> cal.monthcalendar(2019,3) > [[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, > 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]] >>>> > > That looks close to what you want? That seems close, but off by a day? ? David Rock david at graniteweb.com From johnf at jfcomputer.com Mon Mar 4 16:28:36 2019 From: johnf at jfcomputer.com (john fabiani) Date: Mon, 4 Mar 2019 13:28:36 -0800 Subject: [Tutor] how to get the weeks of a month In-Reply-To: References: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> Message-ID: On 3/4/19 1:15 PM, David Rock wrote: >> On Mar 4, 2019, at 13:19, Alan Gauld via Tutor wrote: >> >> On 04/03/2019 18:54, john fabiani wrote: >> >>> I need to print out the weeks of the month - given any month and any year. >> I'm not totally clear how you define a week. >> >> EDIT: OK I see the comment at the end now. >> >>> For example this month would have: >>> >>> 3/1/2019 - 3/3/2019 # notice that this a short week >>> 3/4/2019 - 3/10/2019 >>> 3/11/2019 - 3/17/2019 >>> 3/18/2019 - 3/24/2019 >>> 3/25/2019 - 3/31/2019 # also this can be a short week as in April 2019 >>> last week would be 4/29/2019 - 4/30-2019 > What I think he?s shooting for is something similar to cal output > > $ cal > March 2019 > Su Mo Tu We Th Fr Sa > 1 2 > 3 4 5 6 7 8 9 > 10 11 12 13 14 15 16 > 17 18 19 20 21 22 23 > 24 25 26 27 28 29 30 > 31 > > >> So what I think you want is to >> >> start with the first day and print each day up to Sunday. >> Newline >> print the current date up to sunday >> newline >> repeat until you run out of days in the month. >> >> >>>>> import calendar as cal >>>>> cal.monthcalendar(2019,3) >> [[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, >> 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]] >> That looks close to what you want? > > That seems close, but off by a day? > > > ? > David Rock > david at graniteweb.com I knew there was a simple why to get it done!? But where is it off my a day? Johnf From david at graniteweb.com Mon Mar 4 16:35:56 2019 From: david at graniteweb.com (David Rock) Date: Mon, 4 Mar 2019 15:35:56 -0600 Subject: [Tutor] how to get the weeks of a month In-Reply-To: References: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> Message-ID: <80495842-8F50-4CEB-BB96-E8A0290DBB8D@graniteweb.com> > On Mar 4, 2019, at 15:28, john fabiani wrote: > > I knew there was a simple why to get it done! But where is it off my a day? > comparing $ cal March 2019 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 to import calendar as cal cal.monthcalendar(2019,3) [[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]] I see the first element of the array is [0, 0, 0, 0, 1, 2, 3] where I would have expected [0, 0, 0, 0, 0, 1, 2] Which I?m sure is just a question of ?defining the start of the week? properly, but if you just took it as-is, Mar 1 would be Thursday, not Friday if you translated literally. ? David Rock david at graniteweb.com From sjeik_appie at hotmail.com Mon Mar 4 16:45:57 2019 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Mon, 4 Mar 2019 21:45:57 +0000 Subject: [Tutor] schedulers In-Reply-To: Message-ID: On 28 Feb 2019 15:45, nathan tech wrote: Hi there, I recently started working on a backup program, and the one big feature everyone wants in backup programs is the ability to schedule backups, right? but I'm thinking, should I do this? ==? this might interest you: https://docs.python.org/3/library/sched.html. Or simply the Windows Task Scheduler? From johnf at jfcomputer.com Mon Mar 4 17:12:39 2019 From: johnf at jfcomputer.com (john fabiani) Date: Mon, 4 Mar 2019 14:12:39 -0800 Subject: [Tutor] how to get the weeks of a month In-Reply-To: <80495842-8F50-4CEB-BB96-E8A0290DBB8D@graniteweb.com> References: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> <80495842-8F50-4CEB-BB96-E8A0290DBB8D@graniteweb.com> Message-ID: <21804730-4c96-134e-2186-eaf1706f1a0f@jfcomputer.com> On 3/4/19 1:35 PM, David Rock wrote: >> On Mar 4, 2019, at 15:28, john fabiani wrote: >> >> I knew there was a simple why to get it done! But where is it off my a day? >> > comparing > > $ cal > March 2019 > Su Mo Tu We Th Fr Sa > 1 2 > 3 4 5 6 7 8 9 > 10 11 12 13 14 15 16 > 17 18 19 20 21 22 23 > 24 25 26 27 28 29 30 > 31 > > to > > import calendar as cal > cal.monthcalendar(2019,3) > [[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, > 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]] > > I see the first element of the array is > [0, 0, 0, 0, 1, 2, 3] > > where I would have expected > [0, 0, 0, 0, 0, 1, 2] > > Which I?m sure is just a question of ?defining the start of the week? properly, but if you just took it as-is, Mar 1 would be Thursday, not Friday if you translated literally. > > > ? > David Rock > david at graniteweb.com My understanding is - a week starts on Monday and ends on Sunday. So that looks correct.? Below I use a max function but I wonder if I should use a min function too.? Recall I am looking for the string of the dates for the week. Here is my code: import datetime import calendar #get the weeks of a month to get the dates to display tday = datetime.datetime(2020,03,01) weeksInMonth =calendar.monthcalendar(tday.year, tday.month) lastdayof1stweek = weeksInMonth[0][6] firstweek = tday.strftime("%m-%d_%Y")+ " - "+ datetime.datetime(tday.year, tday.month, lastdayof1stweek).strftime("%m-%d-%Y") print firstweek for i in range(len(weeksInMonth)): ??? if i == 0: ??????? continue ??? firstday = weeksInMonth[i][0] ??? lastday =? max(weeksInMonth[i]) ??? weekstr = datetime.datetime(tday.year, tday.month, firstday).strftime("%m-%d-%Y") + ' - ' + datetime.datetime(tday.year, tday.month, lastday).strftime("%m-%d-%Y") ??? print weekstr def max(arr): ??? max_ = arr[0] ??? for item in arr: ??????? if item > max_: ??????????? max_ = item ??? return max_ From ben+python at benfinney.id.au Mon Mar 4 17:30:45 2019 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 05 Mar 2019 09:30:45 +1100 Subject: [Tutor] how to get the weeks of a month References: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> <80495842-8F50-4CEB-BB96-E8A0290DBB8D@graniteweb.com> <21804730-4c96-134e-2186-eaf1706f1a0f@jfcomputer.com> Message-ID: <86va0y2rxm.fsf@benfinney.id.au> john fabiani writes: > My understanding is - a week starts on Monday and ends on Sunday. That's behaviour specific to a timezone. Which one are you using, and does your program know to consult the timezone data for when a week begins and ends? -- \ ?I went to San Francisco. I found someone's heart.? ?Steven | `\ Wright | _o__) | Ben Finney From david at graniteweb.com Mon Mar 4 17:47:07 2019 From: david at graniteweb.com (David Rock) Date: Mon, 4 Mar 2019 16:47:07 -0600 Subject: [Tutor] how to get the weeks of a month In-Reply-To: <86va0y2rxm.fsf@benfinney.id.au> References: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> <80495842-8F50-4CEB-BB96-E8A0290DBB8D@graniteweb.com> <21804730-4c96-134e-2186-eaf1706f1a0f@jfcomputer.com> <86va0y2rxm.fsf@benfinney.id.au> Message-ID: > On Mar 4, 2019, at 16:30, Ben Finney wrote: > > john fabiani writes: > >> My understanding is - a week starts on Monday and ends on Sunday. > > That's behaviour specific to a timezone. Which one are you using, and > does your program know to consult the timezone data for when a week > begins and ends? That?s why I said "Which I?m sure is just a question of ?defining the start of the week? properly.? ?Properly? is in the eye of the beholder. As long as it?s performing the way you expect it to perform, you should be fine. If all I saw was the output you had, I?d think something was broken because I think in terms of first day being Sunday, so maybe include a note in the output what the First day of the week is if that?s appropriate? ? David Rock david at graniteweb.com From alan.gauld at yahoo.co.uk Mon Mar 4 18:00:17 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 4 Mar 2019 23:00:17 +0000 Subject: [Tutor] how to get the weeks of a month In-Reply-To: <86va0y2rxm.fsf@benfinney.id.au> References: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> <80495842-8F50-4CEB-BB96-E8A0290DBB8D@graniteweb.com> <21804730-4c96-134e-2186-eaf1706f1a0f@jfcomputer.com> <86va0y2rxm.fsf@benfinney.id.au> Message-ID: On 04/03/2019 22:30, Ben Finney wrote: > john fabiani writes: > >> My understanding is - a week starts on Monday and ends on Sunday. > > That's behaviour specific to a timezone. Which one are you using, and > does your program know to consult the timezone data for when a week > begins and ends? That's a whole different can of worms. Last time I checked there were around 40 different timezones and only about 35 supported by most OS. Some of them vary their rules year on year and their time intervals can be as little as 20 minutes apart to 3 hours apart. Its a real rats nest where local governance overrules standards at the drop of a hat! Fortunately you only have the days and months to worry about here! -- 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 johnf at jfcomputer.com Mon Mar 4 19:56:00 2019 From: johnf at jfcomputer.com (john fabiani) Date: Mon, 4 Mar 2019 16:56:00 -0800 Subject: [Tutor] how to get the weeks of a month In-Reply-To: References: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> <80495842-8F50-4CEB-BB96-E8A0290DBB8D@graniteweb.com> <21804730-4c96-134e-2186-eaf1706f1a0f@jfcomputer.com> <86va0y2rxm.fsf@benfinney.id.au> Message-ID: <3fb738eb-0456-4a84-4818-56b4fece5963@jfcomputer.com> On 3/4/19 2:47 PM, David Rock wrote: >> On Mar 4, 2019, at 16:30, Ben Finney wrote: >> >> john fabiani writes: >> >>> My understanding is - a week starts on Monday and ends on Sunday. >> That's behaviour specific to a timezone. Which one are you using, and >> does your program know to consult the timezone data for when a week >> begins and ends? > > That?s why I said "Which I?m sure is just a question of ?defining the start of the week? properly.? ?Properly? is in the eye of the beholder. As long as it?s performing the way you expect it to perform, you should be fine. If all I saw was the output you had, I?d think something was broken because I think in terms of first day being Sunday, so maybe include a note in the output what the First day of the week is if that?s appropriate? > > > ? > David Rock > david at graniteweb.com > > I didn't think about that.? I will in the future. Thanks, Johnf From breamoreboy at gmail.com Mon Mar 4 22:58:25 2019 From: breamoreboy at gmail.com (Mark Lawrence) Date: Tue, 5 Mar 2019 03:58:25 +0000 Subject: [Tutor] how to get the weeks of a month In-Reply-To: <21804730-4c96-134e-2186-eaf1706f1a0f@jfcomputer.com> References: <0ae343c4-c3cd-b3b0-704b-8a5f7b51765c@jfcomputer.com> <80495842-8F50-4CEB-BB96-E8A0290DBB8D@graniteweb.com> <21804730-4c96-134e-2186-eaf1706f1a0f@jfcomputer.com> Message-ID: On 04/03/2019 22:12, john fabiani wrote: > > > On 3/4/19 1:35 PM, David Rock wrote: >>> On Mar 4, 2019, at 15:28, john fabiani wrote: >>> >>> I knew there was a simple why to get it done!? But where is it off my >>> a day? >>> >> comparing >> >> $ cal >> ????? March 2019 >> Su Mo Tu We Th Fr Sa >> ???????????????? 1? 2 >> ? 3? 4? 5? 6? 7? 8? 9 >> 10 11 12 13 14 15 16 >> 17 18 19 20 21 22 23 >> 24 25 26 27 28 29 30 >> 31 >> >> to >> >> import calendar as cal >> cal.monthcalendar(2019,3) >> [[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, >> 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]] >> >> I see the first element of the array is >> [0, 0, 0, 0, 1, 2, 3] >> >> where I would have expected >> [0, 0, 0, 0, 0, 1, 2] >> >> Which I?m sure is just a question of ?defining the start of the week? >> properly, but if you just took it as-is, Mar 1 would be Thursday, not >> Friday if you translated literally. >> >> >> ? >> David Rock >> david at graniteweb.com > My understanding is - a week starts on Monday and ends on Sunday. From https://docs.python.org/3/library/calendar.html#calendar.setfirstweekday calendar.setfirstweekday(weekday) Sets the weekday (0 is Monday, 6 is Sunday) to start each week. The values MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY are provided for convenience. For example, to set the first weekday to Sunday: import calendar calendar.setfirstweekday(calendar.SUNDAY) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From jjhartley at gmail.com Wed Mar 6 17:17:57 2019 From: jjhartley at gmail.com (James Hartley) Date: Wed, 6 Mar 2019 16:17:57 -0600 Subject: [Tutor] directory structure with tests? Message-ID: It is preferable to sprinkle tests files throughout the directories of a project, or coalesce all tests in a test directory? Thanks! From mats at wichmann.us Wed Mar 6 17:38:19 2019 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 6 Mar 2019 15:38:19 -0700 Subject: [Tutor] directory structure with tests? In-Reply-To: References: Message-ID: On 3/6/19 3:17 PM, James Hartley wrote: > It is preferable to sprinkle tests files throughout the directories of a > project, or coalesce all tests in a test directory? "It depends". There are people who have strong opinions. If you're going to use conventions like naming the test for the functionality in foo.py as test_foo.py, it makes sense for the two to be nearby each other. That can be the same directory, or a subdirectory (a harness like Py.Test will find files named test_* and assume they're tests whichever place they're in). Very large projects may well split tests into more subdirectories based on their purpose or usage. On the other hand, if your project is going to be turned into a package, you might want to keep the tests in a separate directory, as it may be somewhat easier to select what goes into the package if they're not sprinkled together. You may also end up handling unit tests and integration tests differently. How's that for a definitive answer? :) From alan.gauld at yahoo.co.uk Wed Mar 6 18:41:44 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 6 Mar 2019 23:41:44 +0000 Subject: [Tutor] directory structure with tests? In-Reply-To: References: Message-ID: On 06/03/2019 22:38, Mats Wichmann wrote: > "It depends". ... > How's that for a definitive answer? :) Pretty good! and I agree with all of it :-) Although personally I always use a test subdirectory and then, for building a distributable, use a file filter (eg find in *nix) to prune those out. All version controlled of course! -- 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 cs at cskk.id.au Wed Mar 6 18:57:09 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 7 Mar 2019 10:57:09 +1100 Subject: [Tutor] directory structure with tests? In-Reply-To: References: Message-ID: <20190306235709.GA85456@cskk.homeip.net> On 06Mar2019 23:41, Alan Gauld wrote: >On 06/03/2019 22:38, Mats Wichmann wrote: >> "It depends". >... >> How's that for a definitive answer? :) > >Pretty good! and I agree with all of it :-) > >Although personally I always use a test subdirectory >and then, for building a distributable, use a file >filter (eg find in *nix) to prune those out. >All version controlled of course! There are those who argue that it is better to include the test files in the distribution. Aside from transparency and completeness, it may also aid bug reports as a test might only fail in the end user's environment and not that of the developer. Being able to get the error output from the end user running the tests is a win there. Q: How many user support people does it take to change a light bulb? A: We have an exact copy of the light bulb here and it seems to be working fine. Can you tell me what kind of system you have? For variety, in my own code I keep the tests for foo.py in foo_tests.py, the better to be seen next to foo.py in listings and file completion. Cheers, Cameron Simpson From chris_roysmith at internode.on.net Fri Mar 8 21:53:11 2019 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Sat, 9 Mar 2019 13:53:11 +1100 Subject: [Tutor] Tkinter: Can's figure out how to put a frame in a second window Message-ID: <0c794fbb-74a1-d3e8-3bf4-05df486f59dd@internode.on.net> Hi. running Linux, with python3.6 I am trying to learn how to put a frame on a second window. I get no errors showing in the terminal, and I get no traceback. What is happening is that the contents of the frame appear in the master window. I was expecting them to show in the second window. Also I expected the frame to be sunken, but there is no obvious signs of the frame, not even a colored background. What am I doing wrong? Thank you, Chris Roy-Smith here is my code: #! /usr/bin/python3 from tkinter import * def NewWindow(): ??? sw=Toplevel(master) ??? sw.title('New Window') ??? Label(sw, text='new window').grid(row=0, column=0) ??? sframe=Frame(sw, relief=SUNKEN, bg='red').grid(row=1, column=0) ??? Label(sframe, text='Label in a frame').grid(row=2, column=0) ??? Label(sframe, text='Second label in this frame').grid(row=3, column=0) ??? Button(sw, text='close window', command=sw.destroy).grid(row=5, column=0) master=Tk() master.title('Master Window') Button(master, text='open window', command=NewWindow).grid(row=1, column=1) Button(master, text='quit', command=master.destroy).grid(row=2, column=1) Label(master, text="learning").grid(row=0, column=0) master.mainloop() From alan.gauld at yahoo.co.uk Sat Mar 9 06:13:43 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 9 Mar 2019 11:13:43 +0000 Subject: [Tutor] Tkinter: Can's figure out how to put a frame in a second window In-Reply-To: <0c794fbb-74a1-d3e8-3bf4-05df486f59dd@internode.on.net> References: <0c794fbb-74a1-d3e8-3bf4-05df486f59dd@internode.on.net> Message-ID: On 09/03/2019 02:53, Chris Roy-Smith wrote: > What is happening is that the contents of the frame appear in the master > window. I was expecting them to show in the second window. Also I > expected the frame to be sunken, but there is no obvious signs of the > frame, not even a colored background. > > What am I doing wrong? Its a very common mistake in Tkinter. When you use one of the layout managers, in this case grid() the return value is always None. > def NewWindow(): > ??? sw=Toplevel(master) > ??? sw.title('New Window') > ??? Label(sw, text='new window').grid(row=0, column=0) > ??? sframe=Frame(sw, relief=SUNKEN, bg='red').grid(row=1, column=0) So you are here assigning None to sframe. > ??? Label(sframe, text='Label in a frame').grid(row=2, column=0) > ??? Label(sframe, text='Second label in this frame').grid(row=3, column=0) And when you pass None as the parent to a widget Tk defaults to the root. So your widgets appear in your main window. Change the sframe line to two lines: sframe=Frame(sw, relief=SUNKEN, bg='red') sframe.grid(row=1, column=0) and all will be well.... except the sunken relief wont work. TYhats because the sunken form requires a border width of at least 2 pixels to be visible. So you need to add border=2 (or more) to make it work. So the final line should be: sframe=Frame(sw, border=2, relief=SUNKEN, bg='red') sframe.grid(row=1, column=0) As I say its a very common mistake and so, any time weird things happen, always check that anywhere you assign a widget to a variable you call the layout manager on a separate line. 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 asad.hasan2004 at gmail.com Sat Mar 9 08:08:03 2019 From: asad.hasan2004 at gmail.com (Asad) Date: Sat, 9 Mar 2019 18:38:03 +0530 Subject: [Tutor] How to create a structure from a Log file Message-ID: Hi All , I would like to know , how can I approach this problem to create a easy structure from the logfile using python so I can review the logfiles quite efficiently . Please share suggestion tip or already written codes. Thanks, From bgailer at gmail.com Sat Mar 9 11:44:52 2019 From: bgailer at gmail.com (Bob Gailer) Date: Sat, 9 Mar 2019 11:44:52 -0500 Subject: [Tutor] How to create a structure from a Log file In-Reply-To: References: Message-ID: Would you give us more information? What is an example of a log file? How would you like to see it presented? The more information you give us the easier it is for us to help. On Mar 9, 2019 11:20 AM, "Asad" wrote: Hi All , I would like to know , how can I approach this problem to create a easy structure from the logfile using python so I can review the logfiles quite efficiently . Please share suggestion tip or already written codes. Thanks, _______________________________________________ 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 Sat Mar 9 13:04:10 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 9 Mar 2019 18:04:10 +0000 Subject: [Tutor] How to create a structure from a Log file In-Reply-To: References: Message-ID: On 09/03/2019 13:08, Asad wrote: > I would like to know , how can I approach this problem to create > a easy structure from the logfile using python so I can review the logfiles > quite efficiently . Please share suggestion tip or already written codes. That depends on several things: 1) What is the format of the log file? 2) what data does it contain 3) what data do you need to see and organised by what criteria? 4) What do you mean by efficiency? - Speed of processing the logfile? - Speed of writing the reports? - Speed of generating the reports? The OS and Python version would be useful too. -- 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 Sat Mar 9 13:02:57 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 09 Mar 2019 11:02:57 -0700 Subject: [Tutor] How to create a structure from a Log file In-Reply-To: References: Message-ID: <1DCEAD9E-F152-4357-9FEB-85C2759EC34D@wichmann.us> On March 9, 2019 6:08:03 AM MST, Asad wrote: >Hi All , > > I would like to know , how can I approach this problem to create >a easy structure from the logfile using python so I can review the >logfiles >quite efficiently . Please share suggestion tip or already written >codes. > > > >Thanks, >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor The great things about logfiles is they're (typically) all text, and they have regular, prredictable contents. Before you can do anything useful, though, you have to figure out the pattern of the contents. That can vary wildly... in a system logfile there may be many programs that contribute so the patterns can take more work to figure out; if it's a logfile for one specific program, it may be easier. What kinds of things are you looking for? What kind of data are you expecting? You should be able to find lots of examples of logfile processing on the internet, but whether any of that is of any use to you beyond providing a model for techniques is up to whether your logfile is like any of the ones being processed in those examples. As in other replies - tell us more. Also, just a general comment, this list usually works out better if you have more specific questions (I tried AAA and instead of giving me BBB I got CCC), -- Sent from a mobile device with K-9 Mail. Please excuse my brevity. From asad.hasan2004 at gmail.com Sat Mar 9 13:44:25 2019 From: asad.hasan2004 at gmail.com (Asad) Date: Sun, 10 Mar 2019 00:14:25 +0530 Subject: [Tutor] How to create a structure from a Log file In-Reply-To: References: Message-ID: Hi All , Please a sample logfile [main] [ 2019-01-21 10:13:14.041 CET ] [AssistantLogHandler.configureLogging:176] Begin tracing.. INFO: Jan 21, 2019 10:13:14 AM oracle.assistants.dbua.driver.UpgradeDriver configureLogging INFO: Loading upgrade driver, DBUA running in mode : NONE DBUA running on platform : Linux [main] [ 2019-01-21 10:13:14.237 CET ] [ClusterVerification.getInstance:553] Method Entry. workDir=/tmp frameworkHome=/u01/app/oracle/product/12.2.0.1/dbhome_1 [main] [ 2019-01-21 10:13:14.289 CET ] [CVUVariables.initialize:1456] Start parse all variables from variables.xml... [main] [ 2019-01-21 10:13:14.304 CET ] [VerificationUtil.getVariablesXmlURI:10687] ==== XML variables file: file:/u01/app/oracle/product/12.2.0.1/dbhome_1/cv/cvdata/variables.xml [main] [ 2019-01-21 10:13:14.305 CET ] [VerificationUtil.getVariablesXmlSchemaURI:10669] ==== XML variables schema file: file:/u01/app/oracle/product/ 12.2.0.1/dbhome_1/cv/cvdata/variables.xsd [main] [ 2019-01-21 10:13:14.305 CET ] [CVUVariables.getRootElement:1721] ==== URIs obtained :xsd URI = file:/u01/app/oracle/product/ 12.2.0.1/dbhome_1/cv/cvdata/variables.xsd [main] [ 2019-01-21 10:13:14.306 CET ] [CVUVariables.getRootElement:1722] ==== URIs obtained :xml URI = file:/u01/app/oracle/product/ 12.2.0.1/dbhome_1/cv/cvdata/variables.xml [main] [ 2019-01-21 10:13:14.306 CET ] [CVUVariables.getRootElement:1735] xsdFile exists : file:/u01/app/oracle/product/ 12.2.0.1/dbhome_1/cv/cvdata/variables.xsd [main] [ 2019-01-21 10:13:14.307 CET ] [CVUVariables.getRootElement:1750] xmlFile exists : file:/u01/app/oracle/product/ 12.2.0.1/dbhome_1/cv/cvdata/variables.xml [main] [ 2019-01-21 10:13:14.307 CET ] [CVUVariables.getRootElement:1763] setting xmlFactory to use xsdFile : file:/u01/app/oracle/product/ 12.2.0.1/dbhome_1/cv/cvdata/variables.xsd [main] [ 2019-01-21 10:13:14.408 CET ] [CVUVariables.getRootElement:1794] The xml variables file: file:/u01/app/oracle/product/ 12.2.0.1/dbhome_1/cv/cvdata/variables.xml, was parsed correctly [main] [ 2019-01-21 10:13:14.410 CET ] [CVUVariables.parse:1521] Version found ALL [main] [ 2019-01-21 10:13:14.411 CET ] [CVUVariables.parse:1524] Process common variables [main] [ 2019-01-21 10:13:14.415 CET ] [CVUVariableData.:98] CVUVariableData created with names: "s_silent,SILENT" [main] [ 2019-01-21 10:13:14.416 CET ] [CVUVariables.parse:1521] Version found 12.2 [main] [ 2019-01-21 10:13:14.416 CET ] [CVUVariables.parse:1529] Process variables for the release: 12.2 [main] [ 2019-01-21 10:13:14.418 CET ] [CVUVariableData.:98] CVUVariableData created with names: I was thinking to convert it in a excel format or any other format which better viewable Thanks, From chris_roysmith at internode.on.net Sat Mar 9 15:27:44 2019 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Sun, 10 Mar 2019 07:27:44 +1100 Subject: [Tutor] Tkinter: Can's figure out how to put a frame in a second window In-Reply-To: References: <0c794fbb-74a1-d3e8-3bf4-05df486f59dd@internode.on.net> Message-ID: <8415e3e0-da88-cc83-7ade-80ac6d14e36a@internode.on.net> On 9/3/19 10:13 pm, Alan Gauld via Tutor wrote: > On 09/03/2019 02:53, Chris Roy-Smith wrote: > >> What is happening is that the contents of the frame appear in the master >> window. I was expecting them to show in the second window. Also I >> expected the frame to be sunken, but there is no obvious signs of the >> frame, not even a colored background. >> >> What am I doing wrong? > Its a very common mistake in Tkinter. > When you use one of the layout managers, in this case grid() > the return value is always None. > >> def NewWindow(): >> ??? sw=Toplevel(master) >> ??? sw.title('New Window') >> ??? Label(sw, text='new window').grid(row=0, column=0) >> ??? sframe=Frame(sw, relief=SUNKEN, bg='red').grid(row=1, column=0) > So you are here assigning None to sframe. > >> ??? Label(sframe, text='Label in a frame').grid(row=2, column=0) >> ??? Label(sframe, text='Second label in this frame').grid(row=3, column=0) > And when you pass None as the parent to a widget Tk > defaults to the root. So your widgets appear in your main window. > > Change the sframe line to two lines: > > sframe=Frame(sw, relief=SUNKEN, bg='red') > sframe.grid(row=1, column=0) > > > and all will be well.... > > except the sunken relief wont work. > TYhats because the sunken form requires a border width of at least 2 > pixels to be visible. So you need to add border=2 (or more) to make > it work. > > So the final line should be: > > sframe=Frame(sw, border=2, relief=SUNKEN, bg='red') > sframe.grid(row=1, column=0) > > As I say its a very common mistake and so, any time weird things > happen, always check that anywhere you assign a widget to a variable > you call the layout manager on a separate line. > > HTH Thanks Alan, Simple when you know. I remember having a similar issue with Entry widgets, Regards, Chris Roy-Smith From breamoreboy at gmail.com Sat Mar 9 19:53:28 2019 From: breamoreboy at gmail.com (Mark Lawrence) Date: Sun, 10 Mar 2019 00:53:28 +0000 Subject: [Tutor] How to create a structure from a Log file In-Reply-To: References: Message-ID: On 09/03/2019 18:44, Asad wrote: > Hi All , > > Please a sample logfile > > [main] [ 2019-01-21 10:13:14.041 CET ] > [AssistantLogHandler.configureLogging:176] Begin tracing.. > INFO: Jan 21, 2019 10:13:14 AM oracle.assistants.dbua.driver.UpgradeDriver > configureLogging > INFO: Loading upgrade driver, > DBUA running in mode : NONE > DBUA running on platform : Linux > > > [main] [ 2019-01-21 10:13:14.237 CET ] > [ClusterVerification.getInstance:553] Method Entry. workDir=/tmp > frameworkHome=/u01/app/oracle/product/12.2.0.1/dbhome_1 > [main] [ 2019-01-21 10:13:14.289 CET ] [CVUVariables.initialize:1456] > Start parse all variables from variables.xml... > [main] [ 2019-01-21 10:13:14.304 CET ] > [VerificationUtil.getVariablesXmlURI:10687] ==== XML variables file: > file:/u01/app/oracle/product/12.2.0.1/dbhome_1/cv/cvdata/variables.xml > [main] [ 2019-01-21 10:13:14.305 CET ] > [VerificationUtil.getVariablesXmlSchemaURI:10669] ==== XML variables > schema file: file:/u01/app/oracle/product/ > 12.2.0.1/dbhome_1/cv/cvdata/variables.xsd > [main] [ 2019-01-21 10:13:14.305 CET ] [CVUVariables.getRootElement:1721] > ==== URIs obtained :xsd URI = file:/u01/app/oracle/product/ > 12.2.0.1/dbhome_1/cv/cvdata/variables.xsd > [main] [ 2019-01-21 10:13:14.306 CET ] [CVUVariables.getRootElement:1722] > ==== URIs obtained :xml URI = file:/u01/app/oracle/product/ > 12.2.0.1/dbhome_1/cv/cvdata/variables.xml > [main] [ 2019-01-21 10:13:14.306 CET ] [CVUVariables.getRootElement:1735] > xsdFile exists : file:/u01/app/oracle/product/ > 12.2.0.1/dbhome_1/cv/cvdata/variables.xsd > [main] [ 2019-01-21 10:13:14.307 CET ] [CVUVariables.getRootElement:1750] > xmlFile exists : file:/u01/app/oracle/product/ > 12.2.0.1/dbhome_1/cv/cvdata/variables.xml > [main] [ 2019-01-21 10:13:14.307 CET ] [CVUVariables.getRootElement:1763] > setting xmlFactory to use xsdFile : file:/u01/app/oracle/product/ > 12.2.0.1/dbhome_1/cv/cvdata/variables.xsd > [main] [ 2019-01-21 10:13:14.408 CET ] [CVUVariables.getRootElement:1794] > The xml variables file: file:/u01/app/oracle/product/ > 12.2.0.1/dbhome_1/cv/cvdata/variables.xml, was parsed correctly > [main] [ 2019-01-21 10:13:14.410 CET ] [CVUVariables.parse:1521] Version > found ALL > [main] [ 2019-01-21 10:13:14.411 CET ] [CVUVariables.parse:1524] Process > common variables > [main] [ 2019-01-21 10:13:14.415 CET ] [CVUVariableData.:98] > CVUVariableData created with names: "s_silent,SILENT" > [main] [ 2019-01-21 10:13:14.416 CET ] [CVUVariables.parse:1521] Version > found 12.2 > [main] [ 2019-01-21 10:13:14.416 CET ] [CVUVariables.parse:1529] Process > variables for the release: 12.2 > [main] [ 2019-01-21 10:13:14.418 CET ] [CVUVariableData.:98] > CVUVariableData created with names: > > > I was thinking to convert it in a excel format or any other format which > better viewable > > Thanks, I suggest that you start with this https://docs.python.org/3/library/stdtypes.html#string-methods to grab your data. Then how about https://docs.python.org/3/library/csv.html for writing your data. Should this not be adequate please let us know and we'll get you sorted :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From bgailer at gmail.com Sun Mar 10 10:17:42 2019 From: bgailer at gmail.com (Bob Gailer) Date: Sun, 10 Mar 2019 10:17:42 -0400 Subject: [Tutor] How to create a structure from a Log file In-Reply-To: References: Message-ID: Thank you for showing us a sample of the log file. That is half the battle. Would you now reformat a couple of the lines so that we can see how you'd like to see them. It also may be the case that the advice given by others will be sufficient to guide you From asad.hasan2004 at gmail.com Mon Mar 11 02:53:14 2019 From: asad.hasan2004 at gmail.com (Asad) Date: Mon, 11 Mar 2019 12:23:14 +0530 Subject: [Tutor] How to create a structure from a Log file In-Reply-To: References: Message-ID: Hi All , I think this format would be easy in a row and table format Date Time Message 1/21/2019 10:13:14.237 CET Method Entry. workDir=/tmp frameworkHome=/u01/app/oracle/product/12.2.0.1/dbhome_1 I was also thinking of pandas but not sure how to proceed . Thanks, On Sun, Mar 10, 2019 at 7:47 PM Bob Gailer wrote: > Thank you for showing us a sample of the log file. That is half the > battle. Would you now reformat a couple of the lines so that we can see how > you'd like to see them. > > It also may be the case that the advice given by others will be sufficient > to guide you > -- Asad Hasan +91 9582111698 From bgailer at gmail.com Mon Mar 11 08:32:40 2019 From: bgailer at gmail.com (Bob Gailer) Date: Mon, 11 Mar 2019 08:32:40 -0400 Subject: [Tutor] How to create a structure from a Log file In-Reply-To: References: Message-ID: On Mar 11, 2019 2:53 AM, "Asad" wrote: > > Hi All , > > I think this format would be easy in a row and table format > > Date > Time > Message > 1/21/2019 > 10:13:14.237 CET > Method Entry. workDir=/tmp frameworkHome=/u01/app/oracle/product/ 12.2.0.1/dbhome_1 For me the easiest would be to create an HTML page and display it in the browser. You could start out with the table tag and then write each segment of the log file into the proper column and end with a closing table tag. I hope that's sufficient cuz I'm dictating this into my cell phone and don't particularly want to try to actually dictate HTML or type it on a cell phone keyboard. Excel is also a good idea and there are several python modules dedicated to creating Excel files. A Google search would reveal those. From diana.katz at gmail.com Mon Mar 11 12:10:09 2019 From: diana.katz at gmail.com (Diana Katz) Date: Mon, 11 Mar 2019 12:10:09 -0400 Subject: [Tutor] Question for tutoring page Message-ID: <7D091341-1B6D-49D7-93CA-30D4B21C922C@gmail.com> Hi, What is the best way to create an artificial intelligence program using python - that could recognize a 3D object and then rank drawings of the object as to which are more accurate. It was suggested to us to use raspberry pi and python and perhaps tweak existing apps. Any help as to how to start this project and the best method would be greatly appreciated. Best, Diana Sent from my iPhone From hm3landers at gmail.com Wed Mar 13 05:55:39 2019 From: hm3landers at gmail.com (Robert Landers) Date: Wed, 13 Mar 2019 05:55:39 -0400 Subject: [Tutor] Trouble Downloading To MacOS Mojave 10.14.3 released mid 2014 Message-ID: Hello Tutor, I am having trouble finding a python 3.7.2 download for my MacOS Mojave 10.14.3 released mid 2014. I would like to use Xcode to learn python. Is my OS too old? Please provide guidance. Thanks. From paul at allysonmccombs.com Wed Mar 13 10:11:23 2019 From: paul at allysonmccombs.com (Paul McCombs) Date: Wed, 13 Mar 2019 07:11:23 -0700 Subject: [Tutor] Trouble Downloading To MacOS Mojave 10.14.3 released mid 2014 In-Reply-To: References: Message-ID: On Wed, Mar 13, 2019, 5:05 AM Robert Landers wrote: > Hello Tutor, > > I am having trouble finding a python 3.7.2 download for my MacOS Mojave > 10.14.3 released mid 2014. > I would like to use Xcode to learn python. > > Is my OS too old? > > Please provide guidance. > > Thanks. > _______________________________ > MacOS Mojave 10.14.3 was released in December 2018. Python 3.7.2 is available for MacOS versions as early as 10.6, which came out in 2009. Can you clarify what you mean? Paul McCombs https://www.python.org/downloads/release/python-372/ macOS users - For Python 3.7 releases, we provide two binary installer options for download. The default variant is 64-bit-only and works on macOS 10.9 (Mavericks) and later systems. We also continue to provide a 64-bit/32-bit variant that works on all versions of macOS from 10.6 (Snow Leopard) on. Both variants now come with batteries- From zachary.ware+pytut at gmail.com Wed Mar 13 12:40:52 2019 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Wed, 13 Mar 2019 11:40:52 -0500 Subject: [Tutor] Trouble Downloading To MacOS Mojave 10.14.3 released mid 2014 In-Reply-To: References: Message-ID: On Wed, Mar 13, 2019 at 7:06 AM Robert Landers wrote: > Hello Tutor, Hi Robert, and welcome! > I am having trouble finding a python 3.7.2 download for my MacOS Mojave > 10.14.3 released mid 2014. If you go to https://www.python.org/downloads/, the large yellow "Download Python ..." button near the top of the page should give you the installer for the latest version of Python. > I would like to use Xcode to learn python. Xcode doesn't really have any support for Python. You may be able to use it to edit Python code, but you'll probably be happier with any number of other editors that do properly support Python, like VS Code, PyCharm, Sublime, or Atom (full disclosure: of those I've only used VS Code and PyCharm, and usually use vim instead). > Is my OS too old? No, in fact I'm pretty sure you have the latest version (for reference, macOS Mojave 10.14.3 was released a month ago on 7Feb2019) :). The Python installer may say 10.9, but that is the minimum required version; anything later than that is supported (including 10.14). Hope this helps, -- Zach From sydney.shall at kcl.ac.uk Wed Mar 13 13:10:36 2019 From: sydney.shall at kcl.ac.uk (Shall, Sydney) Date: Wed, 13 Mar 2019 17:10:36 +0000 Subject: [Tutor] Trouble Downloading To MacOS Mojave 10.14.3 released mid 2014 In-Reply-To: References: Message-ID: On 13/03/2019 14:11, Paul McCombs wrote: > On Wed, Mar 13, 2019, 5:05 AM Robert Landers wrote: > >> Hello Tutor, >> >> I am having trouble finding a python 3.7.2 download for my MacOS Mojave >> 10.14.3 released mid 2014. >> I would like to use Xcode to learn python. >> >> Is my OS too old? >> >> Please provide guidance. >> >> Thanks. >> _______________________________ >> > > MacOS Mojave 10.14.3 was released in December 2018. > > Python 3.7.2 is available for MacOS versions as early as 10.6, which came > out in 2009. > > Can you clarify what you mean? > > Paul McCombs > > https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.python.org%2Fdownloads%2Frelease%2Fpython-372%2F&data=01%7C01%7Csydney.shall%40kcl.ac.uk%7C6d5116a86151497caefd08d6a7d23e37%7C8370cf1416f34c16b83c724071654356%7C0&sdata=XM0%2FLvaSE7wYpSfvZWuaKBYARoUGKCkBS9XWD2bZbzE%3D&reserved=0 > > macOS users > > - For Python 3.7 releases, we provide two binary installer options for > download. The default variant is 64-bit-only and works on macOS 10.9 > (Mavericks) and later systems. We also continue to provide a 64-bit/32-bit > variant that works on all versions of macOS from 10.6 (Snow Leopard) on. > Both variants now come with batteries- > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Ftutor&data=01%7C01%7Csydney.shall%40kcl.ac.uk%7C6d5116a86151497caefd08d6a7d23e37%7C8370cf1416f34c16b83c724071654356%7C0&sdata=8vKb0Mzx6M3QldhtDKDcnNNUXsuSFAvKGyX3lmwAXOU%3D&reserved=0 > I am using MacOS Mojave 10.4.3. I am also using Python 3.7.0 supplied by Anaconda. It works fine. Sydney -- _________ Professor Sydney Shall Department of Haematology/Oncology Phone: +(0)2078489200 E-Mail: sydney.shall [Correspondents outside the College should add @kcl.ac.uk] From sydney.shall at kcl.ac.uk Wed Mar 13 13:12:34 2019 From: sydney.shall at kcl.ac.uk (Shall, Sydney) Date: Wed, 13 Mar 2019 17:12:34 +0000 Subject: [Tutor] Trouble Downloading To MacOS Mojave 10.14.3 released mid 2014 In-Reply-To: References: Message-ID: On 13/03/2019 14:11, Paul McCombs wrote: > On Wed, Mar 13, 2019, 5:05 AM Robert Landers wrote: > >> Hello Tutor, >> >> I am having trouble finding a python 3.7.2 download for my MacOS Mojave >> 10.14.3 released mid 2014. >> I would like to use Xcode to learn python. >> >> Is my OS too old? >> >> Please provide guidance. >> >> Thanks. >> _______________________________ >> > > MacOS Mojave 10.14.3 was released in December 2018. > > Python 3.7.2 is available for MacOS versions as early as 10.6, which came > out in 2009. > > Can you clarify what you mean? > > Paul McCombs > > https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.python.org%2Fdownloads%2Frelease%2Fpython-372%2F&data=01%7C01%7Csydney.shall%40kcl.ac.uk%7C6d5116a86151497caefd08d6a7d23e37%7C8370cf1416f34c16b83c724071654356%7C0&sdata=XM0%2FLvaSE7wYpSfvZWuaKBYARoUGKCkBS9XWD2bZbzE%3D&reserved=0 > > macOS users > > - For Python 3.7 releases, we provide two binary installer options for > download. The default variant is 64-bit-only and works on macOS 10.9 > (Mavericks) and later systems. We also continue to provide a 64-bit/32-bit > variant that works on all versions of macOS from 10.6 (Snow Leopard) on. > Both variants now come with batteries- > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Ftutor&data=01%7C01%7Csydney.shall%40kcl.ac.uk%7C6d5116a86151497caefd08d6a7d23e37%7C8370cf1416f34c16b83c724071654356%7C0&sdata=8vKb0Mzx6M3QldhtDKDcnNNUXsuSFAvKGyX3lmwAXOU%3D&reserved=0 > I meant to add that I use Spyder 3.3.2 and that works fine too. -- _________ Professor Sydney Shall Department of Haematology/Oncology Phone: +(0)2078489200 E-Mail: sydney.shall [Correspondents outside the College should add @kcl.ac.uk] From alan.gauld at yahoo.co.uk Wed Mar 13 13:14:00 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 13 Mar 2019 17:14:00 +0000 Subject: [Tutor] Question for tutoring page In-Reply-To: <7D091341-1B6D-49D7-93CA-30D4B21C922C@gmail.com> References: <7D091341-1B6D-49D7-93CA-30D4B21C922C@gmail.com> Message-ID: On 11/03/2019 16:10, Diana Katz wrote: > What is the best way to ..program using python - that could recognize > a 3D object and then rank drawings of the object as to which are more > accurate. I notice nobody has responded so I thought I'd let you know your mail was received. Unfortunately what you ask is extremely specialised and quite advanced so I suspect nobody on the list feels competent to advise you - I certainly don't! > It was suggested to us to use raspberry pi and python The type of computer is unlikely to make much difference. But how do you plan on "recognising" these shapes? Do you have image files or are you expecting it to do some optical scanning from a camera or similar? > and perhaps tweak existing apps. That's good advise if you can find any suitable apps to tweak that are both open source and written in Python. Personally I don't know of anything remotely like what you suggest! I assume you are already a competent programmer in some kind of language (Python, PHP, Javascript, Java, VB, C++?)already? If not, this is an extremely challenging first project! (Even assuming you can find a starting point and don't have to write it from scratch.) -- 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 arj.python at gmail.com Wed Mar 13 13:28:17 2019 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Wed, 13 Mar 2019 21:28:17 +0400 Subject: [Tutor] Question for tutoring page In-Reply-To: <7D091341-1B6D-49D7-93CA-30D4B21C922C@gmail.com> References: <7D091341-1B6D-49D7-93CA-30D4B21C922C@gmail.com> Message-ID: I'll outline what you need those types of recognition are done by machine learning, just some maths using computation. You normally give your program some images to train. Instead of letting the program to figure out by itself, you give it some realistic drawings, the best of the bunch and a picture of the real thing. It will then compare the difference between the real picture and the drawings and get an idea of what good drawings are. If you have some 40 pics, you use 30 to train and some 10 to test. Now when you use the program, when you throw at it a new drawing, it will rank it based on what it learnt. For the raspberry pi part i think it's convenient to move around but not really needed. >From my experience with these kinds of projects, google "image recognition with python" maybe you'll see opencv etc. before that just make sure to learn at least some basic python. Then you google concepts you don't know. little by little, you'll understand what's going on. like take an image recognition tutorial with image set provided and complete it. That approach is better than taking a machine learning tutorial etc. Should you need any help, please tell! I don't promise but i'll try to write such a project as a tutorial. Yours, Abdur-Rahmaan Janhangeer http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ Mauritius From gdickerson222 at gmail.com Fri Mar 15 21:54:41 2019 From: gdickerson222 at gmail.com (Glenn Dickerson) Date: Fri, 15 Mar 2019 21:54:41 -0400 Subject: [Tutor] (no subject) Message-ID: class Student(): def__init__(self, name, major, gpa, is_on_probation): self.name = name self.major = major self.gpa = gpa self.is_on_probation = is_on_probation import Student student1 = Student('Jim', 'Business', 3.1, False) student2 = Student('Pam', 'Art', 2.5, True) print(student1.name) print(student2.gpa) I entered this in IDLE and it failed. Any advice? Thank you. From steve at pearwood.info Sat Mar 16 05:14:33 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 16 Mar 2019 20:14:33 +1100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <20190316091433.GK12502@ando.pearwood.info> Hi Glenn, and welcome. On Fri, Mar 15, 2019 at 09:54:41PM -0400, Glenn Dickerson wrote: > class Student(): > def__init__(self, name, major, gpa, is_on_probation): > self.name = name > self.major = major > self.gpa = gpa > self.is_on_probation = is_on_probation > > > import Student > student1 = Student('Jim', 'Business', 3.1, False) > student2 = Student('Pam', 'Art', 2.5, True) > print(student1.name) > print(student2.gpa) > > I entered this in IDLE and it failed. Any advice? Thank you. Yes -- read the error message. What does it say? (Reading, and understanding, error messages is probably the most important skill a programmer can have.) -- Steven From alan.gauld at yahoo.co.uk Sat Mar 16 05:26:33 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 16 Mar 2019 09:26:33 +0000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 16/03/2019 01:54, Glenn Dickerson wrote: > class Student(): > > def__init__(self, name, major, gpa, is_on_probation): > self.name = name > self.major = major > self.gpa = gpa > self.is_on_probation = is_on_probation > > > import Student > student1 = Student('Jim', 'Business', 3.1, False) > student2 = Student('Pam', 'Art', 2.5, True) > print(student1.name) > print(student2.gpa) > > I entered this in IDLE and it failed. Any advice? Thank you. Please, never just say "it failed". How did it fail? Did you get an error message? If so, send us the complete message Did IDLE crash? Did the PC crash? Did it run with no output? Did it run with the wrong output? There are so many ways for a program to "fail" we need more specific details. In this case I can guess what might have happened. I suspect you need to read up on how to import and use an external file, but again there are at least 3 possible errors that you might have and I can't tell which from the details you provided. -- 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 Mar 16 05:28:36 2019 From: __peter__ at web.de (Peter Otten) Date: Sat, 16 Mar 2019 10:28:36 +0100 Subject: [Tutor] (no subject) References: Message-ID: Glenn Dickerson wrote: > class Student(): > > def__init__(self, name, major, gpa, is_on_probation): > self.name = name > self.major = major > self.gpa = gpa > self.is_on_probation = is_on_probation > > > import Student > student1 = Student('Jim', 'Business', 3.1, False) > student2 = Student('Pam', 'Art', 2.5, True) > print(student1.name) > print(student2.gpa) > > I entered this in IDLE and it failed. Any advice? First of all, try to be as precise as you can with your error descriptions. What was the exact error, what looked the traceback like? Do not retype, use cut-and-paste to put them into your post. Was it something like File "Student.py", line 3 def__init__(self, name, major, gpa, is_on_probation): ^ SyntaxError: invalid syntax Then the problem is the missing space between the keyword "def" and the method name "__init__". Or was it Traceback (most recent call last): File "student.py", line 10, in import Student ImportError: No module named 'Student' That's because Student is a class rather than a module, and you neither need to nor can import it directly. If you remove the import statement your code should work. But wait, there's another option. If you saw Traceback (most recent call last): File "Student.py", line 10, in import Student File "Student.py", line 11, in student1 = Student('Jim', 'Business', 3.1, False) TypeError: 'module' object is not callable then a "Student" module was imported successfully. However, as it has the same name as your class, the name "Student" is now bound to the module, and unlike the class a module cannot be called. The solution then is to rename the module, from Student.py to student.py, say, as lowercase module names are the preferred convention anyway. So there are at least three possible problems in your tiny and almost correct code snippet. In a script that does some actual work the number of possible problems explodes, and that's why most of us don't even start to debug a piece of code without a detailed error description and a traceback -- it's usually a waste of time. From sjeik_appie at hotmail.com Sat Mar 16 06:31:08 2019 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Sat, 16 Mar 2019 10:31:08 +0000 Subject: [Tutor] Question for tutoring page In-Reply-To: Message-ID: On 13 Mar 2019 18:14, Alan Gauld via Tutor wrote: On 11/03/2019 16:10, Diana Katz wrote: > What is the best way to ..program using python - that could recognize > a 3D object and then rank drawings of the object as to which are more > accurate. ===>> check this out: https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html From valerio at pbds.eu Sat Mar 16 13:14:20 2019 From: valerio at pbds.eu (Valerio Pachera) Date: Sat, 16 Mar 2019 18:14:20 +0100 (CET) Subject: [Tutor] Remove soft line break In-Reply-To: <800104116.88.1551355527069.JavaMail.zimbra@pbds.eu> References: <431086936.29.1549300383777.JavaMail.zimbra@pbds.eu> <800104116.88.1551355527069.JavaMail.zimbra@pbds.eu> Message-ID: <581256597.11.1552756460192.JavaMail.zimbra@pbds.eu> ----- Messaggio originale ----- > Da: "Valerio Pachera" > A: "Tutor Python" > Inviato: Gioved?, 28 febbraio 2019 13:05:27 > Oggetto: Re: [Tutor] Remove soft line break > ... > I noticed that the end of file doesn't get preserve if I create a copy of the > file ... I've been told by a collegue that the when the file is opened by interpreter it uses the end of line of the system the program runs on. In my case the os is linux, so python uses '\n' a end of line, no matter what's written in the file. The end of line, ore better "newline" may be chosen when open the file. open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) So, if I have this string s = ''' hallo, this is a multi line text ''' I can save it in a text file for windows this way: f = open('test.txt', 'w', newline='\r\n') f.write(s) f.close() From valerio at pbds.eu Sat Mar 16 13:39:13 2019 From: valerio at pbds.eu (Valerio Pachera) Date: Sat, 16 Mar 2019 18:39:13 +0100 (CET) Subject: [Tutor] Merge a dictionary into a string Message-ID: <373880484.35.1552757953542.JavaMail.zimbra@pbds.eu> Consider this: import collections d = OrderedDict(a='hallo', b='world') I wish to get a single string like this: 'a "hallo" b "world"' Notice I wish the double quote to be part of the string. In other words I want to wrap the value of a and b. I was thinking to use such function I created: def mywrap(text, char='"'): return(char + text + char) I can't think anything better than s = '' for k, v in d.items(): s += ' '.join( (k, mywrap(v)) ) + ' ' or s = '' for k, v in d.items(): s += k + ' ' + mywrap(v) + ' ' What do you think? It's fine enough but I wonder if there's a better solution. Thank you. From alan.gauld at yahoo.co.uk Sat Mar 16 14:09:17 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 16 Mar 2019 18:09:17 +0000 Subject: [Tutor] Merge a dictionary into a string In-Reply-To: <373880484.35.1552757953542.JavaMail.zimbra@pbds.eu> References: <373880484.35.1552757953542.JavaMail.zimbra@pbds.eu> Message-ID: On 16/03/2019 17:39, Valerio Pachera wrote: > I wish to get a single string like this: > > 'a "hallo" b "world"' > > Notice I wish the double quote to be part of the string. > In other words I want to wrap the value of a and b. When dealing with string layouts I tend to go to string formatting... >>> d= {'a':"alpha",'b':"beta"} >>> ' '.join(['{} "{}"'.format(k,v) for k,v in d.items()]) 'a "alpha" b "beta"' >>> Or using old C style formatting, it's very slightly shorter: >>> ' '.join(['%s "%s"'% (k,v) for k,v in d.items()]) 'a "alpha" b "beta"' 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 __peter__ at web.de Sat Mar 16 14:44:22 2019 From: __peter__ at web.de (Peter Otten) Date: Sat, 16 Mar 2019 19:44:22 +0100 Subject: [Tutor] Merge a dictionary into a string References: <373880484.35.1552757953542.JavaMail.zimbra@pbds.eu> Message-ID: Valerio Pachera wrote: > Consider this: > > import collections > d = OrderedDict(a='hallo', b='world') > > I wish to get a single string like this: > > 'a "hallo" b "world"' > > Notice I wish the double quote to be part of the string. > In other words I want to wrap the value of a and b. > > I was thinking to use such function I created: > > def mywrap(text, char='"'): > return(char + text + char) > > I can't think anything better than > > s = '' > for k, v in d.items(): > s += ' '.join( (k, mywrap(v)) ) + ' ' > > or > > s = '' > for k, v in d.items(): > s += k + ' ' + mywrap(v) + ' ' > > What do you think? > It's fine enough but I wonder if there's a better solution. In Python 3.6 and above you can use f-strings: >>> d = dict(a="hello", b="world") >>> " ".join(f'{k} "{v}"' for k, v in d.items()) 'a "hello" b "world"' By the way, are you sure that the dictionary contains only strings without spaces and '"'? From akleider at sonic.net Sat Mar 16 18:51:24 2019 From: akleider at sonic.net (Alex Kleider) Date: Sat, 16 Mar 2019 15:51:24 -0700 Subject: [Tutor] Merge a dictionary into a string In-Reply-To: <373880484.35.1552757953542.JavaMail.zimbra@pbds.eu> References: <373880484.35.1552757953542.JavaMail.zimbra@pbds.eu> Message-ID: <053471083f930a67e99bd4579ec25a40@sonic.net> On 2019-03-16 10:39, Valerio Pachera wrote: > Consider this: > > import collections > d = OrderedDict(a='hallo', b='world') > > I wish to get a single string like this: > > 'a "hallo" b "world"' > > Notice I wish the double quote to be part of the string. > In other words I want to wrap the value of a and b. > > I was thinking to use such function I created: > > def mywrap(text, char='"'): > return(char + text + char) > > I can't think anything better than > > s = '' > for k, v in d.items(): > s += ' '.join( (k, mywrap(v)) ) + ' ' > > or > > s = '' > for k, v in d.items(): > s += k + ' ' + mywrap(v) + ' ' > > What do you think? > It's fine enough but I wonder if there's a better solution. > Would the following not give you what you want: (I've not used OrderedDict but I believe it would work for dict so assume ok for OrderedDict.) my_which_string = "a = '{a}' b = '{b}'".format(**d) From alan.gauld at yahoo.co.uk Sat Mar 16 19:57:23 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 16 Mar 2019 23:57:23 +0000 Subject: [Tutor] Merge a dictionary into a string In-Reply-To: References: <373880484.35.1552757953542.JavaMail.zimbra@pbds.eu> Message-ID: On 16/03/2019 18:44, Peter Otten wrote: > > In Python 3.6 and above you can use f-strings: > >>>> d = dict(a="hello", b="world") >>>> " ".join(f'{k} "{v}"' for k, v in d.items()) > 'a "hello" b "world"' Cool, I'd missed f-strings. Time for some reading.... Thanks Peter, -- 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 Sat Mar 16 21:19:25 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 16 Mar 2019 19:19:25 -0600 Subject: [Tutor] Merge a dictionary into a string In-Reply-To: References: <373880484.35.1552757953542.JavaMail.zimbra@pbds.eu> Message-ID: On March 16, 2019 5:57:23 PM MDT, Alan Gauld via Tutor wrote: >On 16/03/2019 18:44, Peter Otten wrote: >> >> In Python 3.6 and above you can use f-strings: >> >>>>> d = dict(a="hello", b="world") >>>>> " ".join(f'{k} "{v}"' for k, v in d.items()) >> 'a "hello" b "world"' > >Cool, I'd missed f-strings. Time for some reading.... > >Thanks Peter, f-strings are great, but a lot of people have to support multiple python versions so they're not a big option for everyone... yet. -- Sent from a mobile device with K-9 Mail. Please excuse my brevity. From mats at wichmann.us Sun Mar 17 13:05:17 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 17 Mar 2019 11:05:17 -0600 Subject: [Tutor] Merge a dictionary into a string In-Reply-To: <373880484.35.1552757953542.JavaMail.zimbra@pbds.eu> References: <373880484.35.1552757953542.JavaMail.zimbra@pbds.eu> Message-ID: <48b17e2d-e2cc-0bfd-c333-9d535550c9b9@wichmann.us> On 3/16/19 11:39 AM, Valerio Pachera wrote: > Consider this: > > import collections > d = OrderedDict(a='hallo', b='world') > > I wish to get a single string like this: > > 'a "hallo" b "world"' > > Notice I wish the double quote to be part of the string. > In other words I want to wrap the value of a and b. So the question that comes to mind is "why"? I don't mean that in the negative sense as in you don't want to do that, but your use case may drive the choice of possible solutions. For example, I once got asked very nearly this question by someone who it turned out wanted to serialize the dict into something that could later be used to load up a dict. String is what he thought of, but in this case json, or pickle, turned out to be a better solution for what he wanted than a string. If you only want to print the string for informational purposes, the suggestions here will work well. You can even define your own class which inherits from OrderedDict and just provides a new definition of the method which produces a string representation and gives you what you want without calling anything, like this: >>> class MyOrderedDict(OrderedDict): ... def __repr__(self): ... return " ".join(f'{k} "{v}"' for k, v in self.items()) ... >>> >>> d = MyOrderedDict(a='hallo', b='world') >>> print(d) a "hallo" b "world" >>> x = str(d) >>> x 'a "hallo" b "world"' From PyTutor at danceswithmice.info Sun Mar 17 15:12:50 2019 From: PyTutor at danceswithmice.info (David L Neil) Date: Mon, 18 Mar 2019 08:12:50 +1300 Subject: [Tutor] Merge a dictionary into a string In-Reply-To: <48b17e2d-e2cc-0bfd-c333-9d535550c9b9@wichmann.us> References: <373880484.35.1552757953542.JavaMail.zimbra@pbds.eu> <48b17e2d-e2cc-0bfd-c333-9d535550c9b9@wichmann.us> Message-ID: <34c106e0-4f42-bb76-24a4-e8e64e0eee15@DancesWithMice.info> On 18/03/19 6:05 AM, Mats Wichmann wrote: > On 3/16/19 11:39 AM, Valerio Pachera wrote: >> Consider this: >> import collections >> d = OrderedDict(a='hallo', b='world') >> I wish to get a single string like this: >> 'a "hallo" b "world"' >> Notice I wish the double quote to be part of the string. >> In other words I want to wrap the value of a and b. > So the question that comes to mind is "why"? I don't mean that in the > negative sense as in you don't want to do that, but your use case may > drive the choice of possible solutions. Reading the OP, I immediately recognised the problem - meantime others had responded and the f'string' suggestion seemed most apropos. Somewhat intrigued, and to answer the use-case question, I went looking in my personal collection of RDBMS routines and "snippets" (which have hardly been updated since Py2, excepting (IIRC) when MySQL's Connector-Python extended into dictionary-cursors). The Connector will automatically delimit field/colNMs passed within a variable collection ('escaping' practice, highly recommended!) - a SELECT clause (for example). However, such automation is not applied to similar appearing in other clauses. One of my helper-routines creates a comma-separated string by first surrounding columnNMs with back-ticks and then .join()ing. It's not rocket-surgery, but has been handy and import-ed many, many times. YAGNI: me being me [puffs-out chest in a most unattractive fashion], one of the function's optional arguments offers a choice of delimiter. Can't recall ever using it 'elsewhere' though. Thanks to the OP, and respondents making me think. Have added to my >=v3.6 Backlog... -- Regards =dn From toby at tobiah.org Mon Mar 18 11:34:02 2019 From: toby at tobiah.org (Tobiah) Date: Mon, 18 Mar 2019 08:34:02 -0700 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: You need a space between 'def' and '__init__'. Then you must remove the import statement. The class is already defined in your scope and there is no module called 'Student'. Tobiah On 3/15/19 6:54 PM, Glenn Dickerson wrote: > class Student(): > > def__init__(self, name, major, gpa, is_on_probation): > self.name = name > self.major = major > self.gpa = gpa > self.is_on_probation = is_on_probation > > > import Student > student1 = Student('Jim', 'Business', 3.1, False) > student2 = Student('Pam', 'Art', 2.5, True) > print(student1.name) > print(student2.gpa) From street.sweeper at mailworks.org Wed Mar 20 01:06:00 2019 From: street.sweeper at mailworks.org (street.sweeper at mailworks.org) Date: Wed, 20 Mar 2019 01:06:00 -0400 Subject: [Tutor] removing xml elements with ElementTree Message-ID: An opportunity to work in Python, and the necessity of working with some XML too large to visualize, got me thinking about an answer Alan Gauld had written to me a few years ago (https://mail.python.org/pipermail/tutor/2015-June/105810.html). I have applied that information in this script, but I have another question :) Let's say I have an xml file like this: -------------- order.xml ---------------- Bob 321 Main St D20 4 CS211 1 BL5 7 AC400 1 ---------- end order.xml ---------------- Items CS211 and AC400 are not valid items, and I want to remove their nodes. I came up with the following (python 3.6.7 on linux): ------------ xml_delete_test.py -------------------- import os import xml.etree.ElementTree as ET hd = os.path.expanduser('~') inputxml = os.path.join(hd,'order.xml') outputxml = os.path.join(hd,'fixed_order.xml') valid_items = ['D20','BL5'] tree = ET.parse(inputxml) root = tree.getroot() saleslines = root.find('saleslines').findall('salesline') for e in saleslines[:]: if e.find('item').text not in valid_items: saleslines.remove(e) tree.write(outputxml) ---------- end xml_delete_test.py ------------------ The above code runs without error, but simply writes the original file to disk. The desired output would be: -------------- fixed_order.xml ---------------- Bob 321 Main St D20 4 BL5 7 ---------- end fixed_order.xml ---------------- What I find particularly confusing about the problem is that after running xml_delete_test.py in the Idle editor, if I go over to the shell and type saleslines, I can see that it's now a list of two elements. I run the following: for i in saleslines: print(i.find('item').text) and I see that it's D20 and BL5, my two valid items. Yet when I write tree out to the disk, it has the original four. Do I need to refresh tree somehow? Thanks! From __peter__ at web.de Wed Mar 20 09:32:19 2019 From: __peter__ at web.de (Peter Otten) Date: Wed, 20 Mar 2019 14:32:19 +0100 Subject: [Tutor] removing xml elements with ElementTree References: Message-ID: street.sweeper at mailworks.org wrote: > An opportunity to work in Python, and the necessity of working with some > XML too large to visualize, got me thinking about an answer Alan Gauld had > written to me a few years ago > (https://mail.python.org/pipermail/tutor/2015-June/105810.html). I have > applied that information in this script, but I have another question :) > > Let's say I have an xml file like this: > > -------------- order.xml ---------------- > > > Bob > 321 Main St > > > D20 > 4 > > > CS211 > 1 > > > BL5 > 7 > > > AC400 > 1 > > > > > ---------- end order.xml ---------------- > > Items CS211 and AC400 are not valid items, and I want to remove their > nodes. I came up with the following (python 3.6.7 on linux): > > ------------ xml_delete_test.py -------------------- > > import os > import xml.etree.ElementTree as ET > > hd = os.path.expanduser('~') > inputxml = os.path.join(hd,'order.xml') > outputxml = os.path.join(hd,'fixed_order.xml') > > valid_items = ['D20','BL5'] > > tree = ET.parse(inputxml) > root = tree.getroot() > saleslines = root.find('saleslines').findall('salesline') > for e in saleslines[:]: > if e.find('item').text not in valid_items: > saleslines.remove(e) > > tree.write(outputxml) > > ---------- end xml_delete_test.py ------------------ > > The above code runs without error, but simply writes the original file to > disk. The desired output would be: > > -------------- fixed_order.xml ---------------- > > > Bob > 321 Main St > > > D20 > 4 > > > BL5 > 7 > > > > > ---------- end fixed_order.xml ---------------- > > What I find particularly confusing about the problem is that after running > xml_delete_test.py in the Idle editor, if I go over to the shell and type > saleslines, I can see that it's now a list of two elements. I run the > following: > > for i in saleslines: > print(i.find('item').text) > > and I see that it's D20 and BL5, my two valid items. Yet when I write > tree out to the disk, it has the original four. Do I need to refresh tree > somehow? > > Thanks! First of all, thank you for this clear and complete problem description! > saleslines = root.find('saleslines').findall('salesline') Here findall() returns a new list of matches which is completely independent of the element tree. Therefore > saleslines.remove(e) will remove the element e from this indepent list, and only from that. To remove an element from the tree you have to know its parent, and then parent_element.remove(child_element) will actually modify the tree. In your case the parent is always , so you can restrict yourself to its children: saleslines = root.find('saleslines') for e in saleslines.findall('salesline'): if e.find('item').text not in valid_items: saleslines.remove(e) From marshmallowaddictors at gmail.com Wed Mar 20 10:30:06 2019 From: marshmallowaddictors at gmail.com (Eric Oh Yeah Yeah) Date: Wed, 20 Mar 2019 10:30:06 -0400 Subject: [Tutor] Help Message-ID: How do I make Python 3 pick a random variable out of a set of variables I give it? From alan.gauld at yahoo.co.uk Wed Mar 20 18:50:24 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 20 Mar 2019 22:50:24 +0000 Subject: [Tutor] Help In-Reply-To: References: Message-ID: On 20/03/19 14:30, Eric Oh Yeah Yeah wrote: > How do I make Python 3 pick a random variable out of a set of variables I > give it? There are several options but if you look in the random module you should find one that suits your particular needs. choice() or randrange() may be good options. If that's not enough of a hint come back with more specific details about what kind of "set of variables" you are using. Alan G. From chris_roysmith at internode.on.net Wed Mar 20 18:43:31 2019 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Thu, 21 Mar 2019 09:43:31 +1100 Subject: [Tutor] is there a graphics library for common tkinter Button functions? Message-ID: <48c3a3d8-583e-556e-c0a1-3cafff04aecc@internode.on.net> Hi, Is there a "graphics library" of common button uses? that is things like forward record, back record, 1st record, last record, printer, save and the likes. I don't have very artistic abilities, so would prefer to save making my own library. Thank you Chris Roy-Smith From alan.gauld at yahoo.co.uk Wed Mar 20 19:19:07 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 20 Mar 2019 23:19:07 +0000 Subject: [Tutor] is there a graphics library for common tkinter Button functions? In-Reply-To: <48c3a3d8-583e-556e-c0a1-3cafff04aecc@internode.on.net> References: <48c3a3d8-583e-556e-c0a1-3cafff04aecc@internode.on.net> Message-ID: On 20/03/19 22:43, Chris Roy-Smith wrote: > Is there a "graphics library" of common button uses? that is things like > forward record, back record, 1st record, last record, printer, save and > the likes. The short answer is no. But you can assign any bitmap image to a button. (You can use other formats too but bitmaps are easiest in my experience!) But putting an image on the button does not give it any functionality. You need to program that yourself. HTH, Alan G. From chris_roysmith at internode.on.net Wed Mar 20 20:28:37 2019 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Thu, 21 Mar 2019 11:28:37 +1100 Subject: [Tutor] is there a graphics library for common tkinter Button functions? In-Reply-To: References: <48c3a3d8-583e-556e-c0a1-3cafff04aecc@internode.on.net> Message-ID: On 21/3/19 10:19 am, Alan Gauld via Tutor wrote: > On 20/03/19 22:43, Chris Roy-Smith wrote: >> Is there a "graphics library" of common button uses? that is things >> like forward record, back record, 1st record, last record, printer, >> save and the likes. > > The short answer is no. But you can assign any bitmap image > to a button. (You can use other formats too but bitmaps are > easiest in my experience!) > > But putting an image on the button does not give it any > functionality. You need to program that yourself. > > HTH, > > Alan G. > > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Thanks Alan, Yes I knew that buttons need a function to do anything. I was hoping that the wheel didn't need re-inventing. From gdickerson222 at gmail.com Thu Mar 21 01:19:37 2019 From: gdickerson222 at gmail.com (Glenn Dickerson) Date: Thu, 21 Mar 2019 01:19:37 -0400 Subject: [Tutor] my gmail of 5 minutes ago Message-ID: I am using Linux Mint 18.3 and Python 3.5. Thank you, Glenn Dickerson From gdickerson222 at gmail.com Thu Mar 21 01:13:14 2019 From: gdickerson222 at gmail.com (Glenn Dickerson) Date: Thu, 21 Mar 2019 01:13:14 -0400 Subject: [Tutor] error message Message-ID: Thank you for all of your responses to: class Student(): def__init__(self, name, major, gpa, is_on_probation): self.name = name self.major = major self.gpa = gpa self.is_on_probation = is_on_probation import Student student1 = Student('Jim', 'Business', 3.1, False) print(student1.name) I don't understand this error message. Thank you so much, Glenn Dickerson Traceback (most recent call last): File "/home/glen/app.py", line 1, in import Student File "/home/glen/Student.py", line 2 def__init__(self, name, major, gpa, is_on_probation): ^ SyntaxError: invalid syntax >>> From steve at pearwood.info Thu Mar 21 07:01:33 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 21 Mar 2019 22:01:33 +1100 Subject: [Tutor] error message In-Reply-To: References: Message-ID: <20190321110133.GM12502@ando.pearwood.info> > I don't understand this error message. Thank you so much, Glenn Dickerson > > Traceback (most recent call last): > File "/home/glen/app.py", line 1, in > import Student > File "/home/glen/Student.py", line 2 > def__init__(self, name, major, gpa, is_on_probation): > ^ > SyntaxError: invalid syntax Syntax errors are sometimes the hardest to decipher, because the message is usually pretty generic and uninformative, and the caret ^ will appear where the interpreter *notices* the problem, not where the problem *starts*. In this case, the problem is you are missing a space between the "def" keyword and the "__init__" method name. -- Steven From alan.gauld at yahoo.co.uk Thu Mar 21 07:11:27 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 21 Mar 2019 11:11:27 +0000 Subject: [Tutor] is there a graphics library for common tkinter Button functions? In-Reply-To: References: <48c3a3d8-583e-556e-c0a1-3cafff04aecc@internode.on.net> Message-ID: On 21/03/19 00:28, Chris Roy-Smith wrote: > Yes I knew that buttons need a function to do anything. > > I was hoping that the wheel didn't need re-inventing. So far as the button images go a Google search should throw up lots of free icons you can use and a program like ImageMagick can resize and change format as needed. As for assigning them its trivial: bmps = "/path/to/your/images/" bUp = tk.Button(parent, image=bmps+"up.bmp", command=doUp) HTH Alan G. From alan.gauld at yahoo.co.uk Thu Mar 21 07:28:48 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 21 Mar 2019 11:28:48 +0000 Subject: [Tutor] error message In-Reply-To: References: Message-ID: On 21/03/19 05:13, Glenn Dickerson wrote: > Thank you for all of your responses to: > > class Student(): > def__init__(self, name, major, gpa, is_on_probation): > self.name = name > self.major = major > self.gpa = gpa > self.is_on_probation = is_on_probation > Presumably the lines above ar in a separate file called Student.py? And the lines below are in another file called app.py? If so thats a good start. Steve (and others have already pointed out the need for a space after def (otherwise python looks for a function called def__init__() and wonderswhy yu have a colon after its invocation) But that will only lead you to the next error. > import Student > student1 = Student('Jim', 'Business', 3.1, False) When accessing an object in an imported module you must precede the object's name with the module: student1 = Student.Student(....) # Student class in the Student module Alternatively you can explicitly import the Student class (and nothing else!) from Student with: from Student import Student I which case you can use it as you do in your code. In your case it doesn't really matter which of the two styles you choose. In more complex programs explicit module naming might make your code clearer (eg. if you have many modules). Alternatively, pulling in the specific object might save you some typing if you reference the object several times. You need to choose which is most appropriate based on your code. HTH Alan G. From Kayla.Jones at zimmerbiomet.com Thu Mar 21 08:43:00 2019 From: Kayla.Jones at zimmerbiomet.com (Jones, Kayla) Date: Thu, 21 Mar 2019 12:43:00 +0000 Subject: [Tutor] LPTHW ex15 question Message-ID: <39e0090ec49b49d68b67219e8dc8dd10@SBDEXDAG08.zmr.zimmer.com> I am working through excersise 15 of LPTHW and am getting an error message in powershell that I can't figure out. I've attached a screenshot to help. Any suggestions would be appreciated. Kayla J. From sjeik_appie at hotmail.com Thu Mar 21 16:26:50 2019 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Thu, 21 Mar 2019 20:26:50 +0000 Subject: [Tutor] is there a graphics library for common tkinter Button functions? In-Reply-To: Message-ID: On 21 Mar 2019 12:11, Alan Gauld via Tutor wrote: On 21/03/19 00:28, Chris Roy-Smith wrote: > Yes I knew that buttons need a function to do anything. > > I was hoping that the wheel didn't need re-inventing. ===>> A few are available: https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/bitmaps.html. But you could use Material Design: https://material.io/tools/icons/?style=baseline From alan.gauld at yahoo.co.uk Thu Mar 21 16:42:00 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 21 Mar 2019 20:42:00 +0000 Subject: [Tutor] LPTHW ex15 question In-Reply-To: <39e0090ec49b49d68b67219e8dc8dd10@SBDEXDAG08.zmr.zimmer.com> References: <39e0090ec49b49d68b67219e8dc8dd10@SBDEXDAG08.zmr.zimmer.com> Message-ID: On 21/03/19 12:43, Jones, Kayla wrote: > ...getting an error message in powershell that I can't figure out. > I've attached a screenshot to help. Any suggestions would be appreciated. First suggestion is not to send images as attachments since the server strips them out as potential security risks. Instead copy and paste the text into the mail message. Also don't assume we know what the book or exercise are about. You need to tell us. Alan G. From PyTutor at danceswithmice.info Thu Mar 21 16:11:06 2019 From: PyTutor at danceswithmice.info (DL Neil) Date: Fri, 22 Mar 2019 09:11:06 +1300 Subject: [Tutor] LPTHW ex15 question In-Reply-To: <39e0090ec49b49d68b67219e8dc8dd10@SBDEXDAG08.zmr.zimmer.com> References: <39e0090ec49b49d68b67219e8dc8dd10@SBDEXDAG08.zmr.zimmer.com> Message-ID: <941847b4-c6ce-2e01-5d44-41d21c0f965f@DancesWithMice.info> Kayla, On 22/03/19 1:43 AM, Jones, Kayla wrote: > I am working through excersise 15 of LPTHW and am getting an error message in powershell that I can't figure out. I've attached a screenshot to help. Any suggestions would be appreciated. Attachments don't seem to work on the mailing list. Plus, if you copy-paste code (and errmsgs) then we can do the same! (to reproduce the problem/save time coding a fix) -- Regards =dn From mats at wichmann.us Thu Mar 21 17:30:43 2019 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 21 Mar 2019 15:30:43 -0600 Subject: [Tutor] LPTHW ex15 question In-Reply-To: References: <39e0090ec49b49d68b67219e8dc8dd10@SBDEXDAG08.zmr.zimmer.com> Message-ID: <6b4d6b94-8b5e-b896-b820-6b3ac8fece7c@wichmann.us> On 3/21/19 2:42 PM, Alan Gauld via Tutor wrote: > On 21/03/19 12:43, Jones, Kayla wrote: >> ...getting an error message in powershell that I can't figure out. > >> I've attached a screenshot to help.? Any suggestions would be > appreciated. > > First suggestion is not to send images as attachments since > the server strips them out as potential security risks. > > Instead copy and paste the text into the mail message. > > Also don't assume we know what the book or exercise are about. > You need to tell us. I presume the acronym is Learn Python The Hard Way, which many of us don't have (I do not) From nathan-tech at hotmail.com Thu Mar 21 19:00:40 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Thu, 21 Mar 2019 23:00:40 +0000 Subject: [Tutor] Import error, no module named app script Message-ID: Hi guys, So I recently begun using Mac to program in python. For one of the modules I am going to be using, it calls app script in order to send commands to voiceover, which is a screen reader for the MAC. The only problem is, it gives an error about no module named appscript. Indeed, when I go into the python command and try import appscript, it says no module named appscript. I tried pip install apscript, but it said requirement already satissfied. Hoping someone can help. Nate From mats at wichmann.us Thu Mar 21 21:54:54 2019 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 21 Mar 2019 19:54:54 -0600 Subject: [Tutor] Import error, no module named app script In-Reply-To: References: Message-ID: <82dcd607-5e3e-01ad-50d0-b3f22eeb1f7e@wichmann.us> On 3/21/19 5:00 PM, nathan tech wrote: > Hi guys, > So I recently begun using Mac to program in python. > For one of the modules I am going to be using, it calls app script in order to send commands to voiceover, which is a screen reader for the MAC. > The only problem is, it gives an error about no module named appscript. Indeed, when I go into the python command and try import appscript, it says no module named appscript. > I tried pip install apscript, but it said requirement already satissfied. > Hoping someone can help. > Nate You seem to have a disagreement in spelling - apscript vs. appscript. We can't tell if that is just a transcription error on your part. I'd be a bit discouraged by this page: http://appscript.sourceforge.net/ which says rather loudly: Please note that appscript is no longer developed or supported, and its use is not recommended for new projects. From edwardkanja254 at gmail.com Fri Mar 22 01:54:39 2019 From: edwardkanja254 at gmail.com (Edward Kanja) Date: Fri, 22 Mar 2019 08:54:39 +0300 Subject: [Tutor] My problem in simple terms In-Reply-To: References: Message-ID: Greetings, I'm referring to my question i sent earlier, kindly if you have a hint on how i can solve my problem i will really appreciate. After running regular expressions using python my output has lot of square brackets i.e. [][][][][][][][][]. How do i substitute this with empty string so as to have a clear output which i will latter export to an excel file. Thanks a lot. Regards, Kanja Edward. P.O.BOX 1203-00300, NAIROBI. *+254720724793* www.linkedin.com/in/edward-kanja-bba16a106 On Mon, Mar 4, 2019 at 2:44 PM Edward Kanja wrote: > Hi there , > Earlier i had sent an email on how to use re.sub function to eliminate > square brackets. I have simplified the statements. Attached txt file named > unon.Txt has the data im extracting from. The file named code.txt has the > codes I'm using to extract the data.The regular expression works fine but > my output has too many square brackets. How do i do away with them thanks. > > > > Regards, > Kanja Edward. > P.O.BOX 1203-00300, > NAIROBI. > *+254720724793* > www.linkedin.com/in/edward-kanja-bba16a106 > From mats at wichmann.us Fri Mar 22 10:53:39 2019 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 22 Mar 2019 08:53:39 -0600 Subject: [Tutor] My problem in simple terms In-Reply-To: References: Message-ID: <8c103d83-2336-0870-e3ad-943de1456c4c@wichmann.us> On 3/21/19 11:54 PM, Edward Kanja wrote: > Greetings, > I'm referring to my question i sent earlier, kindly if you have a hint on > how i can solve > my problem i will really appreciate. After running regular expressions > using python > my output has lot of square brackets i.e. [][][][][][][][][]. How do i > substitute this with empty > string so as to have a clear output which i will latter export to an excel > file. > Thanks a lot. I think you got the key part of the answer already: you're getting empty lists as matches, which when printed, look like []. Let's try to be more explicit: $ python3 Python 3.7.2 (default, Jan 16 2019, 19:49:22) [GCC 8.2.1 20181215 (Red Hat 8.2.1-6)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import re >>> help(re.findall) Help on function findall in module re: findall(pattern, string, flags=0) Return a list of all non-overlapping matches in the string. If one or more capturing groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result. re.findall *always* returns a list, even if there is no match. If we add more debug prints in your code so it looks like this: import re with open ('unon.txt') as csvfile: for line in csvfile: print("line=", line) index_no=re.findall(r'(\|\s\d{5,8}\s)',line) print("index_no (type %s)" % type(index_no), index_no) names=re.findall(r'(\|[A-Za-z]\w*\s\w*\s\w*\s\w*\s)',line) print("names (type %s)" % type(names), names) #Address=re.findall(r'\|\s([A-Z0-9-,/]\w*\s\w*\s)',line) duty_station=re.findall(r'\|\s[A-Z]*\d{2}\-\w\w\w\w\w\w\w\s',line) print("duty_station (type %s)" % type(duty_station), duty_station) You can easily see what happens as your data is processed - I ran this on your data file and the first few times through looks like this: line= ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- index_no (type ) [] names (type ) [] duty_station (type ) [] line= |Rawzeea NLKPP | VE11-Nairobi | 20002254-MADIZ | 00 | 00 |Regular Scheme B | 15-JAN-2019 To 31-DEC-2019 | No | index_no (type ) [] names (type ) ['|Rawzeea NLKPP '] duty_station (type ) ['| VE11-Nairobi '] line= |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| index_no (type ) [] names (type ) [] duty_station (type ) [] You see each result of re.findall has given you a list, and most are empty. The first and third lines are separators, containing no useful data, and you get no matches at all. The second line provided you with a match for "names" and for "duty_station", but not for "index_no". Your code will need to be prepared for those sorts of outcomes. Just looking at the data, it's table data, presumably from a spreadsheet, but does not really present in a format that is easy to process, because individual lines are not complete. A separator line with all dashes seems to be the marker between complete entries, which then take up 14 lines, including additional marker lines which follow slightly different patterns - they may contain | marks or leading spaces. You will need to decide how regular your table data is and how to work with it, most examples of handling table data assume that one row is a complete entry, so you probably won't find a lot of information on this. In your case I'm looking at line 2 containing 8 fields, line 4 containing 9 fields, line 6 10 fields, and then lines 8-14 being relatively free-form consisting of multiple lines. Is there any chance you can generate your data file in a different way to make it easier to process? From matthew.herzog at gmail.com Fri Mar 22 17:45:54 2019 From: matthew.herzog at gmail.com (Matthew Herzog) Date: Fri, 22 Mar 2019 17:45:54 -0400 Subject: [Tutor] (no subject) Message-ID: I have a Python3 script that reads the first eight characters of every filename in a directory in order to determine whether the file was created before or after 180 days ago based on each file's name. The file names all begin with YYYYMMDD or erased_YYYYMMDD_etc.xls. I can collect all these filenames already. I need to tell my script to ignore any filename that does not conform to the standard eight leading numerical characters, example: 20180922 or erased_20171207_1oIkZf.so. Here is my code. if name.startswith('scrubbed_'): fileDate = datetime.strptime(name[9:17], DATEFMT).date() else: fileDate = datetime.strptime(name[0:8], DATEFMT).date() I need logic to prevent the script from 'choking' on files that don't fit these patterns. The script needs to carry on with its work and forget about non-conformant filenames. Do I need to add code that causes an exception or just add an elif block? Thanks. -- The plural of anecdote is not "data." From alan.gauld at yahoo.co.uk Fri Mar 22 20:37:09 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 23 Mar 2019 00:37:09 +0000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 22/03/19 21:45, Matthew Herzog wrote: > I need to tell my script to ignore any filename that does not conform to > the standard eight leading numerical characters, example: 20180922 or > erased_20171207_1oIkZf.so. Normally we try to dissuade people from using regex when string methods will do but in this case a regex sounds like it might be the best option. A single if statement should suffice if re.match("[0-9]{8}|erased_",fname): Your code here There are other ways to write the regex but the above should be clear... HTH, Alan G. From cs at cskk.id.au Fri Mar 22 21:04:39 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 23 Mar 2019 12:04:39 +1100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <20190323010438.GA78796@cskk.homeip.net> On 22Mar2019 17:45, Matthew Herzog wrote: >I have a Python3 script that reads the first eight characters of every >filename in a directory in order to determine whether the file was created >before or after 180 days ago based on each file's name. The file names all >begin with YYYYMMDD or erased_YYYYMMDD_etc.xls. I can collect all these >filenames already. >I need to tell my script to ignore any filename that does not conform to >the standard eight leading numerical characters, example: 20180922 or >erased_20171207_1oIkZf.so. >Here is my code. > >if name.startswith('scrubbed_'): > fileDate = datetime.strptime(name[9:17], DATEFMT).date() > else: > fileDate = datetime.strptime(name[0:8], DATEFMT).date() > >I need logic to prevent the script from 'choking' on files that don't fit >these patterns. The script needs to carry on with its work and forget about >non-conformant filenames. Do I need to add code that causes an exception or >just add an elif block? Just an elif. Untested example: for name in all_the_filenames: if name.startswith('erased_') and has 8 digits after that: extract date after "erased" ... elif name starts with 8 digits: extract date at the start else: print("skipping", repr(name)) continue ... other stuff using the extracted date ... The "continue" causes execution to go straight to the next loop iteration, effectively skipping the rest of the loop body. An exception won't really do what you want (well, not neatly). Alan's suggestion of a regexp may be a sensible way to test filenames for conformance to your rules. \d{8} matches 8 digits. It doesn't do any tighter validation such as sane year, month or day values: 99999999 would be accepted. Which may be ok, and it should certainly be ok for your first attempt: tighten things up later. Cheers, Cameron Simpson From __peter__ at web.de Sat Mar 23 05:16:41 2019 From: __peter__ at web.de (Peter Otten) Date: Sat, 23 Mar 2019 10:16:41 +0100 Subject: [Tutor] (no subject) References: Message-ID: Matthew Herzog wrote: > I have a Python3 script that reads the first eight characters of every > filename in a directory in order to determine whether the file was created > before or after 180 days ago based on each file's name. The file names all > begin with YYYYMMDD or erased_YYYYMMDD_etc.xls. I can collect all these > filenames already. > I need to tell my script to ignore any filename that does not conform to > the standard eight leading numerical characters, example: 20180922 or > erased_20171207_1oIkZf.so. > Here is my code. > > if name.startswith('scrubbed_'): > fileDate = datetime.strptime(name[9:17], DATEFMT).date() > else: > fileDate = datetime.strptime(name[0:8], DATEFMT).date() > > I need logic to prevent the script from 'choking' on files that don't fit > these patterns. The script needs to carry on with its work and forget > about non-conformant filenames. Do I need to add code that causes an > exception or just add an elif block? Personally I would use a try...except clause because with that you can handle invalid dates like 99999999_etc.xls gracefully. So ERASED = "erased_" def strip_prefix(name): if name.startswith(ERASED): name = name[len(ERASED):] return name def extract_date(name): datestr = strip_prefix(name)[:8] return datetime.datetime.strptime(datestr, DATEFMT).date() for name in ...: try: file_date = extract_date(name) except ValueError: pass else: print(file_date) From ingoogni at gmail.com Sat Mar 23 06:04:19 2019 From: ingoogni at gmail.com (ingo janssen) Date: Sat, 23 Mar 2019 11:04:19 +0100 Subject: [Tutor] properly propagate problems Message-ID: One thing I often struggle with is how to deal with exceptions, especially when I have a chain of functions that use each others output and/or long running processes. As the answer will probably be "it depends" take for example this program flow: open a file and read into BytesIO buffer get a FTP connection from pool send buffer to plantuml.jar in memory FTP server render file to image get image from FTP server push the image onto CherryPy bus push (SSE) the image to web browser def read_file(input_file): try: with open(input_file, 'rb') as f: buffer = io.BytesIO(f.read()) except FileNotFoundError as e: print(e) .... return buffer assume the file is not found, I cannot just kill the whole process. Catching the exception is one thing, but how to deal with it properly, I have to inform the client somehow what went wrong. In this case I could push the error message into the returned buffer and just go from there and the image will show the message. I could also bypass the whole process somehow and push the error message directly on the CherryPy bus. What is wisdom, are there some general rules to follow in such cases ingo From anandwarik at gmail.com Sat Mar 23 09:31:52 2019 From: anandwarik at gmail.com (anand warik) Date: Sat, 23 Mar 2019 19:01:52 +0530 Subject: [Tutor] After virtualenv, how to use ide Message-ID: I had installed Python on Ubuntu 14.04 using Anaconda package long back after failing to install independently for a long time. I was quietly using it's packaged ide Spyder and had no troubles, in fact I love spider more then atom. I recently learned how to setup a virtual environment though, which recommends to never use system wide Python install. But even after activating the virtual environment, I don't know how can i ask the spyder ide to use the new Python directory. Can anyone help me change the setting of Spyder so that it uses different versions? From robertvstepp at gmail.com Sat Mar 23 14:18:20 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 23 Mar 2019 13:18:20 -0500 Subject: [Tutor] After virtualenv, how to use ide In-Reply-To: References: Message-ID: On Sat, Mar 23, 2019 at 12:50 PM anand warik wrote: > > I had installed Python on Ubuntu 14.04 using Anaconda package long back > after failing to install independently for a long time. I was quietly using > it's packaged ide Spyder and had no troubles, in fact I love spider more > then atom. I recently learned how to setup a virtual environment though, > which recommends to never use system wide Python install. But even after > activating the virtual environment, I don't know how can i ask the spyder > ide to use the new Python directory. Can anyone help me change the setting > of Spyder so that it uses different versions? A quick search yields this Stack Overflow thread with what appears to be several useful links embedded: https://stackoverflow.com/questions/30170468/how-to-run-spyder-in-virtual-environment -- boB From mats at wichmann.us Sat Mar 23 18:41:47 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 23 Mar 2019 16:41:47 -0600 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <1951f49d-81e9-2485-45dc-41e89fd62e0b@wichmann.us> On 3/23/19 3:16 AM, Peter Otten wrote: > Personally I would use a try...except clause because with that you can > handle invalid dates like 99999999_etc.xls gracefully. So > > ERASED = "erased_" > > > def strip_prefix(name): > if name.startswith(ERASED): > name = name[len(ERASED):] > return name > > > def extract_date(name): > datestr = strip_prefix(name)[:8] > return datetime.datetime.strptime(datestr, DATEFMT).date() > > > for name in ...: > try: > file_date = extract_date(name) > except ValueError: > pass > else: > print(file_date) I'd endorse this approach as well.. with a DATEFMT of "%Y%m%d", you will get a ValueError for every date string that is not a valid date... which you then just ignore (the "pass"), since that's what you wanted to do. From cs at cskk.id.au Sat Mar 23 19:03:18 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 24 Mar 2019 10:03:18 +1100 Subject: [Tutor] properly propagate problems In-Reply-To: References: Message-ID: <20190323230318.GA78149@cskk.homeip.net> On 23Mar2019 11:04, ingo janssen wrote: >One thing I often struggle with is how to deal with exceptions, >especially when I have a chain of functions that use each others >output and/or long running processes. As the answer will probably be >"it depends" Oh yes! The core rule of thumb is "don't catch an exception which you don't know how to handle", but that is for truly unexpected errors not envisaged by the programmer. Then your programme aborts with a debugging stack trace. Your situation below is more nuanced. Discussion below. >take for example this program flow: > >open a file and read into BytesIO buffer >get a FTP connection from pool >send buffer to plantuml.jar in memory FTP server >render file to image >get image from FTP server >push the image onto CherryPy bus >push (SSE) the image to web browser > >def read_file(input_file): > try: > with open(input_file, 'rb') as f: > buffer = io.BytesIO(f.read()) > except FileNotFoundError as e: > print(e) > .... > return buffer > >assume the file is not found, I cannot just kill the whole process. >Catching the exception is one thing, but how to deal with it properly, >I have to inform the client somehow what went wrong. Given a function like that, I would be inclined to do one of 2 things: A) don't make a policy decision (catching the exception) this close to the failure, instead let the exception out and let the caller handle it: def read_file(input_file): with open(input_file, 'rb') as f: return io.BytesIO(f.read()) filename = "foo" try: buffer = read_file(filename) except OSError as e: error("could not load %r: %s", filename, e) ... failure action, maybe return from the function ... ... proceed with buffer ... This leaves the policy decision with the calling code, which may have a better idea about what is suitable. For example, you might pass some useful response to your web client here. The low level function read_file() doesn't know that it is part of a web service. The handy thing about exceptions is that you can push that policy decision quite a long way out. Provided the outer layer where you decide to catch the exception knows that this involved accessing a file you can put that try/except quite a long way out and still produce a sensible looking error response. Also, the further out the policy try/except lives, the simpler the inner functions can be because they don't need to handle failure - they can be written for success provided that failures raise exceptions, making them _much_ simpler and easier to maintain. And with far fewer policy decisions! The flip side to this is that there is a limit to how far out in the call chain this try/except can sensibly happen: if you're far enough out that the catching code _doesn't_ know that there was a file read involved, the error message becomes more vague (although you still have the exception instance itself with the low level detail). B) to return None on failure: def read_file(input_file): try: with open(input_file, 'rb') as f: return io.BytesIO(f.read()) except OSError as e: error( "read_file(%r): could not read input file: %s", input_file, e) return None None is a useful sentinel value for failure. Note that sometimes you will want something else if None is meaningful return value in ordinary circumstances. Then your calling code can handle this without exceptions: buffer = read_file("foo") if buffer is None: ... return nice message to web client ... else: ... process the image ... However, it does mean that this handling has to happen right at the call to read_file. That can be fine, but might be inconvenient. Finally, some related points: I find it useful to distinguish "mechanism" and "policy". In my ideal world a programme is at least 90% mechanism with a thin layer of policy outside it. Here "policy" is what might be termed "business logic" or "application logic" in some circumstances: what to do to achieve the high level goal. The high level is where you decide how to behave in various circumstances. This has a few advantages: almost all low level code is mechanism: it has a well defined, usually simple, purpose. By having almost all failures raise an exception you can make the low level functions very simple: do A then B then C until success, where you return the result; raise exceptions when things go wrong (failure to open files, invalid input parameters, what have you). This produces what I tend to call "white list" code: code which only returns a result when all the required operations succeed. This is option (A) above, and makes for very simple inner functions. For option (B) "return None on failure", this is where we decide that specific failures are in fact valid execution paths, and None is a valid function return, indicating some kind of null result. You might still raise exceptions of various types for invalid input in this case; the None is only for a well defined expected non-answer. Regarding uncaught exceptions: As you say, you don't want your whole app to abort. So while you may catch specific exception types at some inner layer, you might want to catch _all_ exceptions at the very outermost layer and log them (with a stack trace), but not abort. So: try: ... process client request ... except Exception as e: # log exception and stack trace to the application log error("handler failed: %s", e, exc_info=True) return 500 series web response to client here ... This is one of those situaions where you might use the normally reviled "catch all exceptions" anti-pattern: at the outermost layer of some kind of service programme such as a daemon or web app handling requests: report the exception and carry on with the application. Remember the Zen: errors should not pass silently. Always log something when you catch an exception. Note that a primary reason to hate "catch all" is that such code often then proceeds to do more work with the bogus results. In a daemon or a web app, you're aborting _that request_. Any further work is shiny and new from a new request, not continuing with nonsensical data left around by a catch-all. Fortunately web frameworks like Flask or CherryPy usually embed such a catch-everything in their handler logic, outside you own code (after all, what if you own catch-everything was buggy?) So you don't normally need to write one of these things yourself. Which is good really, most of the time - they are a recipe for accidentally hiding errors. Let the framework do that one - it has been debugged for you. Another issue is the distinction between what to log and what to show the client. You usually DO NOT want to let the nitty gritty of the exception get to the end user: that way lies accidental leaking of credentials or private implementation details. So log details, but return fairly bland information to the client. Try to write your code so that this is the default behaviour. Again, web frameworks generally do just this in their outermost catch-all handler: only if you turn on some kind of DEBUG mode does it splurge private stuff over the web page for ease of debugging in development. Finally, I'm sure you've thought to yourself: if I catch an exception a long way from where it happened, won't the exception message lack all sorts of useful context about what happened? How useful is a log entry like this (from the outermost "OCR the document" level): error("OCR failed: %s", e) producing: OCR failed: permission denied because of a permission issue on a specific (but here, unnamed) file? My own solution to this issue is my cs.pfx module (you can install this with "pip install cs.pfx"). This provides a context manager named Pfx which adorns exceptions with call stack information, totally under your control. It also has various .error and .warning etc methods which produce prefixed log messages. Example: from cs.pfx import Pfx def read_file(input_file): with Pfx("read_file(%r)", input_file): with open(input_file, 'rb') as f: return io.BytesIO(f.read()) and outer calls might look like: def produce_image(image_name): with Pfx("produce_image(%r)", image_name): filename = path_to_image_file(image_name) buffer = read_file(filename) ... do stuff with the buffer ... If the inner open fails, the exception message, which is originally like this: [Errno 2] No such file or directory: 'fffff' becomes: produce_image('image_name'): read_file("/path/to/image_name.png"): [Errno 2] No such file or directory: '/path/to/image_name.png' How much context you get depends on where you put the "with Pfx(...):" statements. It also furthers simple code, because you no longer need to pepper your own exceptions with annoying repetitive context, just the core message: def read_file(input_file): with Pfx("read_file(%r)", input_file): if not input_file.startswith('/'): raise ValueError("must be an absolute path") with open(input_file, 'rb') as f: return io.BytesIO(f.read()) Because of the Pfx the ValueError gets the input_file value in question prefixed automatically, so you don't need to include it in your raise statement. Hoping all this helps. Short takeaway: decide what's mechanism and what is policy, and try to put policy further out in higher level code. Cheers, Cameron Simpson Go not to the elves for counsel, for they will say both no and yes. - Frodo, The Fellowship of the Ring From robertvstepp at gmail.com Sat Mar 23 23:15:22 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 23 Mar 2019 22:15:22 -0500 Subject: [Tutor] How to avoid "UnboundLocalError: local variable 'goal_year' referenced before assignment"? Message-ID: I have just written a small program earlier today to allow the user (me) to enter a date by which I wish to finish reading a book (= massive programming-related book) and calculate how many pages I need to read each day (starting today) in order to finish the book by the target date. Looking over my code I found that I was repeating a fairly regular pattern in collecting the user input, so I thought I would *improve* my working program by removing the duplicated code into a single function. So I came up with: from datetime import date def get_input(greeting_msg, identifier, input_prompt, date_value_err_ck, err_msg, conditions): """ ??? """ if greeting_msg: print(greeting_msg) while True: try: identifier = int(input(input_prompt)) if date_value_err_ck: date(*date_value_err_ck) except ValueError: print(err_msg) continue for (condition, condition_msg) in conditions: if condition: print(condition_msg) break else: return identifier When I attempt to use this function with: goal_year_params = { 'greeting_msg': "Please enter the date by which you wish to attain" " your goal.\n", 'identifier': 'goal_year', 'input_prompt': "Enter year of your goal as an integer: ", 'date_value_err_ck': (goal_year, 1, 1), 'err_msg': "That is not a valid year. Please try again.", 'conditions': [ ('goal_year < date.today().year', "Have you invented a time machine? If not, please enter a" " year that makes more sense!"), ('goal_year >= date.today().year + 100', "Have you discovered the secret to eternal life? And how" " long is this book anyway? Please enter a year that" " makes more sense.")]} goal_year = get_input(**goal_year_params) I get the following traceback: Traceback (most recent call last): File "pages_per_day.py", line 250, in start_pgm() File "pages_per_day.py", line 246, in start_pgm goal_date_obj, pages_read, total_pages_to_read = get_inputs() File "pages_per_day.py", line 63, in get_inputs 'date_value_err_ck': (goal_year, 1, 1), UnboundLocalError: local variable 'goal_year' referenced before assignment I understand this result, but cannot come up with a way to implement my desired DRY strategy as I am stuck on how to get around this "local variable ... referenced before assignment" issue. On subsequent passes "goal_year" will become "goal_month" and then "goal_day" as the user needs to input all three of these numbers. Is there a way to accomplish my goal or am I attempting to be too clever? -- boB From cs at cskk.id.au Sun Mar 24 01:22:28 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 24 Mar 2019 16:22:28 +1100 Subject: [Tutor] How to avoid "UnboundLocalError: local variable 'goal_year' referenced before assignment"? In-Reply-To: References: Message-ID: <20190324052228.GA86716@cskk.homeip.net> Discussion below your post here, since I feel I should quote it all: On 23Mar2019 22:15, boB Stepp wrote: >I have just written a small program earlier today to allow the user >(me) to enter a date by which I wish to finish reading a book (= >massive programming-related book) and calculate how many pages I need >to read each day (starting today) in order to finish the book by the >target date. Looking over my code I found that I was repeating a >fairly regular pattern in collecting the user input, so I thought I >would *improve* my working program by removing the duplicated code >into a single function. So I came up with: > >from datetime import date > >def get_input(greeting_msg, identifier, input_prompt, date_value_err_ck, > err_msg, conditions): > """ ??? """ > > if greeting_msg: > print(greeting_msg) > > while True: > try: > identifier = int(input(input_prompt)) > if date_value_err_ck: > date(*date_value_err_ck) > except ValueError: > print(err_msg) > continue > for (condition, condition_msg) in conditions: > if condition: > print(condition_msg) > break > else: > return identifier > >When I attempt to use this function with: > >goal_year_params = { > 'greeting_msg': "Please enter the date by which you wish to attain" > " your goal.\n", > 'identifier': 'goal_year', > 'input_prompt': "Enter year of your goal as an integer: ", > 'date_value_err_ck': (goal_year, 1, 1), > 'err_msg': "That is not a valid year. Please try again.", > 'conditions': [ > ('goal_year < date.today().year', > "Have you invented a time machine? If not, please enter a" > " year that makes more sense!"), > ('goal_year >= date.today().year + 100', > "Have you discovered the secret to eternal life? And how" > " long is this book anyway? Please enter a year that" > " makes more sense.")]} >goal_year = get_input(**goal_year_params) > >I get the following traceback: > >Traceback (most recent call last): > File "pages_per_day.py", line 250, in > start_pgm() > File "pages_per_day.py", line 246, in start_pgm > goal_date_obj, pages_read, total_pages_to_read = get_inputs() > File "pages_per_day.py", line 63, in get_inputs > 'date_value_err_ck': (goal_year, 1, 1), >UnboundLocalError: local variable 'goal_year' referenced before assignment > >I understand this result, but cannot come up with a way to implement >my desired DRY strategy as I am stuck on how to get around this "local >variable ... referenced before assignment" issue. On subsequent >passes "goal_year" will become "goal_month" and then "goal_day" as the >user needs to input all three of these numbers. Is there a way to >accomplish my goal or am I attempting to be too clever? You're not attemnpting to be too clever, but you are making some basic errors. For example, you can't just run a string like 'goal_year < date.today).year' and you shouldn't be trying i.e. do _not_ reach for the eval() function. Let's look at your get_input() function. It basicly loops until you get a value you're happy with, and returns that value. Or it would if it were correct. Let's examine the main loop body: try: identifier = int(input(input_prompt)) if date_value_err_ck: date(*date_value_err_ck) except ValueError: print(err_msg) continue for (condition, condition_msg) in conditions: if condition: print(condition_msg) break else: return identifier To start with, you have confusion in the code bwteen the name you're intending to use for the input value (the "identifier" parameter) and the value you're reading from the user. You go: identifier = int(input(input_prompt)) That immediatey destroys the name you passed in as a parameter. Instead, use a distinct variable for the input value. Let's be imaginitive and call it "value": value = int(input(input_prompt)) and at the bottom of the get_input() function you should: return value Then you try to create a date from that value (though you don't save it anywhere). I presume you want to use the datetime.date() constructor. So: # at the start of your programme import datetime then in the function: date = datetime.date(*date_value_err_ck) I think your plan is that datetime.date() will also raise a ValueError for a bad year number in "value". So you want, in fact, to call: date = datetime.date(goal_year, 1, 1) And the construction of that will be different on your subsequent calls where you have goal_year and are inputting goal_month, and so on. Your date_value_err_ck comes from your dict at the bottom of the programme. However, it is in the natural of things that you're computing the content of that dict _before_ you call get_input(). Let me introduce you to the lambda. You _don't_ want to compute (goal_year,1,1) beofre the call to get_input (and in fact doing so is the immediate cause of your error message). What you actaully want is to compute that value _during_ the get_input function, _after_ you've read a value from the user. So you should not be passing a tuple (goal_year,1,1). You should be passing a function which computes that tuple from the input value. Let me suggest this dict entry for the bottom dict: 'date_constructor': lambda goal_year: datetime.date(goal_year, 1, 1), instead of your 'date_value_err_ck' entry. The lambda is shorthand for a single function definition: it gets you a function which accepts a single parameter "goal_year" and returns a datetime.date result. Note that here, at the bottom of the programme, it DOES NOT CALL the function. It just makes the function. The longer way to do this might look like this: def date_from_year(goal_year): return datetime.date(goal_year, 1, 1) goal_year_params = { .... 'date_constructor': date_from_year, .... } The lambda is just a single line function definition, and doesn't get a function name. The above could also be written: date_from_year = lambda goal_year: datetime.date(goal_year, 1, 1) goal_year_params = { .... 'date_constructor': date_from_year, .... } much like the one liner I started with. So your get_input name now accepts a "date_constructor" parameter and you would change the input step from this: try: identifier = int(input(input_prompt)) if date_value_err_ck: date(*date_value_err_ck) except ValueError: to this: try: value = int(input(input_prompt)) date = date_constructor(value) except ValueError: So you're passing in a function to make the date, and only calling it once you've read in the value. This solves your use of goal_year before it is defined in 2 ways: it doesn't use the value before the call to get_input and also the name "goal_year" in the constructor function is local to that function - it may as well be called "year". Now to your conditions. You have: 'conditions': [ ('goal_year < date.today().year', "Have you invented a time machine? If not, please enter a" " year that makes more sense!"), ('goal_year >= date.today().year + 100', "Have you discovered the secret to eternal life? And how" " long is this book anyway? Please enter a year that" " makes more sense.")]} These seem to be tests for bad values (personally I tend to write tests for good values, but this is an arbitrary choice). These should also be written as lambdas: 'conditions': [ (lambda goal_year: goal_year < date.today().year, "Have you invented a time machine? If not, please enter a" " year that makes more sense!"), (lambda goal_year: goal_year >= date.today().year + 100, "Have you discovered the secret to eternal life? And how" " long is this book anyway? Please enter a year that" " makes more sense.")]} so that now the conditions are functions, not strings. (And you want to call datetime.date, not date, because we're using a variable "date" locally anyway.) Then the get_input function test becomes: for condition, condition_msg in conditions: if condition(value): print(condition_msg) break i.e. call each function with the input value. But wait, there's more! After you have phase 1 complete (inputting goal_year) you then want goal_month. Let me introduce you to the closure: For the goal_year you have the year-based constructor using only the input value: 'date_constructor': lambda goal_year: datetime.date(goal_year, 1, 1), in your setup dict. For the goal_month you want both the goal_year and the input value. So... 'date_constructor': lambda goal_month: datetime.date(goal_year, goal_month, 1), But, I hear you ask, I'm not passing in the goal_year to the get_input() for the month value! Indeed, your code will look like this: goal_year_params = { .... 'date_constructor': lambda goal_year: datetime.date(goal_year, 1, 1), .... } goal_year = get_input(**goal_year_params) goal_month_params = { .... 'date_constructor': lambda goal_month: datetime.date(goal_year, goal_month, 1), .... } goal_month = get_input(**goal_month_params) That's your plan, yes? Well, when you define the goal_month constructor lambda function, goal_year _is_ a local variable. And _because_ it is not a parameter of the lambda, the lambda finds it in the scope where it was defined: the scope at the bottom of your programme. This is called a "closure": functions has access to the scope they are define in for identifiers not locally defined (eg in the parameters or the function local variables - which are those assigned to in the code). This means you get to pass in a lambda which still just takes one parameter (the input value, which it will use for the month) and still have access to the goal_year from the place the lambda was defined. A final note regarding "what is a local variable"? A local variable is one which is assigned to in the function code. Looks: x = 1 y = 2 z = 3 def func(x): y = 5 return x + y + z In "func", "y" is local because of the "y = 5" line. "x" is also local because it is a function parameter (which amounts to assigning a value to "x" when the function is called. However, "z" comes from the scrope where the function was defined. Cheers, Cameron Simpson From robertvstepp at gmail.com Sun Mar 24 02:54:11 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 24 Mar 2019 01:54:11 -0500 Subject: [Tutor] How to avoid "UnboundLocalError: local variable 'goal_year' referenced before assignment"? In-Reply-To: <20190324052228.GA86716@cskk.homeip.net> References: <20190324052228.GA86716@cskk.homeip.net> Message-ID: Ah! After another long break from coding, I resume a pattern of one step forward, who knows how many steps back, as I forget what I once remembered and understood. It is the wee morning hours where I am at, so I don't think I will make it through everything, but I will make a start... On Sun, Mar 24, 2019 at 12:22 AM Cameron Simpson wrote: > > Discussion below your post here, since I feel I should quote it all: > > On 23Mar2019 22:15, boB Stepp wrote: > >Traceback (most recent call last): > > File "pages_per_day.py", line 250, in > > start_pgm() > > File "pages_per_day.py", line 246, in start_pgm > > goal_date_obj, pages_read, total_pages_to_read = get_inputs() > > File "pages_per_day.py", line 63, in get_inputs > > 'date_value_err_ck': (goal_year, 1, 1), > >UnboundLocalError: local variable 'goal_year' referenced before assignment > > > >I understand this result, but cannot come up with a way to implement > >my desired DRY strategy as I am stuck on how to get around this "local > >variable ... referenced before assignment" issue. On subsequent > >passes "goal_year" will become "goal_month" and then "goal_day" as the > >user needs to input all three of these numbers. Is there a way to > >accomplish my goal or am I attempting to be too clever? > > You're not attemnpting to be too clever, but you are making some basic > errors. For example, you can't just run a string like 'goal_year < > date.today).year' and you shouldn't be trying i.e. do _not_ reach for > the eval() function. Too late! About the time your message hit my inbox, I just finished a working replacement after pulling the the eval() trigger. I found a non-eval() workaround for dealing with date(*date_value_err_ck), but could not come up with anything better than using eval(condition.format(user_input)) to replace the "if condition:", where the embedded "{0}" in "condition" from the calling code is being used to pass in the actual user input value. I imagine a clever user (other than myself) could now wreak all sorts of havoc! I totally got rid of "identifier" as an argument. > Let's look at your get_input() function. It basicly loops until you get > a value you're happy with, and returns that value. Or it would if it > were correct. Let's examine the main loop body: > > try: > identifier = int(input(input_prompt)) > if date_value_err_ck: > date(*date_value_err_ck) > except ValueError: > print(err_msg) > continue > for (condition, condition_msg) in conditions: > if condition: > print(condition_msg) > break > else: > return identifier > > To start with, you have confusion in the code bwteen the name you're > intending to use for the input value (the "identifier" parameter) and > the value you're reading from the user. You go: It wasn't really confusion on my part. What I *wanted* to do was to substitute a more specific identifier from the calling code for the generic "identifier" in the get_input() function, and use it both for the actual user input and to fill in that value wherever I need it. But I could not find a way to accomplish this and just left my question at the last error state. But I guess this is not a doable thing. > identifier = int(input(input_prompt)) > > That immediatey destroys the name you passed in as a parameter. Instead, > use a distinct variable for the input value. Let's be imaginitive and > call it "value": Or "user_input", which I finally wound up with. > Then you try to create a date from that value (though you don't save it > anywhere). I presume you want to use the datetime.date() constructor. > So: > > # at the start of your programme > import datetime > > then in the function: > > date = datetime.date(*date_value_err_ck) > > I think your plan is that datetime.date() will also raise a ValueError > for a bad year number in "value". So you want, in fact, to call: That is, in fact, the plan. Since I only want to check for a bad year, then a bad month, and finally, a bad day of the month, I saw no point in binding a name to the date object. In the part of the code I did not include, I create a date object from date(goal_year, goal_month, goal_day) to use in the rest of the program. > Let me introduce you to the lambda. Or, in my case, "reintroduce" me to the lambda. It has been probably at least a couple of years since I had need of this and I had totally forgotten about it! There are those backward steps again! I guess I will have to head for bed now and look more closely at the rest later today. But I can see that this totally eliminates any need for eval() -- which I did not want to use anyway -- and solves what I was struggling so much with. As for closures, I will have to read that portion *most* carefully later. I know I have asked some questions in the past where these were brought up as answers, but this did not stick in my brain at all. Thanks, Cameron, for the time you took for your in-depth help!!! G'night, boB From ingoogni at gmail.com Sun Mar 24 03:13:08 2019 From: ingoogni at gmail.com (ingo janssen) Date: Sun, 24 Mar 2019 08:13:08 +0100 Subject: [Tutor] properly propagate problems In-Reply-To: <20190323230318.GA78149@cskk.homeip.net> References: <20190323230318.GA78149@cskk.homeip.net> Message-ID: <6d9b4b58-3427-e0e3-eed4-b5132f00e1c2@gmail.com> On 24/03/2019 00:03, Cameron Simpson wrote: > Short takeaway: decide what's mechanism and what is policy, and try to > put policy further out in higher level code. That, Cameron, was a very insightful answer and an eye opener, as I try to 'fix things' as early as possible. It also answered the question that I thought of right after posting, what if it is a library and knows nothing about its use? I'll certainty have a look at Pfx. Thank you Cameron, ingo From anandwarik at gmail.com Sun Mar 24 03:44:36 2019 From: anandwarik at gmail.com (anand warik) Date: Sun, 24 Mar 2019 13:14:36 +0530 Subject: [Tutor] After virtualenv, how to use ide In-Reply-To: References: Message-ID: I gave up on Spyder and shifted to Atom, which seems easy to port to different versions of python created by virtual environments. But now facing a new problem. I have installed a package named autocomplete-python on atom but it doesn't seem to work. I checked few links on stackoverflow as well but still the problem persists. Can someone help me with this? On Sat, Mar 23, 2019 at 11:50 PM boB Stepp wrote: > On Sat, Mar 23, 2019 at 12:50 PM anand warik wrote: > > > > I had installed Python on Ubuntu 14.04 using Anaconda package long back > > after failing to install independently for a long time. I was quietly > using > > it's packaged ide Spyder and had no troubles, in fact I love spider more > > then atom. I recently learned how to setup a virtual environment though, > > which recommends to never use system wide Python install. But even after > > activating the virtual environment, I don't know how can i ask the spyder > > ide to use the new Python directory. Can anyone help me change the > setting > > of Spyder so that it uses different versions? > > A quick search yields this Stack Overflow thread with what appears to > be several useful links embedded: > > https://stackoverflow.com/questions/30170468/how-to-run-spyder-in-virtual-environment > -- > boB > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- *???? ????? * From robertvstepp at gmail.com Sun Mar 24 14:32:41 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 24 Mar 2019 13:32:41 -0500 Subject: [Tutor] [OT] Problem with auto-complete package installation in Atom [Was: Re: After virtualenv, how to use ide] In-Reply-To: References: Message-ID: On Sun, Mar 24, 2019 at 2:45 AM anand warik wrote: > > I gave up on Spyder and shifted to Atom, which seems easy to port to different versions of python created by virtual environments. But now facing a new problem. I have installed a package named autocomplete-python on atom but it doesn't seem to work. I checked few links on stackoverflow as well but still the problem persists. Can someone help me with this? This is actually a question about the Atom code editor, not one about Python, so it is really off-topic for this forum as well as unrelated to your originating topic. However, Tutor is a friendly place, so if someone knows much about Atom, I am sure they will try to help. But I must say, you did not give anyone much information to enable such help to happen. "... but it doesn't seem to work..." is an extraordinarily vague problem description! So you may wish to flesh out the details of your exact problem if you seek help here or elsewhere. But that is not what I wish to discuss. I am going to assume that you are not a professional programmer. If that is incorrect, I profusely apologize in advance! But that is okay. I am not a professional nor expert programmer myself. Because of this, I recall agonizing over the choice of code editor, or, possibly even worse, deciding whether to use an IDE or editor. This is a difficult decision about which many wax poetic and express their opinions with great emotion. If you wish to be amused, just do a search for Emacs vs. Vim editor wars! So the reality of it, whether you are a non-expert like me or a professional coder, choice of editor/IDE is an intensely personal choice. You will spend many hours/days/weeks/months/years/... of your life staring at your choice of editor/IDE, so you should pick something that works well for you. But editor hopping is not the answer! Instead, I would advise you to carefully evaluate your actual *needs*, and cull that list down to something that satisfies your sense of aesthetics. In my case I write some programs to make my life easier at work (*not* programming related). While there my time is split between PCs that are Windows-based and thin-clients connecting to a Solaris-based server. Whatever editor I use at work needs to be available in both environments, and the Solaris one does *not* allow me to install any new software, so it became my primary determinant of editor. When it came down to it, the only substantial code editing support was limited to vi until recently when our servers were upgraded with a newer version of Solaris (Yay!) which came with Vim/gVim. On the other hand, when I do my actual "fun" programming and learning, I mostly do that at home where I can install anything I like. So initially I tried all kinds of editors and IDEs and wasted quite a lot of time! And I did not get comfortable with anything. So I finally decided to just stick with vi/Vim/gVim/Neovim. The point is to stick with something long enough that you can realize its full potential, or, at least work your way in that direction. So, research your needs, both current and future. From that short list select what you will enjoy interacting with on a daily basis. BTW, back to Atom: Your question(s) about it would probably get better results on whatever forums are devoted to it. I am sure there will be at least one if not more. Cheers! boB From robertvstepp at gmail.com Sun Mar 24 15:33:25 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 24 Mar 2019 14:33:25 -0500 Subject: [Tutor] How to avoid "UnboundLocalError: local variable 'goal_year' referenced before assignment"? In-Reply-To: <20190324052228.GA86716@cskk.homeip.net> References: <20190324052228.GA86716@cskk.homeip.net> Message-ID: Oh, happy day! eval() has been expunged from my program!! I will now continue from where I left off earlier. On Sun, Mar 24, 2019 at 12:22 AM Cameron Simpson wrote: > > On 23Mar2019 22:15, boB Stepp wrote: > > The lambda is just a single line function definition, and doesn't get a > function name. > So your get_input name now accepts a "date_constructor" parameter and > you would change the input step from this: > > try: > identifier = int(input(input_prompt)) > if date_value_err_ck: > date(*date_value_err_ck) > except ValueError: > > to this: > > try: > value = int(input(input_prompt)) > date = date_constructor(value) > except ValueError: You could not know this from the code I showed (As I pared it down to illustrate my problem.), but I am also using get_input() to pull in from the user non-date values, too, but they are also all integers: pages_read and total_pages_to_read. Because of this, for these latter two items, "date_value_err_ck" will be assigned "None", which is why I have the "if date_value_err_ck:". > So you're passing in a function to make the date, and only calling it > once you've read in the value. > > This solves your use of goal_year before it is defined in 2 ways: it > doesn't use the value before the call to get_input and also the name > "goal_year" in the constructor function is local to that function - it > may as well be called "year". > > Now to your conditions. You have: > > 'conditions': [ > ('goal_year < date.today().year', > "Have you invented a time machine? If not, please enter a" > " year that makes more sense!"), > ('goal_year >= date.today().year + 100', > "Have you discovered the secret to eternal life? And how" > " long is this book anyway? Please enter a year that" > " makes more sense.")]} > > These seem to be tests for bad values (personally I tend to write tests > for good values, but this is an arbitrary choice). These should also be > written as lambdas: I just like writing corny error messages in response to bad values. ~(:>)) > But wait, there's more! > > After you have phase 1 complete (inputting goal_year) you then want > goal_month. > > Let me introduce you to the closure: > > For the goal_year you have the year-based constructor using only the > input value: > > 'date_constructor': lambda goal_year: datetime.date(goal_year, 1, 1), > > in your setup dict. For the goal_month you want both the goal_year and > the input value. So... > > 'date_constructor': lambda goal_month: datetime.date(goal_year, goal_month, 1), > > But, I hear you ask, I'm not passing in the goal_year to the get_input() > for the month value! Indeed, your code will look like this: > > goal_year_params = { > .... > 'date_constructor': lambda goal_year: datetime.date(goal_year, 1, 1), > .... > } > goal_year = get_input(**goal_year_params) > goal_month_params = { > .... > 'date_constructor': lambda goal_month: datetime.date(goal_year, goal_month, 1), > .... > } > goal_month = get_input(**goal_month_params) > > That's your plan, yes? You understand me perfectly! > Well, when you define the goal_month constructor lambda function, > goal_year _is_ a local variable. And _because_ it is not a parameter of > the lambda, the lambda finds it in the scope where it was defined: the > scope at the bottom of your programme. > > This is called a "closure": functions has access to the scope they are > define in for identifiers not locally defined (eg in the parameters or > the function local variables - which are those assigned to in the code). This is so cool and useful! I recall seeing this mentioned for nested functions, but did not know it worked for lambdas as well. This has been incredibly helpful and educational! Many thanks!! -- boB From alan.gauld at yahoo.co.uk Sun Mar 24 15:53:28 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 24 Mar 2019 19:53:28 +0000 Subject: [Tutor] [OT] Problem with auto-complete package installation in Atom [Was: Re: After virtualenv, how to use ide] In-Reply-To: References: Message-ID: On 24/03/19 18:32, boB Stepp wrote: > apologize in advance! But that is okay. I am not a professional nor > expert programmer myself. Because of this, I recall agonizing over > the choice of code editor, or, possibly even worse, deciding whether > to use an IDE or editor. This is a difficult decision about which > many wax poetic and express their opinions with great emotion. But for professional programmers is a mere detail of very little import. Its almost as irrelevant as the choice of programming language. A professional uses the tools [sh]e is given(or dictated). Sure we all have our favourites (personally I favour a Unix-like OS using emacs/vim or Netbeans) but we use what we must. But in my time I've alo used - CP/M and ed (or wordstar) {assembler, C, Pascal, Logo} - OS/9 and scred, (assembler, C, BASIC} - IBM MVS and ISPF, (COBOL, JCL, SQL} - Windows and VS (or Borland IDE.) {ASM, VB, Pascal, C++, Java) - DEC VAX/VMS and Edit/TPU {C, C++, DCL} They all do the basics and allow you to enter and modify code. Editor (and language) wars are a sign of amateurs at work (or more accurately not "at work"!). Mostly you'll find they are conducted by "experts" who turn out to have less than 10 years experience... in other words, beginners. Sorry for the rant but I get very depressed by some of the discussion that happens on programming fora, especially when there are so many genuinely interesting things to talk about! Alan G. A programmer for 45 years and still learning... From PyTutor at DancesWithMice.info Sun Mar 24 16:26:41 2019 From: PyTutor at DancesWithMice.info (David L Neil) Date: Mon, 25 Mar 2019 09:26:41 +1300 Subject: [Tutor] [OT] Problem with auto-complete package installation in Atom [Was: Re: After virtualenv, how to use ide] In-Reply-To: References: Message-ID: <3c840dad-f0cc-4ae6-38c5-f1799070f62d@DancesWithMice.info> >> apologize in advance!? But that is okay.? I am not a professional nor >> expert programmer myself.? Because of this, I recall agonizing over >> the choice of code editor, or, possibly even worse, deciding whether >> to use an IDE or editor.? This is a difficult decision about which >> many wax poetic and express their opinions with great emotion. > > But for professional programmers is a mere detail of very > little import. Its almost as irrelevant as the choice of > programming language. A professional uses the tools [sh]e > is given(or dictated). Sure we all have our favourites > (personally I favour a Unix-like OS using emacs/vim or Netbeans) > but we use what we must. +1 The last time I had this conversation, in person, we were working on some HTML5 training and a colleague wanted to know how/why I edited something. I was using PyCharm, which he'd not seen before, and used a facility he had never seen, but which was also a little longer than one he would have used in his chosen-tool. Accordingly, he sought to 'educate' me... Having used many editors over the years, cough, mumble, decades; I suggest choosing one AND sticking with it for a long time, eg one year. Maybe as part of your new year celebrations you might choose another. In the meantime, I find it takes a while to stop trying to make 'this editor' look like the last one; and to learn how to best use the powers (and avoid the foibles) of this year's tool to actually improve one's productivity.. The fact remains, many of 'the big names' today offer many more facilities that one person will ever want to use, especially if working exclusively in one language! Perhaps the 80/20 rule applies? eg I've been using SublimeText for the last six+ months, and if I've more than scratched the surface, I'll be surprised. Even in my slightly-expanded-upon-the-vanilla installation, there are still lint-ing codes and sundry Python helps that bemuse me (and, obviously, I've simply ignored). > But in my time I've alo used > - CP/M and ed (or wordstar) {assembler, C, Pascal, Logo} > - OS/9 and scred, (assembler, C, BASIC} > - IBM MVS and ISPF, (COBOL, JCL, SQL} > - Windows and VS (or Borland IDE.) {ASM, VB, Pascal, C++, Java) > - DEC VAX/VMS and Edit/TPU {C, C++, DCL} How about MP/M? OS/360 etc before MVS? Extra respect for avoiding the PC-DOS/MS-DOS fandango. I find that the older I become, the longer it takes to re-wind all this nostalgia. I can't even remember the name of the editor I used on my slide-rule PC! > Sorry for the rant but I get very depressed by some of > the discussion that happens on programming fora, especially > when there are so many genuinely interesting things to > talk about! Agreed that it tends to be a newcomer's topic; and that one tends to use the same tools as the rest of the team; but it is an important consideration - I've never forgotten the first time I was asked (um, instructed) to change every instance of "xyz" in each source-code file in ... whereas today most professional-grade editors will achieve such in a single Find-Replace! (am not sure if that can be achieved with today's Windows-NotePad, but equally, I'd struggle to find a good reason for such an action 'today' and in Python!) -- Regards =dn From cs at cskk.id.au Sun Mar 24 18:04:21 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 25 Mar 2019 09:04:21 +1100 Subject: [Tutor] How to avoid "UnboundLocalError: local variable 'goal_year' referenced before assignment"? In-Reply-To: References: Message-ID: <20190324220421.GA36957@cskk.homeip.net> On 24Mar2019 14:33, boB Stepp wrote: >> On 23Mar2019 22:15, boB Stepp wrote: >> >> The lambda is just a single line function definition, and doesn't get a >> function name. > >> So your get_input name now accepts a "date_constructor" parameter and >> you would change the input step from this: >> >> try: >> identifier = int(input(input_prompt)) >> if date_value_err_ck: >> date(*date_value_err_ck) >> except ValueError: >> >> to this: >> >> try: >> value = int(input(input_prompt)) >> date = date_constructor(value) >> except ValueError: > >You could not know this from the code I showed (As I pared it down to >illustrate my problem.), but I am also using get_input() to pull in >from the user non-date values, too, but they are also all integers: >pages_read and total_pages_to_read. Because of this, for these latter >two items, "date_value_err_ck" will be assigned "None", which is why I >have the "if date_value_err_ck:". Ok. So I realised later that you're not using the date from date_constructor; you're just calling it to see if it raises ValueError as a sanity check. Why not make that a condition, and drop date_value_err_ck altogether? 'conditions': [ (lambda goal_year: datetime.date(goal_year, 1, 1), "This doesn't look like a valid date year to datetime.date()."), and make the condition checking code more robust: for bad_condition, message in conditions: try: badness = bad_condition(input_value) except ValueError as e: print("Invalid goal_year, cannot even run the condition check! " + str(e)) else: if badness: print(message) That reduces your opening try/except to just checking that the input is a valid int, a lot simpler. >> Well, when you define the goal_month constructor lambda function, >> goal_year _is_ a local variable. And _because_ it is not a parameter of >> the lambda, the lambda finds it in the scope where it was defined: the >> scope at the bottom of your programme. >> >> This is called a "closure": functions has access to the scope they are >> define in for identifiers not locally defined (eg in the parameters or >> the function local variables - which are those assigned to in the code). > >This is so cool and useful! I recall seeing this mentioned for nested >functions, but did not know it worked for lambdas as well. Lambda are functions. Same rules. Cheers, Cameron Simpson From robertvstepp at gmail.com Sun Mar 24 22:26:06 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 24 Mar 2019 21:26:06 -0500 Subject: [Tutor] [OT] Problem with auto-complete package installation in Atom [Was: Re: After virtualenv, how to use ide] In-Reply-To: References: Message-ID: On Sun, Mar 24, 2019 at 8:51 PM anand warik wrote: > > I am sorry for not sticking to my original question but editors are complicated in itself. So many costimization instruction to read through just to finally execute a simple .Py file which can be just executed using the terminal. > > I am sorry I didn't documented my details about the system, but I certainly lost the track through how I installed Anaconda for Spyder and then atom. Next time onwards would me more careful. > > It's true I am not professional programmer and therefore trying to make sense of the big picture when learning programming from various sources. Picking up pieces from here and there and applying on the system only to forget how at all I did it in first place. > > Sorry for the inconvenience caused, will be careful next time My intent was not to criticize you, but to help you have the best chance of getting help and to briefly share my struggles to settle on a coding editor. There is nothing to be sorry about! You certainly caused me no inconvenience!! Just because an editor you might use has plenty of customization options and be overly packed with features does not usually mean you need to concern yourself with them, especially starting out. As long as you can create a text file easily and save it where you want to save it, you can use it. You can always run your program from the terminal and many think that is a good idea to do anyway. Sometimes when you run a program from within the editor/IDE it can hide some issues from you. Most editors/IDEs come preset out of the box, so to speak, to be able to do sensible syntax highlighting, etc., so you can worry about fine tuning these things later when you have time and inclination. As for forgetting things, I am a past master at this! But I keep chugging along and people here are quite patient with me. But it pays to try to focus on the details of what you are doing programming-wise as the whole process can and does crash for want of an overlooked detail. Or, in my case, many overlooked details! Keep trying and always try to give the best, most helpful description of problems you are experiencing, what you expected to happen and what actually did happen, so that you have the best chance of getting useful help. Take care! boB From anandwarik at gmail.com Sun Mar 24 21:51:08 2019 From: anandwarik at gmail.com (anand warik) Date: Mon, 25 Mar 2019 07:21:08 +0530 Subject: [Tutor] [OT] Problem with auto-complete package installation in Atom [Was: Re: After virtualenv, how to use ide] In-Reply-To: References: Message-ID: I am sorry for not sticking to my original question but editors are complicated in itself. So many costimization instruction to read through just to finally execute a simple .Py file which can be just executed using the terminal. I am sorry I didn't documented my details about the system, but I certainly lost the track through how I installed Anaconda for Spyder and then atom. Next time onwards would me more careful. It's true I am not professional programmer and therefore trying to make sense of the big picture when learning programming from various sources. Picking up pieces from here and there and applying on the system only to forget how at all I did it in first place. Sorry for the inconvenience caused, will be careful next time On Mon 25 Mar, 2019, 12:02 AM boB Stepp On Sun, Mar 24, 2019 at 2:45 AM anand warik wrote: > > > > I gave up on Spyder and shifted to Atom, which seems easy to port to > different versions of python created by virtual environments. But now > facing a new problem. I have installed a package named autocomplete-python > on atom but it doesn't seem to work. I checked few links on stackoverflow > as well but still the problem persists. Can someone help me with this? > > This is actually a question about the Atom code editor, not one about > Python, so it is really off-topic for this forum as well as unrelated > to your originating topic. However, Tutor is a friendly place, so if > someone knows much about Atom, I am sure they will try to help. But I > must say, you did not give anyone much information to enable such help > to happen. "... but it doesn't seem to work..." is an extraordinarily > vague problem description! So you may wish to flesh out the details > of your exact problem if you seek help here or elsewhere. > > But that is not what I wish to discuss. I am going to assume that you > are not a professional programmer. If that is incorrect, I profusely > apologize in advance! But that is okay. I am not a professional nor > expert programmer myself. Because of this, I recall agonizing over > the choice of code editor, or, possibly even worse, deciding whether > to use an IDE or editor. This is a difficult decision about which > many wax poetic and express their opinions with great emotion. If you > wish to be amused, just do a search for Emacs vs. Vim editor wars! So > the reality of it, whether you are a non-expert like me or a > professional coder, choice of editor/IDE is an intensely personal > choice. You will spend many hours/days/weeks/months/years/... of your > life staring at your choice of editor/IDE, so you should pick > something that works well for you. But editor hopping is not the > answer! Instead, I would advise you to carefully evaluate your actual > *needs*, and cull that list down to something that satisfies your > sense of aesthetics. > > In my case I write some programs to make my life easier at work (*not* > programming related). While there my time is split between PCs that > are Windows-based and thin-clients connecting to a Solaris-based > server. Whatever editor I use at work needs to be available in both > environments, and the Solaris one does *not* allow me to install any > new software, so it became my primary determinant of editor. When it > came down to it, the only substantial code editing support was limited > to vi until recently when our servers were upgraded with a newer > version of Solaris (Yay!) which came with Vim/gVim. On the other > hand, when I do my actual "fun" programming and learning, I mostly do > that at home where I can install anything I like. So initially I > tried all kinds of editors and IDEs and wasted quite a lot of time! > And I did not get comfortable with anything. So I finally decided to > just stick with vi/Vim/gVim/Neovim. The point is to stick with > something long enough that you can realize its full potential, or, at > least work your way in that direction. > > So, research your needs, both current and future. From that short > list select what you will enjoy interacting with on a daily basis. > > BTW, back to Atom: Your question(s) about it would probably get > better results on whatever forums are devoted to it. I am sure there > will be at least one if not more. > > Cheers! > boB > From anandwarik at gmail.com Mon Mar 25 08:12:14 2019 From: anandwarik at gmail.com (anand warik) Date: Mon, 25 Mar 2019 17:42:14 +0530 Subject: [Tutor] [OT] Problem with auto-complete package installation in Atom [Was: Re: After virtualenv, how to use ide] In-Reply-To: References: Message-ID: Thanks for the tip! On Mon 25 Mar, 2019, 7:56 AM boB Stepp On Sun, Mar 24, 2019 at 8:51 PM anand warik wrote: > > > > I am sorry for not sticking to my original question but editors are > complicated in itself. So many costimization instruction to read through > just to finally execute a simple .Py file which can be just executed using > the terminal. > > > > I am sorry I didn't documented my details about the system, but I > certainly lost the track through how I installed Anaconda for Spyder and > then atom. Next time onwards would me more careful. > > > > It's true I am not professional programmer and therefore trying to make > sense of the big picture when learning programming from various sources. > Picking up pieces from here and there and applying on the system only to > forget how at all I did it in first place. > > > > Sorry for the inconvenience caused, will be careful next time > > My intent was not to criticize you, but to help you have the best > chance of getting help and to briefly share my struggles to settle on > a coding editor. There is nothing to be sorry about! You certainly > caused me no inconvenience!! > > Just because an editor you might use has plenty of customization > options and be overly packed with features does not usually mean you > need to concern yourself with them, especially starting out. As long > as you can create a text file easily and save it where you want to > save it, you can use it. You can always run your program from the > terminal and many think that is a good idea to do anyway. Sometimes > when you run a program from within the editor/IDE it can hide some > issues from you. Most editors/IDEs come preset out of the box, so to > speak, to be able to do sensible syntax highlighting, etc., so you can > worry about fine tuning these things later when you have time and > inclination. > > As for forgetting things, I am a past master at this! But I keep > chugging along and people here are quite patient with me. But it pays > to try to focus on the details of what you are doing programming-wise > as the whole process can and does crash for want of an overlooked > detail. Or, in my case, many overlooked details! > > Keep trying and always try to give the best, most helpful description > of problems you are experiencing, what you expected to happen and what > actually did happen, so that you have the best chance of getting > useful help. > > Take care! > boB > From bgailer at gmail.com Tue Mar 26 09:01:56 2019 From: bgailer at gmail.com (Bob Gailer) Date: Tue, 26 Mar 2019 09:01:56 -0400 Subject: [Tutor] How to store scores of a race's players In-Reply-To: References: Message-ID: On Mar 26, 2019 6:55 AM, "^Bart" wrote: > > Hello! > > I need to store scores of three players for more than a race, after every race the user will read "Is the competition finished?", if the competition if finished the user will see the winner who got higest score: > Thank you for reaching out to us for help as You Learn Python. A good mailing list to use instead of the main python list is tutor at python.org. I'm including that list in the reply addresses; please use that address in the future and always reply all so a copy goes to that list. This looks like a homework assignment. Be advised we won't write code for you but we will help you when you get stuck and provide some guidance. I'm personally curious as to who wrote that specification, since it is not well written. One of the questions that fails to address is what if two or more players have the same high score? It's also a little hard to guide you since we don't know what you've already learned. One of the fundamental concepts in computer programming is that of a loop. Since the requirements indicate there will be more than one race that requires a loop. The simplest python construction for a loop is a while. Within that Loop you write the code once rather than rewriting it as you have done. I hope that is enough to get you started. Please apply that advice as best you can and come back with a revised program. > p1 = int (input ("Insert score of the first player: ")) > p2 = int (input ("Insert score of the second player: ")) > p3 = int (input ("Insert score of the third player: ")) > > race = str (input ("Is the competition finished?")) > > totalp1 = 0 > totalp2 = 0 > totalp3 = 0 > > winner = 0 > > if p1 > p2 and p1 > p3: > winner = c1 > elif p2 > p1 and p2 > p3: > winner = p2 > else: > winner = p3 > > if "yes" in race: > print("The winner is:",winner) > else: > p1 = int (input ("Insert score of the first player: ")) > p2 = int (input ("Insert score of the second player: ")) > p3 = int (input ("Insert score of the third player: ")) > > race = str (input ("Is the competition finished?")) > > You read above just my toughts, is there someone who could help me to understand how to solve it? > > Regards. > ^Bart > -- > https://mail.python.org/mailman/listinfo/python-list From bgailer at gmail.com Tue Mar 26 09:57:34 2019 From: bgailer at gmail.com (Bob Gailer) Date: Tue, 26 Mar 2019 09:57:34 -0400 Subject: [Tutor] How to store scores of a race's players In-Reply-To: References: Message-ID: Please do not use a mangled return email address. It causes us a lot of pain when we fail to read your address to fix it and get the message bounced back. The only reason I'm even bothering to resend it is because I put a lot of work into it.: > On > Mar 26, 2019 6:55 AM, "^Bart" wrote: > > > > Hello! > > > > I need to store scores of three players for more than a race, after every race the user will read "Is the competition finished?", if the competition if finished the user will see the winner who got higest score: > > > Thank you for reaching out to us for help as You Learn Python. A good mailing list to use instead of the main python list is tutor at python.org. I'm including that list in the reply addresses; please use that address in the future and always reply all so a copy goes to that list. > > This looks like a homework assignment. Be advised we won't write code for you but we will help you when you get stuck and provide some guidance. I'm personally curious as to who wrote that specification, since it is not well written. One of the questions that fails to address is what if two or more players have the same high score? > > It's also a little hard to guide you since we don't know what you've already learned. One of the fundamental concepts in computer programming is that of a loop. Since the requirements indicate there will be more than one race that requires a loop. The simplest python construction for a loop is a while. Within that Loop you write the code once rather than rewriting it as you have done. I hope that is enough to get you started. Please apply that advice as best you can and come back with a revised program. > > > p1 = int (input ("Insert score of the first player: ")) > > p2 = int (input ("Insert score of the second player: ")) > > p3 = int (input ("Insert score of the third player: ")) > > > > race = str (input ("Is the competition finished?")) > > > > totalp1 = 0 > > totalp2 = 0 > > totalp3 = 0 > > > > winner = 0 > > > > if p1 > p2 and p1 > p3: > > winner = c1 > > elif p2 > p1 and p2 > p3: > > winner = p2 > > else: > > winner = p3 > > > > if "yes" in race: > > print("The winner is:",winner) > > else: > > p1 = int (input ("Insert score of the first player: ")) > > p2 = int (input ("Insert score of the second player: ")) > > p3 = int (input ("Insert score of the third player: ")) > > > > race = str (input ("Is the competition finished?")) > > > > You read above just my toughts, is there someone who could help me to understand how to solve it? > > > > Regards. > > ^Bart > > -- > > https://mail.python.org/mailman/listinfo/python-list From mats at wichmann.us Tue Mar 26 14:45:52 2019 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 26 Mar 2019 12:45:52 -0600 Subject: [Tutor] (no subject) In-Reply-To: References: <1951f49d-81e9-2485-45dc-41e89fd62e0b@wichmann.us> Message-ID: <4ad51dca-4cde-d4c9-c25c-46f1e878ef41@wichmann.us> On 3/26/19 12:26 PM, Matthew Herzog wrote: > elif fileDate = datetime.strptime(name[0:8], DATEFMT).date(): > ? ? ? ? ? ? ? ? ? ?^ > SyntaxError: invalid syntax are you assigning (=) or comparing (==)? you can't do both in one statement because of the side effect implications. [except that starting with Python 3.8 you will be able to (see PEP 572), but that will be a brand new operator, := ] From matthew.herzog at gmail.com Tue Mar 26 14:26:09 2019 From: matthew.herzog at gmail.com (Matthew Herzog) Date: Tue, 26 Mar 2019 14:26:09 -0400 Subject: [Tutor] (no subject) In-Reply-To: <1951f49d-81e9-2485-45dc-41e89fd62e0b@wichmann.us> References: <1951f49d-81e9-2485-45dc-41e89fd62e0b@wichmann.us> Message-ID: elif fileDate = datetime.strptime(name[0:8], DATEFMT).date(): ^ SyntaxError: invalid syntax On Sat, Mar 23, 2019 at 6:43 PM Mats Wichmann wrote: > On 3/23/19 3:16 AM, Peter Otten wrote: > > > Personally I would use a try...except clause because with that you can > > handle invalid dates like 99999999_etc.xls gracefully. So > > > > ERASED = "erased_" > > > > > > def strip_prefix(name): > > if name.startswith(ERASED): > > name = name[len(ERASED):] > > return name > > > > > > def extract_date(name): > > datestr = strip_prefix(name)[:8] > > return datetime.datetime.strptime(datestr, DATEFMT).date() > > > > > > for name in ...: > > try: > > file_date = extract_date(name) > > except ValueError: > > pass > > else: > > print(file_date) > > I'd endorse this approach as well.. with a DATEFMT of "%Y%m%d", you will > get a ValueError for every date string that is not a valid date... which > you then just ignore (the "pass"), since that's what you wanted to do. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- The plural of anecdote is not "data." From drew.jakala71 at tamu.edu Tue Mar 26 17:41:38 2019 From: drew.jakala71 at tamu.edu (Drew Jakala) Date: Tue, 26 Mar 2019 16:41:38 -0500 Subject: [Tutor] issues running python Message-ID: Hello, I'm having issues running python and sublime on my computer. Every time I try to run either one I get an error message that looks something like this: "Traceback (most recent call last): File "", line 1, in python NameError: name 'python' is not defined I think I need to edit my path but I'm not sure how to do that correctly so any help would be greatly appreciated. Thanks, Drew Virus-free. www.avg.com <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> From alan.gauld at yahoo.co.uk Tue Mar 26 20:40:50 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 27 Mar 2019 00:40:50 +0000 Subject: [Tutor] issues running python In-Reply-To: References: Message-ID: On 26/03/2019 21:41, Drew Jakala wrote: > Hello, > I'm having issues running python and sublime on my computer. Every time I > try to run either one I get an error message that looks something like this: > "Traceback (most recent call last): > File "", line 1, in > python > NameError: name 'python' is not defined > I think I need to edit my path but I'm not sure how to do that correctly so > any help would be greatly appreciated. I know nothing about Sublime but it might help if you tell us what OS you use and how exactly you try to run sublime. The error you show us is coming from python itself so it looks like you are trying to run python from inside python. I suspect you may be following some kind of tutorial or help page that says you should type something like: python foo.py This means from the OS pronpt you should type the line given. But you are trying to type it at a python interpreter prompt. The OS prompt usually looks like: C:\WINDOWS> or user at host$ or similar. The Python prompt usually looks like >>> You only type "python" at the OS prompt. -- 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 matthew.herzog at gmail.com Thu Mar 28 12:08:05 2019 From: matthew.herzog at gmail.com (Matthew Herzog) Date: Thu, 28 Mar 2019 12:08:05 -0400 Subject: [Tutor] operate on files based on comparing filenames to today's date Message-ID: I have cobbled together this code which gives me regex matches of files whose names either begin with either YYYYMMDD_? or erased_YYMMDD_? and whose extensions are exml or ewav. Now I need to compare the date string (regex match) in the filename to today's date. If the result of the comparison results in YYYMMDD being older than 180 days, I should print something to indicate this. If not, nothing need be done. Should I write another function to compare each matched regex to today's date or is that overkill? Thanks. todaystring = date.today().strftime("%Y%m%d") oldest = date.today() - timedelta(days=180) def get_files(extensions): all_files = [] for ext in extensions: all_files.extend(Path('/Users/msh/Python/calls').glob(ext)) return all_files for file in get_files(('*.ewav', '*.exml')): print(re.match("[0-9]{8}|erased_",file.name)) -- The plural of anecdote is not "data." From cs at cskk.id.au Thu Mar 28 18:12:56 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Fri, 29 Mar 2019 09:12:56 +1100 Subject: [Tutor] operate on files based on comparing filenames to today's date In-Reply-To: References: Message-ID: <20190328221256.GA1832@cskk.homeip.net> Before getting to your specific question, a few remarks below: On 28Mar2019 12:08, Matthew Herzog wrote: >I have cobbled together this code which gives me regex matches of files >whose names either begin with either YYYYMMDD_? or erased_YYMMDD_? and >whose extensions are exml or ewav. >todaystring = date.today().strftime("%Y%m%d") >oldest = date.today() - timedelta(days=180) >def get_files(extensions): > all_files = [] > for ext in extensions: > all_files.extend(Path('/Users/msh/Python/calls').glob(ext)) > return all_files >for file in get_files(('*.ewav', '*.exml')): > print(re.match("[0-9]{8}|erased_",file.name)) Your regexp in the "print" loop at the bottom does not do what you say. You have: print(re.match("[0-9]{8}|erased_",file.name)) i.e. the regexp is: [0-9]{8}|erased_ (a) That matches 8 digits _or_ the string "erased_". (b) [0-9] can be written as \d for more readability. (c) I'd use: (erased_)?\d{8} which is an optional "erased_" followed by 8 digits. And for your purposes: (erased_)?(\d{8}) which will group the digits together for easy extraction: DATED_FILENAME_re = re.compile(r'(erased_)?(\d{8})') for file in get_files(('*.ewav', '*.exml')): m = DATED_FILENAME_re.match(file) if m: # a suitable filename datepart = m.group(2) # now you can turn that into an actual datetime.date object >Now I need to compare the date string (regex match) in the filename to >today's date. If the result of the comparison results in YYYMMDD being >older than 180 days, I should print something to indicate this. If not, >nothing need be done. >Should I write another function to compare each matched regex to today's >date or is that overkill? Thanks. If you want to figure out the age, you need to convert the YYYYMMDD into a datetime.date and then compare _that_ to today's date. Why? Because the datetime.date type knows how to work with dates correctly, avoiding all sorts of bug prone efforts on your own part (because human calendars are irregular tricky things with many special cases). So I would drop the "todaystring" altogether. You're thinking "get the string from the filename and compare it to "todaystring". But what you _want_ it to measure the age, and that requires working with dates, not strings. So instead convert the filename's string in to a date and do straight arithmetic with today(). So you might upgrade that regexp to group the year, month and day individually, pull them out and make a date object: import datetime ........ DATED_FILENAME_re = re.compile(r'(erased_)?(\d\d\d\d)(\d\d)(\d\d)') ........ # get today as a datetime.date object today = datetime.today() for file in ......: m = DATED_FILENAME_re.match(file) if m: prefix, year, month, day = m.group(1,2,3,4) year = int(year) month = int(month) day = int(day) # turn this into a datetime.date object date = datetime.date(year, month, day) # compute a datetime.timedelta object age = today - date if age.days > 180: print("old!") I think you're right: make a function to return the datetime.date of the filename, so this portion: m = DATED_FILENAME_re.match(file) if m: prefix, year, month, day = m.group(1,2,3,4) year = int(year) month = int(month) day = int(day) # turn this into a datetime.date object date = datetime.date(year, month, day) and return the computed date. Then just keep this in the for loop so it is obvious what's going on; it is too small to hide in a function without a better reason. # compute a datetime.timedelta object age = today - date if age.days > 180: print("old!") Cheers, Cameron Simpson From lucasbreault2400 at gmail.com Thu Mar 28 17:12:22 2019 From: lucasbreault2400 at gmail.com (lucasbreault2400 at gmail.com) Date: Thu, 28 Mar 2019 17:12:22 -0400 Subject: [Tutor] Help with code Message-ID: <2A7BFEA4-B941-4EA2-AB04-199CF86ED6B2@gmail.com> I?m trying to make a password that must contain a number in it. Which method do I use for that? Sent from my iPhone From alan.gauld at yahoo.co.uk Thu Mar 28 19:05:51 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 28 Mar 2019 23:05:51 +0000 Subject: [Tutor] Help with code In-Reply-To: <2A7BFEA4-B941-4EA2-AB04-199CF86ED6B2@gmail.com> References: <2A7BFEA4-B941-4EA2-AB04-199CF86ED6B2@gmail.com> Message-ID: On 28/03/2019 21:12, lucasbreault2400 at gmail.com wrote: > I?m trying to make a password that must contain a number in it. I assume you mean you want to check whether a password has a number in it? Does it need to be a single digit or can there be multiple? > Which method do I use for that? There is a string method called isdigit() that will test for a number. You can loop over each character in the password and apply that method. This smells a bit like homework so I won't go any further than that, but if you get stuck send us your code and we'll try to help some more. -- 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 csveum at my.devry.edu Thu Mar 28 19:55:49 2019 From: csveum at my.devry.edu (Sveum, Christian) Date: Thu, 28 Mar 2019 23:55:49 +0000 Subject: [Tutor] cant get it to run right Message-ID: I am new and I have tried everything I can think I of. I want it to run like a converstion. print(" I am Bob") name = input("What is your name") print("Nice to meet you") age = input("How old are you?") print("I was not programed with a age") city = input("Where do you live?") temperature = float(input("What is the temperature?")) if temperature>=75: print("WOW it is hot") elif temperature==75 or 74 or 73 or 72 or 71 or 70 or 69 or 68 or 67 or 66 or 65: print(" That is nice") else: print(" You can't make me go outside") color = input("What is your favorite color") if color=="Red" or "red" or "yellow" or "Yellow" or "Blue" or "blue" or "orange" or "Orange" or "green" or "Green" or "purple" or "Purple": print ("That is cool") elif color=="white" or "White": input(" That is a shade, Try agian") elif color=="gray" or "Gray": input(" That is a shade, Try agian") elif color=="black" or "Black": input("That is not a color,Try agian") else: print (" I dont know what to say") print("My favorite color is Green") maxNumber=int(input("Pick a number between 1 and 10")) count=0 while count for Windows 10 From PyTutor at danceswithmice.info Thu Mar 28 21:08:15 2019 From: PyTutor at danceswithmice.info (DL Neil) Date: Fri, 29 Mar 2019 14:08:15 +1300 Subject: [Tutor] cant get it to run right In-Reply-To: References: Message-ID: <06661bf3-80e1-c381-28c4-34c47418183a@DancesWithMice.info> On 29/03/19 12:55 PM, Sveum, Christian wrote: > I am new and I have tried everything I can think I of. I want it to run like a converstion. > print(" I am Bob") ... What is not working? How far does the program run before stopping? What error message are you seeing? (why won't you share it with us?) -- Regards =dn From alan.gauld at yahoo.co.uk Thu Mar 28 21:53:01 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 29 Mar 2019 01:53:01 +0000 Subject: [Tutor] cant get it to run right In-Reply-To: References: Message-ID: On 28/03/2019 23:55, Sveum, Christian wrote: > I am new and I have tried everything I can think I of. I want it to run like a converstion. Its always good to be as specific as possible. What exactly do you mean by "can't get it to run right"? Does it crash? Do you get an error message? If so send the message to us. Does it give the "wrong answer" If so tell us what you expected and what you got. > print(" I am Bob") > name = input("What is your name") > print("Nice to meet you") > age = input("How old are you?") > print("I was not programed with a age") > city = input("Where do you live?") You save all these values but don't use them. > temperature = float(input("What is the temperature?")) > if temperature>=75: > print("WOW it is hot") > elif temperature==75 or 74 or 73 or 72 or 71 or 70 or 69 or 68 or 67 or 66 or 65: > print(" That is nice") Now this might be where you hit problems. The code above is valid but doesn't do what I suspect you think it does. Python sees it as: elif (temperature==75) or (74 or 73 or 72 or 71 or 70 or 69 or 68 or 67 or 66 or 65): And the expression (75 or 74 0r...) always evaluates to the first non-zero value, so in this case it's 74 But we know temperature is not equal to 75 (from the if test) so the elif expression always evaluates to 74 which is True and so you get the printed message. To do what you want you have several options. In your case the values form a range so you can test that: elif 75 > temperature > 65: # between 65 and 75 print... Or you can put your values in a list and use 'in': elif temperature in [75,74,73,72,71,70,69,68,67,66,65]: print.... The range option is vest if your user can type in non-whole numbers like 72.5 (as the use of float suggests) The list is best if you have a set of distinct but non consecutive values) > if color=="Red" or "red" or "yellow" or "Yellow" or "Blue" or "blue" or "orange" or "Orange" or "green" or "Green" or "purple" or "Purple": Same here but the list approach is the only option here. But see below... > elif color=="white" or "White": in this case the best approach is to convert the strings to upper or lower case: elif color.lower() == "white": > elif color=="gray" or "Gray": > elif color=="black" or "Black": Same here > while count if count==10: > print( "10 is my favorite number!") > print(count) > count=count+1 > input("Was one of these numbers your favorite number") > if question=="yes": I suspect you meant to assign the input() value to question? 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 nitinchandra1 at gmail.com Fri Mar 29 11:48:41 2019 From: nitinchandra1 at gmail.com (nitin chandra) Date: Fri, 29 Mar 2019 21:18:41 +0530 Subject: [Tutor] Python Django Query Message-ID: Hi Everyone, I need some guidance / corrections to my code. I hosted a demo site on pythonanywhere.com and to test the initial pages I wrote some test code. Good news, put in 10 hrs, tried so many suggestion given on the net, yet non worked. So finally mailing the list. Objective : display main page with multiple links to other pages. Use jQuery plugins, click the link on main page and open other pages. The following path and pages are posted below. Thanks Nitin Chandra ------------------------------------------------------------ ERROR When I click on 'Table' link on 'home' page, the following error is displayed http://selftech.pythonanywhere.com/table.html Page not found (404) Request Method:GET Request URL:http://selftech.pythonanywhere.com/table.html Using the URLconf defined in eduinfra.urls, Django tried these URL patterns, in this order: admin/ [name='home.html'] The current path, table.html, didn't match any of these. You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page. +++++++++++++++++++++++++++++++++++ /home/selfTech/edu/tech/ __init__.py admin.py apps.py models.py tests.py urls.py views.py /home/selfTech/edu/tech/templates/ base.html home.html table.html ******************************************* our 'base.html' code below (test code, gets rendered correctly) ************************* home.html {% extends 'base.html' %} {% block content%} Hello TEAM :) {% endblock %} ******************************* table.html (gives ERROR 404)
Name
********************************************* /home/selfTech/edu/tech/ urls.py from django.urls import path from . import views urlpatterns = [ path('', views.home, name='home'), path('table/', views.table, name='table') ## various combo did not work # path (' ', views.table, name='table') ## nope # path (' ', views.table, name='table.html') # nope ] *************************/////////////////////// views.py from django.shortcuts import render from django.http import HttpResponse # Create your views here. def home (request): /// correctly renders return render (request, "home.html",{}) def table (request): //// gives error return HttpResponse ('

This is a test Page

') # Tried the below code, nada # return render (request, "table.html",{}) *************************\\\\\\\\\\\\\\\\\\\\\\\\\ From adeadmarshal at gmail.com Fri Mar 29 14:01:59 2019 From: adeadmarshal at gmail.com (Ali M) Date: Fri, 29 Mar 2019 22:31:59 +0430 Subject: [Tutor] Encrypting shipped sqlite db in app directory Message-ID: I want to encrypt my sqlite databases which are shipped with the app in it's directory, so that the user can't modify or use it elsewhere, and they will only be accessible for the app to read from, how can i do that? From alan.gauld at yahoo.co.uk Sat Mar 30 03:22:26 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 30 Mar 2019 07:22:26 +0000 Subject: [Tutor] Encrypting shipped sqlite db in app directory In-Reply-To: References: Message-ID: On 29/03/2019 18:01, Ali M wrote: > I want to encrypt my sqlite databases which are shipped with the app in > it's directory, so that the user can't modify or use it elsewhere, and they > will only be accessible for the app to read from, how can i do that? It all depends on how rigorous you want to be. If you only want to make the database difficult to use by an ordinary user then you can do things like make it a hidden file and apply very basic encryption using a Caesar type algorithm or a rot 13 shift. But if you actually want to encrypt the data in a secure way that will deter hackers then that will require industrial strength tools which are much more complex both in operation and use. You should also consider things like file ownership and user accounts for your app, complete with separate logins etc. It all depends on how much security you want to achieve. -- 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 Sat Mar 30 03:28:37 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 30 Mar 2019 07:28:37 +0000 Subject: [Tutor] Encrypting shipped sqlite db in app directory In-Reply-To: References: Message-ID: On 30/03/2019 07:22, Alan Gauld via Tutor wrote: > On 29/03/2019 18:01, Ali M wrote: >> I want to encrypt my sqlite databases which are shipped with the app in >> it's directory, so that the user can't modify or use it elsewhere, and they >> will only be accessible for the app to read from, how can i do that? > You should also consider things like file ownership and > user accounts for your app, complete with separate > logins etc. I meant to add that you could incorporate a checksum too so that you can detect if anyone has tampered with the file. -- 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 Mar 30 04:03:08 2019 From: __peter__ at web.de (Peter Otten) Date: Sat, 30 Mar 2019 09:03:08 +0100 Subject: [Tutor] Encrypting shipped sqlite db in app directory References: Message-ID: Ali M wrote: > I want to encrypt my sqlite databases which are shipped with the app in > it's directory, so that the user can't modify or use it elsewhere, and > they will only be accessible for the app to read from, how can i do that? Assuming you had an encrypted database, where would you put the password? You cannot give it to the user because then he can decrypt the database, and if you put it into the app you are effectively giving it to the user. From robertvstepp at gmail.com Sat Mar 30 23:13:16 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 30 Mar 2019 22:13:16 -0500 Subject: [Tutor] Questions about the deprecation of standard library modules Message-ID: While reading in chapter 3 of "Learning Python, 5th ed." by Mark Lutz, I was playing around with reload() in the imp module. In the interpreter I did a "from imp import reload" and then help(reload). This had a warning that it was deprecated. After a search online I found that the entire imp library was deprecated since Python 3.4. The aforementioned book covers through Python 3.3. This got me to wondering when the imp module would be removed from Python 3, which led me to finding PEP 4. In it it says: Module name: imp Rationale: Replaced by the importlib module. Date: 2013-02-10 Documentation: Deprecated as of Python 3.4. There is no mention as to _when_ the module might be removed. PEP 4 says that a deprecated module may be removed as early as the next Python release after it becomes deprecated. Also the PEP states that if a module exists in both Python 2.7 and Python 3.5, it will not be removed until Python 2.7 is no longer supported, which, I suppose, means next year. So my main question is how does one know in which Python version a deprecated module will be removed? I'm not too concerned about the imp module, but _do_ want to know how the removal process works for deprecated standard library modules that I might be interested in. -- boB From mats at wichmann.us Sat Mar 30 23:55:04 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 30 Mar 2019 21:55:04 -0600 Subject: [Tutor] Questions about the deprecation of standard library modules In-Reply-To: References: Message-ID: <4D6B755A-BE11-473D-8CAC-4542AFCA291D@wichmann.us> On March 30, 2019 9:13:16 PM MDT, boB Stepp wrote: >While reading in chapter 3 of "Learning Python, 5th ed." by Mark Lutz, >I was playing around with reload() in the imp module. In the >interpreter I did a "from imp import reload" and then help(reload). >This had a warning that it was deprecated. After a search online I >found that the entire imp library was deprecated since Python 3.4. >The aforementioned book covers through Python 3.3. This got me to >wondering when the imp module would be removed from Python 3, which >led me to finding PEP 4. In it it says: > >Module name: imp >Rationale: Replaced by the importlib module. >Date: 2013-02-10 >Documentation: Deprecated as of Python 3.4. > >There is no mention as to _when_ the module might be removed. PEP 4 >says that a deprecated module may be removed as early as the next >Python release after it becomes deprecated. Also the PEP states that >if a module exists in both Python 2.7 and Python 3.5, it will not be >removed until Python 2.7 is no longer supported, which, I suppose, >means next year. > >So my main question is how does one know in which Python version a >deprecated module will be removed? I'm not too concerned about the >imp module, but _do_ want to know how the removal process works for >deprecated standard library modules that I might be interested in. bob, there's no real answer. if you look at tempfile, it says tempfile.mktemp() is deprecated since 2.3 (2003) and tempfile.template since 2.0 (2000). both still exist and work... 3.7 and much more so 3.8 become a lot more aggressive in warning about deprecated behavior, and the warnings alone can be an issue (a project I work on has its test suite break completely with 3.8 alpha because it tests stdout/stderr being as expected, and the warnings leak into stderr for the tests. yes imp is one of those). So fix when you have to, I guess, but preferably be proactive and fix earlier... dont count on having X releases. -- Sent from a mobile device with K-9 Mail. Please excuse my brevity.