From cs at cskk.id.au Sat Jun 1 02:45:48 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 1 Jun 2019 16:45:48 +1000 Subject: [Tutor] Interactive editing of variables. In-Reply-To: <00c001d51825$20fe9730$62fbc590$@gmail.com> References: <00c001d51825$20fe9730$62fbc590$@gmail.com> Message-ID: <20190601064548.GA9271@cskk.homeip.net> On 01Jun2019 12:53, Sean Murphy wrote: >Python 3.7, windows 10. > >I have no clue on how to achieve what I want to do and the code I have >creates an hash. As shown below: > >for row in description: [... get some text and present it for editing ...] >I have had a look and cannot find an example where I can interactively >edit a content of a variable at the command line. I do not want to use GUI at >all. As this is a simple program only requiring CLI. I have no problems >showing the prompt, but cannot insert text into the edit (input) area. Any >ideas? If I understand you, you've got your target text and you want to user to be given it so they can modify it, rather than having to retype it in full at the prompt. On a UNIX system you'd use the standand "readline" module, and prefill the text buffer with your text. Is it marked as UNIX only, but I believe there are Windows dropins for this facility. Maybe install this package: https://pypi.org/project/pyreadline-ais/ maybe with "python -m pip install pyreadline-ais". Then use it according to the documentation for the stdlib readline module: https://docs.python.org/3/library/readline.html#module-readline Cheers, Cameron Simpson From alan.gauld at yahoo.co.uk Sat Jun 1 02:49:49 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 1 Jun 2019 07:49:49 +0100 Subject: [Tutor] Interactive editing of variables. In-Reply-To: <00c001d51825$20fe9730$62fbc590$@gmail.com> References: <00c001d51825$20fe9730$62fbc590$@gmail.com> Message-ID: On 01/06/2019 03:53, mhysnm1964 at gmail.com wrote: > I have no clue on how to achieve what I want to do and the code I have > creates an hash. As shown below: Thats because what you want is not a standard feature of CLI apps. You will need to do one of the following(in order of easiness): 1) Use a GUI - it then becomes a trivial matter 2) Use a pseudo GUI like curses to provide cursor control 3) Find a module that already does what you need (maybe readline can be made to work?) 4) Write a function yourself using screen primitives that manage the cursor > for row in description: > text = description_rejex(row) > if text[0] not in narration: > Result = input(text[0]) > narration[result] = text The standard tools allow you to input a new value and overwrite the existing one. But there is no simple way to interactively modify an existing value (and of course you would need to convert it to/from a string for that to be possible) > I have had a look and cannot find an example where I can interactively edit > a content of a variable at the command line. I do not want to use GUI at > all. A GUI makes this a trivial problem. Simply display an edit control and insert the current value as a string. Allow the user to modify it and when done read the new value back. If you don't want to use a GUI you need to provide GUI like controls yourself, either through an existing module or by writing one. Something like easygui would be eminently suitable. But even vanilla Tkinter is almost trivial. The curses library will do it but that is not standard on Windows and I've heard varying reports of how well it works there. The readline library allows basic editing of the commands line but I'm not sure how you would insert your variable into the command line initially... For windows there are a couple of modules available that provide low level cursor control and character input/output, so you could use one of those to write such a function. And if you search hard enough you should find that somebody, somewhere has already done the work for you. But I don't know where... -- 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 Jun 1 02:56:23 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 1 Jun 2019 07:56:23 +0100 Subject: [Tutor] is this doable In-Reply-To: References: Message-ID: On 01/06/2019 00:13, Alan Gauld via Tutor wrote: > Is the language C/C++? If so you may know the OS API calls needed > and you could access those directly from Python using ctypes.... > That might make your job more familiar and easier. I meant to add a nod to Mark Hammond's win32 package too. It includes a process control module with access to most of the Win32 API for process control, which might be simpler than using ctypes to call the raw C API -- 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 mhysnm1964 at gmail.com Sat Jun 1 03:55:07 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sat, 1 Jun 2019 17:55:07 +1000 Subject: [Tutor] Interactive editing of variables. In-Reply-To: References: <00c001d51825$20fe9730$62fbc590$@gmail.com> Message-ID: <013401d5184f$55ddcd20$01996760$@gmail.com> Allen, As I am using Python 3.7 under windows. I have tried to use the win32gui, and Tkinter. Both generate the below errors and I cannot identify a module release to support the version of Python I am using. C:\Users\mhysn>pip install Tkinter graphic 948 Collecting Tkinter ERROR: Could not find a version that satisfies the requirement Tkinter (from versions: none) graphic 948 ERROR: No matching distribution found for Tkinter I gave up on the cursers due to the above issues and looking at example code. It was far to complex to achieve the simple thing I wanted too do. Easygui module has installed and I will look for examples. The issue I have with a lot of GUI programs built for Python they generally fail in the accessibility department for a screen reader. Most open source software which is multi-platform supported fail in this department. I will check out the module and see. This is a major issue for software developers not considering all users and there is legal requirements here. Sorry, I am falling on to my band wagon of in-accessible or non-inclusive design products which is my passion. -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Saturday, 1 June 2019 4:50 PM To: tutor at python.org Subject: Re: [Tutor] Interactive editing of variables. On 01/06/2019 03:53, mhysnm1964 at gmail.com wrote: > I have no clue on how to achieve what I want to do and the code I have > creates an hash. As shown below: Thats because what you want is not a standard feature of CLI apps. You will need to do one of the following(in order of easiness): 1) Use a GUI - it then becomes a trivial matter 2) Use a pseudo GUI like curses to provide cursor control 3) Find a module that already does what you need (maybe readline can be made to work?) 4) Write a function yourself using screen primitives that manage the cursor > for row in description: > text = description_rejex(row) > if text[0] not in narration: > Result = input(text[0]) > narration[result] = text The standard tools allow you to input a new value and overwrite the existing one. But there is no simple way to interactively modify an existing value (and of course you would need to convert it to/from a string for that to be possible) > I have had a look and cannot find an example where I can interactively > edit a content of a variable at the command line. I do not want to use > GUI at all. A GUI makes this a trivial problem. Simply display an edit control and insert the current value as a string. Allow the user to modify it and when done read the new value back. If you don't want to use a GUI you need to provide GUI like controls yourself, either through an existing module or by writing one. Something like easygui would be eminently suitable. But even vanilla Tkinter is almost trivial. The curses library will do it but that is not standard on Windows and I've heard varying reports of how well it works there. The readline library allows basic editing of the commands line but I'm not sure how you would insert your variable into the command line initially... For windows there are a couple of modules available that provide low level cursor control and character input/output, so you could use one of those to write such a function. And if you search hard enough you should find that somebody, somewhere has already done the work for you. But I don't know where... -- 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 mhysnm1964 at gmail.com Sat Jun 1 04:52:48 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sat, 1 Jun 2019 18:52:48 +1000 Subject: [Tutor] Interactive editing of variables. In-Reply-To: References: <00c001d51825$20fe9730$62fbc590$@gmail.com> Message-ID: <016601d51857$64d4d9b0$2e7e8d10$@gmail.com> As I thought. Easygui is not accessible at all with a screen reader due to the underlying graphic library. Win32 could work if I could load it. Since then I could use standard windows objects. Note, I didn't see anything in the quick scan I did ion the API. Very frustrating and disappointing. -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Saturday, 1 June 2019 4:50 PM To: tutor at python.org Subject: Re: [Tutor] Interactive editing of variables. On 01/06/2019 03:53, mhysnm1964 at gmail.com wrote: > I have no clue on how to achieve what I want to do and the code I have > creates an hash. As shown below: Thats because what you want is not a standard feature of CLI apps. You will need to do one of the following(in order of easiness): 1) Use a GUI - it then becomes a trivial matter 2) Use a pseudo GUI like curses to provide cursor control 3) Find a module that already does what you need (maybe readline can be made to work?) 4) Write a function yourself using screen primitives that manage the cursor > for row in description: > text = description_rejex(row) > if text[0] not in narration: > Result = input(text[0]) > narration[result] = text The standard tools allow you to input a new value and overwrite the existing one. But there is no simple way to interactively modify an existing value (and of course you would need to convert it to/from a string for that to be possible) > I have had a look and cannot find an example where I can interactively > edit a content of a variable at the command line. I do not want to use > GUI at all. A GUI makes this a trivial problem. Simply display an edit control and insert the current value as a string. Allow the user to modify it and when done read the new value back. If you don't want to use a GUI you need to provide GUI like controls yourself, either through an existing module or by writing one. Something like easygui would be eminently suitable. But even vanilla Tkinter is almost trivial. The curses library will do it but that is not standard on Windows and I've heard varying reports of how well it works there. The readline library allows basic editing of the commands line but I'm not sure how you would insert your variable into the command line initially... For windows there are a couple of modules available that provide low level cursor control and character input/output, so you could use one of those to write such a function. And if you search hard enough you should find that somebody, somewhere has already done the work for you. But I don't know where... -- 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 nathan-tech at hotmail.com Sat Jun 1 06:02:50 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Sat, 1 Jun 2019 10:02:50 +0000 Subject: [Tutor] is this doable In-Reply-To: <18767fe1-ee08-372e-32c4-73eb41f8ff4c@wichmann.us> References: <18767fe1-ee08-372e-32c4-73eb41f8ff4c@wichmann.us> Message-ID: Hello, Thank you for your responses. I am indeed developing for windows at the moment, with an eye casually glancing in the MAC direction as a possibility for the future that I shall think about. I'm sorry I couldn't explain better, but being only familiar with the concept in my head from one language, I wasn't quite sure what to say for each thing, how to explain it. The language is actually a bit obscure, and is used in the development of MUDs, Multi-user dungeons. I never considered it would be broken down into several modules, but that makes sense. Now it has been mentioned, I do recall, on linux, briefly playing with psutil to retrieve memory values for a few tasks I keep running on my server. To that end, I think I now know roughly what to do. Something like this: import psutil import os path=os.getcwd()+"\\program.exe" slist=[] for x in psutil.process_iter(): ?if(x.exe()==path): ? slist.append([x, x.create_time]_) r.sort() # not sure how to sort by second element, but I'd sort the list by start time if(len(r)>1): ?# send signal to other program to tell it to do something, either through a ntofiy app or through psutil.send_signal() ?# exit the program Hope I'm making more sense now, and thank you for the help everyone. Nate On 01/06/2019 04:30, Mats Wichmann wrote: > On 5/31/19 1:41 PM, nathan tech wrote: >> Hi there, >> >> So for a future project of mine, I was wondering something. >> >> Is it possible, in python, to store a running task id in the registry? >> >> I might be using the complete wrong terms here, because I'm only used to >> doing this with a specific language, but here's what I want to do: >> >> >> python mytest.py: >> >> if(registry.taskid==valid_task): >> >> ?print 'already open' >> >> ?send to open program to make a ding noise. >> >> >> I understand that the second part, the "send to program" requires the >> program to handle being sent a "wake up!" event, which is fine, it's the >> "is it already running" which I am not sure on. > there's a lot your question leaves unasked... do you want to just code > your own apps and have one be able to poke another? that's one problem, > you can define the interface yourself. Or do you want to be able to > poke arbitrary running tasks? that ends up more complicated. many > systems have notification APIs that you can make use of, some of those > are more oriented to that model (the mobile systems Android and Tizen), > some a little less but still support it (Windows - it's a more prevalent > thing in the UWP model). > > the psutil module can let you find things out about processes, might be > useful in your "is the task running" query. > > if it's okay to start processes together and it's not arbitrary, the > multiprocessing module may be of some help. > > > > > _______________________________________________ > 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 Jun 1 14:58:13 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 1 Jun 2019 19:58:13 +0100 Subject: [Tutor] Interactive editing of variables. In-Reply-To: <013401d5184f$55ddcd20$01996760$@gmail.com> References: <00c001d51825$20fe9730$62fbc590$@gmail.com> <013401d5184f$55ddcd20$01996760$@gmail.com> Message-ID: On 01/06/2019 08:55, mhysnm1964 at gmail.com wrote: > As I am using Python 3.7 under windows. I have tried to use the win32gui, > and Tkinter. Both generate the below errors and I cannot identify a module > release to support the version of Python I am using. Tkinter should be included in the standard Windows download you should not need to install it. try >>> import tkinter # not Tkinter! If there are no errors it is available > I gave up on the cursers due to the above issues and looking at example > code. It was far to complex to achieve the simple thing I wanted too do. Here is some basic Tkinter code that does what I think you want. You have to close the window by clicking the close icon but that could be incorporated in the button code. import tkinter as tk myVar = "" def get_value(edt): global myVar myVar = edt.get() def edit_val(val): top = tk.Tk() ed = tk.Entry(top) ed.insert(tk.END, val) ed.pack() tk.Button(top,text="Store changes", command=lambda : get_value(ed)).pack() top.mainloop() myVar = input("Enter a start value: ") print( "Before: ", myVar) edit_val(myVar) print( "After: ", myVar) > The issue I have with a lot of GUI programs built for Python they generally > fail in the accessibility department for a screen reader. I can't help there I have nearly zero experience of using accessibility tools. But I'd expect any GUI toolkit to work with the standard OS tools. After all they are ultimately all built using the underlying primitive GUI API > Most open source software which is multi-platform supported fail in this Of course, since most open source authors have no incentive to develop accessible specific code. They are just scratching their own itch and making it available to anyone who wants to use it. That's how open source works. > .... This is a major issue for software developers > not considering all users and there is legal requirements here. Sorry, I am > falling on to my band wagon of in-accessible or non-inclusive design > products which is my passion. I can understand why folks get excited about it, especially if they (or friends/family) need that feature. But the cost (both in time and money) of doing so is considerable and if nobody is paying (or there is a time deadline) then it tends not to get done. That's capitalism in action. -- 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 mhysnm1964 at gmail.com Sat Jun 1 08:20:31 2019 From: mhysnm1964 at gmail.com (Sean Murphy) Date: Sat, 1 Jun 2019 22:20:31 +1000 Subject: [Tutor] Interactive editing of variables. In-Reply-To: References: <00c001d51825$20fe9730$62fbc590$@gmail.com> <016601d51857$64d4d9b0$2e7e8d10$@gmail.com> Message-ID: <3B9D4941-68CB-4765-B800-11686280CF49@gmail.com> Mike, thanks for this debug tool. I might have a look at it. No that?s not the goal I?m after. I want to have a basic l input line which is already populated with a value. The value is extracted from a list of strength. Each element in the list are unique. I want to modify the string and then re-insert it back into the same element as the new modified string I?m currently look at read line or piegnurl module this might do the trick. Using GUI is way over the top for the simple thing I?m trying to do The model which Alan kindly suggested if not accessible with a screen reader. As I cannot see e.g. blind, this is a critical component of my programming. Please excuse any errors because I?m using Siri to send this email. My experience is the part > On 1 Jun 2019, at 10:05 pm, Mike Barnett wrote: > > TL;DR > There's a run-time debugger out, "imwatchingyou" that is being developed daily, fast paced, that enables debugging without stopped your code or using an IDE. > > I'm unsure if I'm super late to the party of if I understand the "problem" / request properly. I apologize if I'm off-base. > > Do you want to be able to examine and modify variables while your program is running, without breaking into it with a debugger? Is that part of it? > > If so, I've been working my ass off for a couple of weeks to do just that, develop a realtime "watcher" / debugger. > > You'll find the project here: > https://github.com/PySimpleGUI/imwatchingyou > > At this time, you must pip install imwatchingyou. If you're concerned because of the name "imwatchingyou", it's meant to be YOU are watching YOUR code. If I wanted to watch your Python code, I would have named it "makefreemoneyminingbitcoininpython". > > There is a nice screenshot, although things have been changing. There are 2 primary windows. The brown one in the upper right of the screen is a realtime update of all of your local variables. Then there's the large debug window that allows you to do more including running the built-in REPL. > > There is also a version being built into the PySimpleGUI library itself so you do not have to do any imports, etc. All PySimpleGUI will have this watch/debug capability in it. > > > > > @mike > > -----Original Message----- > From: mhysnm1964 at gmail.com > Sent: Saturday, June 1, 2019 4:53 AM > To: 'Alan Gauld' ; tutor at python.org > Subject: Re: [Tutor] Interactive editing of variables. > > As I thought. Easygui is not accessible at all with a screen reader due to the underlying graphic library. Win32 could work if I could load it. Since then I could use standard windows objects. > > Note, I didn't see anything in the quick scan I did ion the API. > > > Very frustrating and disappointing. > -----Original Message----- > From: Tutor On Behalf Of Alan Gauld via Tutor > Sent: Saturday, 1 June 2019 4:50 PM > To: tutor at python.org > Subject: Re: [Tutor] Interactive editing of variables. > >> On 01/06/2019 03:53, mhysnm1964 at gmail.com wrote: >> >> I have no clue on how to achieve what I want to do and the code I have >> creates an hash. As shown below: > > Thats because what you want is not a standard feature of CLI apps. > You will need to do one of the following(in order of easiness): > 1) Use a GUI - it then becomes a trivial matter > 2) Use a pseudo GUI like curses to provide cursor control > 3) Find a module that already does what you need > (maybe readline can be made to work?) > 4) Write a function yourself using screen primitives > that manage the cursor > > >> for row in description: >> text = description_rejex(row) >> if text[0] not in narration: >> Result = input(text[0]) >> narration[result] = text > > The standard tools allow you to input a new value and overwrite the existing one. But there is no simple way to interactively modify an existing value (and of course you would need to convert it to/from a string for that to be > possible) > >> I have had a look and cannot find an example where I can interactively >> edit a content of a variable at the command line. I do not want to use >> GUI at all. > > A GUI makes this a trivial problem. Simply display an edit control and insert the current value as a string. Allow the user to modify it and when done read the new value back. If you don't want to use a GUI you need to provide GUI like controls yourself, either through an existing module or by writing one. Something like easygui would be eminently suitable. But even vanilla Tkinter is almost trivial. > > The curses library will do it but that is not standard on Windows and I've heard varying reports of how well it works there. > > The readline library allows basic editing of the commands line but I'm not sure how you would insert your variable into the command line initially... > > For windows there are a couple of modules available that provide low level cursor control and character input/output, so you could use one of those to write such a function. > > And if you search hard enough you should find that somebody, somewhere has already done the work for you. But I don't know where... > > -- > 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 mike_barnett at hotmail.com Sat Jun 1 08:05:16 2019 From: mike_barnett at hotmail.com (Mike Barnett) Date: Sat, 1 Jun 2019 12:05:16 +0000 Subject: [Tutor] Interactive editing of variables. In-Reply-To: <016601d51857$64d4d9b0$2e7e8d10$@gmail.com> References: <00c001d51825$20fe9730$62fbc590$@gmail.com> <016601d51857$64d4d9b0$2e7e8d10$@gmail.com> Message-ID: TL;DR There's a run-time debugger out, "imwatchingyou" that is being developed daily, fast paced, that enables debugging without stopped your code or using an IDE. I'm unsure if I'm super late to the party of if I understand the "problem" / request properly. I apologize if I'm off-base. Do you want to be able to examine and modify variables while your program is running, without breaking into it with a debugger? Is that part of it? If so, I've been working my ass off for a couple of weeks to do just that, develop a realtime "watcher" / debugger. You'll find the project here: https://github.com/PySimpleGUI/imwatchingyou At this time, you must pip install imwatchingyou. If you're concerned because of the name "imwatchingyou", it's meant to be YOU are watching YOUR code. If I wanted to watch your Python code, I would have named it "makefreemoneyminingbitcoininpython". There is a nice screenshot, although things have been changing. There are 2 primary windows. The brown one in the upper right of the screen is a realtime update of all of your local variables. Then there's the large debug window that allows you to do more including running the built-in REPL. There is also a version being built into the PySimpleGUI library itself so you do not have to do any imports, etc. All PySimpleGUI will have this watch/debug capability in it. @mike -----Original Message----- From: mhysnm1964 at gmail.com Sent: Saturday, June 1, 2019 4:53 AM To: 'Alan Gauld' ; tutor at python.org Subject: Re: [Tutor] Interactive editing of variables. As I thought. Easygui is not accessible at all with a screen reader due to the underlying graphic library. Win32 could work if I could load it. Since then I could use standard windows objects. Note, I didn't see anything in the quick scan I did ion the API. Very frustrating and disappointing. -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Saturday, 1 June 2019 4:50 PM To: tutor at python.org Subject: Re: [Tutor] Interactive editing of variables. On 01/06/2019 03:53, mhysnm1964 at gmail.com wrote: > I have no clue on how to achieve what I want to do and the code I have > creates an hash. As shown below: Thats because what you want is not a standard feature of CLI apps. You will need to do one of the following(in order of easiness): 1) Use a GUI - it then becomes a trivial matter 2) Use a pseudo GUI like curses to provide cursor control 3) Find a module that already does what you need (maybe readline can be made to work?) 4) Write a function yourself using screen primitives that manage the cursor > for row in description: > text = description_rejex(row) > if text[0] not in narration: > Result = input(text[0]) > narration[result] = text The standard tools allow you to input a new value and overwrite the existing one. But there is no simple way to interactively modify an existing value (and of course you would need to convert it to/from a string for that to be possible) > I have had a look and cannot find an example where I can interactively > edit a content of a variable at the command line. I do not want to use > GUI at all. A GUI makes this a trivial problem. Simply display an edit control and insert the current value as a string. Allow the user to modify it and when done read the new value back. If you don't want to use a GUI you need to provide GUI like controls yourself, either through an existing module or by writing one. Something like easygui would be eminently suitable. But even vanilla Tkinter is almost trivial. The curses library will do it but that is not standard on Windows and I've heard varying reports of how well it works there. The readline library allows basic editing of the commands line but I'm not sure how you would insert your variable into the command line initially... For windows there are a couple of modules available that provide low level cursor control and character input/output, so you could use one of those to write such a function. And if you search hard enough you should find that somebody, somewhere has already done the work for you. But I don't know where... -- 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 mike_barnett at hotmail.com Sat Jun 1 08:55:19 2019 From: mike_barnett at hotmail.com (Mike Barnett) Date: Sat, 1 Jun 2019 12:55:19 +0000 Subject: [Tutor] Interactive editing of variables. In-Reply-To: <3B9D4941-68CB-4765-B800-11686280CF49@gmail.com> References: <00c001d51825$20fe9730$62fbc590$@gmail.com> <016601d51857$64d4d9b0$2e7e8d10$@gmail.com> <3B9D4941-68CB-4765-B800-11686280CF49@gmail.com> Message-ID: Oh, do you need WxPython for all your GUI type work since it can be used with screen readers? A GUI like you're describing in PySimpleGUI could be 15 or 20 lines of code in total depending on the number of these fields you have. Do you happen to have a sketch of this screen? Even pencil and paper would be helpful. I mocked this up really quickly. It takes short cuts to give you a mock-up. It's possible to write little GUIs like you're talking about easily, so that they're not over the top but rather "useful". Here's a mockup running in PySimpleGUI / tkinter: https://repl.it/@PySimpleGUI/Reddit-Change-variables-GUI My estimate wasn't too far off.... 18 lines of code in total. Is this the kind of modification of variables you where talking about? @mike -----Original Message----- From: Sean Murphy Sent: Saturday, June 1, 2019 8:21 AM To: Mike Barnett Cc: Alan Gauld ; tutor at python.org Subject: Re: [Tutor] Interactive editing of variables. Mike, thanks for this debug tool. I might have a look at it. No that?s not the goal I?m after. I want to have a basic l input line which is already populated with a value. The value is extracted from a list of strength. Each element in the list are unique. I want to modify the string and then re-insert it back into the same element as the new modified string I?m currently look at read line or piegnurl module this might do the trick. Using GUI is way over the top for the simple thing I?m trying to do The model which Alan kindly suggested if not accessible with a screen reader. As I cannot see e.g. blind, this is a critical component of my programming. Please excuse any errors because I?m using Siri to send this email. My experience is the part > On 1 Jun 2019, at 10:05 pm, Mike Barnett wrote: > > TL;DR > There's a run-time debugger out, "imwatchingyou" that is being developed daily, fast paced, that enables debugging without stopped your code or using an IDE. > > I'm unsure if I'm super late to the party of if I understand the "problem" / request properly. I apologize if I'm off-base. > > Do you want to be able to examine and modify variables while your program is running, without breaking into it with a debugger? Is that part of it? > > If so, I've been working my ass off for a couple of weeks to do just that, develop a realtime "watcher" / debugger. > > You'll find the project here: > https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgith > ub.com%2FPySimpleGUI%2Fimwatchingyou&data=02%7C01%7C%7C6fb3d0274c9 > d4f43c41408d6e68b8c39%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636 > 949884355243495&sdata=D4hun2k3Al2O2cwoSbLwBTlHZDCe17t0n8ijIBoWJUw% > 3D&reserved=0 > > At this time, you must pip install imwatchingyou. If you're concerned because of the name "imwatchingyou", it's meant to be YOU are watching YOUR code. If I wanted to watch your Python code, I would have named it "makefreemoneyminingbitcoininpython". > > There is a nice screenshot, although things have been changing. There are 2 primary windows. The brown one in the upper right of the screen is a realtime update of all of your local variables. Then there's the large debug window that allows you to do more including running the built-in REPL. > > There is also a version being built into the PySimpleGUI library itself so you do not have to do any imports, etc. All PySimpleGUI will have this watch/debug capability in it. > > > > > @mike > > -----Original Message----- > From: mhysnm1964 at gmail.com > Sent: Saturday, June 1, 2019 4:53 AM > To: 'Alan Gauld' ; tutor at python.org > Subject: Re: [Tutor] Interactive editing of variables. > > As I thought. Easygui is not accessible at all with a screen reader due to the underlying graphic library. Win32 could work if I could load it. Since then I could use standard windows objects. > > Note, I didn't see anything in the quick scan I did ion the API. > > > Very frustrating and disappointing. > -----Original Message----- > From: Tutor On Behalf > Of Alan Gauld via Tutor > Sent: Saturday, 1 June 2019 4:50 PM > To: tutor at python.org > Subject: Re: [Tutor] Interactive editing of variables. > >> On 01/06/2019 03:53, mhysnm1964 at gmail.com wrote: >> >> I have no clue on how to achieve what I want to do and the code I >> have creates an hash. As shown below: > > Thats because what you want is not a standard feature of CLI apps. > You will need to do one of the following(in order of easiness): > 1) Use a GUI - it then becomes a trivial matter > 2) Use a pseudo GUI like curses to provide cursor control > 3) Find a module that already does what you need > (maybe readline can be made to work?) > 4) Write a function yourself using screen primitives > that manage the cursor > > >> for row in description: >> text = description_rejex(row) >> if text[0] not in narration: >> Result = input(text[0]) >> narration[result] = text > > The standard tools allow you to input a new value and overwrite the > existing one. But there is no simple way to interactively modify an > existing value (and of course you would need to convert it to/from a > string for that to be > possible) > >> I have had a look and cannot find an example where I can >> interactively edit a content of a variable at the command line. I do >> not want to use GUI at all. > > A GUI makes this a trivial problem. Simply display an edit control and insert the current value as a string. Allow the user to modify it and when done read the new value back. If you don't want to use a GUI you need to provide GUI like controls yourself, either through an existing module or by writing one. Something like easygui would be eminently suitable. But even vanilla Tkinter is almost trivial. > > The curses library will do it but that is not standard on Windows and I've heard varying reports of how well it works there. > > The readline library allows basic editing of the commands line but I'm not sure how you would insert your variable into the command line initially... > > For windows there are a couple of modules available that provide low level cursor control and character input/output, so you could use one of those to write such a function. > > And if you search hard enough you should find that somebody, somewhere has already done the work for you. But I don't know where... > > -- > Alan G > Author of the Learn to Program web site > https://nam04.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.a > lan-g.me.uk%2F&data=02%7C01%7C%7C6fb3d0274c9d4f43c41408d6e68b8c39% > 7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636949884355253494&sd > ata=5G%2FD8iVbgRlq%2B41o1bpa9KacdJVP9Rb%2FSIrSEJqSYAI%3D&reserved= > 0 > https://nam04.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.a > mazon.com%2Fauthor%2Falan_gauld&data=02%7C01%7C%7C6fb3d0274c9d4f43 > c41408d6e68b8c39%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C63694988 > 4355253494&sdata=LpYuRbiplkrc9fx5VlVONwxqXYBt6zf5153LbFUdawE%3D&am > p;reserved=0 > Follow my photo-blog on Flickr at: > https://nam04.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.f > lickr.com%2Fphotos%2Falangauldphotos&data=02%7C01%7C%7C6fb3d0274c9 > d4f43c41408d6e68b8c39%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636 > 949884355253494&sdata=n3LvzXSRtWaUCjCsh%2Fq6HGWYrzd4TMCDqhjZv3tR2I > c%3D&reserved=0 > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail > .python.org%2Fmailman%2Flistinfo%2Ftutor&data=02%7C01%7C%7C6fb3d02 > 74c9d4f43c41408d6e68b8c39%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7 > C636949884355253494&sdata=S9%2FGkEGX2elD4heaEL4Typ4%2FfeMWFhx1PEE1 > tsIlAhE%3D&reserved=0 > > > From alan.gauld at yahoo.co.uk Sat Jun 1 15:01:14 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 1 Jun 2019 20:01:14 +0100 Subject: [Tutor] Interactive editing of variables. In-Reply-To: <016601d51857$64d4d9b0$2e7e8d10$@gmail.com> References: <00c001d51825$20fe9730$62fbc590$@gmail.com> <016601d51857$64d4d9b0$2e7e8d10$@gmail.com> Message-ID: On 01/06/2019 09:52, mhysnm1964 at gmail.com wrote: > the underlying graphic library. Win32 could work if I could load it. Since > then I could use standard windows objects. If you are running windows then you can access the Win32 DLLs via ctypes. The win32 package should also be easily installable as a binary. If not try using the ActiveState distribution of python because it bundles all the windows tools in the installer. Personally I always use ActiveState pyton for my Windows boxes. -- 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 Jun 1 15:24:14 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 1 Jun 2019 13:24:14 -0600 Subject: [Tutor] Interactive editing of variables. In-Reply-To: References: <00c001d51825$20fe9730$62fbc590$@gmail.com> <013401d5184f$55ddcd20$01996760$@gmail.com> Message-ID: <785e2291-4531-48c9-65a7-7cb161e16d39@wichmann.us> >> The issue I have with a lot of GUI programs built for Python they generally >> fail in the accessibility department for a screen reader. > > I can't help there I have nearly zero experience of using accessibility > tools. But I'd expect any GUI toolkit to work with the standard > OS tools. After all they are ultimately all built using the > underlying primitive GUI API On the gui front, tk developers make no bones about tk not having been built with accessibility considerations tk (and thus tkinter which is just the Python binding to tk), is not going to work with a screen reader. wxPython is probably the best choice, it explicitly has support (although sadly only for Windows): https://docs.wxpython.org/wx.Accessible.html possibly some of the other toolkits do - I wouldn't rule out Qt either, but haven't any experience. From cs at cskk.id.au Sat Jun 1 19:35:35 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 2 Jun 2019 09:35:35 +1000 Subject: [Tutor] is this doable In-Reply-To: References: Message-ID: <20190601233535.GA63981@cskk.homeip.net> On 01Jun2019 10:02, nathan tech wrote: >Now it has been mentioned, I do recall, on linux, briefly playing with >psutil to retrieve memory values for a few tasks I keep running on my >server. > >To that end, I think I now know roughly what to do. > > >Something like this: > >import psutil >import os > >path=os.getcwd()+"\\program.exe" Remark: try: r'\program.exe' instead of: "\\program.exe" because backslash escapes have meaning inside single or double quotes it is generally more reliable to use raw strings (r'....') for such strings to avoid accidents. >slist=[] >for x in psutil.process_iter(): > ?if(x.exe()==path): > ? slist.append([x, x.create_time]_) > >r.sort() # not sure how to sort by second element, but I'd sort the list >by start time list.sort() and sorted() accept an optional key= parameter which is a function that takes an element and returns the sort key. For example (untested): r.sort(key=lambda x: x[1]) to sort on x[1]. >if(len(r)>1): Stylisticly we tend to just write: if r: It is a convention in Python that collections (such as lists) are "false" if they are empty. Urr, I see you said >1, not >0; this remark probably isn't relevant. But you could drop the outer brackets, not needed in Python: if len(r) > 1: It sounds like your "task" is then a running instance of "program.exe", yes? In that case the usual practive is to keep a "pid file" around with a distinctive pathname associated with the task. That is just a text file containing the process id of the task program. So rather than iterating over the process list with psutils (which will show you _other_ instances of program.exe, perhaps being run for another purpose), you just read the pid from the pid file and see if it is running. If it is, assume the task is already active. Untested incomplete example: pid_filepath = 'taskname.pid' try: pid = int(open(pid_filepath).read().strip()) os.kill(pid, 0) except (OSError, ValueError): # missing pid file, invalid contents) running = False else: running = True The "os.kill(pid, 0)" is a UNIX specific idiom for testing for a process; you can send signal 0 to a process you own successfully (the process itself never sees it); it will fail if the process doesn't exist or isn't yours. There should be a Windows equivalent for probing a process. The converse part where you start the process includes this: P = subprocess.Popen(.....) # start the program with open(pid_filename, 'w') as pidf: print(P.pid, file=pidf) to update the process id file. Cheers, Cameron Simpson From mhysnm1964 at gmail.com Sat Jun 1 19:47:07 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sun, 2 Jun 2019 09:47:07 +1000 Subject: [Tutor] Interactive editing of variables. In-Reply-To: References: <00c001d51825$20fe9730$62fbc590$@gmail.com> <016601d51857$64d4d9b0$2e7e8d10$@gmail.com> Message-ID: <006401d518d4$53dc7d70$fb957850$@gmail.com> Allan, That is what I have done before I went to bed. Installed ActiveState and using Python 3.6 as that is the release build they have up on their site. -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Sunday, 2 June 2019 5:01 AM To: tutor at python.org Subject: Re: [Tutor] Interactive editing of variables. On 01/06/2019 09:52, mhysnm1964 at gmail.com wrote: > the underlying graphic library. Win32 could work if I could load it. > Since then I could use standard windows objects. If you are running windows then you can access the Win32 DLLs via ctypes. The win32 package should also be easily installable as a binary. If not try using the ActiveState distribution of python because it bundles all the windows tools in the installer. Personally I always use ActiveState pyton for my Windows boxes. -- 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 mhysnm1964 at gmail.com Sat Jun 1 20:10:54 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sun, 2 Jun 2019 10:10:54 +1000 Subject: [Tutor] Interactive editing of variables. In-Reply-To: <785e2291-4531-48c9-65a7-7cb161e16d39@wichmann.us> References: <00c001d51825$20fe9730$62fbc590$@gmail.com> <013401d5184f$55ddcd20$01996760$@gmail.com> <785e2291-4531-48c9-65a7-7cb161e16d39@wichmann.us> Message-ID: <009a01d518d7$a6aeb380$f40c1a80$@gmail.com> Mike, Allan and Matt, Thanks for the information and help. I will check out Mike's code and have a play. Allan you provided some good resources and based upon what Matt stated before and using Easegui. This is not a path I can use. Thanks for the help anyway. Matt, a shame. I am not sure what can be done in this area for TK, as it is open source other than someone with the knowledge introducing accessibility. At the level required to provide the required accessibility framework for different platforms for the default UI controls , is far beyond my skill level. I believe later versions of QT support iaccess2 framework which is an accessibility framework. I know from a legal point of view. If a developer or company built a product based upon the GUI environments and sold it to the USA Government or to the public. They are opening themselves to potential legal action. This concept applies in other countries. Accessibility is on a up swing and Microsoft, Apple, Google, Cisco and others are focusing on this area due to the change in the landscape. What I do not know, how this applies to open source. If there is no commercial transaction. Then this is the area I am unsure if any of the laws I am indirectly referring to impact. Anyway, this is getting off scope. Just highlighting so people are aware. Accessibility is a part of best practice for UX, UI and development. -----Original Message----- From: Tutor On Behalf Of Mats Wichmann Sent: Sunday, 2 June 2019 5:24 AM To: tutor at python.org Subject: Re: [Tutor] Interactive editing of variables. >> The issue I have with a lot of GUI programs built for Python they >> generally fail in the accessibility department for a screen reader. > > I can't help there I have nearly zero experience of using > accessibility tools. But I'd expect any GUI toolkit to work with the > standard OS tools. After all they are ultimately all built using the > underlying primitive GUI API On the gui front, tk developers make no bones about tk not having been built with accessibility considerations tk (and thus tkinter which is just the Python binding to tk), is not going to work with a screen reader. wxPython is probably the best choice, it explicitly has support (although sadly only for Windows): https://docs.wxpython.org/wx.Accessible.html possibly some of the other toolkits do - I wouldn't rule out Qt either, but haven't any experience. _______________________________________________ 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 Sun Jun 2 09:15:30 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 2 Jun 2019 14:15:30 +0100 Subject: [Tutor] Interactive editing of variables. In-Reply-To: <009a01d518d7$a6aeb380$f40c1a80$@gmail.com> References: <00c001d51825$20fe9730$62fbc590$@gmail.com> <013401d5184f$55ddcd20$01996760$@gmail.com> <785e2291-4531-48c9-65a7-7cb161e16d39@wichmann.us> <009a01d518d7$a6aeb380$f40c1a80$@gmail.com> Message-ID: On 02/06/2019 01:10, mhysnm1964 at gmail.com wrote: > What I do not know, how this applies to open source. If there is no > commercial transaction. Then this is the area I am unsure if any of the laws > I am indirectly referring to impact. Caveat: I am not a lawyer... I know it has a big impact on commercial software but that has some potential return on investment in terms of increased market share. For open source it is hard to see the return and the most likely scenario would be to greatly reduce the availability of such software. Consider the potential impact of applying such requirements to open source or other "free" software. This would potentially mean that any software that was shared or distributed in any way would need to comply. That would include a bit of code I knocked together for my own benefit and my friend wanted to get a copy. I've got to say, sorry I need to make it accessible first... now consider that my friend and I are working on some scientific research, maybe even medical research into treatments for blindness or cancer or whatever. Now I can't share my research tools with other researchers because I lack the time and or knowledge to convert the software to be accessible. The implications are that a lot of software would never see the light of day regardless of the potential benefits it could deliver. It would be a foolish law which prevented (or even substantially delayed) progress in the name of making all software accessible. It would also be very difficult to enforce since you would need to either prohibit the sharing of all non-accessible software or somehow, arbitrarily, define what constitutes regulated "distribution". Legislating for commercial distribution is much easier and more justifiable since profits are available to pay for the extra effort/costs. Where no profit exists then distribution is much less easily defined; it would be a legal minefield I suspect. > Anyway, this is getting off scope. Just highlighting > so people are aware. Accessibility is a part of best practice > for UX, UI and development. Indeed, and in an ideal world all of the building blocks would incorporate it at the foundation level. Unfortunately we are a long way from that. Also, in most cases the supporting hardware is not readily available to deliver the potential accessibility options that could be provided. It is very difficult to write software for features that you cannot test (think cursor control via blow tube or eye movement or command input by tones. All possible now but need specialist kit which individual developers won't have.) It is hard enough testing software for all the "standard" forms of input - mice, pens, touch screens, keyboards, voice, etc.... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Tue Jun 4 04:49:17 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 4 Jun 2019 18:49:17 +1000 Subject: [Tutor] Interactive editing of variables. In-Reply-To: <00c001d51825$20fe9730$62fbc590$@gmail.com> References: <00c001d51825$20fe9730$62fbc590$@gmail.com> Message-ID: <20190604084917.GB4221@ando.pearwood.info> Hi Sean, On Sat, Jun 01, 2019 at 12:53:00PM +1000, mhysnm1964 at gmail.com wrote: > I have had a look and cannot find an example where I can interactively edit > a content of a variable at the command line. I do not want to use GUI at > all. As this is a simple program only requiring CLI. I have no problems > showing the prompt, but cannot insert text into the edit (input) area. Any > ideas? Let me see if I understand what you are asking for. The built-in function "input()" takes one argument, the prompt, and lets the user type a response. So if I run the function: input("What is your name?") the terminal will display the prompt, and put the text cursor next to it. That is how things work now. I think what you are asking for is the ability to pass a second argument to the function, like this: input("What is your name?", "George") and the terminal will display the prompt, then place the word "George" next to the prompt in the edit-zone. The user (you) can then backspace over the word and type something new, or otherwise edit it. Is that what you want? If so, then on Linux or Unix systems, you can do this: import readline def myinput(prompt='', initial=''): readline.set_startup_hook(lambda: readline.insert_text(initial)) try: response = input(prompt) finally: readline.set_startup_hook(None) return response The bad news is that this only works on Linux and other Unix systems with readline installed (which nearly all of them do). It won't work under Windows. But if you install the third-party library pyreadline, you *may* be able to use that instead: import pyreadline as readline and the rest might work. (I don't have Windows and can't try it.) -- Steven From mats at wichmann.us Tue Jun 4 08:08:03 2019 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 4 Jun 2019 06:08:03 -0600 Subject: [Tutor] Interactive editing of variables. In-Reply-To: <20190604084917.GB4221@ando.pearwood.info> References: <00c001d51825$20fe9730$62fbc590$@gmail.com> <20190604084917.GB4221@ando.pearwood.info> Message-ID: On 6/4/19 2:49 AM, Steven D'Aprano wrote: > The bad news is that this only works on Linux and other Unix systems > with readline installed (which nearly all of them do). It won't work > under Windows. > > But if you install the third-party library pyreadline, you *may* be able > to use that instead: > > import pyreadline as readline > > and the rest might work. (I don't have Windows and can't try it.) you don't need to change code, the installation of pyreadline also adds a stub so that "import readline" works. From unitedclean04 at hotmail.com Tue Jun 4 08:40:24 2019 From: unitedclean04 at hotmail.com (tom milikic) Date: Tue, 4 Jun 2019 12:40:24 +0000 Subject: [Tutor] Fw: Download issues for Python In-Reply-To: References: , Message-ID: Hi my name is Tom and I come from Brisbane in Australia. I cannot seem to download Python on my computer as I really need it to help with my Quantitative Finance studies. It seems to say that some program is missing from my computer? What do I do? I am running Windows and a Toshiba computer. I have looked in the help section and I still cant seem to fix or find the issue with why it is not downloading onto my computer? It states that this is missing from my computer -: api-ms-win-crt-runtime-l1-1-0.dl Also Python 3.7.2 and Python 3.7.3 appear in my download folders but when I click them nothing happens? If you can help would be much appreciated Thanks Tom [https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif] Virus-free. www.avast.com From grant.b.edwards at gmail.com Tue Jun 4 11:17:51 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Tue, 4 Jun 2019 15:17:51 -0000 (UTC) Subject: [Tutor] Fw: Download issues for Python References: Message-ID: On 2019-06-04, tom milikic wrote: > [...] > > It states that this is missing from my computer -: > > api-ms-win-crt-runtime-l1-1-0.dl [You left off the second 'l'. Precision is very important when dealing with software problems.] You can often the answer just by Googling the error message: https://www.google.com/search?q=python+install+missing+api-ms-win-crt-runtime-l1-1-0.dll&oq=python+install+missing+api-ms-win-crt-runtime-l1-1-0.dll -- Grant Edwards grant.b.edwards Yow! I'm thinking about at DIGITAL READ-OUT systems gmail.com and computer-generated IMAGE FORMATIONS ... From nathan-tech at hotmail.com Tue Jun 4 19:37:23 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Tue, 4 Jun 2019 23:37:23 +0000 Subject: [Tutor] would someone please explain this concept to me Message-ID: Hi there, So I have just fixed a huge bug in my program, but don't understand exactly... How it bugged. Confused? I sure was. Here's some code: globals.py: feeds={} blank_feed={} blank_feed["checked"]=1 blank_feed["feed"]=0 main file: import globals as g # some code that loads a feed into the variable knm g.feeds[link]=g.blank_feed; g.feeds[link]["feed"]=knm #in the below code, the variable link has a different value: # load a feed into the variable r g.feeds[link]=g.blank_feed g.feeds[link]["feed"]=r Now at this point, python would set the first loaded feed to the same thing as the second loaded feed. It also set g.blank_feed to the second feed, as well. I replaced the last three lines with this: # load a feed into the variable r g.feeds[link]=g.blank_feed; g.feeds[link]["feed"]=r And it works. but why does it work? Why does that semi unlink all the variables? Thanks Nathan From alan.gauld at yahoo.co.uk Tue Jun 4 20:52:43 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 5 Jun 2019 01:52:43 +0100 Subject: [Tutor] would someone please explain this concept to me In-Reply-To: References: Message-ID: On 05/06/2019 00:37, nathan tech wrote: > Hi there, > > So I have just fixed a huge bug in my program, but don't understand > exactly... How it bugged. Neither do I, your explanation raises more questions than answers. > globals.py: > feeds={} > blank_feed={} > blank_feed["checked"]=1 > blank_feed["feed"]=0 > > > main file: > > import globals as g > # some code that loads a feed into the variable knm Your terminology is odd here. You are not loading anything into knm, rather you are assigning the value of knm to the "feed" dictionary entry. > g.feeds[link]=g.blank_feed; > g.feeds[link]["feed"]=knm > > #in the below code, the variable link has a different value: > > # load a feed into the variable r Similarly this assigns the value of r to the "feed" entry - the same "feed" entry as for knm so you have now overwritten the knm value in the dictionary.. > g.feeds[link]=g.blank_feed > g.feeds[link]["feed"]=r > Now at this point, python would set the first loaded feed to the same > thing as the second loaded feed. It also set g.blank_feed to the second > feed, as well. Sorry, I have no idea what you mean by that. Which is the "first loaded feed"? knm?, r or the feeds dictionary? And what is the "second loaded feed"? What do you mean by a "feed" in this context? You are setting g.feeds[link] to g.blank_feed in both cases. Your line g.feeds[link]["feed"]=r is exactly the same as if you typed g.blank_feed["feed"]=r If you set the knm and r variables to fixed integer values (say 42 and 666) that would allow you to tell us what variable has what value. And what you expected them to store. (The fact that knm and r may normally contain some other kind of object is not relevant to this question.) > I replaced the last three lines with this: > > # load a feed into the variable r > g.feeds[link]=g.blank_feed; > g.feeds[link]["feed"]=r > > And it works. What works? What is different? I still don't know what you think should be happening, what is happening first time round, and what is now happening. > but why does it work? > > Why does that semi unlink all the variables? The semicolon should not "unlink all variables", whatever that means. But I have no idea what you are seeing so cannot comment any further. You need to give us more specifics. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Tue Jun 4 21:08:17 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 5 Jun 2019 11:08:17 +1000 Subject: [Tutor] would someone please explain this concept to me In-Reply-To: References: Message-ID: <20190605010817.GF4221@ando.pearwood.info> On Tue, Jun 04, 2019 at 11:37:23PM +0000, nathan tech wrote: > globals.py: > > feeds={} > blank_feed={} > blank_feed["checked"]=1 > blank_feed["feed"]=0 That is more easily, and better, written as: feeds = {} blank_feed = {"checked": 1, "feed": 0} > main file: > > import globals as g > # some code that loads a feed into the variable knm Do you mean something like this? If so, you should say so. knm = "some feed" > g.feeds[link]=g.blank_feed; What's "link" here? And there is no need for the semi-colon. > g.feeds[link]["feed"]=knm Right... what the above line does is *precisely* the same as g.blank_feed["feed"] = knm Follow the program logic. I'm inserting 1970s BASIC style line numbers to make it easier to discuss the code, remember that you can't actually do that in Python. 10: g.feeds[link] = g.blank_feed 20: g.feeds[link]["feed"] = knm Line 10 sets g.feeds[link] to the dict "blank_feed". *Not* a copy: you now have two ways of referring to the same dict: "g.blank_feed" and "g.feeds[link]" both refer to the one dict, just as "Nathan" and "Mr Tech" are two ways of referring to the same person (you). So line 20 does this: - look for the name "g", which gives the "globals.py" module; - inside that module, look for the name "feeds", which gives the "feeds" dict; - look inside that dict for the key "link" (whatever value that currently holds), which by line 10 has been set to the same dict "blank_feed". - inside the blank_feed dict, set key "feed" to "knm". > #in the below code, the variable link has a different value: > # load a feed into the variable r Something like this? r = "a different feed" > g.feeds[link]=g.blank_feed Now you have *three* ways of naming the same dict: "g.blank_feed", "g.feeds[link]", "g.feeds[different_link]" but they all point to the same dict. > g.feeds[link]["feed"]=r > > > Now at this point, python would set the first loaded feed to the same > thing as the second loaded feed. It also set g.blank_feed to the second > feed, as well. No, there is only one feed in total. You just keep updating the same feed under different names. > I replaced the last three lines with this: > > # load a feed into the variable r > g.feeds[link]=g.blank_feed; > g.feeds[link]["feed"]=r > > And it works. I don't see any difference between the replacement code and the original code. The code you show does exactly the same thing. > but why does it work? > > Why does that semi unlink all the variables? Semi-colon? It doesn't. You must have made other changes as well, semi-colons don't have any runtime effect. They are *purely* syntax to tell the parser to seperate multiple statements on one line. -- Steven From steve at pearwood.info Tue Jun 4 21:14:54 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 5 Jun 2019 11:14:54 +1000 Subject: [Tutor] would someone please explain this concept to me In-Reply-To: References: Message-ID: <20190605011453.GG4221@ando.pearwood.info> In case you are still confused, we can simplify the whole process by eliminating the unnecessary use of an external module. blank = {} feeds = {} feeds['a'] = blank feeds['a'][1] = "Hello" print(blank) # will print {1: 'Hello'} We can see that the dict named "blank" and the dict named "feeds['a']" are actually the same dict: print(feeds['a'] is blank) # Not the same as == (equals). In case you still need more evidence: feeds['b'] = blank feeds['b'][1] = "Goodbye" print(blank) print(feeds) will print: {1: 'Goodbye'} {'a': {1: 'Goodbye'}, 'b': {1: 'Goodbye'}} (possibly the a and the b will be swapped around). -- Steven From nathan-tech at hotmail.com Wed Jun 5 06:47:09 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Wed, 5 Jun 2019 10:47:09 +0000 Subject: [Tutor] Fwd: Re: would someone please explain this concept to me In-Reply-To: References: Message-ID: Thought I addressed this to the list... Aparrently my mail client hates me. -------- Forwarded Message -------- Subject: Re: [Tutor] would someone please explain this concept to me Date: Wed, 5 Jun 2019 02:37:49 +0100 From: nathan tech To: Steven D'Aprano Hiya, Thanks, first, for being able to understand what I was asking. I admit I read over my own email and went, uh... I think I need to rewrite that. So, I see what you are saying there, and it makes sense. I want to actually do the following though: d={} # a dict of some kind feed1=somefeed feed2=another feed of some kind d["feed 1's url"]=feed1 d["feed 2's url"]=feed2 The way I do this is there are certain properties, if you were, that also need to be included, I.e, each entry should be its own dict containing feed, last check, check regularity, ETC. That is why I was going to use g.blank-feed as the template, assign that to d["feed 1's link"] then just update the feed part. Is there a way to do this? Thanks Nathan On 05/06/2019 02:08, Steven D'Aprano wrote: On Tue, Jun 04, 2019 at 11:37:23PM +0000, nathan tech wrote: globals.py: feeds={} blank_feed={} blank_feed["checked"]=1 blank_feed["feed"]=0 That is more easily, and better, written as: feeds = {} blank_feed = {"checked": 1, "feed": 0} main file: import globals as g # some code that loads a feed into the variable knm Do you mean something like this? If so, you should say so. knm = "some feed" g.feeds[link]=g.blank_feed; What's "link" here? And there is no need for the semi-colon. g.feeds[link]["feed"]=knm Right... what the above line does is *precisely* the same as g.blank_feed["feed"] = knm Follow the program logic. I'm inserting 1970s BASIC style line numbers to make it easier to discuss the code, remember that you can't actually do that in Python. 10: g.feeds[link] = g.blank_feed 20: g.feeds[link]["feed"] = knm Line 10 sets g.feeds[link] to the dict "blank_feed". *Not* a copy: you now have two ways of referring to the same dict: "g.blank_feed" and "g.feeds[link]" both refer to the one dict, just as "Nathan" and "Mr Tech" are two ways of referring to the same person (you). So line 20 does this: - look for the name "g", which gives the "globals.py" module; - inside that module, look for the name "feeds", which gives the "feeds" dict; - look inside that dict for the key "link" (whatever value that currently holds), which by line 10 has been set to the same dict "blank_feed". - inside the blank_feed dict, set key "feed" to "knm". #in the below code, the variable link has a different value: # load a feed into the variable r Something like this? r = "a different feed" g.feeds[link]=g.blank_feed Now you have *three* ways of naming the same dict: "g.blank_feed", "g.feeds[link]", "g.feeds[different_link]" but they all point to the same dict. g.feeds[link]["feed"]=r Now at this point, python would set the first loaded feed to the same thing as the second loaded feed. It also set g.blank_feed to the second feed, as well. No, there is only one feed in total. You just keep updating the same feed under different names. I replaced the last three lines with this: # load a feed into the variable r g.feeds[link]=g.blank_feed; g.feeds[link]["feed"]=r And it works. I don't see any difference between the replacement code and the original code. The code you show does exactly the same thing. but why does it work? Why does that semi unlink all the variables? Semi-colon? It doesn't. You must have made other changes as well, semi-colons don't have any runtime effect. They are *purely* syntax to tell the parser to seperate multiple statements on one line. From alan.gauld at yahoo.co.uk Wed Jun 5 14:45:33 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 5 Jun 2019 19:45:33 +0100 Subject: [Tutor] Fwd: Re: would someone please explain this concept to me In-Reply-To: References: Message-ID: > That is why I was going to use g.blank-feed as the template, > assign that to d["feed 1's link"] then just update the feed part. The simplest way is just to assign a new blank dictionary. Don;t assign the same dictionary to each feed. (Incidentally your description above is much clearer than the one you initially posted!) feeds[link] = {key1:value1, key2:value2} If you need to pre-populate the dictionary with many values when you assign it (or need to compute the values) I'd recommend writing a small function that creates a new dictionary, and adds the values then returns the dictionary. Or maybe, better still, use a class and populate the feeds dictionary with instances of the class. class Feed: def __init__(self, val1=default1, val2=default2, val3=default3): self.key1 = val1 self.key2 = val2 self.key3 = val3 feeds[link1] = Feed(v1,v2,v3) feeds[link2] = Feed() # use default values After all that's exactly what a class is - a template for an object. What you definitely don't want to do is what you have been doing and assigning the same single dictionary object to each link entry. -- 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 nathan-tech at hotmail.com Wed Jun 5 15:47:46 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Wed, 5 Jun 2019 19:47:46 +0000 Subject: [Tutor] Fwd: Re: would someone please explain this concept to me In-Reply-To: References: Message-ID: hi there, Thanks for that, I never thought of those options, though now you mention them, I'm going, duh! I also, defined in globals, have a variable called checklimit. EG: g.checklimit. Am I ok to assign that to values? so for example if I do: feeds[feed1]["limit"]=g.checklimit And later did g.checklimit=7000 Would it change feeds[feed1]["limit"] too? Thanks Nathan On 05/06/2019 19:45, Alan Gauld via Tutor wrote: >> That is why I was going to use g.blank-feed as the template, >> assign that to d["feed 1's link"] then just update the feed part. > The simplest way is just to assign a new blank dictionary. Don;t assign > the same dictionary to each feed. (Incidentally your description above > is much clearer than the one you initially posted!) > > feeds[link] = {key1:value1, key2:value2} > > If you need to pre-populate the dictionary with many values when > you assign it (or need to compute the values) I'd recommend > writing a small function that creates a new dictionary, > and adds the values then returns the dictionary. > > Or maybe, better still, use a class and populate the feeds > dictionary with instances of the class. > > class Feed: > def __init__(self, val1=default1, val2=default2, val3=default3): > self.key1 = val1 > self.key2 = val2 > self.key3 = val3 > > feeds[link1] = Feed(v1,v2,v3) > feeds[link2] = Feed() # use default values > > After all that's exactly what a class is - a template for an object. > > What you definitely don't want to do is what you have been > doing and assigning the same single dictionary object to each > link entry. > From alan.gauld at yahoo.co.uk Wed Jun 5 19:57:36 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 6 Jun 2019 00:57:36 +0100 Subject: [Tutor] Fwd: Re: would someone please explain this concept to me In-Reply-To: References: Message-ID: On 05/06/2019 20:47, nathan tech wrote: > so for example if I do: > > feeds[feed1]["limit"]=g.checklimit > > And later did g.checklimit=7000 > > Would it change feeds[feed1]["limit"] too? No, because the feeds value is still referencing the original value object. The issue arises when you modify a mutable object that is references by two (or more) variables. If the value is immutable then the references will retain the original value. Specifically, in your case. The first example you set the feeds value to a dictionary. Then you modified the contents of the dictionary but did not change the dictionary itself. base_dict = {} # create object feeds['foo'] = base_dict # reference to same dict object base_dict['x'] = bar # modified dict referred to by both variables But in the second example you actially change the object that checklimit refers to. checklimit = 22 # immutable value assigned feeds['bar'] = checklimit # both refer to same immutable value base_var = 66 # now feeds refers to original object: 22 # and checklimit refers to new object: 66 In the first case you do not change the object that base_dict refers to, you only change its content. In the second case you make checklimit refer to a completely new object. Does that make sense? PS. Notice that the use of a globals module, g, is completely irrelevant to this issue. It has nothing to do with the values being in a module, the issue is purely about references to objects and whether you modify the referenced object or its contents. -- 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 nathan-tech at hotmail.com Wed Jun 5 20:47:49 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Thu, 6 Jun 2019 00:47:49 +0000 Subject: [Tutor] Fwd: Re: would someone please explain this concept to me In-Reply-To: References: Message-ID: Hi alan, thanks so much for clearing that up. Now you've explained it in that way, I understand what it is doing and how it went wrong. Thank you so much! Nathan On 06/06/2019 00:57, Alan Gauld via Tutor wrote: > On 05/06/2019 20:47, nathan tech wrote: > >> so for example if I do: >> >> feeds[feed1]["limit"]=g.checklimit >> >> And later did g.checklimit=7000 >> >> Would it change feeds[feed1]["limit"] too? > No, because the feeds value is still referencing the > original value object. The issue arises when you modify a mutable > object that is references by two (or more) variables. If the value > is immutable then the references will retain the original value. > > Specifically, in your case. > The first example you set the feeds value to a dictionary. Then you > modified the contents of the dictionary but did not change the > dictionary itself. > > base_dict = {} # create object > feeds['foo'] = base_dict # reference to same dict object > base_dict['x'] = bar # modified dict referred to by both variables > > > But in the second example you actially change the object > that checklimit refers to. > > checklimit = 22 # immutable value assigned > feeds['bar'] = checklimit # both refer to same immutable value > base_var = 66 # now feeds refers to original object: 22 > # and checklimit refers to new object: 66 > > In the first case you do not change the object that base_dict refers to, > you only change its content. In the second case you make checklimit > refer to a completely new object. > > Does that make sense? > > PS. Notice that the use of a globals module, g, is completely irrelevant > to this issue. It has nothing to do with the values being in a module, > the issue is purely about references to objects and whether you modify > the referenced object or its contents. > From alan.gauld at yahoo.co.uk Thu Jun 6 03:41:00 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 6 Jun 2019 08:41:00 +0100 Subject: [Tutor] Fwd: Re: would someone please explain this concept to me In-Reply-To: References: Message-ID: On 06/06/2019 00:57, Alan Gauld via Tutor wrote: > But in the second example you actially change the object > that checklimit refers to. > > checklimit = 22 # immutable value assigned > feeds['bar'] = checklimit # both refer to same immutable value > base_var = 66 # now feeds refers to original object: 22 > # and checklimit refers to new object: 66 Oops, base_var should of course be checklimit! I started out using base_var then changed it to checklimit to match the original code. But this one slipped through unchanged. Sorry. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From gursimran.maken at gmail.com Fri Jun 7 13:42:56 2019 From: gursimran.maken at gmail.com (Gursimran Maken) Date: Fri, 7 Jun 2019 23:12:56 +0530 Subject: [Tutor] Pickles and Shelves Concept Message-ID: Hi All, I am not getting the concept of pickle and shelves in python, I mean what's the use of both the concepts, when to use them in code instead of using file read and write operations. Could anyone please explain me the concepts. Thank you, Gursimran. From david at graniteweb.com Fri Jun 7 19:14:12 2019 From: david at graniteweb.com (David Rock) Date: Fri, 7 Jun 2019 18:14:12 -0500 Subject: [Tutor] Pickles and Shelves Concept In-Reply-To: References: Message-ID: > On Jun 7, 2019, at 12:42, Gursimran Maken wrote: > > Hi All, > > I am not getting the concept of pickle and shelves in python, I mean what's > the use of both the concepts, when to use them in code instead of using > file read and write operations. > > Could anyone please explain me the concepts. The simplest way to look at it is they are ways to save python data objects to disk. That way, you can have a dataset in an already-usable format for later on use. By contrast, file read/write is usually for raw data in/out in an unprocessed form that is not readily usable in your program. So you would see something like this: read data from file store data in a dictionary pickle and shelve the dictionary for later Then later on? grab the shelved pickle access the dict that was shelved If the data you are working with will always be fresh/new, the file reads is probably more usable. The shelving of data is most useful for easier retrieval later so you don?t have to re-process the raw data every time. ? David Rock david at graniteweb.com From alan.gauld at yahoo.co.uk Fri Jun 7 20:02:43 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 8 Jun 2019 01:02:43 +0100 Subject: [Tutor] Pickles and Shelves Concept In-Reply-To: References: Message-ID: On 07/06/2019 18:42, Gursimran Maken wrote: > I am not getting the concept of pickle and shelves in python, I mean what's > the use of both the concepts, when to use them in code instead of using > file read and write operations. You are right in that you could save all your data to a file using read/write primitives. Of course you'd need to convert them all to/from strings. And all collections would need to be iterated over (and if the collection contained other collections they'd need to be iterated too). But with pickle you can just save the entire data structure with all its contents, in a single operation and it can be a collection (of any type and depth) and the contents can be any kind of mixed data. You don't need to care, pickle takes care of it all for you. And you can read it back again in a single operation and all the data will be restored to its original type and location. But with pickle you still need to store things in sequence and read them back out in the sequence that you stored them. If all you are doing is saving the state of your program and restoring it that isn't a problem. But if you only want to retrieve one piece of your data later then its a bit of a nuisance. That's where shelve comes in. By acting like a persistent dictionary you can assign keys to your data and then restore it in any sequence, or only restore some of it, you can. Does that help? -- 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 martin at linux-ip.net Sat Jun 8 01:11:55 2019 From: martin at linux-ip.net (Martin A. Brown) Date: Fri, 7 Jun 2019 22:11:55 -0700 Subject: [Tutor] Pickles and Shelves Concept In-Reply-To: References: Message-ID: Hello, >I am not getting the concept of pickle and shelves in python, I >mean what's the use of both the concepts, when to use them in code >instead of using file read and write operations. > >Could anyone please explain me the concepts. I see you already have two answers (from David Rock and Alan Gauld). I will add a slightly different answer and try also to explain some of the history (at a very high level). * Programs need to take input from "somewhere". * Programs need data structures in memory on which to operate. There are many different ways to store data "somewhere" and in order to create the data structures in memory (on which your program will operate), you need to have code that knows how to read that data from "somewhere". So, there's data that has been written to disk. I often call this the serialized form form of the data [0]. There different usages for serialization, but ones I'll talk about are the serialized formats that we typically write to disk to store data for a program to read. Here are a few such serialized formats: * JSON or XML (homage: SGML) * pickles and shelves * GNU dbm, ndbm, cdb, rocksdb and probably 1 million others * custom binary formats * plain text files (be careful with your encoding...) Digression: You might ask... what about SQL? Technically, the serialization is something that the SQL database software takes care of and your application doesn't. So no need to know about the serialized format. This can be freeing, at some complexity. But, back to your question. Every one of the serialized formats comes with some advantages and some disadvantages. Some are easy. Some are flexible. Other formats are structured with bindings in many languages. Some are tied closely to a single language or even specific language versions. Some formats are even defined by a single application or program that somebody has written. What about pickle and shelve? Where do they fit? Both pickle and shelve are well-maintained and older Python-specific formats that allow you to serialize Python objects and data structures to disk. This is extremely convenient if you are unlikely to change Python versions or to change your data structures. Need your program to "remember" something from a prior run? When it starts up, it can read a ./state.pickle straight into memory, pick up where it left off and perform some operation, and then, when complete, save the dat astructure back to ./state (or more safely to a new file ./state.$timestamp) and exit. This is a convenient way to store Python objects and data structures. Advantage: Native Python. Dead simple to use (you still have to be careful about file-writing logic, overwriting old files can be bad, but it's a bit up to you). You can dump many Python data structures and objects to disk. Disadvantages: Files are only be readable by Python (excluding motivated implementers in other languages). If you would like to use pickle or shelve, please ask again on this list for specific advice on these. The shelve module is intended to make it easy to have a data structure in memory that is backed by a data file on disk. This is very similar to what the dbm module also offers. The pickle module is more geared toward loading an entire data structure from the disk into memory. There are other options, that have been used for decades (see below my sig for an incomplete and light-hearted history of serialization in the digital world). The option for serialization formats and accessing them from Python are many. Pickle and shelve are very Python specific, but will be very easy to use and will be more forgiving if you happen to try to store some code as well as "pure" data. If you are going to need to exchange data with other programs, consider JSON. Reading and writing to JSON format is as easy as reading and writing to a shelve (which is a Python pickle format under the hood). Here's a two liner that will take the environment of a running program and dump that into a human- and machine-readable JSON format. Step A: import os, sys, json json.dump(dict(os.environ), sys.stdout, indent=4, sort_keys=True) Now, let's say that you want to read that in another program (and I'll demonstrate just dumping the in-memory representation to your terminal). Step B: import sys, json, pprint pprint.pprint(json.load(sys.stdin)) So, going back to your original question. >I am not getting the concept of pickle and shelves in python, I >mean what's the use of both the concepts, when to use them in code >instead of using file read and write operations. You can, of course, use file read / write operations whenever you need to load data into memory from the disk (or "somewhere") or to write data from memory into the disk (or "somewhere"). The idea behind tools and libraries like... pickle and shelve (which are Python-specific), JSON (flexible and used by many languages and web applications), XML (massively flexible, but offering an astonishing mind-boggling array of controls and tools on the data) text or binary files (ultimate flexibility and responsibility for the application developer) ...is to make your job as a programmer easier, by offering you libraries and standards that support some of the difficult aspects of data validation, encoding and even logic. Each format can impose some limitation -- and choosing the right tool can be tricky, because it depends on how much control you need, how many languages you need to support and what you're hoping to accomplish. If you are unsure and you are just beginning to explore serialization options, I would suggest learning very well how to read and write plain text files for anything intended for humans. For machine to machine communication, JSON is an excellent choice today, as you will find a wide array of tools, great support from the Python standard library and a direct mapping from any JSON you find in the world to a data structure that you can load into memory. If a future application has very rigid rules, you can layer on logic using JSON-Schema or you can look at something like XML, which is used for a fair number of complex data interchange formats. In summary, if you scratched very deeply under the hood, you'll see that pickle and shelve both call file.read() and file.write() but they are hiding all of the logic and complexity of turning your data structure into a file on disk. The json.load() and json.dump() calls are doing the same thing. There are equivalants in the dbm, csv and a few of the XML modules. So, the pickle and shelve are just well-tested and implemented libraries to make your job easier, as are many of these other data serialization libraries. I hope the above was helpful in explaining some of the concepts. -Martin [0] https://en.wikipedia.org/wiki/Serialization#Pickle [1] Please note, there are probably better places to read about JSON, XML and CSV, but the point I'm making here is that they are standardized serialization and interchange formats and are reasonably well-specified (with the historical exception of CSV). JSON: https://tools.ietf.org/html/rfc8259 CSV: https://tools.ietf.org/html/rfc4180 XML: https://tools.ietf.org/html/rfc3076 A skewed and degraded history of serialization formats could look something like this: 1950s: You'll need to write a program (on punchcards) to read octal punchouts from this deck of cards. Or from this super-fancy new magnetic tape drive. Oh, you wanted it in a searchable data structure? Well...you'd better put that logic into your application. 1960s: Now that we have less expensive disks, how about giving standard input, standard output to each program and having ASCII files. Wouldn't that be cool? 1970s: Yo, people -- could we come up with common structures for storing files on disk so we don't have to write the same logic in every application? Ok...how about ... CSV? 1980s: Hello, ISO! Could we come up with a way to define serialization encodings and formats that are more flexible and shared, since this network thing seems to be starting to reach across national boundaries? Oh, yes, there's Standard Generalized Markup Language (SGML) 1990s: SGML seems a bit complicated and there's this effort to standardize on encodings ("Unicode", eh?). Can we make SGML tighter? Yes, it was a good start, but we can tighten it up, let's call it XML. 2000s: Wouldn't it be nice to have a flexible and dynamic, Unicode-encoded, multilingual serialization format that we could shoot over the network, store on disks and move between processes that we have running in browsers in many places? Yes! Let's call it JSON, JavaScript Object Notation. -- Martin A. Brown http://linux-ip.net/ From alan.gauld at yahoo.co.uk Sat Jun 8 07:54:51 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 8 Jun 2019 12:54:51 +0100 Subject: [Tutor] Pickles and Shelves Concept In-Reply-To: References: Message-ID: On 08/06/2019 01:02, Alan Gauld via Tutor wrote: > keys to your data and then restore it in any sequence, or > only restore some of it, you can. Apologies for the Yoda-speak at the end! A bit of editing that didn't quite work out as intended... -- 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 mhysnm1964 at gmail.com Sat Jun 8 08:27:28 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sat, 8 Jun 2019 22:27:28 +1000 Subject: [Tutor] regular expression query Message-ID: <002001d51df5$8ab37f30$a01a7d90$@gmail.com> Hello all, Windows 10 OS, Python 3.6 I have a couple of queries in relation to extracting content using regular expressions. I understand the pattern chars (.?*+), Meta-chars \d, \D, \W, \W and so on. The class structure [.]. The group I believe I understand (.). The repeat feature {m,n}. the difference between the methods match, search, findall, sub and ETC. The challenge I am finding is getting a pattern to extract specific word(s). Trying to identify the best method to use and how to use the \1 when using forward and backward search pattern (Hoping I am using the right term). Basically I am trying to extract specific phrases or digits to place in a dictionary within categories. Thus if "ROYaL_BANK 123123123" is found, it is placed in a category called transfer funds. Other might be a store name which likewise is placed in the store category. Note, I have found a logic error with "ROYAL_BANK 123123123", but that isn't a concern. The extraction of the text is. Line examples: Royal_bank M-BANKING PAYMENT TRANSFER 123456 to 9922992299 Royal_bank M-BANKING PAYMENT TRANSFER 123456 FROM 9922992299 PAYMENT TO SARWARS-123123123 ROYAL_BANK INTERNET BANKING BPAY Kangaroo Store {123123123} result = re.sub(r'ROYAL_BANK INTERNET BANKING FUNDS TFER TRANSFER \d+ TO ', 'ROYAL_BANK ', line) r'ROYAL_BANK INTERNET BANKING TRANSFER Mouth in foot EFTPOS Amazon PAY/SALARY FROM foo bar 123123123 PAYMENT TO Tax Man 666 And other similar structures. Below is the function I am currently using. Not sure if the sub, match or search is going to be the best method. The reason why I am using a sub is to delete the unwanted text. The searchmatch/findall could do the same if I use a group. Also I have not used any tests in the below and logically I think I should. As the code will override the results if not found in the later tests. If there is a more elegant way to do it then having: If line.startswith('text string to match'): Regular expression el If line.startswith('text string to match'): regular expression return result I would like to know. The different regular expressions I have used are: # this sometimes matches and sometimes does not. I want all the text up to the from or to, to be replaced with "ROYAL_BANK". Ending up with ROYAL_BANK 123123123 result= re.sub(r'ROYAL_BANK M-BANKING PAYMENT TRANSFER \d+ (TO|FROM) ', 'ROYAL_BANK ', line) # the below returns from STARWARS and it shouldn't. I should just get STARWARS. result = re.match(r'PAYMENT TO (SARWARS)-\d+ ', line) # the below should (doesn't work the last time I tested it) should return the words between the (.) result = re.match(r'ROYAL_BANK INTERNET BANKING BPAY (.*) [{].*$', '\1', line) # the below patterns should remove the text at the beginning of the string result = re.sub(r'ROYAL_BANK INTERNET BANKING FUNDS TFER TRANSFER \d+ TO ', 'ROYAL_BANK ', line) result = re.sub(r'ROYAL_BANK INTERNET BANKING TRANSFER ', '', line) result = re.sub(r'EFTPOS ', '', line) # The below does not work and I am trying to use the back or forward search feature. Is this syntax wrong or the pattern wrong? I cannot work it out from the information I have read. result = re.sub(r'PAY/SALARY FROM (*.) \d+$', '\1', line) result = re.sub(r'PAYMENT TO (*.) \d+', '\1', line) Sean From gursimran.maken at gmail.com Sat Jun 8 12:37:05 2019 From: gursimran.maken at gmail.com (Gursimran Maken) Date: Sat, 8 Jun 2019 22:07:05 +0530 Subject: [Tutor] Pickles and Shelves Concept In-Reply-To: References: Message-ID: Thank you all for explaining me the above concept. On Sat, Jun 8, 2019 at 5:27 PM Alan Gauld via Tutor wrote: > On 08/06/2019 01:02, Alan Gauld via Tutor wrote: > > > keys to your data and then restore it in any sequence, or > > only restore some of it, you can. > > Apologies for the Yoda-speak at the end! > A bit of editing that didn't quite work out as intended... > > -- > 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 cs at cskk.id.au Sun Jun 9 19:35:28 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 10 Jun 2019 09:35:28 +1000 Subject: [Tutor] regular expression query In-Reply-To: <002001d51df5$8ab37f30$a01a7d90$@gmail.com> References: <002001d51df5$8ab37f30$a01a7d90$@gmail.com> Message-ID: <20190609233528.GA61294@cskk.homeip.net> On 08Jun2019 22:27, Sean Murphy wrote: >Windows 10 OS, Python 3.6 Thanks for this. >I have a couple of queries in relation to extracting content using >regular expressions. I understand [...the regexp syntax...] >The challenge I am finding is getting a pattern to >extract specific word(s). Trying to identify the best method to use and how >to use the \1 when using forward and backward search pattern (Hoping I am >using the right term). Basically I am trying to extract specific phrases or >digits to place in a dictionary within categories. Thus if "ROYaL_BANK >123123123" is found, it is placed in a category called transfer funds. Other >might be a store name which likewise is placed in the store category. I'll tackle your specific examples lower down, and make some suggestions. >Note, I have found a logic error with "ROYAL_BANK 123123123", but that >isn't a concern. The extraction of the text is. > >Line examples: >Royal_bank M-BANKING PAYMENT TRANSFER 123456 to 9922992299 >Royal_bank M-BANKING PAYMENT TRANSFER 123456 FROM 9922992299 >PAYMENT TO SARWARS-123123123 >ROYAL_BANK INTERNET BANKING BPAY Kangaroo Store {123123123} >EFTPOS Amazon >PAY/SALARY FROM foo bar 123123123 >PAYMENT TO Tax Man 666 Thanks. Assuming the below is a cut/paste accident from some code: result = re.sub(r'ROYAL_BANK INTERNET BANKING FUNDS TFER TRANSFER \d+ TO ', 'ROYAL_BANK ', line) r'ROYAL_BANK INTERNET BANKING TRANSFER Mouth in foot >And other similar structures. Below is the function I am currently using. >Not sure if the sub, match or search is going to be the best method. The >reason why I am using a sub is to delete the unwanted text. The >searchmatch/findall could do the same if I use a group. Also I have not >used any tests in the below and logically I think I should. As the code will >override the results if not found in the later tests. If there is a more >elegant way to do it then having: > >If line.startswith('text string to match'): > Regular expression >el If line.startswith('text string to match'): > regular expression >return result There is. How far you take it depends on how variable your input it. Banking statement data I would expect to have relatively few formats (unless the banking/financ industry is every bit as fragmented as I sometimes believe, in which case the structure might be less driven by _your_ bank and instead arbitrarily garbled according the the various other entities due to getting ad hoc junk as the description). >I would like to know. The different regular expressions I have used >are: > ># this sometimes matches and sometimes does not. I want all the text up to >the from or to, to be replaced with "ROYAL_BANK". Ending up with ROYAL_BANK >123123123 > > result= re.sub(r'ROYAL_BANK M-BANKING PAYMENT TRANSFER \d+ (TO|FROM) ', >'ROYAL_BANK ', line) Looks superficially ok. Got an example input line where it fails? Not that the above is case sentitive, so if "to" etc can be in lower case (as in your example text earlier) this will fail. See the re.I modifier. ># the below returns from STARWARS and it shouldn't. I should just get >STARWARS. > > result = re.match(r'PAYMENT TO (SARWARS)-\d+ ', line) Well, STARWARS seems misseplt above. And you should get a "match" object, with "STARWARS" in .group(1). So earlier you're getting a str in result, and here you're getting an re.match object (or None for a failed match). ># the below should (doesn't work the last time I tested it) should >return the words between the (.) > > result = re.match(r'ROYAL_BANK INTERNET BANKING BPAY (.*) [{].*$', '\1', line) "should" what? It would help to see the input line you expect this to match. And re.match is not an re.sub - it looks like you have these confused here, based on the following '\`',line parameters. ># the below patterns should remove the text at the beginning of the string > result = re.sub(r'ROYAL_BANK INTERNET BANKING FUNDS TFER TRANSFER \d+ TO ', 'ROYAL_BANK ', line) > result = re.sub(r'ROYAL_BANK INTERNET BANKING TRANSFER ', '', line) > result = re.sub(r'EFTPOS ', '', line) Sure. Got an example line where this does not happen? ># The below does not work and I am trying to use the back or forward >search feature. Is this syntax wrong or the pattern wrong? I cannot work it out >from the information I have read. > > result = re.sub(r'PAY/SALARY FROM (*.) \d+$', '\1', line) > result = re.sub(r'PAYMENT TO (*.) \d+', '\1', line) You've got "*." You probably mean ".*" Main issues: 1: Your input data seems to be mixed case, but all your regexps are case sensitive. They will not match if the case is different eg "Royal_Bank" vs "ROYAL_BANK", "to" vs "TO", etc. Use the re.I modified to make your regexps case insensitive. 2: You're using re.sub a lot. I'd be inclined to always use re.match and to pull information from the match object you get back. Untested example sketch: m = re.match('(ROYAL_BANK|COMMONER_CREDIT_UNION) INTERNET BANKING FUNDS TFER TRANSFER (\d+) TO (.*)', line) if m: category = m.match(1) id_number = m.match(2) recipient = m.match(3) else: m = re.match(.......) ... more tests here ... ... ... else: ... report unmatched line for further consideration ... 3: You use ".*" a lot. This is quite prone to matching too much. You might find things like "\S+" better, which matches a single nonwhitespace "word". It depends a bit on your input. Cheers, Cameron Simpson From sai.allu at nutanix.com Mon Jun 10 12:50:26 2019 From: sai.allu at nutanix.com (Sai Allu) Date: Mon, 10 Jun 2019 16:50:26 +0000 Subject: [Tutor] Python printing parentheses and quotes Message-ID: Hello! I was just wondering if anybody encountered an issue where the Python interpreter was changing how it interprets print statements. So I'm using default Python on Mac OSX (2.7.10 I'm pretty sure) and running with the "python script.py" command. Basically what happened was that I had a few lines in the script like this ip = "10.41.17.237" print(" Welcome to Squid Monitoring for ", ip) print("") and the output was like this (" Welcome to Squid Monitoring for 10.41.17.237") ("") So it was printing parentheses and quotes. The above result might not be exactly accurate because I didn't save the output, but it was something generally like that. Then I changed a few small things in the script but nothing big ("import sys", adding "#!usr/bin/env python", and accidentally trying to close the Python interpreter by using ^C multiple times). I didn't really change too much though but maybe I changed something simple that I didn't know would cause something like that. Is there any reason why Python would start to print parentheses and quotes like that. Thank you! Best Wishes, Sai Allu P.S. After I upgrade to Python3 this started working. When I kept Python2, then I was able to add an extra statement like Print("Yo") in Sublime Text and this printed Yo just like expected. But actually maybe I had Python3 before I added this print statement, I'm not too sure. From mats at wichmann.us Mon Jun 10 14:12:39 2019 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 10 Jun 2019 12:12:39 -0600 Subject: [Tutor] Python printing parentheses and quotes In-Reply-To: References: Message-ID: <4258b801-e527-4772-46c5-c15f7ba2ee18@wichmann.us> On 6/10/19 10:50 AM, Sai Allu wrote: > Hello! > > I was just wondering if anybody encountered an issue where the Python interpreter was changing how it interprets print statements. So I'm using default Python on Mac OSX (2.7.10 I'm pretty sure) and running with the "python script.py" command. > > Basically what happened was that I had a few lines in the script like this > ip = "10.41.17.237" > print(" Welcome to Squid Monitoring for ", ip) > print("") > > and the output was like this > > (" Welcome to Squid Monitoring for 10.41.17.237") > > ("") > > So it was printing parentheses and quotes. The above result might not be exactly accurate because I didn't save the output, but it was something generally like that. In Python 2, print is a statement. In Python 3 it's a function and behaves like you're expecting. However, the behavior you're seeing is odd (printing parentheses is a surprise unless there's more going on than you've listed) If you want them consistent across both versions, add a statement at the very top: from __future__ import print_function From alan.gauld at yahoo.co.uk Mon Jun 10 14:38:24 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 10 Jun 2019 19:38:24 +0100 Subject: [Tutor] Python printing parentheses and quotes In-Reply-To: References: Message-ID: On 10/06/2019 17:50, Sai Allu wrote: > Basically what happened was that I had a few lines in the script like this > ip = "10.41.17.237" > print(" Welcome to Squid Monitoring for ", ip) > print("") > > and the output was like this > > (" Welcome to Squid Monitoring for 10.41.17.237") > > ("") Are you sure? Is that a cut n paste or just how you think you remember it? The reason i ask is that its not what i see and not what I'd expect. In Python v2 print is a statement which means that Python sees your first print line like: print (" Welcome to Squid Monitoring for ", "10.41.17.237") That is it thinks you want it to print a tuple of 2 strings and what I see as output is: (' Welcome to Squid Monitoring for ', '10.41.17.237') Which is a tuple of 2 strings... Now if I remove the parentheses it looks like: print " Welcome to Squid Monitoring for ", "10.41.17.237" Which is telling Python to print two strings joined by a space. And I see the output: Welcome to Squid Monitoring for 10.41.17.237 And in both cases the second print just prints out an empty string with no quotes. Are you sure that's not what you saw? > P.S. After I upgrade to Python3 this started working. In Python 3 print is a function so it needs the parentheses. Without them it will report a syntax error. So for Python 3 your original code is correct. -- 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 sai.allu at nutanix.com Mon Jun 10 14:53:46 2019 From: sai.allu at nutanix.com (Sai Allu) Date: Mon, 10 Jun 2019 18:53:46 +0000 Subject: [Tutor] Python printing parentheses and quotes In-Reply-To: <4258b801-e527-4772-46c5-c15f7ba2ee18@wichmann.us> References: , <4258b801-e527-4772-46c5-c15f7ba2ee18@wichmann.us> Message-ID: But then how come it was working earlier for me without that import statement. Python doesn't interpret it as a statement exclusively, before it worked fine as a function. Best Wishes, Sai Allu ________________________________ From: Mats Wichmann Sent: Monday, June 10, 2019 11:12 AM To: Sai Allu; tutor at python.org Subject: Re: [Tutor] Python printing parentheses and quotes On 6/10/19 10:50 AM, Sai Allu wrote: > Hello! > > I was just wondering if anybody encountered an issue where the Python interpreter was changing how it interprets print statements. So I'm using default Python on Mac OSX (2.7.10 I'm pretty sure) and running with the "python script.py" command. > > Basically what happened was that I had a few lines in the script like this > ip = "10.41.17.237" > print(" Welcome to Squid Monitoring for ", ip) > print("") > > and the output was like this > > (" Welcome to Squid Monitoring for 10.41.17.237") > > ("") > > So it was printing parentheses and quotes. The above result might not be exactly accurate because I didn't save the output, but it was something generally like that. In Python 2, print is a statement. In Python 3 it's a function and behaves like you're expecting. However, the behavior you're seeing is odd (printing parentheses is a surprise unless there's more going on than you've listed) If you want them consistent across both versions, add a statement at the very top: from __future__ import print_function From sai.allu at nutanix.com Mon Jun 10 15:04:39 2019 From: sai.allu at nutanix.com (Sai Allu) Date: Mon, 10 Jun 2019 19:04:39 +0000 Subject: [Tutor] Python printing parentheses and quotes In-Reply-To: References: , <4258b801-e527-4772-46c5-c15f7ba2ee18@wichmann.us>, Message-ID: Actually I'm pretty sure what happened was that the "#! usr/bin/python" was in a module that was being imported. So the Python interpreter cached it or somehow crashed randomly, which meant that the print was working as a keyword instead of a function. But when I removed that "#! usr/bin/python" line and then rewrote the print statements, it went back to working normally. Thank you for the help though! Sai Allu ________________________________ From: Sai Allu Sent: Monday, June 10, 2019 11:53 AM To: Mats Wichmann; tutor at python.org; Deepak Dixit Subject: Re: [Tutor] Python printing parentheses and quotes But then how come it was working earlier for me without that import statement. Python doesn't interpret it as a statement exclusively, before it worked fine as a function. Best Wishes, Sai Allu ________________________________ From: Mats Wichmann Sent: Monday, June 10, 2019 11:12 AM To: Sai Allu; tutor at python.org Subject: Re: [Tutor] Python printing parentheses and quotes On 6/10/19 10:50 AM, Sai Allu wrote: > Hello! > > I was just wondering if anybody encountered an issue where the Python interpreter was changing how it interprets print statements. So I'm using default Python on Mac OSX (2.7.10 I'm pretty sure) and running with the "python script.py" command. > > Basically what happened was that I had a few lines in the script like this > ip = "10.41.17.237" > print(" Welcome to Squid Monitoring for ", ip) > print("") > > and the output was like this > > (" Welcome to Squid Monitoring for 10.41.17.237") > > ("") > > So it was printing parentheses and quotes. The above result might not be exactly accurate because I didn't save the output, but it was something generally like that. In Python 2, print is a statement. In Python 3 it's a function and behaves like you're expecting. However, the behavior you're seeing is odd (printing parentheses is a surprise unless there's more going on than you've listed) If you want them consistent across both versions, add a statement at the very top: from __future__ import print_function From avichein at gmail.com Mon Jun 10 17:20:33 2019 From: avichein at gmail.com (Avi Chein) Date: Mon, 10 Jun 2019 17:20:33 -0400 Subject: [Tutor] Installing Python Message-ID: Hi, I'm trying to install Python 3.6 on my MacOS Mojave but it isn't installing properly. Can someone help me out? I'm a college student and have never used Python before. Would be much appreciated! Thans, Avi From alan.gauld at yahoo.co.uk Mon Jun 10 19:32:26 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 11 Jun 2019 00:32:26 +0100 Subject: [Tutor] Installing Python In-Reply-To: References: Message-ID: On 10/06/2019 22:20, Avi Chein wrote: > I'm trying to install Python 3.6 on my MacOS Mojave but it isn't installing > properly. When asking for help, on any forum, it's never a good idea to say that something "doesn't work" or "isn't installing properly". That gives us nothing to work on. What exactly is happening? Where did you download from? How did you try to install it? What actually happened? - Error messages? Or just a non-functioning icon or menu? Or nothing at all? The more specific the information you give us the better the chance that we can answer. -- 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 Mon Jun 10 20:34:34 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 11 Jun 2019 10:34:34 +1000 Subject: [Tutor] Python printing parentheses and quotes In-Reply-To: References: Message-ID: <20190611003434.GA84875@cskk.homeip.net> On 10Jun2019 19:04, Sai Allu wrote: >Actually I'm pretty sure what happened was that the "#! usr/bin/python" was in a module that was being imported. So the Python interpreter cached it or somehow crashed randomly, which meant that the print was working as a keyword instead of a function. > >But when I removed that "#! usr/bin/python" line and then rewrote the print statements, it went back to working normally. My personal suspicision is that what you might have been doing is this (notice the trailing comma): print("",) or maybe: print("this", that") Look: % python Python 2.7.16 (default, Apr 1 2019, 15:01:04) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("",) ('',) >>> % python3 Python 3.7.3 (default, Mar 30 2019, 03:38:02) [Clang 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("",) >>> What is happening? In Python 2, print is a statement unless you use the __future__ import already mentioned. That means that this: print("",) is a "print" of the expression ("",), which is a 1-tuple, and gets printed as a tuple. The more likely scenario is when you're printing mulitple things: print("this", "that") which is still a "print" of a tuple. However, in Python 3 print is a function which means that the brackets are part of the function call. So this: print("") or: print("this", "that") is a call to the "print()" function, passing one or two arguments, which get printed. And printing "" (the former case) is an empty string. Please revisit your code can test this. Subtle issues like this are why we like to receive _exact_ cut/paste of your code and the matching output, not a retype of what you thought you ran. If you encounter something weird like this, it is well worth your time (and ours) if you make a tiny standalone script showing the problem, as small as possible. Then paste it into your message and paste in the output (this list drops attachments). That we we can all run exactly the same code, and solve your actual problem. And start always using: from __future__ import print_function in Python if you're using print. That will make your prints behave the same regardless if whether they are using Python 2 or 3. Cheers, Cameron Simpson From sai.allu at nutanix.com Mon Jun 10 20:52:52 2019 From: sai.allu at nutanix.com (Sai Allu) Date: Tue, 11 Jun 2019 00:52:52 +0000 Subject: [Tutor] Python printing parentheses and quotes In-Reply-To: <20190611003434.GA84875@cskk.homeip.net> References: , <20190611003434.GA84875@cskk.homeip.net> Message-ID: Thank you Cameron, This was something that I was working on last week and I've been able to fix the script since then. I was just curious why the previous version did that and you might be right, thank you for the help though! Best Wishes, Sai Allu ________________________________ From: Cameron Simpson Sent: Monday, June 10, 2019 5:34 PM To: Sai Allu Cc: Mats Wichmann; tutor at python.org; Deepak Dixit Subject: Re: [Tutor] Python printing parentheses and quotes On 10Jun2019 19:04, Sai Allu wrote: >Actually I'm pretty sure what happened was that the "#! usr/bin/python" was in a module that was being imported. So the Python interpreter cached it or somehow crashed randomly, which meant that the print was working as a keyword instead of a function. > >But when I removed that "#! usr/bin/python" line and then rewrote the print statements, it went back to working normally. My personal suspicision is that what you might have been doing is this (notice the trailing comma): print("",) or maybe: print("this", that") Look: % python Python 2.7.16 (default, Apr 1 2019, 15:01:04) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("",) ('',) >>> % python3 Python 3.7.3 (default, Mar 30 2019, 03:38:02) [Clang 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("",) >>> What is happening? In Python 2, print is a statement unless you use the __future__ import already mentioned. That means that this: print("",) is a "print" of the expression ("",), which is a 1-tuple, and gets printed as a tuple. The more likely scenario is when you're printing mulitple things: print("this", "that") which is still a "print" of a tuple. However, in Python 3 print is a function which means that the brackets are part of the function call. So this: print("") or: print("this", "that") is a call to the "print()" function, passing one or two arguments, which get printed. And printing "" (the former case) is an empty string. Please revisit your code can test this. Subtle issues like this are why we like to receive _exact_ cut/paste of your code and the matching output, not a retype of what you thought you ran. If you encounter something weird like this, it is well worth your time (and ours) if you make a tiny standalone script showing the problem, as small as possible. Then paste it into your message and paste in the output (this list drops attachments). That we we can all run exactly the same code, and solve your actual problem. And start always using: from __future__ import print_function in Python if you're using print. That will make your prints behave the same regardless if whether they are using Python 2 or 3. Cheers, Cameron Simpson From jhoeksem at nd.edu Tue Jun 11 10:35:22 2019 From: jhoeksem at nd.edu (John Hoeksema) Date: Tue, 11 Jun 2019 10:35:22 -0400 Subject: [Tutor] Broadcasting using sockets over adhoc wifi Message-ID: Hello! Summer researcher using Raspbian and Python 3.5. I'm trying to use a Raspberry Pi 3 B+ to broadcast a message using the sockets library to other Pis (same model) over their shared ad-hoc network. All of the Pis can ping the others over the ad hoc network. The Pis can also communicate using pretty standard client-server code and the python socket library . However, when I try to *broadcast* a message, the Pis give a "Network is unreachable" message (full error down below). A grad student I'm working with said that the script he provided me expects the server to be run in infrastructure mode, and configuration for ad-hoc mode is required to make it work correctly. This is confirmed, as I have successfully run the code on a desktop. I have poured over man pages and stackoverflow, and can't seem to find resources for how to configure socket broadcasts for ad-hoc networks. Any thoughts? The function is supposed to broadcast a message to a specific port after every [frequency] seconds, and any machines on the same network and connected to the same port should receive the message. The function is the only method of the *Server* class. *Broadcast function:* def broadcast(self, frequency, port): server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) server.settimeout(0.2) while True: server.sendto("GET OUT OF MY SWAMP", ('', port)) print("message sent...") time.sleep(frequency) *Error message:* Traceback (most recent call last): File "myServer.py", line 31, in s.broadcast(float(frequency),int(port)) File "myServer.py", line 22, in broadcast server.sendto("GET OUT OF MY SWAMP", ('', port)) socket.error: [Errno 101] Network is unreachable Thank you for your time. Please let me know any information you think would be useful! Best, John -- *John Hoeksema* Computer Science University of Notre Dame '21 From savageapple850 at gmail.com Tue Jun 11 06:59:07 2019 From: savageapple850 at gmail.com (Cravan) Date: Tue, 11 Jun 2019 18:59:07 +0800 Subject: [Tutor] Error when trying to insert csv values into a sql table Message-ID: Here is the stack overflow link: https://stackoverflow.com/questions/56540292/error-when-trying-to-insert-csv-values-into-a-sql-table I'm getting a weird error code when I try to store values from a csv into an sql table in a movie review assignment. I have already edited my apostrophes and spacing and looked up examples from google to try and resolve my error to no avail. I also ensured that i defined DATABASE_URL properly. Sorry for the long traceback error at the end :P Please note that my csv values are stored in lists in each cell. They are arranged in a single column such as The Lego Movie;2014;100;tt1490017;7.8 This is my main code import os from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker engine = create_engine(os.getenv("DATABASE_URL")) # database engine object from SQLAlchemy that manages connections to the database ????????????????????????????????????????????????? # DATABASE_URL is an environment variable that indicates where the database lives db = scoped_session(sessionmaker(bind=engine)) def main(): ??? f = open("movies.csv","r") ??? reader = csv.reader(f) ??? for row in f: # loop gives each column a name ??????? vals = row.split(';') ??????? title = vals[0] ??????? year = vals[1] ??????? runtime = vals[2] ??????? imdbID = vals[3] ??????? imdbRating = vals[4] ??????? db.execute('INSERT INTO movies (Title, Year, Runtime, imdbID, imdbRating) VALUES (:title, :year, :runtime, :imdbID, :imdbRating)', ????????????????????? {'title': title, 'year': year, 'runtime': runtime, 'imdbID': imdbID, 'imdbRating': imdbRating}) # substitute values from CSV line into SQL command, as per this dict ??????? print(f"Added movie named {title} of {year} lasting {runtime} minutes. Its imdbID is {imdbID} and its imdbRating is {imdbRating}.") This is the sql CREATE TABLE movies ( ????? Title SERIAL PRIMARY KEY, ????? Year INTEGER NOT NULL, ????? Runtime INTEGER NOT NULL ????? imdbID VARCHAR NOT NULL, ????? imdbRating INTEGER NOT NULL ? ); This is the error i got: Traceback (most recent call last): ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/engine/base.py", line 1244, in _execute_context ??? cursor, statement, parameters, context ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/engine/default.py", line 550, in do_execute ??? cursor.execute(statement, parameters) psycopg2.errors.UndefinedTable: relation "movies" does not exist LINE 1: INSERT INTO movies (Title, Year, Runtime, imdbID, imdbRating... The above exception was the direct cause of the following exception: Traceback (most recent call last): ? File "import.py", line 25, in ??? main() ? File "import.py", line 21, in main ??? {'title': title, 'year': year, 'runtime': runtime, 'imdbID': imdbID, 'imdbRating': imd bRating}) # substitute values from CSV line into SQL command, as per this dict ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/orm/scoping.py", line 162, in do ??? return getattr(self.registry(), name)(*args, **kwargs) ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/orm/session.py", line 1268, in execute ??? clause, params or {} ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/orm/session.py", line 1268, in execute ??? clause, params or {} ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/engine/base.py", line 988, in execute ??? return meth(self, multiparams, params) ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/sql/elements.py", line 287, in _execute_on_connection ??? return connection._execute_clauseelement(self, multiparams, params) ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/engine/base.py", line 1107, in _execute_clauseelement ??? distilled_params, ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/engine/base.py", line 1248, in _execute_context e, statement, parameters, cursor, context ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/engine/base.py", line 1466, in _handle_dbapi_exception ??? util.raise_from_cause(sqlalchemy_exception, exc_info) ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/util/compat.py", line 383, in raise_from_cause ??? reraise(type(exception), exception, tb=exc_tb, cause=cause) ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/util/compat.py", line 128, in reraise ??? raise value.with_traceback(tb) ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/engine/base.py", line 1244, in _execute_context ??? cursor, statement, parameters, context ? File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla lchemy/engine/default.py", line 550, in do_execute cursor.execute(statement, parameters) sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "movies" does n ot exist LINE 1: INSERT INTO movies (Title, Year, Runtime, imdbID, imdbRating... ??????????????????? ^ [SQL: INSERT INTO movies (Title, Year, Runtime, imdbID, imdbRating) VALUES (%(title)s, %(y ear)s, %(runtime)s, %(imdbID)s, %(imdbRating)s)] [parameters: {'title': 'Title', 'year': 'Year', 'runtime': 'Runtime', 'imdbID': 'imdbID', 'imdbRating': 'imdbRating\n'}] (Background on this error at: http://sqlalche.me/e/f405) From __peter__ at web.de Tue Jun 11 12:26:13 2019 From: __peter__ at web.de (Peter Otten) Date: Tue, 11 Jun 2019 18:26:13 +0200 Subject: [Tutor] Error when trying to insert csv values into a sql table References: Message-ID: Cravan wrote: > Here is the stack overflow link: > https://stackoverflow.com/questions/56540292/error-when-trying-to-insert-csv-values-into-a-sql-table > > > > I'm getting a weird error code when I try to store values from a csv into > an sql table in a movie review assignment. Like they say on stackoverflow: it very much looks like the movies table doesn't exist. Maybe you have forgotton a commit somewhere? Please double-check that the table is actually created before you look for other less likely causes of your problem. From guettliml at thomas-guettler.de Thu Jun 13 04:22:28 2019 From: guettliml at thomas-guettler.de (=?UTF-8?Q?Thomas_G=c3=bcttler?=) Date: Thu, 13 Jun 2019 10:22:28 +0200 Subject: [Tutor] Where to store test-code? Message-ID: <6d10a366-859c-9bde-a245-c4629a81db9e@thomas-guettler.de> Up to now I use this structure: src/myapp/setup.py src/myapp/myapp/real_code.py Now I want to write a test for a method which is implemented real_code.py. Where should I write store the file which contains the unittest? Is there a guideline for the directory structure of tests? I know that there are several ways. I know that all these ways work. Nevertheless it would be great to have a sane default. If there is a guideline IDEs could assist to create new tests at a common location. Related: https://youtrack.jetbrains.com/issue/JT-53069 Regards, Thomas -- Thomas Guettler http://www.thomas-guettler.de/ I am looking for feedback: https://github.com/guettli/programming-guidelines From mats at wichmann.us Thu Jun 13 10:01:31 2019 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 13 Jun 2019 08:01:31 -0600 Subject: [Tutor] Where to store test-code? In-Reply-To: <6d10a366-859c-9bde-a245-c4629a81db9e@thomas-guettler.de> References: <6d10a366-859c-9bde-a245-c4629a81db9e@thomas-guettler.de> Message-ID: <2c0d5f21-4f7b-ff2a-d193-957be065fa2b@wichmann.us> On 6/13/19 2:22 AM, Thomas G?ttler wrote: > Up to now I use this structure: > > src/myapp/setup.py > src/myapp/myapp/real_code.py > > Now I want to write a test for a method which is implemented real_code.py. > > Where should I write store the file which contains the unittest? > > Is there a guideline for the directory structure of tests? > > I know that there are several ways. I know that all these ways work. > Nevertheless > it would be great to have a sane default. If there is a guideline IDEs > could assist > to create new tests at a common location. This question gets asked all the time, and it's hard to answer. Other than the general requirement that the placement of the test should not make it hard to find the code to be tested, *your* answer will be determined by the scope of the project and by preferences. You can put tests inline in the docstring, if you want to use doctest. People dismiss this as a viable alternative, but the Python standard library does this in some places - you don't get to write exhaustive unit tests this way, but you can have something that serves as an example and a quick test at the same time. For some examples, try this: pydoc difflib (hopefully you're not on Windows where that's probably not in the search path) If using pytest or unittest, you can put your tests in the same directory as the code to be tested, naming the test file for foo.py as test_foo.py, that way they're picked up automatically. For your example, in src/myapp/myapp. If you want separation of your app/module code and the tests , you can put them a level up, thus src/myapp. If you will have a lot of "source" files, you'll probably want a parallel directory (probably simplifies deploy, if you intend to deploy the code without the tests), thus src/myapp/tests (which is what it sounds like what you're angling for us to tell you :) ). If you do that though, there's a complication - tests will now have a harder time finding the module code which is to be imported in running the tests, and you'll have to take a few extra steps to make that work right. If this were a TV courtroom drama someone would have risen to say "objection, calls for speculation" - these things don't have an absolute answer. Anyway, the pytest project has some commentary on this that will likely be more well thought out than my ramblings: https://docs.pytest.org/en/latest/goodpractices.html From tom at hale.ee Thu Jun 13 11:33:47 2019 From: tom at hale.ee (Tom Hale) Date: Thu, 13 Jun 2019 22:33:47 +0700 Subject: [Tutor] Running Lib/test/test_shutil.py Message-ID: <9f60bb73-c3a2-9144-2802-f8686236095f@hale.ee> Hi all, I hope this is the best place to ask (please let me know if there is a more appropriate list): Checking out CPython v3.8.0b1, I'm trying to run: % python Lib/test/test_shutil.py I'm getting: Traceback (most recent call last): File "Lib/test/test_shutil.py", line 19, in from shutil import (make_archive, ImportError: cannot import name '_GiveupOnFastCopy' from 'shutil' (/usr/lib/python3.7/shutil.py) Am I trying to run the test file in the right way? Context: I'm proposing to add /shutil.(sym)?link/ and want to start writing my tests with running the existing tests :) Is there a doc that I've missed? Cheers! -- Tom Hale From __peter__ at web.de Thu Jun 13 14:19:28 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 13 Jun 2019 20:19:28 +0200 Subject: [Tutor] Running Lib/test/test_shutil.py References: <9f60bb73-c3a2-9144-2802-f8686236095f@hale.ee> Message-ID: Tom Hale wrote: > Hi all, > > I hope this is the best place to ask (please let me know if there is a > more appropriate list): > > Checking out CPython v3.8.0b1, I'm trying to run: > > % python Lib/test/test_shutil.py Are you sure % python invokes the 3.8 interpreter? > I'm getting: > > Traceback (most recent call last): > File "Lib/test/test_shutil.py", line 19, in > from shutil import (make_archive, > ImportError: cannot import name '_GiveupOnFastCopy' from 'shutil' > (/usr/lib/python3.7/shutil.py) > The traceback suggests that it may be 3.7. Try % ./python Lib/test/test_shutil.py or, if you have installed python3.8 % python3.8 Lib/test/test_shutil.py If you still get an error with a path into the /usr/lib/python3.7 stdlib have a look at the PYTHONPATH environment variable. > Am I trying to run the test file in the right way? > > Context: I'm proposing to add /shutil.(sym)?link/ and want to start > writing my tests with running the existing tests :) > > Is there a doc that I've missed? > > Cheers! > From mats at wichmann.us Thu Jun 13 14:35:45 2019 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 13 Jun 2019 12:35:45 -0600 Subject: [Tutor] Running Lib/test/test_shutil.py In-Reply-To: References: <9f60bb73-c3a2-9144-2802-f8686236095f@hale.ee> Message-ID: On 6/13/19 12:19 PM, Peter Otten wrote: > Tom Hale wrote: > >> Hi all, >> >> I hope this is the best place to ask (please let me know if there is a >> more appropriate list): >> >> Checking out CPython v3.8.0b1, I'm trying to run: >> >> % python Lib/test/test_shutil.py > > Are you sure > > % python > > invokes the 3.8 interpreter? > >> I'm getting: >> >> Traceback (most recent call last): >> File "Lib/test/test_shutil.py", line 19, in >> from shutil import (make_archive, >> ImportError: cannot import name '_GiveupOnFastCopy' from 'shutil' >> (/usr/lib/python3.7/shutil.py) >> > > The traceback suggests that it may be 3.7. and indeed _GiveupOnFastCopy is a 3.8-only thing (an exception, it turns out), so this must be the deal. From cs at cskk.id.au Thu Jun 13 18:08:52 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Fri, 14 Jun 2019 08:08:52 +1000 Subject: [Tutor] Broadcasting using sockets over adhoc wifi In-Reply-To: References: Message-ID: <20190613220852.GA55176@cskk.homeip.net> On 11Jun2019 10:35, John Hoeksema wrote: >Summer researcher using Raspbian and Python 3.5. > >I'm trying to use a Raspberry Pi 3 B+ to broadcast a message using the >sockets library to other Pis (same model) over their shared ad-hoc network. >All of the Pis can ping the others over the ad hoc network. The Pis can >also communicate using pretty standard client-server code and the python >socket library . However, >when I try to *broadcast* a message, the Pis give a "Network is >unreachable" message (full error down below). A grad student I'm working >with said that the script he provided me expects the server to be run in >infrastructure mode, and configuration for ad-hoc mode is required to make >it work correctly. This is confirmed, as I have successfully run the code >on a desktop. I have poured over man pages and stackoverflow, and can't >seem to find resources for how to configure socket broadcasts for ad-hoc >networks. Any thoughts? I would start by debugging a bit outside of Python first. And I've never used an ad hoc wifi network, but after setup I _imagine_ that it looks like a normal local network: an IP address and a network mask, so "braodcast" is the usual notion of the IP address with all 1s in the local part. So 1: Does a manual command line "ping" of the broadcast address work? Eg, if the local network were 192.168.3.x/24 and I went: ping 192.168.3.255 (or on Linux): ping -b 192.168.3.255 I'm assuming your '' below is an actualy broadcast address? Or is it a special string? If so, what happens if you hand construct a broadcast IP like the *.255 above? Any different? Does tcpdump show anything useful, either locally or on one of the other Pis? Though given "Network is unreachable" I'd guess no packets get sent at all. I repeat my disclaimer: I've not used an ad hoc wifi network. Cheers, Cameron Simpson From sijin.john at aressindia.net Fri Jun 14 02:35:53 2019 From: sijin.john at aressindia.net (Sijin John) Date: Fri, 14 Jun 2019 11:35:53 +0500 (MVT) Subject: [Tutor] Download audios & videos using web scraping from news website or facebook Message-ID: <807961546.476826.1560494153172.JavaMail.zimbra@aressindia.net> Hello Sir/Mam, I am trying to Download audios & videos using web scraping from news website (eg: https://www.bbc.com/news/video_and_audio/headlines) or Facebook & I could't. So in real scenario is it really possible to download audios/videos using python code ? Thanks & Regards From alan.gauld at yahoo.co.uk Fri Jun 14 05:37:56 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 14 Jun 2019 10:37:56 +0100 Subject: [Tutor] Download audios & videos using web scraping from news website or facebook In-Reply-To: <807961546.476826.1560494153172.JavaMail.zimbra@aressindia.net> References: <807961546.476826.1560494153172.JavaMail.zimbra@aressindia.net> Message-ID: On 14/06/2019 07:35, Sijin John wrote: > I am trying to Download audios & videos using web scraping from news website > (eg: https://www.bbc.com/news/video_and_audio/headlines) or Facebook & I could't. > So in real scenario is it really possible to download audios/videos using python code ? Of course, just as its possible to do it in any other language. In fact there are several specialist libraries available to make the task easier. It may not be legal however and the web site may have taken steps to prevent you from succeeding or at least make it very difficult. But that has nothing to do with Python, it would be just as difficult in any language. So, if you are having difficulty the problem likely lies with 1) your code and how you are using the tools. 2) the website you are scraping having anti-scraping measures in place. But since you haven't shown us any code ewe can't really comment or make any suggestions. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Fri Jun 14 05:39:11 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 14 Jun 2019 19:39:11 +1000 Subject: [Tutor] Download audios & videos using web scraping from news website or facebook In-Reply-To: <807961546.476826.1560494153172.JavaMail.zimbra@aressindia.net> References: <807961546.476826.1560494153172.JavaMail.zimbra@aressindia.net> Message-ID: <20190614093909.GW4221@ando.pearwood.info> On Fri, Jun 14, 2019 at 11:35:53AM +0500, Sijin John wrote: > I am trying to Download audios & videos using web scraping from news > website (eg: https://www.bbc.com/news/video_and_audio/headlines) or > Facebook & I could't. So in real scenario is it really possible to > download audios/videos using python code ? Please don't mistake "I don't know how to do this" for "this cannot be done". https://youtube-dl.org/ Scraping websites, especially scraping them for videos, can be *very* complex. -- Steven From tom at hale.ee Fri Jun 14 10:53:26 2019 From: tom at hale.ee (Tom Hale) Date: Fri, 14 Jun 2019 21:53:26 +0700 Subject: [Tutor] os.is_file and os.is_dir missing from CPython 3.8.0b? Message-ID: <45b52328-bd74-3459-2b60-6db940debcee@hale.ee> I'm trying to use os.is_dir, but I'm not finding it or os.is_file. What am I missing here? Python 3.8.0b1 (tags/v3.8.0b1:3b5deb01, Jun 13 2019, 22:28:20) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. :>>> import os :>>> print(os.__dict__.keys()) dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__file__', '__cached__', '__builtins__', 'abc', 'sys', 'st', '__all__', '_exists', '_get_exports_list', 'name', 'linesep', 'stat', 'access', 'ttyname', 'chdir', 'chmod', 'fchmod', 'chown', 'fchown', 'lchown', 'chroot', 'ctermid', 'getcwd', 'getcwdb', 'link', 'listdir', 'lstat', 'mkdir', 'nice', 'getpriority', 'setpriority', 'posix_spawn', 'posix_spawnp', 'readlink', 'copy_file_range', 'rename', 'replace', 'rmdir', 'symlink', 'system', 'umask', 'uname', 'unlink', 'remove', 'utime', 'times', 'execv', 'execve', 'fork', 'register_at_fork', 'sched_get_priority_max', 'sched_get_priority_min', 'sched_getparam', 'sched_getscheduler', 'sched_rr_get_interval', 'sched_setparam', 'sched_setscheduler', 'sched_yield', 'sched_setaffinity', 'sched_getaffinity', 'openpty', 'forkpty', 'getegid', 'geteuid', 'getgid', 'getgrouplist', 'getgroups', 'getpid', 'getpgrp', 'getppid', 'getuid', 'getlogin', 'kill', 'killpg', 'setuid', 'seteuid', 'setreuid', 'setgid', 'setegid', 'setregid', 'setgroups', 'initgroups', 'getpgid', 'setpgrp', 'wait', 'wait3', 'wait4', 'waitid', 'waitpid', 'getsid', 'setsid', 'setpgid', 'tcgetpgrp', 'tcsetpgrp', 'open', 'close', 'closerange', 'device_encoding', 'dup', 'dup2', 'lockf', 'lseek', 'read', 'readv', 'pread', 'preadv', 'write', 'writev', 'pwrite', 'pwritev', 'sendfile', 'fstat', 'isatty', 'pipe', 'pipe2', 'mkfifo', 'mknod', 'major', 'minor', 'makedev', 'ftruncate', 'truncate', 'posix_fallocate', 'posix_fadvise', 'putenv', 'unsetenv', 'strerror', 'fchdir', 'fsync', 'sync', 'fdatasync', 'WCOREDUMP', 'WIFCONTINUED', 'WIFSTOPPED', 'WIFSIGNALED', 'WIFEXITED', 'WEXITSTATUS', 'WTERMSIG', 'WSTOPSIG', 'fstatvfs', 'statvfs', 'confstr', 'sysconf', 'fpathconf', 'pathconf', 'abort', 'getloadavg', 'urandom', 'setresuid', 'setresgid', 'getresuid', 'getresgid', 'getxattr', 'setxattr', 'removexattr', 'listxattr', 'get_terminal_size', 'cpu_count', 'get_inheritable', 'set_inheritable', 'get_blocking', 'set_blocking', 'scandir', 'fspath', 'getrandom', 'memfd_create', 'environ', 'F_OK', 'R_OK', 'W_OK', 'X_OK', 'NGROUPS_MAX', 'TMP_MAX', 'WCONTINUED', 'WNOHANG', 'WUNTRACED', 'O_RDONLY', 'O_WRONLY', 'O_RDWR', 'O_NDELAY', 'O_NONBLOCK', 'O_APPEND', 'O_DSYNC', 'O_RSYNC', 'O_SYNC', 'O_NOCTTY', 'O_CREAT', 'O_EXCL', 'O_TRUNC', 'O_LARGEFILE', 'O_PATH', 'O_TMPFILE', 'PRIO_PROCESS', 'PRIO_PGRP', 'PRIO_USER', 'O_CLOEXEC', 'O_ACCMODE', 'SEEK_HOLE', 'SEEK_DATA', 'O_ASYNC', 'O_DIRECT', 'O_DIRECTORY', 'O_NOFOLLOW', 'O_NOATIME', 'EX_OK', 'EX_USAGE', 'EX_DATAERR', 'EX_NOINPUT', 'EX_NOUSER', 'EX_NOHOST', 'EX_UNAVAILABLE', 'EX_SOFTWARE', 'EX_OSERR', 'EX_OSFILE', 'EX_CANTCREAT', 'EX_IOERR', 'EX_TEMPFAIL', 'EX_PROTOCOL', 'EX_NOPERM', 'EX_CONFIG', 'ST_RDONLY', 'ST_NOSUID', 'ST_NODEV', 'ST_NOEXEC', 'ST_SYNCHRONOUS', 'ST_MANDLOCK', 'ST_WRITE', 'ST_APPEND', 'ST_NOATIME', 'ST_NODIRATIME', 'ST_RELATIME', 'POSIX_FADV_NORMAL', 'POSIX_FADV_SEQUENTIAL', 'POSIX_FADV_RANDOM', 'POSIX_FADV_NOREUSE', 'POSIX_FADV_WILLNEED', 'POSIX_FADV_DONTNEED', 'P_PID', 'P_PGID', 'P_ALL', 'WEXITED', 'WNOWAIT', 'WSTOPPED', 'CLD_EXITED', 'CLD_DUMPED', 'CLD_TRAPPED', 'CLD_CONTINUED', 'F_LOCK', 'F_TLOCK', 'F_ULOCK', 'F_TEST', 'RWF_DSYNC', 'RWF_HIPRI', 'RWF_SYNC', 'RWF_NOWAIT', 'POSIX_SPAWN_OPEN', 'POSIX_SPAWN_CLOSE', 'POSIX_SPAWN_DUP2', 'SCHED_OTHER', 'SCHED_FIFO', 'SCHED_RR', 'SCHED_BATCH', 'SCHED_IDLE', 'SCHED_RESET_ON_FORK', 'XATTR_CREATE', 'XATTR_REPLACE', 'XATTR_SIZE_MAX', 'RTLD_LAZY', 'RTLD_NOW', 'RTLD_GLOBAL', 'RTLD_LOCAL', 'RTLD_NODELETE', 'RTLD_NOLOAD', 'RTLD_DEEPBIND', 'GRND_RANDOM', 'GRND_NONBLOCK', 'MFD_CLOEXEC', 'MFD_ALLOW_SEALING', 'MFD_HUGETLB', 'MFD_HUGE_SHIFT', 'MFD_HUGE_MASK', 'MFD_HUGE_64KB', 'MFD_HUGE_512KB', 'MFD_HUGE_1MB', 'MFD_HUGE_2MB', 'MFD_HUGE_8MB', 'MFD_HUGE_16MB', 'MFD_HUGE_32MB', 'MFD_HUGE_256MB', 'MFD_HUGE_512MB', 'MFD_HUGE_1GB', 'MFD_HUGE_2GB', 'MFD_HUGE_16GB', 'pathconf_names', 'confstr_names', 'sysconf_names', 'error', 'waitid_result', 'stat_result', 'statvfs_result', 'sched_param', 'times_result', 'uname_result', 'terminal_size', 'DirEntry', '_exit', 'path', 'curdir', 'pardir', 'sep', 'pathsep', 'defpath', 'extsep', 'altsep', 'devnull', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'SEEK_SET', 'SEEK_CUR', 'SEEK_END', 'makedirs', 'removedirs', 'renames', 'walk', 'fwalk', '_fwalk', 'execl', 'execle', 'execlp', 'execlpe', 'execvp', 'execvpe', '_execvpe', 'get_exec_path', 'MutableMapping', '_Environ', '_putenv', '_unsetenv', 'getenv', 'supports_bytes_environ', 'environb', 'getenvb', 'fsencode', 'fsdecode', 'P_WAIT', 'P_NOWAIT', 'P_NOWAITO', '_spawnvef', 'spawnv', 'spawnve', 'spawnvp', 'spawnvpe', 'spawnl', 'spawnle', 'spawnlp', 'spawnlpe', 'popen', '_wrap_close', 'fdopen', '_fspath', 'PathLike']) >>>> Thanks, -- Tom Hale From alan.gauld at yahoo.co.uk Fri Jun 14 13:43:16 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 14 Jun 2019 18:43:16 +0100 Subject: [Tutor] os.is_file and os.is_dir missing from CPython 3.8.0b? In-Reply-To: <45b52328-bd74-3459-2b60-6db940debcee@hale.ee> References: <45b52328-bd74-3459-2b60-6db940debcee@hale.ee> Message-ID: On 14/06/2019 15:53, Tom Hale wrote: > I'm trying to use os.is_dir, but I'm not finding it or os.is_file. I've never heard of these functions, but I'm still on v3.6, never having found a reason to upgrade. So I assume... > Python 3.8.0b1 (tags/v3.8.0b1:3b5deb01, Jun 13 2019, 22:28:20) ...these are new introductions in 3.8? If so, how do they differ from the os.path.isfile() and os.path.isdir() functions that already exist? Could you use those as alternatives? As to why the new functions aren't showing up, I've no idea, sorry. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Fri Jun 14 13:48:24 2019 From: __peter__ at web.de (Peter Otten) Date: Fri, 14 Jun 2019 19:48:24 +0200 Subject: [Tutor] os.is_file and os.is_dir missing from CPython 3.8.0b? References: <45b52328-bd74-3459-2b60-6db940debcee@hale.ee> Message-ID: Tom Hale wrote: > I'm trying to use os.is_dir, but I'm not finding it or os.is_file. > > What am I missing here? Scroll up a bit in the documentation: https://docs.python.org/3.8/library/os.html#os.DirEntry Both is_file() and is_dir() are methods of the DirEntry object. See also https://docs.python.org/3.8/library/os.path.html#os.path.isfile From mats at wichmann.us Fri Jun 14 14:23:55 2019 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 14 Jun 2019 12:23:55 -0600 Subject: [Tutor] Download audios & videos using web scraping from news website or facebook In-Reply-To: <807961546.476826.1560494153172.JavaMail.zimbra@aressindia.net> References: <807961546.476826.1560494153172.JavaMail.zimbra@aressindia.net> Message-ID: <60312f24-4c04-4f01-08c4-bc174ee0200b@wichmann.us> On 6/14/19 12:35 AM, Sijin John wrote: > Hello Sir/Mam, > I am trying to Download audios & videos using web scraping from news website (eg: https://www.bbc.com/news/video_and_audio/headlines) or Facebook & I could't. So in real scenario is it really possible to download audios/videos using python code ? as others have pointed out, remove the "using python code" from this question to be more accurate. Modern media-serving websites often try quite hard to not have you be able to grab the media objects except on their terms. This often means they are delivered in a streaming manner through a (non-open) player app, and there never is a file at all that you're allowed to directly access. And there's often some wrapping which ends up delivering advertising, because that's probably how the site monetizes their content. If you're expected to access it, there's usually an API for that, which you would use rather than scraping. Obviously people figure out ways, as the youtube downloader shows. From mhysnm1964 at gmail.com Sat Jun 15 00:51:23 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sat, 15 Jun 2019 14:51:23 +1000 Subject: [Tutor] deleting elements out of a list. Message-ID: <013801d52335$fcffa970$f6fefc50$@gmail.com> All, I am not sure how to tackle this issue. I am using Windows 10 and Python 3.6 from Activestate. I have a list of x number of elements. Some of the elements are have similar words in them. For example: Dog food Pal Dog Food Pal qx1323 Cat food kitty Absolute cleaning inv123 Absolute Domestic cleaning inv 222 Absolute d 3333 Fitness first 02/19 Fitness first I wish to remove duplicates. I could use the collection.Count method. This fails due to the strings are not unique, only some of the words are. My thinking and is only rough sudo code as I am not sure how to do this and wish to learn and not sure how to do without causing gtraceback errors. I want to delete the match pattern from the list of strings. Below is my attempt and I hope this makes sense. description = load_files() # returns a list for text in description: words = text.split() for i in enumerate(words): Word = ' '.join(words[:i]) print (word) answer = input('Keep word?') if answer == 'n': continue for i, v in enumerate(description): if word in description[i]: description.pop[i] The initial issues I see with the above is the popping of an element from description list will cause a error. If I copy the description list into a new list. And use the new list for the outer loop. I will receive multiple occurrences of the same text. This could be addressed by a if test. But I am wondering if there is a better method. 2nd code example: description = load_files() # returns a list search_txt = description.copy() # I have not verify if this is the right syntax for the copy method.] for text in search_txt: words = text.split() for i in enumerate(words): Word = ' '.join(words[:i]) print (word) answer = input('Keep word (ynq)?') if answer == 'n': continue elif answer = 'q': break for i, v in enumerate(description): if word in description[i]: description.pop[i] Any improvements? Sean From mhysnm1964 at gmail.com Sat Jun 15 00:53:43 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sat, 15 Jun 2019 14:53:43 +1000 Subject: [Tutor] Differences between while and for Message-ID: <013d01d52336$4fd60590$ef8210b0$@gmail.com> All, In C, Perl and other languages. While only uses a conditional statement and for uses an iteration. In python while and for seems to be the same and I cannot see the difference. Python does not have an until (do while) where the test is done at the end of the loop. Permitting a once through the loop block. Am I correct or is there a difference and if so what is it? Why doesn't Python have an until statement? From steve at pearwood.info Sat Jun 15 03:24:50 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 15 Jun 2019 17:24:50 +1000 Subject: [Tutor] Differences between while and for In-Reply-To: <013d01d52336$4fd60590$ef8210b0$@gmail.com> References: <013d01d52336$4fd60590$ef8210b0$@gmail.com> Message-ID: <20190615072450.GZ4221@ando.pearwood.info> On Sat, Jun 15, 2019 at 02:53:43PM +1000, mhysnm1964 at gmail.com wrote: > All, > > > > In C, Perl and other languages. While only uses a conditional statement and > for uses an iteration. In python while and for seems to be the same and I > cannot see the difference. Python ``while`` uses a conditional statement, and Python ``for`` uses iteration. Python's ``for`` is like "foreach" in some other languages. while condition: ... for x in values: ... > Python does not have an until (do while) where > the test is done at the end of the loop. Permitting a once through the loop > block. Am I correct or is there a difference and if so what is it? Correct, there is no "do until" in Python. > Why doesn't Python have an until statement? Because Guido didn't want one :-) Because it is unnecessary: any "do until" can be written as a regular while loop, using a break: # do...until with test at the end while True: do_something() if test: break # "loop and a half" # https://users.cs.duke.edu/~ola/patterns/plopd/loops.html#loop-and-a-half while True: do_something() if test: break do_something_else() -- Steven From cs at cskk.id.au Sat Jun 15 03:54:46 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 15 Jun 2019 17:54:46 +1000 Subject: [Tutor] deleting elements out of a list. In-Reply-To: <013801d52335$fcffa970$f6fefc50$@gmail.com> References: <013801d52335$fcffa970$f6fefc50$@gmail.com> Message-ID: <20190615075446.GA10792@cskk.homeip.net> On 15Jun2019 14:51, Sean Murphy wrote: >I am not sure how to tackle this issue. I am using Windows 10 and >Python 3.6 from Activestate. > >I have a list of x number of elements. Some of the elements are have similar >words in them. For example: > >Dog food Pal >Dog Food Pal qx1323 >Cat food kitty >Absolute cleaning inv123 >Absolute Domestic cleaning inv 222 >Absolute d 3333 >Fitness first 02/19 >Fitness first I'm going to assume that you have a list of strings, each being a line from a file. >I wish to remove duplicates. I could use the collection.Count method. This >fails due to the strings are not unique, only some of the words are. You need to define this more tightly. Suppose the above were your input. What would it look like after "removing duplicates"? By providing an explicit example of what you expect afterwards it is easier for us to understand you, and will also help you with your implementation. Do you intend to discard the second occurence of every word, turning line 2 above into "qx1323"? Or to remove similar lines, for some definition of "similar", which might discard line 2 above? Your code examples below seem to suggest that your want to discard words you've already seen. >My >thinking and is only rough sudo code as I am not sure how to do this and Aside: "pseudo", not "sudo". >wish to learn and not sure how to do without causing gtraceback errors. I >want to delete the match pattern from the list of strings. Below is my >attempt and I hope this makes sense. > >description = load_files() # returns a list >for text in description: > words = text.split() > for i in enumerate(words): enumerate() yields a sequence of (i, v), so you need i, v in the loop: for i, word in enumerate(words): Or you need the loop variable to be a tuple and to pull out the enumeration counter and the associated value inside the loop: for x in enumerate(words): i, word = x > Word = ' '.join(words[:i]) Variable names in Python are case sensitive. You want "word", not "Word". However, if you really want each word of the line you've got that from text.split(). The expression "words[:i]" means the letters of word from index 0 through to i-1. For example, "kitt" if "i" were 4. The join string operation joins an iterable of strings. Unfortunately for you, a string is itself iterable: you get each character, but as a string (Python does not have a distinct "character" type, it just has single character strings). So if "word" were "kitt" above, you get: "k i t t" from the join. Likely not what you want. What _do_ you want? > print (word) > answer = input('Keep word?') > if answer == 'n': > continue > for i, v in enumerate(description): > if word in description[i]: > description.pop[i] There are some problems here. The big one is that you're modifying a list while you're iterating over it. This is always hazardous - it usually leading to accidentally skipping elements. Or not, depending how the iteration happens. It is generally safer to iterate over the list and construct a distinct new line to replace it, without modifying the original list. This way the enumerate cannot get confused. So instead of discarding from the list, you conditionally add to the new list: new_description = [] for i, word in enumerate(description): if word not in description[i]: new_description.append(word) Note the "not" above. We invert the condition ("not in" instead of "in") because we're inverting the action (appending something instead of discarding it). However, I think you have some fundamental confusion about what your iterating over. I recommend that you adopt better variable names, and more formally describe your data. If "description" is actualy a list of descriptions then give it a plural name like "descriptions". When you iterate over it, you can then use the singular form for each element i.e. "description" instead of "text". Instead of writing loops like: for i, v in enumerate(descriptions): give "v" a better name, like "description". That way your code inside the loop is better described, and mistakes more obvious because the code will suddenly read badly in some way. >The initial issues I see with the above is the popping of an element >from >description list will cause a error. It often won't. Instead if will mangle your iteration because after the pop the index "i" no longer refers to what you expect, it now points one word further along. Towards the _end_ of the loop you'll get an error, but only once "i" starts to exceed the length of the list (because you've been shortening it). >If I copy the description list into a >new list. And use the new list for the outer loop. I will receive multiple >occurrences of the same text. This could be addressed by a if test. But I am >wondering if there is a better method. The common idom is to leave the original unchanged and copy into a new list as in my example above. But taking a copy and iterating over that is also reasonable. You will still have issues with the popping, because the index "i" will no longer be aligned with the modified list. If you really want to modify in place, avoid enumerate. Instead, make "i" an index into the list as you do, but maintain it yourself. Loop from left to right in the list until you come off the end: i = 0 while i < len(description): if ... we want to pop the element ...: description.pop(i) else: i = i + 1 Here we _either_ discard from the list and _do not_ advance "i", or we advance "i". Either way "i" then points at the next word, in the former case because the next word has shuffled down once position and in the latter because "i" has moved forwards. Either way "i" gets closer to the end of the list. We leave the loop when "i" gets past the end. >2nd code example: > >description = load_files() # returns a list >search_txt = description.copy() # I have not verify if this is the right >syntax for the copy method.] A quick way is: search_text = description[:] but lists have a .copy method which does the same thing. >for text in search_txt: > words = text.split() > for i in enumerate(words): > Word = ' '.join(words[:i]) > print (word) > answer = input('Keep word (ynq)?') > if answer == 'n': > continue > elif answer = 'q': > break > for i, v in enumerate(description): > if word in description[i]: > description.pop[i] The inner for loop still has all the same issues as before. The outer loop is now more robust because you've iterating over the copy. Cheers, Cameron Simpson From cs at cskk.id.au Sat Jun 15 04:11:54 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 15 Jun 2019 18:11:54 +1000 Subject: [Tutor] Differences between while and for In-Reply-To: <013d01d52336$4fd60590$ef8210b0$@gmail.com> References: <013d01d52336$4fd60590$ef8210b0$@gmail.com> Message-ID: <20190615081153.GA93705@cskk.homeip.net> On 15Jun2019 14:53, Sean Murphy wrote: >In C, Perl and other languages. While only uses a conditional statement >and >for uses an iteration. In python while and for seems to be the same and I >cannot see the difference. No, they're really much as in other languages. In general (most languages), a for loop is for iterating over some collection or list. A while is not explicitly for iteration over some collection, it is for repeating an action until some condition fails (or the inverse of the condition is achieved). Let's take C's for loop. It really is closely related to a while. That's because C is a pretty low level language. Early C is almost like a structured assembly language (well, it is a lot better, but it is deliberately close to the underlying machine). So C's for loop goes: for (setup; condition; advance) statement-or-block You can leave any of these out. It is equivalent to this while loop: setup while condition: statement-or-block advance but it is almost always used for iteration: s="foo" for (i=0; s[i]; i++) ... which counts "i" along the string "s". You _can_ use of for arbitrary while loops, but idiomatically that is rarely done - it is conceptually useful to use "for" for various kinds of iteration and "while" for more arbitrary repetition. Python is a bit more rigid. The "while" loop is just like "while" in other languages: do something while a condition holds. But a "for" loop in Python is inherently about iteration; it is defined as: for variable in iterable: suite and applies to any "iterable", some object or expression which can be iterated over. Any object which is iterable may be used. So it is very oriented towards collections of various kinds: lists, tuples, dictionary (iterates over the keys) and so on. >Python does not have an until (do while) where >the test is done at the end of the loop. Permitting a once through the loop >block. Am I correct or is there a difference and if so what is it? You're correct. >Why doesn't Python have an until statement? Basicly because it isn't necessary. It is usually easy enough to work around the lack that nobody has made a conincing case (meaning nobody has convinced the core developers). It would probably be written: do: ... while condition in some form if it ever came in to avoid using an new keyword ("until"). It does sometimes take a little contortion to make a do/while loop into a Python while - you usually have to perform some kind of hack to make the condition initially true. In the extreme case you just treat the first loop specially: first = True while first or the-actual-condition: ... do stuff ... first = False if you want to use "first" during the "do stuff". Or you could be a bit more reliable and go: first_test = True while first_test or the-actual-condition: first_test = False ... do stuff ... putting the flag up the top next to the condition. Cheers, Cameron Simpson From mhysnm1964 at gmail.com Sat Jun 15 05:27:01 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sat, 15 Jun 2019 19:27:01 +1000 Subject: [Tutor] Differences between while and for In-Reply-To: <20190615072450.GZ4221@ando.pearwood.info> References: <013d01d52336$4fd60590$ef8210b0$@gmail.com> <20190615072450.GZ4221@ando.pearwood.info> Message-ID: <001a01d5235c$7e6131c0$7b239540$@gmail.com> Steven Thanks. -----Original Message----- From: Tutor On Behalf Of Steven D'Aprano Sent: Saturday, 15 June 2019 5:25 PM To: tutor at python.org Subject: Re: [Tutor] Differences between while and for On Sat, Jun 15, 2019 at 02:53:43PM +1000, mhysnm1964 at gmail.com wrote: > All, > > > > In C, Perl and other languages. While only uses a conditional > statement and for uses an iteration. In python while and for seems to > be the same and I cannot see the difference. Python ``while`` uses a conditional statement, and Python ``for`` uses iteration. Python's ``for`` is like "foreach" in some other languages. while condition: ... for x in values: ... > Python does not have an until (do while) where the test is done at the > end of the loop. Permitting a once through the loop block. Am I > correct or is there a difference and if so what is it? Correct, there is no "do until" in Python. > Why doesn't Python have an until statement? Because Guido didn't want one :-) Because it is unnecessary: any "do until" can be written as a regular while loop, using a break: # do...until with test at the end while True: do_something() if test: break # "loop and a half" # https://users.cs.duke.edu/~ola/patterns/plopd/loops.html#loop-and-a-half while True: do_something() if test: break do_something_else() -- Steven _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From mhysnm1964 at gmail.com Sat Jun 15 05:35:57 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sat, 15 Jun 2019 19:35:57 +1000 Subject: [Tutor] Follow-up on my removing elements from lists question. Message-ID: <002b01d5235d$bd9e5a60$38db0f20$@gmail.com> This is a follow-up on my previous question for removing elements. Below is the code I am currently using. I am removing the elements at the end of the outer loop. The data structure goes along this: [ ['123123',[2019-2-18', 'transaction text', 'amount'], v ['123123',[2019-2-18', 'transaction text', 'amount'], ['123123',[2019-2-18', 'transaction text', 'amount'] ] The 2nd column where the transaction text I am modifying the content and using the end result of the built-up words as the string match as you will see in the code. This is all working fine. The last loop in the code I am trying to delete the elements in reverse order. This doesn't work. The length of the list reduces by 1. When it should have reduced by 42. Is the logic wrong? This is in Python 3.6 under windows 10. unknown_transactions.sort(key=lambda x: x[2]) while True: # re-initialise each time the current transaction text has been processed. for row in unknown_transactions: # remove common words from transactions which are not required. Such as 'WITHDRAWAL' and 'DEPOSIT'. line = regex_transaction(row[2]) # If the common words are not found, return a null and do not modify the transaction description. if line != '': # not a null string # now find unique string and add it to the compare_transactions list object. words = line.split() print ('List length:', len(unknown_transactions)) word = '' for i, v in enumerate(words, start=1): word = ' '.join(words[:i]) print (word) answer = input('Use word y, otherwise any other key continues...') if answer != 'y': continue # end if # end for # now loop through the unknown transactions and copy to transaction dictionary delete_transactions = [] for e, v in enumerate (unknown_transactions): if word in unknown_transactions[e][2]: if not word in transaction: transaction[word] = unknown_transactions else: transaction[word].append(unknown_transactions) # end if delete_transactions.append (e) # end if # end for print ('number of elements to remove:', len(delete_transactions)) for del_element in reversed(delete_transactions): unknown_transactions.pop(del_element) # end if # end for if len(unknown_transactions) == 0: break # end if # end while From akleider at sonic.net Sat Jun 15 12:42:20 2019 From: akleider at sonic.net (Alex Kleider) Date: Sat, 15 Jun 2019 09:42:20 -0700 Subject: [Tutor] Follow-up on my removing elements from lists question. In-Reply-To: <002b01d5235d$bd9e5a60$38db0f20$@gmail.com> References: <002b01d5235d$bd9e5a60$38db0f20$@gmail.com> Message-ID: <7df4d157a870c7c628e28fef2e644c58@sonic.net> On 2019-06-15 02:35, mhysnm1964 at gmail.com wrote: > This is a follow-up on my previous question for removing elements. > Below is > the code I am currently using. I am removing the elements at the end of > the > outer loop. The data structure goes along this: > > > > [ > > ['123123',[2019-2-18', 'transaction text', 'amount'], > > v ['123123',[2019-2-18', 'transaction text', 'amount'], > > ['123123',[2019-2-18', 'transaction text', 'amount'] > > ] > I suggest you match up your single quote and your square bracket pairs. It looks to me that you have one extra single quote and one extra opening square bracket in each line (or perhaps you're missing their closing partners.) From mats at wichmann.us Sat Jun 15 14:14:32 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 15 Jun 2019 12:14:32 -0600 Subject: [Tutor] Follow-up on my removing elements from lists question. In-Reply-To: <002b01d5235d$bd9e5a60$38db0f20$@gmail.com> References: <002b01d5235d$bd9e5a60$38db0f20$@gmail.com> Message-ID: On 6/15/19 3:35 AM, mhysnm1964 at gmail.com wrote: Data structure: ['123123',[2019-2-18', 'transaction text', 'amount'], I presume the second opening brace is a typo and was supposed to be a quote mark? > The 2nd column where the transaction text I am modifying the content and > using the end result of the built-up words as the string match as you will > see in the code. This is all working fine. The last loop in the code I am > trying to delete the elements in reverse order. This doesn't work. The > length of the list reduces by 1. When it should have reduced by 42. Is the > logic wrong? This is in Python 3.6 under windows 10. There's a lot that looks odd in this code, let me poke at a few: You _appear_ to be be trying to avoid modifying 'unknown_transactions' while looping over it, which is admirable if that was the intent: for e, v in enumerate (unknown_transactions): if word in unknown_transactions[e][2]: if not word in transaction: transaction[word] = unknown_transactions else: transaction[word].append(unknown_transactions) delete_transactions.append (e) for del_element in reversed(delete_transactions): unknown_transactions.pop(del_element) but that whole sequence is inside another loop over 'unknown_transactions'. Is that intentional? It seem odd to loop over something inside a loop over that thing. As a hint, if you want to modify a list while looping you can loop over a copy of it, like for s in somelist[:]: or if you don't like the slice notation, for s in list(somelist): you're looping over 'unknown_transactions' here, but you're not using the loop value 'v' at all in the body. Perhaps that's the thing you wanted to add to 'transactions'? adding 'unknown_transactions' seems strange. usually if you're using multiple levels of indexing you're not necessarily wrong, but leaving yourself (or others) hard to read code. In the past I've written such things and a year later I come back to look and wonder about the magic in something like [e][2]: if word in unknown_transactions[e][2]: You can unpack each transaction list instead to avoid indexing it, and if you are deleting on the spot, you don't need the enumerate value either. So a possible rewrite could be: for u in unknown_transactions[:]: id, date, text, amount = u if word in text: if not word in transaction: transaction[word] = u else: transaction[word].append(u) unknown_transactions.remove(u) the inner 'if' statement could become a try block as well if you wanted: try: transaction[word].append(u) except KeyError: transaction[word] = u And you can save a level of indentation by inverting your early if-statement, from if line != '': whole bunch of code to if not line: continue whole bunch of code Just to make things a little more readable. From alan.gauld at yahoo.co.uk Sat Jun 15 16:55:18 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 15 Jun 2019 21:55:18 +0100 Subject: [Tutor] Differences between while and for In-Reply-To: <013d01d52336$4fd60590$ef8210b0$@gmail.com> References: <013d01d52336$4fd60590$ef8210b0$@gmail.com> Message-ID: On 15/06/2019 05:53, mhysnm1964 at gmail.com wrote: > In C, Perl and other languages. As a point of interest not all languages have these constructs. Oberon, for example, only has a while loop because it can be used to simulate all other loop types. Some Lisp dialects don't even have a loop construct because recursion can be used instead. In addition to for, while and repeat/until some languages (eg ADA and some BASICs) include a general loop construct. And of course in assembler GOTO is the ultimate loop construct. Don;t assume that just because one language supports a particular construct that others will or should also support it. The variety of control structures offered is one of the defining features of any programming language. -- 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 beachkidken at gmail.com Sat Jun 15 17:23:53 2019 From: beachkidken at gmail.com (Ken Green) Date: Sat, 15 Jun 2019 17:23:53 -0400 Subject: [Tutor] Installing Python v3 on a laptop Windows 10 Message-ID: <6b5a3c77-0fa7-49f5-0c50-305135ca2610@gmail.com> It has been some 18 months that I last installed Python onto my laptop Windows. Having had freshly completely reinstalled Windows 10 and its various updates. I already installed PSREdit500 successfully several weeks ago, I am now ready to install Python, preferable the latest version of Python 3. I understood there is a preferable method of installing Python into Windows. I pray tell on how about to do it, gentlemen. I have been using Python v2.7.15 along with Geany v1.32 in my computer running on Ubuntu 18.04.2. As you can see, there is a sharp learning curve for me on how to use and learn Python v3. Thanks. Ken From alan.gauld at yahoo.co.uk Sat Jun 15 17:25:03 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 15 Jun 2019 22:25:03 +0100 Subject: [Tutor] deleting elements out of a list. In-Reply-To: <013801d52335$fcffa970$f6fefc50$@gmail.com> References: <013801d52335$fcffa970$f6fefc50$@gmail.com> Message-ID: On 15/06/2019 05:51, mhysnm1964 at gmail.com wrote: Caveat: I'm picking this up late in the day and only had a cursory look at it, so may be missing some critical insight... > I have a list of x number of elements. Some of the elements are have similar > words in them. For example: Define "similar". It implies not identical. What is different? What makes them similar? Every time you introduce vague inequalities you imply the need for some kind of intelligent function that removes the ambiguity and vagueness. it definitively says that these two items are similar or not similar. Can you write such a function? If so the problem should become relatively simple. > Dog food Pal > Dog Food Pal qx1323 > Cat food kitty > Absolute cleaning inv123 > Absolute Domestic cleaning inv 222 > Absolute d 3333 > Fitness first 02/19 > Fitness first > > I wish to remove duplicates. So what would the output look like if the above is the input? My guess of what you want is: qx1323 Cat kitty Domestic d 3333 02/19 Is that right? Or is my idea of similar and duplicate different to yours? > I could use the collection.Count method. This > fails due to the strings are not unique, only some of the words are. Sorry, I can't understand that. It makes no sense to me. You need to define strings and words in this context > description = load_files() # returns a list A list of what? characters, words, lines? > for text in description: > words = text.split() > for i in enumerate(words): > Word = ' '.join(words[:i]) This is weird. enumerate returns tuples which you assign to i. But then you use i in a slice opertion. But slice expects an integer. > print (word) > answer = input('Keep word?') > if answer == 'n': > continue > for i, v in enumerate(description): > if word in description[i]: > description.pop[i] Without any clue what the description data looks like we can't really decipher what the code above does. > description list will cause a error. If I copy the description list into a > new list. And use the new list for the outer loop. I will receive multiple > occurrences of the same text. I'm not sure thats true but it denends on what description looks like. > > description = load_files() # returns a list > > search_txt = description.copy() > > for text in search_txt: > words = text.split() > for i in enumerate(words): > Word = ' '.join(words[:i]) > print (word) > answer = input('Keep word (ynq)?') > if answer == 'n': > continue > elif answer = 'q': > break > > for i, v in enumerate(description): > if word in description[i]: > description.pop[i] The usual way to remove things from a list is to create a new list using a filter newlist = filter(test_function, oldlist) or a list comprehension newlist = [item for item in oldlist if test_function(item)] Which brings us back to the beginning. Can you write a test function that unambiguously defines what needs to be removed? -- 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 PyTutor at DancesWithMice.info Sat Jun 15 17:03:00 2019 From: PyTutor at DancesWithMice.info (David L Neil) Date: Sun, 16 Jun 2019 09:03:00 +1200 Subject: [Tutor] Follow-up on my removing elements from lists question. In-Reply-To: <002b01d5235d$bd9e5a60$38db0f20$@gmail.com> References: <002b01d5235d$bd9e5a60$38db0f20$@gmail.com> Message-ID: On 15/06/19 9:35 PM, mhysnm1964 at gmail.com wrote: > This is a follow-up on my previous question for removing elements. Below is > the code I am currently using. I am removing the elements at the end of the > outer loop. The data structure goes along this: > [ > ['123123',[2019-2-18', 'transaction text', 'amount'], > v ['123123',[2019-2-18', 'transaction text', 'amount'], > ['123123',[2019-2-18', 'transaction text', 'amount'] > ] > The 2nd column where the transaction text I am modifying the content and > using the end result of the built-up words as the string match as you will > see in the code. This is all working fine. The last loop in the code I am > trying to delete the elements in reverse order. This doesn't work. The > length of the list reduces by 1. When it should have reduced by 42. Is the > logic wrong? This is in Python 3.6 under windows 10. > > unknown_transactions.sort(key=lambda x: x[2]) > while True: > # re-initialise each time the current transaction text has been processed. > for row in unknown_transactions: > # remove common words from transactions which are not required. Such > as 'WITHDRAWAL' and 'DEPOSIT'. > line = regex_transaction(row[2]) > # If the common words are not found, return a null and do not modify > the transaction description. (from a very weak understanding of your previous question and the total code-base thus far) Consideration nr1: Write the code as comments first. Initially these will be at a fairly 'high level'. These comments can later be turned into function/method names, and more comments added within those. Wash, rinse, and repeat. The idea is to help re-state your thinking into Python code, and to structure the code into functional units. Even skilled Python-coders often find that this helps to keep the use-case foremost in-mind. Consideration nr2: (assuming that the total data-volume is easily RAM-resident) Rather than (appearing to) taking-in a 'group' of transactions and then select them according to ease/difficulty of 'translation', gradually removing/whittling the numbers down - hopefully to zero; why not consider adding another field to each record, which will note if it has already been processed (by whichever method) or conversely, which list-elements are yet to be completed? Thus each method of interpretation will first check the 'completed' field, and if not complete, apply the relevant analysis... Thus there is no concept of 'removal' and no danger of 'losing' anything! -- Regards =dn From alan.gauld at yahoo.co.uk Sat Jun 15 19:31:09 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 16 Jun 2019 00:31:09 +0100 Subject: [Tutor] Installing Python v3 on a laptop Windows 10 In-Reply-To: <6b5a3c77-0fa7-49f5-0c50-305135ca2610@gmail.com> References: <6b5a3c77-0fa7-49f5-0c50-305135ca2610@gmail.com> Message-ID: On 15/06/2019 22:23, Ken Green wrote: > I understood there is a preferable method > of installing Python into Windows. I pray > tell on how about to do it, gentlemen. It depends a bit on which python distribution you use, there are several. Personally for Windows I always recommend the ActiveState free version. It bundles several useful extra Windows tools and puts the docs in Windows help format for you. -- 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 eryksun at gmail.com Sat Jun 15 20:20:11 2019 From: eryksun at gmail.com (eryk sun) Date: Sat, 15 Jun 2019 19:20:11 -0500 Subject: [Tutor] Installing Python v3 on a laptop Windows 10 In-Reply-To: References: <6b5a3c77-0fa7-49f5-0c50-305135ca2610@gmail.com> Message-ID: On 6/15/19, Alan Gauld via Tutor wrote: > On 15/06/2019 22:23, Ken Green wrote: > >> I understood there is a preferable method >> of installing Python into Windows. I pray >> tell on how about to do it, gentlemen. > > It depends a bit on which python distribution you use, > there are several. > > Personally for Windows I always recommend the ActiveState free > version. It bundles several useful extra Windows tools and > puts the docs in Windows help format for you. The compiled HTML (python*.chm) documentation is also included in the official PSF (python.org) distribution. It's in the "Doc" folder. The installer should create a shortcut to it in the start menu. The current release of Windows 10 includes a `python` command that installs the 3.7 app bundle from the Microsoft Store. This is a simple one-click install method that should be easy for anyone. Unfortunately the above-mentioned python37.chm file is not included in this distribution. From mats at wichmann.us Sat Jun 15 21:27:48 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 15 Jun 2019 19:27:48 -0600 Subject: [Tutor] Installing Python v3 on a laptop Windows 10 In-Reply-To: <6b5a3c77-0fa7-49f5-0c50-305135ca2610@gmail.com> References: <6b5a3c77-0fa7-49f5-0c50-305135ca2610@gmail.com> Message-ID: On 6/15/19 3:23 PM, Ken Green wrote:\ You've already gotten some good answers, don't consider this as contradictory. > I understood there is a preferable method > of installing Python into Windows. I pray > tell on how about to do it, gentlemen. There isn't, there are actually many ways, and to some extent it depends on what you want to do. For example, in addition to what you've heard, these days there are a ton of people doing data analysis, Big Data, etc. and they often prefer to install Python through the Anaconda distribution, which has optimised for getting the particularly relevant packages installed easily alongside and in sync with Python, and then keeping those up to date. In the near future, but maybe not quite there yet, Windows 10 will also have a Python "app" preinstalled, which, when you launch it, installs the current version of Python through the Microsoft Store. I think you can already install via the Microsoft Store, but it's not something that magically appears on your system even before you think to look for it... see here: https://www.microsoft.com/en-us/p/python-37/9nj46sx7x90p?activetab=pivot:overviewtab That looks like it wants later than the 1809 version, but should be ready for the 1903 version of Windows 10? and you wanted a simple answer.... From mhysnm1964 at gmail.com Sat Jun 15 20:51:07 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sun, 16 Jun 2019 10:51:07 +1000 Subject: [Tutor] deleting elements out of a list. In-Reply-To: References: <013801d52335$fcffa970$f6fefc50$@gmail.com> Message-ID: <012a01d523dd$9676a480$c363ed80$@gmail.com> Allan, I will respond to your reply in the other thread. As I want to respond to everyone in one email. Thanks for the clarification on the questions or information I did not provide. 101 troubleshooting assistance. Do not assume others know what you know. ? -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Sunday, 16 June 2019 7:25 AM To: tutor at python.org Subject: Re: [Tutor] deleting elements out of a list. On 15/06/2019 05:51, mhysnm1964 at gmail.com wrote: Caveat: I'm picking this up late in the day and only had a cursory look at it, so may be missing some critical insight... > I have a list of x number of elements. Some of the elements are have > similar words in them. For example: Define "similar". It implies not identical. What is different? What makes them similar? Every time you introduce vague inequalities you imply the need for some kind of intelligent function that removes the ambiguity and vagueness. it definitively says that these two items are similar or not similar. Can you write such a function? If so the problem should become relatively simple. > Dog food Pal > Dog Food Pal qx1323 > Cat food kitty > Absolute cleaning inv123 > Absolute Domestic cleaning inv 222 > Absolute d 3333 > Fitness first 02/19 > Fitness first > > I wish to remove duplicates. So what would the output look like if the above is the input? My guess of what you want is: qx1323 Cat kitty Domestic d 3333 02/19 Is that right? Or is my idea of similar and duplicate different to yours? > I could use the collection.Count method. This fails due to the strings > are not unique, only some of the words are. Sorry, I can't understand that. It makes no sense to me. You need to define strings and words in this context > description = load_files() # returns a list A list of what? characters, words, lines? > for text in description: > words = text.split() > for i in enumerate(words): > Word = ' '.join(words[:i]) This is weird. enumerate returns tuples which you assign to i. But then you use i in a slice opertion. But slice expects an integer. > print (word) > answer = input('Keep word?') > if answer == 'n': > continue > for i, v in enumerate(description): > if word in description[i]: > description.pop[i] Without any clue what the description data looks like we can't really decipher what the code above does. > description list will cause a error. If I copy the description list > into a new list. And use the new list for the outer loop. I will > receive multiple occurrences of the same text. I'm not sure thats true but it denends on what description looks like. > > description = load_files() # returns a list > > search_txt = description.copy() > > for text in search_txt: > words = text.split() > for i in enumerate(words): > Word = ' '.join(words[:i]) > print (word) > answer = input('Keep word (ynq)?') > if answer == 'n': > continue > elif answer = 'q': > break > > for i, v in enumerate(description): > if word in description[i]: > description.pop[i] The usual way to remove things from a list is to create a new list using a filter newlist = filter(test_function, oldlist) or a list comprehension newlist = [item for item in oldlist if test_function(item)] Which brings us back to the beginning. Can you write a test function that unambiguously defines what needs to be removed? -- 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 cemv96 at hotmail.com Mon Jun 17 01:30:18 2019 From: cemv96 at hotmail.com (Cem Vardar) Date: Mon, 17 Jun 2019 05:30:18 +0000 Subject: [Tutor] How to Scrape Text from PDFs Message-ID: <7EB9FE79-90D9-4762-B45D-B90B8F6EB3E1@hotmail.com> Hello, I have been working on assignment that was described to me as ?fairly trivial? for a couple of days now. I have some PDF files that have links for some websites and I need to extract these links from these files by using Python. I would be very glad if someone could point me in the direction of some resources that would give me the essential skills specific for this task. Sincerely, Cem From wrw at mac.com Mon Jun 17 16:33:48 2019 From: wrw at mac.com (William Ray Wing) Date: Mon, 17 Jun 2019 16:33:48 -0400 Subject: [Tutor] How to Scrape Text from PDFs In-Reply-To: <7EB9FE79-90D9-4762-B45D-B90B8F6EB3E1@hotmail.com> References: <7EB9FE79-90D9-4762-B45D-B90B8F6EB3E1@hotmail.com> Message-ID: <7B5678CE-3D24-42AE-BA4C-775B5D4BC596@mac.com> > On Jun 17, 2019, at 1:30 AM, Cem Vardar wrote: > > Hello, > > I have been working on assignment that was described to me as ?fairly trivial? for a couple of days now. I have some PDF files that have links for some websites and I need to extract these links from these files by using Python. I would be very glad if someone could point me in the direction of some resources that would give me the essential skills specific for this task. > Unfortunately, a PDF can contain anything from almost PostScript to a bit map. But lets assume your PDFs are of the almost PostScript flavor. In that case you can simply read them as text, and then use standard Python?s standard string searching for http:// or https://. Each time you find one, stop and parse (again with string handling) the URL looking for one of the typical terminators (e.g. .com, .net, .org etc.). It might help to cheat a bit and open one of the PDFs with a standard text editor and using it, search for http:// and see what turns up. I?ll bet it will be fairly clear. Bill > Sincerely, > Cem > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From beachkidken at gmail.com Mon Jun 17 16:59:47 2019 From: beachkidken at gmail.com (Ken Green) Date: Mon, 17 Jun 2019 16:59:47 -0400 Subject: [Tutor] Installing Python v3 on a laptop Windows 10 (SOLVED) In-Reply-To: References: <6b5a3c77-0fa7-49f5-0c50-305135ca2610@gmail.com> Message-ID: <7048728f-d2eb-ef3f-acac-635e4189b607@gmail.com> On 15/06/2019 22:23, Ken Green wrote: > I understood there is a preferable method > of installing Python into Windows. I pray > tell on how about to do it, gentlemen. Thank you gentlemen for the prompt responses to my inquiry. I believe it would be best for me to use the ActiveState installation for my laptop. I like Microsoft trying to make it easily to download Python but I am not sure if it has been fully implemented yet. Again, thanks guys. Ken Green From alan.gauld at yahoo.co.uk Mon Jun 17 18:38:57 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 17 Jun 2019 23:38:57 +0100 Subject: [Tutor] How to Scrape Text from PDFs In-Reply-To: <7EB9FE79-90D9-4762-B45D-B90B8F6EB3E1@hotmail.com> References: <7EB9FE79-90D9-4762-B45D-B90B8F6EB3E1@hotmail.com> Message-ID: On 17/06/2019 06:30, Cem Vardar wrote: > some PDF files that have links for some websites and I need to extract these links There is a module that may help: PyPDF2 Here is a post showing how to extract the text from a PDF which should include the links. https://stackoverflow.com/questions/34837707/how-to-extract-text-from-a-pdf-file There may even be more specific extraction tools if you look more closely... -- 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 btwadsworth at gmail.com Tue Jun 18 09:28:35 2019 From: btwadsworth at gmail.com (Ben Wadsworth) Date: Tue, 18 Jun 2019 08:28:35 -0500 Subject: [Tutor] Installing Python on Server Message-ID: Hi, When installing Python on a windows server, will the server require a restart? Thank you! From alan.gauld at yahoo.co.uk Tue Jun 18 19:15:18 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 19 Jun 2019 00:15:18 +0100 Subject: [Tutor] Installing Python on Server In-Reply-To: References: Message-ID: On 18/06/2019 14:28, Ben Wadsworth wrote: > Hi, > When installing Python on a windows server, will the server require a > restart? I've never tried so can't be sure. But it shouldn't. Python doesn't require any special access. But then again, it is Windows, so you can never tell. -- 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 mjch at mjch.net Tue Jun 18 20:37:42 2019 From: mjch at mjch.net (Malcolm Herbert) Date: Wed, 19 Jun 2019 10:37:42 +1000 Subject: [Tutor] How to Scrape Text from PDFs In-Reply-To: <7EB9FE79-90D9-4762-B45D-B90B8F6EB3E1@hotmail.com> References: <7EB9FE79-90D9-4762-B45D-B90B8F6EB3E1@hotmail.com> Message-ID: <16525baf-0f16-4ee9-8836-9d72d513eb3e@www.fastmail.com> This isn't a response that's python-related, sorry, I'm still learning python myself, but more questions around the nature of the PDF and where I might start looking to solve the problem, were it mine. The URLs that you are intending to match - are they themselves clickable when you open the PDF in another reader? If so, then you might have better luck looking for the PDF element that provides that capability rather than trying to text-scrape to recover them. Although unlikely inside a URL, text in a PDF can be laid out on the page in a completely arbitrary manner and to properly do PDF-to-text conversion you may need to track position on the page for each glyph as well as the font mapping vector - a glyph of an 'A' for instance might not actually be mapped to the ASCII/Unicode for 'A' ... all of which can make this a complete nightmare for the unwary. So - when I last looked at generating a PDF with a live link element, this was implemented as blue underlined text (to make it look like a link) with an invisible box placed over the top which contained the PDF magic to make that do what I wanted when the user clicked on it. I would suspect that what you might want would be a Python library that can pull apart a PDF into it's structural elements and then hunt through there for the appropriate "URL box" or whatever it's called ... Hope that helps, Malcolm -- Malcolm Herbert mjch at mjch.net From savageapple850 at gmail.com Wed Jun 19 00:18:35 2019 From: savageapple850 at gmail.com (Cravan) Date: Wed, 19 Jun 2019 12:18:35 +0800 Subject: [Tutor] Unexpected result when running flask application. Message-ID: <2878D313-2422-499A-9D4E-5F403AEA0A69@gmail.com> Hi all, I am experiencing an unexpected result when I try to run my flask application. The movie.html page prints out nothing except those in the

. This appears on my webpage: I want it to print out all the stuff related to the movie in my sql table, e.g. year, runtime etc. How should I modify my code then? Also, when trying out search.html, it prints method not allowed for the requested URL. How should I rectify this? Cravan -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: error.html URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: error2.html URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: index.html URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: layout.html URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: movie_individual.html URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: movie_specific.html URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: movies.html URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: search.html URL: From alan.gauld at yahoo.co.uk Wed Jun 19 04:54:46 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 19 Jun 2019 09:54:46 +0100 Subject: [Tutor] Unexpected result when running flask application. In-Reply-To: <2878D313-2422-499A-9D4E-5F403AEA0A69@gmail.com> References: <2878D313-2422-499A-9D4E-5F403AEA0A69@gmail.com> Message-ID: On 19/06/2019 05:18, Cravan wrote: > Hi all, > > I am experiencing an unexpected result when I try to run my flask application. > The movie.html page prints out nothing except those in the

. This appears on my webpage: Note that the mail server does not allow (for security reasons) binary attachments so we lost your image. However, your html files are not in HTML. I'm not a Flask expert but every time I've used Flask the html pages have been real HTML. Yours appear to be in some strange pseudo markup language. If this is something unique to Flask then I suspect you will need to ask on a Flask support page or list. It doesn't seem to be a Python language related issue at this point. And your layout.html template is virtually empty. I think you need to write some valid HTML somewhere. -- 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 Jun 19 18:56:29 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 20 Jun 2019 08:56:29 +1000 Subject: [Tutor] Unexpected result when running flask application. In-Reply-To: References: Message-ID: <20190619225629.GA36554@cskk.homeip.net> On 19Jun2019 09:54, Alan Gauld wrote: >On 19/06/2019 05:18, Cravan wrote: >> I am experiencing an unexpected result when I try to >> run my flask application. >> The movie.html page prints out nothing except those in the

. This appears on my webpage: > >Note that the mail server does not allow (for security reasons) >binary attachments so we lost your image. Cravan, you might find it useful to "View Source" of that page in your browser. You can also use command line tools like "curl" or "wget" to directly fetch the page content. >However, your html files are not in HTML. >I'm not a Flask expert but every time I've used Flask the >html pages have been real HTML. Yours appear to be in some >strange pseudo markup language. It is very common in Flask to write HTML pages using Jinja templates, which is what his examples look like. Of course this adds more complexity, if he forgets to use Jinja to render the content to HTML before returning it. >If this is something unique to Flask then I suspect you will >need to ask on a Flask support page or list. It doesn't seem >to be a Python language related issue at this point. He has, as it happens, over in flask at python.org. Cheers, Cameron Simpson From aarizpe448 at gmail.com Wed Jun 19 18:30:43 2019 From: aarizpe448 at gmail.com (Antonio Arizpe) Date: Wed, 19 Jun 2019 17:30:43 -0500 Subject: [Tutor] Hii Message-ID: Hey ive looked everywhere and i would really appreciate the guidance i know its not too complicated by google search results dodge my real question i just need help with a script thats registers keystrikes and adds up all the times youve struck a key and gives a number of the total amount of times the keyboard was struck. nothing specific about characters. just how many times it was struck in a real number. please any help would be greatly appreciated From alan.gauld at yahoo.co.uk Thu Jun 20 04:28:23 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 20 Jun 2019 09:28:23 +0100 Subject: [Tutor] Hii In-Reply-To: References: Message-ID: On 19/06/2019 23:30, Antonio Arizpe wrote: > i just need help with a script thats registers keystrikes and adds up all > the times youve struck a key and gives a number of the total amount of > times the keyboard was struck. nothing specific about characters. just how > many times it was struck in a real number. It is possible, but it will likely be OS specific so you need to tell us which OS you are using/targeting. Also any code that you've tried always helps along with any error messages. Also tell us about any 3rd party libraries you are using. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mhysnm1964 at gmail.com Thu Jun 20 06:44:06 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Thu, 20 Jun 2019 20:44:06 +1000 Subject: [Tutor] word printing issue Message-ID: <042101d52755$16e5f3d0$44b1db70$@gmail.com> All, I have a list of strings that I want to break them into separate words, and a combination of words then store them into a list. Example below of a string: "Hello Python team". The data structure: [ ['Hello'], ['Hello', 'Python'], ['Hello', 'Python', 'team'], ]'Python'], ]'Python', 'team'], ['team'] ] I want to know if there is a better method in doing this without the requirement of a module. Eventually I want to count the number of hits in a list of strings based upon the word combinations regardless where they are located in the string. This last part is something I am struggling with to get results that are correct. I have stored them in a dictionary and get unexpected totals. Was thinking of using collection and still working on it. If the above could be improved. I would be grateful. Sean From alan.gauld at yahoo.co.uk Thu Jun 20 12:25:45 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 20 Jun 2019 17:25:45 +0100 Subject: [Tutor] word printing issue In-Reply-To: <042101d52755$16e5f3d0$44b1db70$@gmail.com> References: <042101d52755$16e5f3d0$44b1db70$@gmail.com> Message-ID: On 20/06/2019 11:44, mhysnm1964 at gmail.com wrote: > I have a list of strings that I want to break them into separate words, and > a combination of words then store them into a list. Example below of a > string: > "Hello Python team". > The data structure: > [ ['Hello'], > ['Hello', 'Python'], > ['Hello', 'Python', 'team'], > ]'Python'], > ]'Python', 'team'], > ['team'] ] > > > > I want to know if there is a better method in doing this without the > requirement of a module. Modules are there to be used... Here is one with itertools from the standard library that gets close: input = "hello Python team".split() result = [] for n in range(len(input): result += [item for item in it.combinations(input,n+1)] If you really want to do it from scratch then Google combinations algorithm, or look on wikipedia. -- 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 Thu Jun 20 12:51:43 2019 From: david at graniteweb.com (David Rock) Date: Thu, 20 Jun 2019 11:51:43 -0500 Subject: [Tutor] Hii In-Reply-To: References: Message-ID: <0C2A0D26-4128-4F84-80E7-DE1CAD8505A0@graniteweb.com> > On Jun 20, 2019, at 03:28, Alan Gauld via Tutor wrote: > > On 19/06/2019 23:30, Antonio Arizpe wrote: > >> i just need help with a script thats registers keystrikes and adds up all >> the times youve struck a key and gives a number of the total amount of >> times the keyboard was struck. nothing specific about characters. just how >> many times it was struck in a real number. > > It is possible, but it will likely be OS specific so you need to > tell us which OS you are using/targeting. > > Also any code that you've tried always helps along with any error > messages. Also tell us about any 3rd party libraries you are using. Also, what?s the use case? Do you want this to be something that is only capturing keystrokes within the program itself, or do you mean to capture all keystrokes happening regardless of application focus (i.e., system-level key logging)? ? David Rock david at graniteweb.com From alan.gauld at yahoo.co.uk Thu Jun 20 12:27:26 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 20 Jun 2019 17:27:26 +0100 Subject: [Tutor] Fwd: Re: Hii In-Reply-To: References: Message-ID: Forwarding to list. Please use Reply-All or Reply-List when responding to list emails. -------- Forwarded Message -------- Subject: Re: [Tutor] Hii Date: Thu, 20 Jun 2019 08:50:31 -0500 From: Antonio Arizpe To: Alan Gauld i am using python 3.7 On Thu, Jun 20, 2019 at 8:43 AM Antonio Arizpe > wrote: it is for windows 7 64 bits but i will be targeting windows 7 and 10 32 and 64 bits i currently use a script i was able to work together and its for automated screenshots i imagined for the key strike counter for it to be similar because i imagined it as defining keystrikes as x = 1 and for every new key strike would apply x+=1 and saving it in a text file in a directory. im sorry im a little new for third party libraries im using im really just using the default script that comes with python installation to make example here is the code i use for automated screenshots import sys import os from datetime import date import pyautogui import time import shutil today = str(date.today()) os.chdir ('C:\\Program Files\\Python37\\tll') os.mkdir (today) x=1 while x<1080: pyautogui.screenshot('/Program Files/Python37/tll/'+str(today)+'/image'+str(x)+'.png') x+=1 time.sleep(30) On Thu, Jun 20, 2019 at 3:30 AM Alan Gauld via Tutor > wrote: On 19/06/2019 23:30, Antonio Arizpe wrote: > i just need help with a script thats registers keystrikes and adds up all > the times youve struck a key and gives a number of the total amount of > times the keyboard was struck. nothing specific about characters. just how > many times it was struck in a real number. It is possible, but it will likely be OS specific so you need to tell us which OS you are using/targeting. Also any code that you've tried always helps along with any error messages. Also tell us about any 3rd party libraries you are using. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist?? -?? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From aarizpe448 at gmail.com Thu Jun 20 17:59:54 2019 From: aarizpe448 at gmail.com (Antonio Arizpe) Date: Thu, 20 Jun 2019 16:59:54 -0500 Subject: [Tutor] Fwd: Re: Hii In-Reply-To: References: Message-ID: On Thu, Jun 20, 2019 at 12:36 PM Alan Gauld via Tutor wrote: > Forwarding to list. > Please use Reply-All or Reply-List when responding to list emails. > > > > -------- Forwarded Message -------- > Subject: Re: [Tutor] Hii > Date: Thu, 20 Jun 2019 08:50:31 -0500 > From: Antonio Arizpe > To: Alan Gauld > > > > i am using python 3.7 > > On Thu, Jun 20, 2019 at 8:43 AM Antonio Arizpe > wrote: > > it is for windows 7 64 bits but i will be targeting windows 7 and 10 > 32 and 64 bits > > i currently use a script i was able to work together and its for > automated screenshots > i imagined for the key strike counter for it to be similar because i > imagined it as defining keystrikes as x = 1 and for every new key > strike would apply x+=1 and saving it in a text file in a directory. > im sorry im a little new for third party libraries im using im > really just using the default script that comes with python > installation > to make example here is the code i use for automated screenshots > > import sys > import os > from datetime import date > import pyautogui > import time > import shutil > > > today = str(date.today()) > > os.chdir ('C:\\Program Files\\Python37\\tll') > os.mkdir (today) > > > x=1 > while x<1080: > pyautogui.screenshot('/Program > Files/Python37/tll/'+str(today)+'/image'+str(x)+'.png') > x+=1 > time.sleep(30) > > On Thu, Jun 20, 2019 at 3:30 AM Alan Gauld via Tutor > > wrote: > > On 19/06/2019 23:30, Antonio Arizpe wrote: > > > i just need help with a script thats registers keystrikes and > adds up all > > the times youve struck a key and gives a number of the total > amount of > > times the keyboard was struck. nothing specific about > characters. just how > > many times it was struck in a real number. > > It is possible, but it will likely be OS specific so you need to > tell us which OS you are using/targeting. > > Also any code that you've tried always helps along with any error > messages. Also tell us about any 3rd party libraries you are using. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist?? -?? Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Thu Jun 20 22:10:30 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 21 Jun 2019 12:10:30 +1000 Subject: [Tutor] word printing issue In-Reply-To: <042101d52755$16e5f3d0$44b1db70$@gmail.com> References: <042101d52755$16e5f3d0$44b1db70$@gmail.com> Message-ID: <20190621021030.GF2417@ando.pearwood.info> Hi Sean, Your subject line says "word printing issue", but the body of your email says nothing about an issue printing words. Have you tried print(word)? Further comments and questions below. On Thu, Jun 20, 2019 at 08:44:06PM +1000, mhysnm1964 at gmail.com wrote: > I have a list of strings that I want to break them into separate words, and > a combination of words then store them into a list. Example below of a > string: > > "Hello Python team". > > The data structure: > > [ ['Hello'], > ['Hello', 'Python'], > ['Hello', 'Python', 'team'], > ['Python'], > ['Python', 'team'], > ['team'] ] I've taken the liberty of correcting some obviouis typos in the data structure. Sean wrote: > I want to know if there is a better method in doing this without the > requirement of a module. Better than what? How are you doing it now? Why don't you want to use a module? Sean wrote: > Eventually I want to count the number of hits in a > list of strings based upon the word combinations regardless where they are > located in the string. This last part is something I am struggling with to > get results that are correct. I'm struggling to understand what you mean. I don't understand what the word combinations part has to do with the problem. If all you want to do is count each word, you could try this: from collections import Counter text = "Hello Python team" c = Counter(text.split()) print(c) which will print something like: Counter({'team': 1, 'Python': 1, 'Hello': 1}) (the order of the counts may be different). > I have stored them in a dictionary and get > unexpected totals. Either your expectations are wrong, and the totals are correct, or your expectations are correct, and the code counting them is wrong. Without knowing either your expectations or the code counting the totals, I cannot guess which is the case. > Was thinking of using collection and still working on it. > If the above could be improved. I would be grateful. Without knowing what you are doing, it is hard to suggest improvements. "Dear cooking experts, I'm baking a cake and it turned out all wrong. What can I do to make it better? Thanks in advance." -- Steven From markos at c2o.pro.br Thu Jun 20 19:39:35 2019 From: markos at c2o.pro.br (Markos) Date: Thu, 20 Jun 2019 20:39:35 -0300 Subject: [Tutor] difference between array([1,0,1]) and array([[1,0,1]]) Message-ID: Hi, I'm studying Numpy and I don't understand the difference between >>> vector_1 = np.array( [ 1,0,1 ] ) with 1 bracket and >>> vector_2 = np.array( [ [ 1,0,1 ] ] ) with 2 brackets The shape of vector_1 is: >>> vector_1.shape (3,) But the shape of vector_2 is: >>> vector_2.shape (1, 3) The transpose on vector_1 don't work: >>> vector_1.T array([1, 0, 1]) But the transpose method in vector_2 works fine: >>> vector_2.T array([[1], ?????? [0], ?????? [1]]) I thought that both vectors would be treated as an matrix of 1 row and 3 columns. Why this difference? Any tip? Thank you, Markos From mhysnm1964 at gmail.com Thu Jun 20 20:00:10 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Fri, 21 Jun 2019 10:00:10 +1000 Subject: [Tutor] word printing issue In-Reply-To: References: <042101d52755$16e5f3d0$44b1db70$@gmail.com> Message-ID: <00bf01d527c4$4c79c660$e56d5320$@gmail.com> Thanks, so much to learn. -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Friday, 21 June 2019 2:26 AM To: tutor at python.org Subject: Re: [Tutor] word printing issue On 20/06/2019 11:44, mhysnm1964 at gmail.com wrote: > I have a list of strings that I want to break them into separate > words, and a combination of words then store them into a list. Example > below of a > string: > "Hello Python team". > The data structure: > [ ['Hello'], > ['Hello', 'Python'], > ['Hello', 'Python', 'team'], > ]'Python'], > ]'Python', 'team'], > ['team'] ] > > > > I want to know if there is a better method in doing this without the > requirement of a module. Modules are there to be used... Here is one with itertools from the standard library that gets close: input = "hello Python team".split() result = [] for n in range(len(input): result += [item for item in it.combinations(input,n+1)] If you really want to do it from scratch then Google combinations algorithm, or look on wikipedia. -- 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 mhysnm1964 at gmail.com Thu Jun 20 20:01:43 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Fri, 21 Jun 2019 10:01:43 +1000 Subject: [Tutor] collections and mappings Message-ID: <00c001d527c4$83b777d0$8b266770$@gmail.com> All, I have reviewed the collection module and do not understand mappings. I have seen this in other languages and have never got the concept. Can someone explain this at a very high level. From alan.gauld at yahoo.co.uk Fri Jun 21 04:11:03 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 21 Jun 2019 09:11:03 +0100 Subject: [Tutor] collections and mappings In-Reply-To: <00c001d527c4$83b777d0$8b266770$@gmail.com> References: <00c001d527c4$83b777d0$8b266770$@gmail.com> Message-ID: On 21/06/2019 01:01, mhysnm1964 at gmail.com wrote: > I have reviewed the collection module and do not understand mappings. I have > seen this in other languages and have never got the concept. Can someone > explain this at a very high level. OK. You are a master of the open ended question so I'm not sure what aspect you don't understand. But I'll start at the beginning and take it as far as Python. But you are opening a vary large can of worms... Mappings, in programming terms, are related to a mathematical concept. See Wikipedia for a more detailed account of math mapping. In simplistic terms a mapping comprises two sets of data, one an input the other an output. The map is the set of relationships between input and output. Thus given input of {a, b, c} and output of {1,2,3,4,5} We might have any of several mappings between these. A simple 1:1 mapping might be {a,1}, {b,2}, {c,3} But we can have 1:N or N:1 or N:M mappings too: {a,1,2} {b,3} {c,4} - a 1:N mapping {a,1} {b,1}, {c,3} - an N:1 mapping {a,1,2} {b,2},{c,1,3} - a N:M mapping Note that the mapping does not have to include all of the output elements. The mapping may be arbitrary, as above or it may be defined as a function: {a, f(a)} {b,f(b)} {c,f(c)} Or even as a set of functions... In programming, and particularly in Python, this tends to be represented as a dictionary (or sometimes a class). So the mappings above could be shown as: input = ['a','b','c'] output = [1,2,3] 1_1 = {'a':1,'b':2,'c':3} 1_N = {'a':(1,2)'b':(1,),'c':(3,)} N_1 = {'a':1,'b':1,'c':3} N_M = {'a':(1,2),'b':(2,),'c:(1,3)} def f(x): return x*2 1_f = {'a':f('a'),'b':f('b'),'c':f('c')} List comprehensions and generator expressions are also commonly used to create mappings, especially the functional sort. I have no idea if the addressed any of your questions but if not please ask again, but with something more specific. PS. To the real mathematicians on the list. My math is very rusty, if I've made any major gaffes please feel free to correct/amend my scribblings. -- 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 Fri Jun 21 09:36:19 2019 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 21 Jun 2019 07:36:19 -0600 Subject: [Tutor] difference between array([1,0,1]) and array([[1,0,1]]) In-Reply-To: References: Message-ID: On 6/20/19 5:39 PM, Markos wrote: > Hi, > > I'm studying Numpy and I don't understand the difference between > >>>> vector_1 = np.array( [ 1,0,1 ] ) > > with 1 bracket and > >>>> vector_2 = np.array( [ [ 1,0,1 ] ] ) > > with 2 brackets the first is one-dimensional, the second two-dimensional. If we expand how we write the second a bit does it make it more clear? np.array([ [1, 0, 1], # no other elements ]) the double brackets look magical, but as soon as you have more than one row it makes sense. > > The shape of vector_1 is: > >>>> vector_1.shape > (3,) > > But the shape of vector_2 is: > >>>> vector_2.shape > (1, 3) > > The transpose on vector_1 don't work: > >>>> vector_1.T > array([1, 0, 1]) > > But the transpose method in vector_2 works fine: > >>>> vector_2.T > array([[1], > ?????? [0], > ?????? [1]]) > > > I thought that both vectors would be treated as an matrix of 1 row and 3 > columns. > > Why this difference? > > Any tip? > > Thank you, > Markos > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From aarizpe448 at gmail.com Fri Jun 21 09:59:11 2019 From: aarizpe448 at gmail.com (Antonio Arizpe) Date: Fri, 21 Jun 2019 08:59:11 -0500 Subject: [Tutor] Fwd: Re: Hii In-Reply-To: References: Message-ID: Hey ive looked everywhere and i would really appreciate the guidance i know its not too complicated by google search results dodge my real question i just need help with a script thats registers keystrikes and adds up all the times youve struck a key and gives a number of the total amount of times the keyboard was struck. nothing specific about characters. just how many times it was struck in a real number. it is for windows 7 64 bits but i will be targeting windows 7 and 10 32 and 64 bits it does not mean to be application specific. just a +1 rule every time the keyboard is pressed so i can run it get a real number at the end of the day im just resending this because i recieved a updated mail to please select respond all when responding to these emails which i had not been On Thu, Jun 20, 2019 at 4:59 PM Antonio Arizpe wrote: > > > On Thu, Jun 20, 2019 at 12:36 PM Alan Gauld via Tutor > wrote: > >> Forwarding to list. >> Please use Reply-All or Reply-List when responding to list emails. >> >> >> >> -------- Forwarded Message -------- >> Subject: Re: [Tutor] Hii >> Date: Thu, 20 Jun 2019 08:50:31 -0500 >> From: Antonio Arizpe >> To: Alan Gauld >> >> >> >> i am using python 3.7 >> >> On Thu, Jun 20, 2019 at 8:43 AM Antonio Arizpe > > wrote: >> >> it is for windows 7 64 bits but i will be targeting windows 7 and 10 >> 32 and 64 bits >> >> i currently use a script i was able to work together and its for >> automated screenshots >> i imagined for the key strike counter for it to be similar because i >> imagined it as defining keystrikes as x = 1 and for every new key >> strike would apply x+=1 and saving it in a text file in a directory. >> im sorry im a little new for third party libraries im using im >> really just using the default script that comes with python >> installation >> to make example here is the code i use for automated screenshots >> >> import sys >> import os >> from datetime import date >> import pyautogui >> import time >> import shutil >> >> >> today = str(date.today()) >> >> os.chdir ('C:\\Program Files\\Python37\\tll') >> os.mkdir (today) >> >> >> x=1 >> while x<1080: >> pyautogui.screenshot('/Program >> Files/Python37/tll/'+str(today)+'/image'+str(x)+'.png') >> x+=1 >> time.sleep(30) >> >> On Thu, Jun 20, 2019 at 3:30 AM Alan Gauld via Tutor >> > wrote: >> >> On 19/06/2019 23:30, Antonio Arizpe wrote: >> >> > i just need help with a script thats registers keystrikes and >> adds up all >> > the times youve struck a key and gives a number of the total >> amount of >> > times the keyboard was struck. nothing specific about >> characters. just how >> > many times it was struck in a real number. >> >> It is possible, but it will likely be OS specific so you need to >> tell us which OS you are using/targeting. >> >> Also any code that you've tried always helps along with any error >> messages. Also tell us about any 3rd party libraries you are >> using. >> >> >> -- >> Alan G >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> http://www.amazon.com/author/alan_gauld >> Follow my photo-blog on Flickr at: >> http://www.flickr.com/photos/alangauldphotos >> >> >> _______________________________________________ >> Tutor maillist?? -?? Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > From mhysnm1964 at gmail.com Fri Jun 21 06:39:46 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Fri, 21 Jun 2019 20:39:46 +1000 Subject: [Tutor] collections and mappings In-Reply-To: References: <00c001d527c4$83b777d0$8b266770$@gmail.com> Message-ID: <009a01d5281d$a71547d0$f53fd770$@gmail.com> Allan, I think I understand, but that type of maths I have not touched in 40 years. Thus why I am not getting the concept. ? It was an open question as I had no clue what it was and I should have asked. Lets park the question for now and I will read the references. As I am not sure if this will help my program that I am struggling with. -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Friday, 21 June 2019 6:11 PM To: tutor at python.org Subject: Re: [Tutor] collections and mappings On 21/06/2019 01:01, mhysnm1964 at gmail.com wrote: > I have reviewed the collection module and do not understand mappings. > I have seen this in other languages and have never got the concept. > Can someone explain this at a very high level. OK. You are a master of the open ended question so I'm not sure what aspect you don't understand. But I'll start at the beginning and take it as far as Python. But you are opening a vary large can of worms... Mappings, in programming terms, are related to a mathematical concept. See Wikipedia for a more detailed account of math mapping. In simplistic terms a mapping comprises two sets of data, one an input the other an output. The map is the set of relationships between input and output. Thus given input of {a, b, c} and output of {1,2,3,4,5} We might have any of several mappings between these. A simple 1:1 mapping might be {a,1}, {b,2}, {c,3} But we can have 1:N or N:1 or N:M mappings too: {a,1,2} {b,3} {c,4} - a 1:N mapping {a,1} {b,1}, {c,3} - an N:1 mapping {a,1,2} {b,2},{c,1,3} - a N:M mapping Note that the mapping does not have to include all of the output elements. The mapping may be arbitrary, as above or it may be defined as a function: {a, f(a)} {b,f(b)} {c,f(c)} Or even as a set of functions... In programming, and particularly in Python, this tends to be represented as a dictionary (or sometimes a class). So the mappings above could be shown as: input = ['a','b','c'] output = [1,2,3] 1_1 = {'a':1,'b':2,'c':3} 1_N = {'a':(1,2)'b':(1,),'c':(3,)} N_1 = {'a':1,'b':1,'c':3} N_M = {'a':(1,2),'b':(2,),'c:(1,3)} def f(x): return x*2 1_f = {'a':f('a'),'b':f('b'),'c':f('c')} List comprehensions and generator expressions are also commonly used to create mappings, especially the functional sort. I have no idea if the addressed any of your questions but if not please ask again, but with something more specific. PS. To the real mathematicians on the list. My math is very rusty, if I've made any major gaffes please feel free to correct/amend my scribblings. -- 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 alan.gauld at yahoo.co.uk Fri Jun 21 18:06:47 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 21 Jun 2019 23:06:47 +0100 Subject: [Tutor] Fwd: Re: Hii In-Reply-To: References: Message-ID: On 21/06/2019 14:59, Antonio Arizpe wrote: > i just need help with a script thats registers keystrikes and adds up all > the times you've struck a key and gives a number of the total amount of > times the keyboard was struck. nothing specific about characters. just how > many times it was struck in a real number. If you only wanted to do this for your own application it would be relatively simple but since it seems you want to do it for the computer as a whole that raises a whole extra level of complexity. Some issues to consider: 1) If this is not just installed on your own personal computer it could be illegal - breach of personal privacy legislation in many countries prohibits key logging. 2) Do you care about users logging in remotely? Do you need to just log the current user logged in for the current GUI session or do you also want to record activity by other remote hosts logging in? 3) What about virtual machines running on the computer? Do you want to capture keystrokes within those VMs? That might be tricky as it may not show up in the native OS. It may even depend on the VM the user is running. 4) Do you care about which user is logged in or do you want to record keystrokes for every user of the computer? 5) Is it only counting for a single session or for multiple sessions? This is more of an application design question that keylogging per se... Leaving those issues aside and looking only at the keylogging aspects. Your best bet is to find a third party module that does it for you. Failing that you will need to use the OS facilities which is never a trivial exercise. It is also the kind of low level feature that can change between OS versions (especially between 32bit and 64bit versions) If you plan to use it on multiple OS then the technique will likely differ between OS - MacOS and Linux may be similar but windows will be different. Let's assume the simplest case where you only want this for personal use on a computer where you are the only user and don't run any other OS either dual boot or in a VM. in that case you could write a Python application that does keylogging and put it in your startup group. How you notify it to stop recording before the computer shuts down is another issue and how it records/displays its results needs thought too. On Windows you might need to use ctypes to access the raw Win32 API or the PyWwin32 Python package may include functions that will do the job for you. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Fri Jun 21 18:09:33 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 21 Jun 2019 23:09:33 +0100 Subject: [Tutor] collections and mappings In-Reply-To: <009a01d5281d$a71547d0$f53fd770$@gmail.com> References: <00c001d527c4$83b777d0$8b266770$@gmail.com> <009a01d5281d$a71547d0$f53fd770$@gmail.com> Message-ID: On 21/06/2019 11:39, mhysnm1964 at gmail.com wrote: > I think I understand, but that type of maths I have not touched in 40 years. The real point is that in Python terms a mapping is nearly always just another name for a dictionary. Either a set of key/value pairs or a set of key/function pairs. Very occasionally it will be a class rather than a dictionary. -- 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 aarizpe448 at gmail.com Fri Jun 21 18:18:02 2019 From: aarizpe448 at gmail.com (Antonio Arizpe) Date: Fri, 21 Jun 2019 17:18:02 -0500 Subject: [Tutor] Fwd: Re: Hii In-Reply-To: References: Message-ID: if it cannot escape the simplicity of logging the keyboard as a individual mechanical entity i can understand that better now because you are right it all comes down to the OS or the driver handling to even be able to begin the register of a device. but i am more than willing to be as basic implementation as i need to be i only need this to function on windows 7 and 10, 32 and 64 bits i dont need any specificity to the counter other than the numeric sum of clicks pressed on the local machine. as far as this idea goes its very fair to make it as simple as possible. no VMs, single user, no key logging other than just a number at the end of the day. i would just really appreciate it if before shutting down i can open a text file and it says 2552 On Fri, Jun 21, 2019 at 5:09 PM Alan Gauld via Tutor wrote: > On 21/06/2019 14:59, Antonio Arizpe wrote: > > > i just need help with a script thats registers keystrikes and adds up all > > the times you've struck a key and gives a number of the total amount of > > times the keyboard was struck. nothing specific about characters. just > how > > many times it was struck in a real number. > > If you only wanted to do this for your own application it would be > relatively simple but since it seems you want to do it for the > computer as a whole that raises a whole extra level of complexity. > > Some issues to consider: > > 1) If this is not just installed on your own personal computer it could > be illegal - breach of personal privacy legislation in many countries > prohibits key logging. > > 2) Do you care about users logging in remotely? Do you need to just log > the current user logged in for the current GUI session or do you also > want to record activity by other remote hosts logging in? > > 3) What about virtual machines running on the computer? Do you want to > capture keystrokes within those VMs? That might be tricky as it may not > show up in the native OS. It may even depend on the VM the user is running. > > 4) Do you care about which user is logged in or do you want to record > keystrokes for every user of the computer? > > 5) Is it only counting for a single session or for multiple sessions? > This is more of an application design question that keylogging per se... > > Leaving those issues aside and looking only at the keylogging aspects. > Your best bet is to find a third party module that does it for you. > Failing that you will need to use the OS facilities which is never a > trivial exercise. It is also the kind of low level feature that can > change between OS versions (especially between 32bit and 64bit versions) > > If you plan to use it on multiple OS then the technique will likely > differ between OS - MacOS and Linux may be similar but windows will > be different. > > Let's assume the simplest case where you only want this for personal use > on a computer where you are the only user and don't run any other OS > either dual boot or in a VM. in that case you could write a Python > application that does keylogging and put it in your startup group. > How you notify it to stop recording before the computer shuts down is > another issue and how it records/displays its results needs thought too. > > On Windows you might need to use ctypes to access the raw Win32 API or > the PyWwin32 Python package may include functions that will do the job > for you. > > 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 > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Fri Jun 21 20:41:14 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 22 Jun 2019 10:41:14 +1000 Subject: [Tutor] Fwd: Re: Hii In-Reply-To: References: Message-ID: <20190622004113.GG2417@ando.pearwood.info> On Fri, Jun 21, 2019 at 05:18:02PM -0500, Antonio Arizpe wrote: > i only need this to function on windows 7 and 10, 32 and 64 bits > i dont need any specificity to the counter other than the numeric sum of > clicks pressed on the local machine. > as far as this idea goes its very fair to make it as simple as possible. > no VMs, single user, no key logging other than just a number at the end of > the day. https://www.geeksforgeeks.org/design-a-keylogger-in-python/ which was the first result here: https://duckduckgo.com/?q=python+key+logger Here's another one: http://dalelane.co.uk/blog/?p=1760 which I found here: https://www.google.com/search?q=python+count+keystrokes -- Steven From steve at pearwood.info Fri Jun 21 21:31:57 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 22 Jun 2019 11:31:57 +1000 Subject: [Tutor] difference between array([1,0,1]) and array([[1,0,1]]) In-Reply-To: References: Message-ID: <20190622013157.GH2417@ando.pearwood.info> On Thu, Jun 20, 2019 at 08:39:35PM -0300, Markos wrote: > Hi, > > I'm studying Numpy and I don't understand the difference between > > >>>vector_1 = np.array( [ 1,0,1 ] ) > > with 1 bracket and > > >>>vector_2 = np.array( [ [ 1,0,1 ] ] ) > > with 2 brackets I'm not really sure what you don't get here. The first is a one dimensional vector with three items, the second is a two dimensional array with one row and three columns. But you know that, because you checked the shape of each one: > >>>vector_1.shape > (3,) > >>>vector_2.shape > (1, 3) If you want a 2D shape, you need to provide a 2D argument. If you want a 3D shape, you need a 3D argument. And so forth. > I thought that both vectors would be treated as an matrix of 1 row and 3 > columns. Why 1x3 rather than 3x1? > Why this difference? Because that's how numpy arrays are designed to work. I'm not sure what sort of answer you expect. -- Steven From steve at pearwood.info Fri Jun 21 21:57:07 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 22 Jun 2019 11:57:07 +1000 Subject: [Tutor] collections and mappings In-Reply-To: <00c001d527c4$83b777d0$8b266770$@gmail.com> References: <00c001d527c4$83b777d0$8b266770$@gmail.com> Message-ID: <20190622015707.GI2417@ando.pearwood.info> On Fri, Jun 21, 2019 at 10:01:43AM +1000, mhysnm1964 at gmail.com wrote: > I have reviewed the collection module and do not understand mappings. I have > seen this in other languages and have never got the concept. Can someone > explain this at a very high level. It might help if you remember that dicts are a kind of mapping. Another name for dicts are "associative arrays". In the most general terms, a mapping is an association between one or more pieces of information and another one or more pieces of information. That is so general as to be completely abstract, and I think it will be more useful to give some concrete examples. If you are American, you probably have a social security number. There is a mapping between your social security number and you, the person: the government associates data about you with the social security number. If you have used a regular paper dictionary, it is a mapping between words and definitions. We say the word maps to the definition. In Python terms, we would use a dict, using the word as the key and the definition as the value. For example: {"Childhood": """"The period of human life intermediate between the idiocy of infancy and the folly of youth?two removes from the sin of manhood and three from the remorse of age.""", "Debt": """An ingenious substitute for the chain and whip of the slave-driver.""", "Piano": """A parlor utensil for subduing the impenitent visitor. It is operated by depressing the keys of the machine and the spirits of the audience.""", "Quotation": """The act of repeating erroneously the words of another. The words erroneously repeated.""" } Does this help? -- Steven From markos at c2o.pro.br Sat Jun 22 11:20:54 2019 From: markos at c2o.pro.br (Markos) Date: Sat, 22 Jun 2019 12:20:54 -0300 Subject: [Tutor] Difference between array( [1, 0, 1] ) and array( [ [1, 0, 1] ] ) In-Reply-To: <1c06a6d8-49f7-485c-951c-7ed7ee72602a@googlegroups.com> References: <165aadea-f9a8-bc41-e79a-b98ec4241bcf@c2o.pro.br> <1c06a6d8-49f7-485c-951c-7ed7ee72602a@googlegroups.com> Message-ID: <95816b1e-5d7d-d594-f6cd-726fcaae74fb@c2o.pro.br> Thanks Edmondo, Stephen, Mats and Steven you for the tips, I studied linear algebra many years ago and I remember only a few rudiments. But I was trying to visualize (in a geometric way) how the numpy represents arrays, and what the geometrical meaning of the transpose operation made by numpy. I think I understood a little bit more. The number of nested brackets indicates the number of array dimensions. the vector ( [1,2] ) is one-dimensional, but the vector ( [ [1,2] ] ) is two-dimensional. v_1 = np.array( [1,2] ) > v_1.shape (2,) > v_1 v_1 > v_1 array( [1, 2] ) > v_2 = np.array( [ [1,2] ] ) > v_2.shape (1, 2) And it does not make sense to transpose a one-dimensional array. > v_1.T array( [1, 2] ) > v_2.T array( [ [1], ???????????? [2] ] ) Anothe example: vector_1 = np.array( [?? 1,?? 2,?? 3,?? 4,?? 5,?? 6,?? 7,?? 8? ] ) ????????????????????????????????? ^ vector_2 = np.array( [??? [1, 2, 3, 4],??? [5, 6, 7, 8]? ]? ) ????????????????????????????????? ^? ^ vector_3 = np.array( [?? [?? [1,2],? [3,4]? ], [? [5,6],?? [7,8] ]? ]? ) ????????????????????????????????? ^ ^ ^ > vector_1 array([1, 2, 3, 4, 5, 6, 7, 8]) > vector_2 array( [ [1, 2, 3, 4], ???????????? [5, 6, 7, 8] ] ) > vector_3 array( [ [ [1, 2], ?????????????? [3, 4] ], ???????????? [ [5, 6], ?????????????? [7, 8] ] ] ) And looking for some tutorial about geometric aspects of matrices and the geometric meaning of the transpose I found that transposed is "mirrored along the diagonal" at: https://www.coranac.com/documents/geomatrix/ >vector_1.T array([1, 2, 3, 4, 5, 6, 7, 8]) > vector_2.T array( [ [1, 5], ???????????? [2, 6], ???????????? [3, 7], ???????????? [4, 8] ] ) > vector_3.T array( [ [ [1, 5], ?????????????? [3, 7]], ???????????? [ [2, 6], ?????????????? [4, 8] ] ] ) Thank you, Markos Em 21-06-2019 07:44, edmondo.giovannozzi at gmail.com escreveu: > Every array in numpy has a number of dimensions, > "np.array" is a function that can create an array numpy given a list. > > when you write > vector_1 = np.array([1,2,1]) > you are passing a list of number to thet function array that will create a 1D array. > As you are showing: > vector_1.shape > will return a tuple with the sizes of each dimension of the array that is: > (3,) > Note the comma thta indicate that is a tuple. > While if you write: > vector_2 = np.array([[1,2,3]]) > You are passing a list of list to the function array that will instruct it to crete a 2D array, even though the size of the first dimension is 1: > vector_2.shape > (1,3) > It is still a tuple as you can see. > Try: > vector_3 = np.array([[1,2,3],[4,5,6]]) > And you'll see that i'll return a 2D array with a shape: > vector_3.shape > (2,3) > As the external list has 2 elements that is two sublists each with 3 elements. > The vector_2 case is just when the external list has only 1 element. > > I hope it is more clear now. > Cherrs, > > > > > > > Il giorno venerd? 21 giugno 2019 08:29:36 UTC+2, Markos ha scritto: >> Hi, >> >> I'm studying Numpy and I don't understand the difference between >> >>>>> vector_1 = np.array( [ 1,0,1 ] ) >> with 1 bracket and >> >>>>> vector_2 = np.array( [ [ 1,0,1 ] ] ) >> with 2 brackets >> >> The shape of vector_1 is: >> >>>>> vector_1.shape >> (3,) >> >> But the shape of vector_2 is: >> >>>>> vector_2.shape >> (1, 3) >> >> The transpose on vector_1 don't work: >> >>>>> vector_1.T >> array([1, 0, 1]) >> >> But the transpose method in vector_2 works fine: >> >>>>> vector_2.T >> array([[1], >> ?????? [0], >> ?????? [1]]) >> >> >> I thought that both vectors would be treated as an matrix of 1 row and 3 >> columns. >> >> Why this difference? >> >> Any tip? >> >> Thank you, >> Markos From johnf at jfcomputer.com Mon Jun 24 12:15:36 2019 From: johnf at jfcomputer.com (johnf) Date: Mon, 24 Jun 2019 09:15:36 -0700 Subject: [Tutor] replacing a loop Message-ID: <3f3beccb-8d9f-81b9-7726-a8fee305a348@jfcomputer.com> Hi folks, I have the following loop (actually repeated many times ) def locChoices(self): ??????? locDS = self.eslocation.getDataSet() ??????? loc_Choices=[''] ??????? locKeys=[0] ??????? for row in locDS: ??????????? loc_Choices.append(row['facility']) ??????????? locKeys.append(row['pkid']) return loc_Choices,locKeys where locDS is a tuple of dicts and a row is a dict. Since I use a lot of similar loops to populate many dropdown controls I started investigating the use of list comprehensions.? But I can't figure out how to use them in this loop and wonder if it will improve the performance.? The data is not very big - about a thousand rows - give or take. So what do you guys think? Johnf From __peter__ at web.de Mon Jun 24 12:48:28 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 24 Jun 2019 18:48:28 +0200 Subject: [Tutor] replacing a loop References: <3f3beccb-8d9f-81b9-7726-a8fee305a348@jfcomputer.com> Message-ID: johnf wrote: > Hi folks, > > > I have the following loop (actually repeated many times ) If you have repetetive code look into ways to parameterize it, like def choices(rows, choices_column, keys_column): ... > > def locChoices(self): > locDS = self.eslocation.getDataSet() > loc_Choices=[''] > locKeys=[0] > for row in locDS: > loc_Choices.append(row['facility']) > locKeys.append(row['pkid']) > > return loc_Choices,locKeys > > where locDS is a tuple of dicts and a row is a dict. > > Since I use a lot of similar loops to populate many dropdown controls I > started investigating the use of list comprehensions. But I can't > figure out how to use them in this loop You need two loops in this case choices = [""] + [row["facility"] for row in ds] keys = [0] + [row["pkid"] for row in ds] > and wonder if it will improve > the performance. No, list comprehensions are unlikely to improve performance. > The data is not very big - about a thousand rows - > give or take. > > So what do you guys think? Are you sure (aka: did you measure that) building these lists takes a significant amount of time? If there is a GUI involved any delay you notice is more likely to stem from filling the widgets than from preparing the lists. From mats at wichmann.us Mon Jun 24 13:48:39 2019 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 24 Jun 2019 11:48:39 -0600 Subject: [Tutor] replacing a loop In-Reply-To: <3f3beccb-8d9f-81b9-7726-a8fee305a348@jfcomputer.com> References: <3f3beccb-8d9f-81b9-7726-a8fee305a348@jfcomputer.com> Message-ID: <1222fc93-73b8-4cb5-a191-4420c74f98a3@wichmann.us> On 6/24/19 10:15 AM, johnf wrote: . > > Since I use a lot of similar loops to populate many dropdown controls I > started investigating the use of list comprehensions.? But I can't > figure out how to use them in this loop and wonder if it will improve > the performance. To amplify a tiny bit on what Peter said: comprehensions are a more concise way to express a loop-to-build-a-collection, but I don't believe were intended specifically as a performance improvement. They may be a little faster on the principle of "avoiding dots" - that is, the lookup every time through your loop from listname.append does take a little bit of time. And probably more significantly, append is a function that has to be called, and the setup stack frame/call/return sequence also takes some time. So depending on circumstances you may see insignificant or significant performance differences. But, again - Rule number one: only optimize when there is a proven speed bottleneck From ar at zeit.io Mon Jun 24 14:48:02 2019 From: ar at zeit.io (Arup Rakshit) Date: Tue, 25 Jun 2019 00:18:02 +0530 Subject: [Tutor] Python type annotation question Message-ID: <6018097F-28EC-4859-B6EE-0738DA738B8B@zeit.io> I am little experimenting with Python type annotation today. I wrote a simple class as below: >>> from datetime import date >>> class Person: ... dob: date ... def __init__(self, dob): ... self.dob = dob ... >>> Person(11) <__main__.Person object at 0x10e078128> >>> p = Person(11) >>> p.dob 11 Although I marked dob as date type, why am I able to assign it an int? So my question how does type annotation helps, when should we use them and what advantages it brings? Thanks, Arup Rakshit ar at zeit.io From mats at wichmann.us Mon Jun 24 15:24:43 2019 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 24 Jun 2019 13:24:43 -0600 Subject: [Tutor] Python type annotation question In-Reply-To: <6018097F-28EC-4859-B6EE-0738DA738B8B@zeit.io> References: <6018097F-28EC-4859-B6EE-0738DA738B8B@zeit.io> Message-ID: <8bf0e609-ae85-b373-601a-95f727067007@wichmann.us> On 6/24/19 12:48 PM, Arup Rakshit wrote: > I am little experimenting with Python type annotation today. I wrote a simple class as below: > >>>> from datetime import date >>>> class Person: > ... dob: date > ... def __init__(self, dob): > ... self.dob = dob > ... >>>> Person(11) > <__main__.Person object at 0x10e078128> >>>> p = Person(11) >>>> p.dob > 11 > > Although I marked dob as date type, why am I able to assign it an int? So my question how does type annotation helps, when should we use them and what advantages it brings? it's a mechansim to support static checking in various forms - tools, IDES, etc. It also serves to document your intent without having to write as much in the docstring. Python remains a "consenting adults" language, that is if you really want to do something, you can, so the type hints are not enforced at runtime. Since it's not always the case that doing something possible is a good idea, the type hints are there to make it easier to detect issues. Your annotation looks funky, by the way, I'd expect to see it written as: > ... def __init__(self, dob: date): From johnf at jfcomputer.com Mon Jun 24 16:50:36 2019 From: johnf at jfcomputer.com (johnf) Date: Mon, 24 Jun 2019 13:50:36 -0700 Subject: [Tutor] replacing a loop In-Reply-To: <1222fc93-73b8-4cb5-a191-4420c74f98a3@wichmann.us> References: <3f3beccb-8d9f-81b9-7726-a8fee305a348@jfcomputer.com> <1222fc93-73b8-4cb5-a191-4420c74f98a3@wichmann.us> Message-ID: Actually I do not see a reply from Peter??????? I don't have a clue what was said. I realize that performance is not a big issue in this case - of course an increase in speed is always welcome.? I was more interested in a better understanding of the list comprehensions.? Since I have so many of these loops I thought it might be best if I tried using comprehensions. Johnf On 6/24/19 10:48 AM, Mats Wichmann wrote: > On 6/24/19 10:15 AM, johnf wrote: > . >> Since I use a lot of similar loops to populate many dropdown controls I >> started investigating the use of list comprehensions.? But I can't >> figure out how to use them in this loop and wonder if it will improve >> the performance. > To amplify a tiny bit on what Peter said: comprehensions are a more > concise way to express a loop-to-build-a-collection, but I don't believe > were intended specifically as a performance improvement. > > They may be a little faster on the principle of "avoiding dots" - that > is, the lookup every time through your loop from listname.append does > take a little bit of time. And probably more significantly, append is a > function that has to be called, and the setup stack frame/call/return > sequence also takes some time. So depending on circumstances you may see > insignificant or significant performance differences. > > But, again - > > Rule number one: only optimize when there is a proven speed bottleneck > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From PyTutor at DancesWithMice.info Mon Jun 24 17:03:44 2019 From: PyTutor at DancesWithMice.info (David L Neil) Date: Tue, 25 Jun 2019 09:03:44 +1200 Subject: [Tutor] replacing a loop In-Reply-To: <3f3beccb-8d9f-81b9-7726-a8fee305a348@jfcomputer.com> References: <3f3beccb-8d9f-81b9-7726-a8fee305a348@jfcomputer.com> Message-ID: <38875b74-2ba5-105a-20a5-48414f607121@DancesWithMice.info> Hi John, On 25/06/19 4:15 AM, johnf wrote: > Hi folks, > I have the following loop (actually repeated many times ) > def locChoices(self): > ??????? locDS = self.eslocation.getDataSet() > ??????? loc_Choices=[''] > ??????? locKeys=[0] > ??????? for row in locDS: > ??????????? loc_Choices.append(row['facility']) > ??????????? locKeys.append(row['pkid']) > return loc_Choices,locKeys > where locDS is a tuple of dicts and a row is a dict. > Since I use a lot of similar loops to populate many dropdown controls I > started investigating the use of list comprehensions.? But I can't > figure out how to use them in this loop and wonder if it will improve > the performance.? The data is not very big - about a thousand rows - > give or take. > So what do you guys think? Just because this morning I needed something to kick-start my sleepy brain into being able to "think"... When most people think of 'multiples' in the context of list-comprehensions, they are talking about "nested-loops" - which are easy-enough (or at least, well-documented). However, I've often wondered about the multiple being on the 'left-hand side' of the equation/expression, and whether that is even possible? It is! Thus: python3 Python 3.6.8 (default, Mar 21 2019, 10:08:12) [GCC 8.3.1 20190223 (Red Hat 8.3.1-2)] on linux Type "help", "copyright", "credits" or "license" for more information. # build the data-structure (I hope) >>> d1 = { 'pkid':1, 'facility':'here' } >>> d2 = { 'pkid':2, 'facility':'there' } >>> d3 = { 'pkid':3, 'facility':'nowhere' } >>> locDS = ( d1, d2, d3 ) # let's check that the data-format is as-described? >>> type( locDS ) >>> for row in locDS: print( row ) ... {'pkid': 1, 'facility': 'here'} {'pkid': 2, 'facility': 'there'} {'pkid': 3, 'facility': 'nowhere'} # are we on the right track? # this is the original code and approach >>> loc_Choices = [ '' ] >>> locKeys = [ 0 ] >>> for row in locDS: ... loc_Choices.append( row[ 'facility' ] ) ... locKeys.append( row[ 'pkid' ] ) ... # which (I hope) shows the existing (and desired) results >>> print( loc_Choices ) ['', 'here', 'there', 'nowhere'] >>> print( locKeys ) [0, 1, 2, 3] # we can do the list inits, cf leaving it to the list comprehension >>> loc_Choices = [ '' ] >>> locKeys = [ 0 ] # but how might we achieve this using a (single) list comprehension? >>> [ [ loc_Choices.append( row[ 'facility' ] ), locKeys.append( row[ 'pkid' ] ) ] for row in locDS ] # in (proper) code, I'd format this over two lines (at least) # correct me if I'm wrong, but these o/ps will # 'disappear into the ether' when run within a pgm... [[None, None], [None, None], [None, None]] # leaving us with 'the proof of the pudding' >>> print( loc_Choices ) ['', 'here', 'there', 'nowhere'] >>> print( locKeys ) [0, 1, 2, 3] >>> Possible considerations: 1 some sample data would have eased the way/its lack leaves room for misinterpretation 2 a list comprehension normally executes more quickly than the traditional (long-form) multi-line code-block. However, it would be worth measuring that on your particular machine h/w and Py__version__. The other 'efficiency' is readability, but "elegance" is largely a matter of (personal) taste. So that ('mine-field') I leave to you... 3 taking the multi-line code-block and splitting it into TWO separate (consecutive) list comprehensions (one for loc_Choices and the other for locKeys) will most-likely be noticeably MORE 'expensive' 4 it is no matter if row/the locDS tuple dict-elements contain more key-value pairs 5 during code review, my colleagues would delight in criticising the choice of names such as locKeys - describing them as "JavaScript" (a 'dirty word' to some), loc_Choices as contrary to PEP-8 ("flake", or whatever), etc - despite that I have a little 'set' of abbreviations with which I do the same, eg locNR. YMMV! 6 however, this simple boy will venture criticism of the inconsistency in using the underline word-separator, eg locKeys but loc_Choices. Even more potential for confusion: locChoices and loc_Choices!? 7 consider the data structures (outside of our view, here). Assuming there are more elements in each 'loc dict', might it be just as easy to leave the two pertinent elements 'there' and iterate over locDS when actually applied. Alternately, perhaps it might be better to construct a dictionary with the 'pkid's as keys and the 'facility' as values for direct-access application? Not knowing how the structure(s) will be utilised makes this pure speculation! 8 any?every time "similar loops" are found, re-factor to a function (let's see if that philosophy kicks-off a retort or two...) 9 was it Knuth who described "premature optimisation" as "evil"? Certainly I'm a believer in 'make it work before you make it better'. So, (after all that!) what are we really achieving here? With such quantities, is it critical to 'save time'? If so, is this part of the code really the greatest time-sink? (and we're back to the 'readability' consideration, previously mentioned) Thanks, for the provocation into answering this long-held question - and for the fact that my brain isn't sleepy any longer. (mind you, such exertion suggests it must be time for a tea-break)... -- Regards =dn From johnf at jfcomputer.com Mon Jun 24 18:24:51 2019 From: johnf at jfcomputer.com (johnf) Date: Mon, 24 Jun 2019 15:24:51 -0700 Subject: [Tutor] replacing a loop In-Reply-To: <38875b74-2ba5-105a-20a5-48414f607121@DancesWithMice.info> References: <3f3beccb-8d9f-81b9-7726-a8fee305a348@jfcomputer.com> <38875b74-2ba5-105a-20a5-48414f607121@DancesWithMice.info> Message-ID: Thank you - it worked!? I'm glad you are no longer sleepy! Actually I wanted the naming to remain consistent with the other loops So the name of the function/method (it is in a class) caused the use of the underscore locChoices == location choices def locChoices(self) cause me to use loc_Choices in the code. I am torn about the readability of the code - at least in my mind I can read the loop a lot easier than I can read the comprehension. Of course I considered the use of a function where I passed the required parameters.? But the issue is the access to the data - that getDataSet() was a performance hit when I used a passed parameter.? I could have opened/accessed/retrieve the data during the opening but thought it best to use a lazy data access as needed. Today was not really about performance but more about learning to use the comprehension.? You see I did attempt to do create the comprehension.? But after seeing your code I realized that I had the '[' and the '{' confused.? I believed I had to use '{'.? I just reviewed a tutorial off the net and that was what they were using. Thanks again, Johnf > > Just because this morning I needed something to kick-start my sleepy > brain into being able to "think"... > > > When most people think of 'multiples' in the context of > list-comprehensions, they are talking about "nested-loops" - which are > easy-enough (or at least, well-documented). > > However, I've often wondered about the multiple being on the > 'left-hand side' of the equation/expression, and whether that is even > possible? > > It is! > Thus: > > python3 > Python 3.6.8 (default, Mar 21 2019, 10:08:12) > [GCC 8.3.1 20190223 (Red Hat 8.3.1-2)] on linux > Type "help", "copyright", "credits" or "license" for more information. > > # build the data-structure (I hope) > >>> d1 = { 'pkid':1, 'facility':'here' } > >>> d2 = { 'pkid':2, 'facility':'there' } > >>> d3 = { 'pkid':3, 'facility':'nowhere' } > >>> locDS = ( d1, d2, d3 ) > > # let's check that the data-format is as-described? > >>> type( locDS ) > > >>> for row in locDS: print( row ) > ... > {'pkid': 1, 'facility': 'here'} > {'pkid': 2, 'facility': 'there'} > {'pkid': 3, 'facility': 'nowhere'} > # are we on the right track? > > # this is the original code and approach > >>> loc_Choices = [ '' ] > >>> locKeys = [ 0 ] > >>> for row in locDS: > ...???? loc_Choices.append( row[ 'facility' ] ) > ...???? locKeys.append( row[ 'pkid' ] ) > ... > # which (I hope) shows the existing (and desired) results > >>> print( loc_Choices ) > ['', 'here', 'there', 'nowhere'] > >>> print( locKeys ) > [0, 1, 2, 3] > > # we can do the list inits, cf leaving it to the list comprehension > >>> loc_Choices = [ '' ] > >>> locKeys = [ 0 ] > > # but how might we achieve this using a (single) list comprehension? > >>> [ [ loc_Choices.append( row[ 'facility' ] ), locKeys.append( row[ > 'pkid' ] ) ] for row in locDS ] > # in (proper) code, I'd format this over two lines (at least) > > # correct me if I'm wrong, but these o/ps will > # 'disappear into the ether' when run within a pgm... > [[None, None], [None, None], [None, None]] > > # leaving us with 'the proof of the pudding' > >>> print( loc_Choices ) > ['', 'here', 'there', 'nowhere'] > >>> print( locKeys ) > [0, 1, 2, 3] > >>> > > > Possible considerations: > > 1 some sample data would have eased the way/its lack leaves room for > misinterpretation > > 2 a list comprehension normally executes more quickly than the > traditional (long-form) multi-line code-block. However, it would be > worth measuring that on your particular machine h/w and Py__version__. > The other 'efficiency' is readability, but "elegance" is largely a > matter of (personal) taste. So that ('mine-field') I leave to you... > > 3 taking the multi-line code-block and splitting it into TWO separate > (consecutive) list comprehensions (one for loc_Choices and the other > for locKeys) will most-likely be noticeably MORE 'expensive' > > 4 it is no matter if row/the locDS tuple dict-elements contain more > key-value pairs > > 5 during code review, my colleagues would delight in criticising the > choice of names such as locKeys - describing them as "JavaScript" (a > 'dirty word' to some), loc_Choices as contrary to PEP-8 ("flake", or > whatever), etc - despite that I have a little 'set' of abbreviations > with which I do the same, eg locNR. YMMV! > > 6 however, this simple boy will venture criticism of the inconsistency > in using the underline word-separator, eg locKeys but loc_Choices. > Even more potential for confusion: locChoices and loc_Choices!? > > 7 consider the data structures (outside of our view, here). Assuming > there are more elements in each 'loc dict', might it be just as easy > to leave the two pertinent elements 'there' and iterate over locDS > when actually applied. Alternately, perhaps it might be better to > construct a dictionary with the 'pkid's as keys and the 'facility' as > values for direct-access application? Not knowing how the structure(s) > will be utilised makes this pure speculation! > > 8 any?every time "similar loops" are found, re-factor to a function > (let's see if that philosophy kicks-off a retort or two...) > > 9 was it Knuth who described "premature optimisation" as "evil"? > Certainly I'm a believer in 'make it work before you make it better'. > So, (after all that!) what are we really achieving here? With such > quantities, is it critical to 'save time'? If so, is this part of the > code really the greatest time-sink? > (and we're back to the 'readability' consideration, previously mentioned) > > > Thanks, for the provocation into answering this long-held question - > and for the fact that my brain isn't sleepy any longer. > (mind you, such exertion suggests it must be time for a tea-break)... From mats at wichmann.us Mon Jun 24 18:39:42 2019 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 24 Jun 2019 16:39:42 -0600 Subject: [Tutor] replacing a loop In-Reply-To: References: <3f3beccb-8d9f-81b9-7726-a8fee305a348@jfcomputer.com> <38875b74-2ba5-105a-20a5-48414f607121@DancesWithMice.info> Message-ID: On 6/24/19 4:24 PM, johnf wrote: > Thank you - it worked!? I'm glad you are no longer sleepy! > > Actually I wanted the naming to remain consistent with the other loops > > So the name of the function/method (it is in a class) caused the use of > the underscore > > locChoices == location choices > > def locChoices(self) cause me to use loc_Choices in the code. > > I am torn about the readability of the code - at least in my mind I can > read the loop a lot easier than I can read the comprehension. That's actually fine. There are plenty of people who don't think comprehensions are more readable, at least unless they're really simple. It's an odd self-fulfilling prophecy: if you use them a lot, they usually start to feel more familiar. If you don't like the look of them, you don't tend to use them, and they never reach that point :) But it's also easy to abuse newer syntax - "newer must mean better" rather than "oh, here's an addition that maybe is cleaner in some cases". Do what works for you if you're the only person likely to look at the code later. If others may need to support it later, you may have to think a little harder about how to write - readability is not a binary choice. > Of course I considered the use of a function where I passed the required > parameters.? But the issue is the access to the data - that getDataSet() > was a performance hit when I used a passed parameter.? I could have > opened/accessed/retrieve the data during the opening but thought it best > to use a lazy data access as needed. > > Today was not really about performance but more about learning to use > the comprehension.? You see I did attempt to do create the > comprehension.? But after seeing your code I realized that I had the '[' > and the '{' confused.? I believed I had to use '{'.? I just reviewed a > tutorial off the net and that was what they were using. With curly braces it would be a dictionary comprehension. I just remembered I once saw a tutorial that I thought did a pretty nice job on comprehensions, and I had saved the link, again it's down to personal taste but take a gander at this to see if it makes sense: https://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/ From alan.gauld at yahoo.co.uk Tue Jun 25 04:45:08 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 25 Jun 2019 09:45:08 +0100 Subject: [Tutor] replacing a loop In-Reply-To: <3f3beccb-8d9f-81b9-7726-a8fee305a348@jfcomputer.com> References: <3f3beccb-8d9f-81b9-7726-a8fee305a348@jfcomputer.com> Message-ID: On 24/06/2019 17:15, johnf wrote: > def locChoices(self): > ??????? locDS = self.eslocation.getDataSet() > ??????? loc_Choices=[''] > ??????? locKeys=[0] > ??????? for row in locDS: > ??????????? loc_Choices.append(row['facility']) > ??????????? locKeys.append(row['pkid']) > > return loc_Choices,locKeys > ... and wonder if it will improve the performance.? Because you are building two lists in one loop it probably won't do much for performance. It might even be worse. As always measure. But something else that might make a small difference is the pre-fetch of the locations. Why not: for location in self.eslocation.getDataSet(): .... You don't use the locDS value anywhere other than in the loop. Also the choice of method name is unfortunate in that you return both choices and keys but the method name implies only choices. Minor nit-picks... -- 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 mhysnm1964 at gmail.com Tue Jun 25 08:24:12 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Tue, 25 Jun 2019 22:24:12 +1000 Subject: [Tutor] tree or link-list questions. Message-ID: <008b01d52b50$e68a0280$b39e0780$@gmail.com> All, Windows 10, python 3.6. I am trying to create a data structure. I might be using the wrong term here, but I am calling it a hierarchical tree. The properties of the tree I am trying to create is: * A single root node * A node can only have a single parent. * A parent can have multiple children. * Each node is unique. This is also my first attempt in doing Object coding ever. Thus the terminology I am not going to use correct. ? Data structure: records = [?the brown fox', ?the brown cow?, ?the brown car?, ?the yellow car?, ?the green house?, ?yellow flowers?, ?the quick brown fox?] What I am trying to do: I want to create a tree structure that stores the first word under the root, the 2nd word under the first word as a child, ETC. the word ?the? for example can only occur once as a node. Below is an example of the tree structure I am trying to achieve: Root Root / the Root / the / brown Root / the / brown / fox Root / the / brown / cow Root / the / brown / car Root / the / yellow Root / the / yellow / car Root / the / green Root / the / green / house Root / the / quick Root / the / quick / brown Root / the / quick / brown / fox Root / yellow Root / yellow / flowers The code I have used to break up the words and build a dictionary to identify the unique words plus the hit count: nar_dict = {} for text in records: words = text.split() l = len(words) for i in range(1, l+1): # only method I have found to correctly join the words. tmp_list = words[:i] key = ' '.join(tmp_list) nar_dict[key] = 0 # perform the searches using dictionary key and count the number of hits. for key, item in nar_dict.items(): print (key, item) for line in records: if line.startswith(key): nar_dict[key] += 1 The result is (note, the output below is using a different data set): {'the': 5, 'the brown': 3, 'the brown cow': 2, 'the brown car': 1, 'the yellow': 1, 'the yellow car': 1, 'yellow': 2, 'yellow house': 1, 'the quick': 1, 'the quick fox': 1, 'yellow flowers': 1} The code below works fine if I want to have a single parent and a single child. The init definition I have used an empty array to store the children. I am not sure how I can reference the new node back to the parent node. I have looked at some packages like anytree and I got completely lost with the code. I have looked for examples on the net and did not find anything I could understand. Here is my code thus far with the children as a list. # Value = hit counter # Name = node name class Node: def __init__(self, name, value): self.parent = None self.child = [] self.name = name self.value = value def add_child(self, name, value): # Compare the new value with the parent node if self.name: if name != self.name: if self.child is None: self.parent = self.child self.child = Node(name, value) else: self.child.add_child(name, value) else: self.name = name self.value = value # Print the tree def PrintTree(self): if self.child: self.child.PrintTree() print( self.name, self.value), I want to be able to do the following syntax: Node.children[-1]value Child = Node.children[0].children[0] Parent = child.parent.name I know I will have to write a search and walk functionality. Any comments and tips on how to fix the parent issue. I would be grateful. Hopefully this is not to open ended. Done my best to be very specific. ? Sean ? Open ended question master ? From shaondebnath72 at gmail.com Tue Jun 25 04:53:21 2019 From: shaondebnath72 at gmail.com (Shaon Debnath) Date: Tue, 25 Jun 2019 14:23:21 +0530 Subject: [Tutor] python Message-ID: I just wanted to know all about map() function in python. From sinardyxing at gmail.com Tue Jun 25 08:39:46 2019 From: sinardyxing at gmail.com (Sinardy Xing) Date: Tue, 25 Jun 2019 20:39:46 +0800 Subject: [Tutor] Basic Question about Visualization for enduser Message-ID: Hi, I am a newbie with python and the data visualization. I have completed pandas.DataFrame stuff and also the matplotlib. All of the example that I learned from internet currently are using the Anaconda Jupyter Notebook. I know there are API where we can output the result of the graph to png, will that be possible all these done automatically and dynamically via an apps ? Thanks Kind regards Sinardy From bgailer at gmail.com Tue Jun 25 09:21:54 2019 From: bgailer at gmail.com (Bob Gailer) Date: Tue, 25 Jun 2019 09:21:54 -0400 Subject: [Tutor] python In-Reply-To: References: Message-ID: On Jun 25, 2019 8:52 AM, "Shaon Debnath" wrote: > > I just wanted to know all about map() function in python. See https://www.geeksforgeeks.org/python-map-function/. If after reading that you still have questions please come back and ask them. Bob Gailer From mats at wichmann.us Tue Jun 25 10:25:35 2019 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 25 Jun 2019 08:25:35 -0600 Subject: [Tutor] Basic Question about Visualization for enduser In-Reply-To: References: Message-ID: <1b81eba6-387a-6431-7bd1-cc9475210c99@wichmann.us> On 6/25/19 6:39 AM, Sinardy Xing wrote: > Hi, > > I am a newbie with python and the data visualization. > I have completed pandas.DataFrame stuff and also the matplotlib. > > All of the example that I learned from internet currently are using the > Anaconda Jupyter Notebook. > I know there are API where we can output the result of the graph to png, > will that be possible all these done automatically and dynamically via an > apps ? You can generate graphs with mathplotlib. If you're specifically looking for a png, it looks a bit like this (assuming you want to follow the usual conventions for shortening the name, which certainly isn't required): from matplotlib import pyplot as plt plt.savefig('foo.png') savefig looks at the file extension in determining what to output. Without a specific question, I'm not sure what else we can say... From alan.gauld at yahoo.co.uk Tue Jun 25 10:28:43 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 25 Jun 2019 15:28:43 +0100 Subject: [Tutor] Basic Question about Visualization for enduser In-Reply-To: References: Message-ID: On 25/06/2019 13:39, Sinardy Xing wrote: > All of the example that I learned from internet currently are using the > Anaconda Jupyter Notebook. > I know there are API where we can output the result of the graph to png, > will that be possible all these done automatically and dynamically via an > apps ? If you mean "is it possible to write an app that does it all?" then the answer is yes. If you mean "is there an existing app that does it all?" the answer is maybe. It depends on exactly what you want to do, what kind of data you have etc. But a Google search is probably your best bet. Python itself is just a programming language so somebody - possibly you! - has to write the apps. -- 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 Tue Jun 25 10:48:30 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 25 Jun 2019 15:48:30 +0100 Subject: [Tutor] tree or link-list questions. In-Reply-To: <008b01d52b50$e68a0280$b39e0780$@gmail.com> References: <008b01d52b50$e68a0280$b39e0780$@gmail.com> Message-ID: On 25/06/2019 13:24, mhysnm1964 at gmail.com wrote: A much less open ended question :-) > class Node: > > def __init__(self, name, value): > self.parent = None > self.child = [] This should be plural if its a list. So call it children... However I'm not really sure it should be a list, most trees have two child nodes - called left and right by convention. But some trees do have multiple children, it's up to you. > self.name = name > self.value = value > > def add_child(self, name, value): > # Compare the new value with the parent node > if self.name: > if name != self.name: > if self.child is None: > self.parent = self.child > self.child = Node(name, value) > else: > self.child.add_child(name, value) > else: > self.name = name > self.value = value > My first comment would be that in OOP we should be trying to use objects so rather than pass name,value into the method pass another Node instance: def add_child(self, newNode): if newNode.name != self.name self.children.append(newNode) newNode.parent = self And if necessary call it like myNode.add_child(Node(name, value)) However, looking at your code above you seem to be confused about which node it adding which. self is the top level node to which you are adding a child. So when finished you want the existing nodes parent to be untouched and it's children list to include the new node. Instead you are overwriting the list with the new Node instance and setting the top level node's parent to the child. You really want the child to have the top level as its parent. The initial test of if self.name shouldn't be necessary since you assign name when you create the node. Without a name the node is pointless so you should check if name is None in the init method and throw an exception (ValueError?) if name is not valid. -- 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 Tue Jun 25 10:48:30 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 25 Jun 2019 15:48:30 +0100 Subject: [Tutor] tree or link-list questions. In-Reply-To: <008b01d52b50$e68a0280$b39e0780$@gmail.com> References: <008b01d52b50$e68a0280$b39e0780$@gmail.com> Message-ID: On 25/06/2019 13:24, mhysnm1964 at gmail.com wrote: A much less open ended question :-) > class Node: > > def __init__(self, name, value): > self.parent = None > self.child = [] This should be plural if its a list. So call it children... However I'm not really sure it should be a list, most trees have two child nodes - called left and right by convention. But some trees do have multiple children, it's up to you. > self.name = name > self.value = value > > def add_child(self, name, value): > # Compare the new value with the parent node > if self.name: > if name != self.name: > if self.child is None: > self.parent = self.child > self.child = Node(name, value) > else: > self.child.add_child(name, value) > else: > self.name = name > self.value = value > My first comment would be that in OOP we should be trying to use objects so rather than pass name,value into the method pass another Node instance: def add_child(self, newNode): if newNode.name != self.name self.children.append(newNode) newNode.parent = self And if necessary call it like myNode.add_child(Node(name, value)) However, looking at your code above you seem to be confused about which node it adding which. self is the top level node to which you are adding a child. So when finished you want the existing nodes parent to be untouched and it's children list to include the new node. Instead you are overwriting the list with the new Node instance and setting the top level node's parent to the child. You really want the child to have the top level as its parent. The initial test of if self.name shouldn't be necessary since you assign name when you create the node. Without a name the node is pointless so you should check if name is None in the init method and throw an exception (ValueError?) if name is not valid. -- 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 sinardyxing at gmail.com Tue Jun 25 10:52:28 2019 From: sinardyxing at gmail.com (Sinardy Xing) Date: Tue, 25 Jun 2019 22:52:28 +0800 Subject: [Tutor] Basic Question about Visualization for enduser In-Reply-To: <1b81eba6-387a-6431-7bd1-cc9475210c99@wichmann.us> References: <1b81eba6-387a-6431-7bd1-cc9475210c99@wichmann.us> Message-ID: Hi Mats, Thanks for the reply. I am so sorry the question was not clear. I was rushing home when composing the question. I have learned python and a little about the pandas lib also the visualization lib such as maplotlib, I also learn on how to generate the output (thanks again for that example I learned that too few days ago). My question is, how currently all of this great technology glue together and as a final product for the enduser. Because I cant imagine that we install Anaconda Jupyter Notebook at frontend for the enduser to use it, and give end user bunch of *.py I read a little bit about Django web framework, I am not sure if my understanding is correct. Therefore we are using Django as frontend tier then it connected to backend python server running the calculation and all other python stuff including the charting and send back the result to django front end server for end user to consume. My question is how is the end to end commonly use by company product, how they present those charts to end user.? Thanks very much for your kind reply, I am very new in this and all the information is via selfstudy and online document including this forum. Regards, Sinardy On Tue, Jun 25, 2019 at 10:27 PM Mats Wichmann wrote: > On 6/25/19 6:39 AM, Sinardy Xing wrote: > > Hi, > > > > I am a newbie with python and the data visualization. > > I have completed pandas.DataFrame stuff and also the matplotlib. > > > > All of the example that I learned from internet currently are using the > > Anaconda Jupyter Notebook. > > I know there are API where we can output the result of the graph to png, > > will that be possible all these done automatically and dynamically via an > > apps ? > > You can generate graphs with mathplotlib. If you're specifically > looking for a png, it looks a bit like this (assuming you want to follow > the usual conventions for shortening the name, which certainly isn't > required): > > from matplotlib import pyplot as plt > > plt.savefig('foo.png') > > savefig looks at the file extension in determining what to output. > > Without a specific question, I'm not sure what else we can say... > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From stephen.m.smith at comcast.net Tue Jun 25 09:50:38 2019 From: stephen.m.smith at comcast.net (stephen.m.smith at comcast.net) Date: Tue, 25 Jun 2019 09:50:38 -0400 Subject: [Tutor] Python Imported Code Message-ID: <00dd01d52b5c$fc6fd230$f54f7690$@comcast.net> Introduction: I have written a 'program' that does some reasonable screen scraping off of a specific website. The program has gotten too large so I have tried to segment it into logical pieces (tkinter logic as a start) but I am having problems. Specifically I need to pass several dictionaries to the module (imported code) that validates some user selection and into the code that navigates through the website. I have also created a variable that is set to 0 when a valid entry has been made by the user (telling the scraper what to do) that needs to be checked in the scraper module that is waiting patiently before it starts. There are multiple threads working as well because I may need to run several session as once. After struggling with my basic need - the ability to pass variables/dictionaries across modules, especially imported modules, I have read everything I can find and tried small, demo programs. But is till can't get it What I need: An ability to create dictionaries (that have validation information in them) and variables (that signal status and contain information entered by the user in tinkter) that I can make available to all components. I have tried using global, but that fails. I also can't seem to use arguments because the module that tkinter fires up with this command: self.process_button = tk.Button(master, text='Process Request', \ font = ('times', regular_font, 'bold'),\ fg = "blue", command=self.Process_Reservation_Request) does not seem to support the presence of arguments. Demo Program - This currently bombs in part 3 - it can't seem to see the value/space created in part1. Any help or reference that will clearly show me how to solve this problem would be most appreciated. I have read a number of points that recommend against using global anywhere (it does not seem to work with imported code) but I just can't find a recommendation for doing something that seems pretty fundamental. Thanks to an and all that try to help! Part 1 import nice import nice2 global myGlobal myGlobal = "initial value" print(myGlobal) nice.changeGlobal() print (nice.myGlobal) nice2.changeGlobal() print("nice version = ", nice.myGlobal) print("nice2 version = ", nice2.myGlobal) print("mainline=", myGlobal) part 2 - stored as nice.py def changeGlobal(): global myGlobal #print("entering changeGlobal part 1", myGlobal) myGlobal = "hello" print("top of myGlobal", myGlobal) myGlobal="bye" print("changeGlobal =", myGlobal) #changeGlobal() Part3 stored as nice2 myGlobal = "hello" def changeGlobal(): global myGlobal print("first value = ", nice.myGlobal) myGlobal="it worked" print("in changeGlobal2 =", myGlobal) #changeGlobal() From alan.gauld at yahoo.co.uk Tue Jun 25 12:48:03 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 25 Jun 2019 17:48:03 +0100 Subject: [Tutor] tree or link-list questions. In-Reply-To: <008b01d52b50$e68a0280$b39e0780$@gmail.com> References: <008b01d52b50$e68a0280$b39e0780$@gmail.com> Message-ID: On 25/06/2019 13:24, mhysnm1964 at gmail.com wrote: ... the tree I am trying to create is: > > * A single root node > * A node can only have a single parent. > * A parent can have multiple children. > * Each node is unique. A tree with multiple children (where multiple is more than 2) is entirely possible but does make the code more complex. Have you considered a binary tree where items go either left or right depending on whether they are "greater than" or "less than" (for some arbitrary definition of greater and less) than the parent node? This is much simpler to code, and you will find lots of examples online. ------ monospace font needed -------
Root
   the
      brown
         fox
         cow
         car
      yellow
         car
      green
         house
      quick
         brown
            fox
   yellow
      flowers
------ end of monospace font ------- The immediate questions you must answer (because only you understand the requirements) are: 1) How do you know which tree branch to navigate down? For example you have two car nodes. If searching for car which is the correct answer? Similarly for fox? 2) How do you know to go down an extra level. For example there are 4 nodes below 'the' What determines whether a new node sits alongside the existing nodes or at a level below. 3) Having decided you need to go a level lower how do you know which subnode to go down? How do you insert the node car into the correct subnode of the?(either brown or yellow) It looks like you are doing this based on the textual context. That means you insertion code has to be stateful since it must remember the previous word inserted (or have access to the original data). That gets complicated really quickly. Similarly, when you try to retrieve the data how do yu know which node to fetch. For example if I search for fox? Surely you will need to provide the full 'path' to fox topick the right one? But if you know the path why search a tree? In that case nested dictionaries would seem a better solution? Something like(missing all the quotes!): data = { the: [ {brown: [ {fox:[]}, {cow:[]}, {car:[]}, ]}, {yellow: [ {car: []} ]}, {green: [ {house: []} ]}, {quick: [ {brown: [ {fox: []} ]}, ]}, ]}, {yellow: [ {flowers: []} ]} ]} Then you can access the appropriate path: myFox = data[the][quick][brown][fox] Just a thought. Since I don't really know how you intend to use this I'm not sure if that would work for you. -- 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 Tue Jun 25 12:56:55 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 25 Jun 2019 17:56:55 +0100 Subject: [Tutor] Basic Question about Visualization for enduser In-Reply-To: References: <1b81eba6-387a-6431-7bd1-cc9475210c99@wichmann.us> Message-ID: On 25/06/2019 15:52, Sinardy Xing wrote: > My question is, how currently all of this great technology glue together > and as a final product for the enduser. Because I cant imagine that we > install Anaconda Jupyter Notebook at frontend for the enduser to use it, > and give end user bunch of *.py You sort of can and there are tools for wrapping it all up in an installable package. More commonly you create a desktop application using the GUI toolkit of your choice and generate the graphical output and display it yourself (using pyplot etc) Or.... > I read a little bit about Django web framework, I am not sure if my > understanding is correct. Therefore we are using Django as frontend tier > then it connected to backend python server running the calculation and all > other python stuff including the charting and send back the result to > django front end server for end user to consume. More or less although the Django framework may well host the server code too. Django is a web framework so it generates web pages(HTML) as output. Thus if you can generate png files in your python code Django can put links to those files in its HTML output and the users browser will display them. > My question is how is the end to end commonly use by company product, how > they present those charts to end user.? Nowadays its usually via the web. Depending on how fancy you want to get. It could simply be a static graphics file (png/pdf/jpeg etc) or maybe a Flash graphic(not common now) or even a movie generated by back end code. Or it could be something more dynamic controlled by Javascript in the browser in which case your backend code simply serves up raw data and the Javascript code turns it into dynamic graphics within the browser screen, possibly via SVG or similar technology. There have never been so many options for delivering content to end users! -- 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 Tue Jun 25 13:17:31 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 25 Jun 2019 18:17:31 +0100 Subject: [Tutor] Python Imported Code In-Reply-To: <00dd01d52b5c$fc6fd230$f54f7690$@comcast.net> References: <00dd01d52b5c$fc6fd230$f54f7690$@comcast.net> Message-ID: On 25/06/2019 14:50, stephen.m.smith at comcast.net wrote: > using global, but that fails. I also can't seem to use arguments because the > module that tkinter fires up with this command: > > self.process_button = tk.Button(master, text='Process Request', \ > font = ('times', regular_font, > 'bold'),\ > fg = "blue", > command=self.Process_Reservation_Request) > > does not seem to support the presence of arguments. There is a common trick to get round that: self.process_button = tk.Button(master, text='Process Request', font = ('times', regular_font,'bold'), fg = "blue", command=lambda :self.Process_Reservation_Request(x,y) ) The lambda defines a function that takes no arguments but which then calls your function with arguments. Of course x and y need to be visible within the function defining the lambda. > Demo Program - This currently bombs in part 3 - it can't seem to see the > value/space created in part1. Any help or reference that will clearly show > Part 1 > > import nice > import nice2 > global myGlobal > myGlobal = "initial value" global here does nothing. global is only useful inside a function. Remember that global in Python means module level - it does not mean visible between modules! > print(myGlobal) > nice.changeGlobal() > print (nice.myGlobal) > nice2.changeGlobal() > > print("nice version = ", nice.myGlobal) > print("nice2 version = ", nice2.myGlobal) > print("mainline=", myGlobal) > > part 2 - stored as nice.py > > def changeGlobal(): > global myGlobal This tells changeGlobal() that there is a variable called myGlobal defined at the module level. That is, within nice. It does not refer to the one you defined in Part 1. > #print("entering changeGlobal part 1", myGlobal) > myGlobal = "hello" This now creates that module level variable with the value 'hello'. > print("top of myGlobal", myGlobal) > > myGlobal="bye" And this redefines nice.myGlobal to be 'bye' The final value will now be seen by Part 1 code as nice.myGlobal with a value of 'bye'. > Part3 stored as nice2 > myGlobal = "hello" This creates a new global variable called myGlobal within the nice2 module. It is visible to Part 1 as nice2.myGlobal > def changeGlobal(): > global myGlobal This tells changeGlobal() to use the module level variable above. ie nice2.myGlobal. > print("first value = ", nice.myGlobal) You didn't import nice into nice2 so this should give an error. > myGlobal="it worked" > print("in changeGlobal2 =", myGlobal) This should set the variable nice2.myGlobal to 'it worked' and that will be seen in Part 1 code as nice2.myGlobal. -- 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 PyTutor at DancesWithMice.info Tue Jun 25 16:00:17 2019 From: PyTutor at DancesWithMice.info (David L Neil) Date: Wed, 26 Jun 2019 08:00:17 +1200 Subject: [Tutor] Was: Basic Question about Visualization for enduser In-Reply-To: References: <1b81eba6-387a-6431-7bd1-cc9475210c99@wichmann.us> Message-ID: On 26/06/19 4:56 AM, Alan Gauld via Tutor wrote: > On 25/06/2019 15:52, Sinardy Xing wrote: > >> My question is, how currently all of this great technology glue together >> and as a final product for the enduser. Because I cant imagine that we >> install Anaconda Jupyter Notebook at frontend for the enduser to use it, >> and give end user bunch of *.py > > You sort of can and there are tools for wrapping it all up in an > installable package. More commonly you create a desktop application > using the GUI toolkit of your choice and generate the graphical output > and display it yourself (using pyplot etc) NB this is NOT intended as a discussion about the specifics of the OP's situation, nor of the OP personally! This illustrates an interesting (at least to me?us) side-effect: in order to reduce "cognitive load" we often introduce training by using the most straight-forward or 'simple' tools. In this case Jupyter (which I think a most marvellous invention). However, such illustrative devices are often not what is used in the 'average' development department (assuming there is such a thing). Thus the training meets the knowledge-need (we hope) but not the requirements of practical application in the work-place. On the other hand, should training in Python, and more specifically, the likes of matplotlib, involve any forlorn attempt at universal coverage of 'the programmer's tool-set'? Your thoughts? -- Regards =dn From alan.gauld at yahoo.co.uk Tue Jun 25 19:42:45 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 26 Jun 2019 00:42:45 +0100 Subject: [Tutor] Training, Was: Basic Question about Visualization for enduser In-Reply-To: References: <1b81eba6-387a-6431-7bd1-cc9475210c99@wichmann.us> Message-ID: Caveat: This is a long term bug bear of mine and I spent many, many hours over the last 20 years in discussion with our local universities discussing how software-engineering training could be made more relevant to the real world. I'll try to keep it focused :-) On 25/06/2019 21:00, David L Neil wrote: > This illustrates an interesting (at least to me?us) side-effect: in > order to reduce "cognitive load" we often introduce training by using > the most straight-forward or 'simple' tools. In this case Jupyter (which > I think a most marvellous invention). Indeed, and for those with a use for it, extremely valuable and not really very simple! > However, such illustrative devices are often not what is used in the > 'average' development department (assuming there is such a thing). Maybe not, but then Python covers a wide gamut and some researchers for example never get beyond Jupyter, matplotlib etc etc. Those tools deliver all they need. You have to remember that software development is only one sphere in which Python is used. It is also a research tool, a prototyping tool, a personal productivity tool, a devops production scripting tool in addition to an application development language. Many Python users have no need to distribute their software. And for others Python is likely to be just one small component in a much bigger project involving Java, C++, SQL, etc. Professional devs may well need to build distributable apps. And they will use different tools. But there is little point in teaching students about them because there are few standards and different toolsets for just about every combination of app type and OS. In fact just about every web server framework has its own solution. And what you do for Python is different to Java or C++ or Smalltalk or Lisp... And if the project has multiple languages - and most do nowadays then you need a hybrid solution. There is no point in trying to do more than demonstrate some arbitrary example of what it takes to build a distributable package. Then make it clear that this is not a general solution, just one example. > the training meets the knowledge-need (we hope) but not the requirements > of practical application in the work-place. Frankly the biggest problem with most training regimes is that they put way too much focus on coding and programming languages. Professional devs spend much more of their time analysing, designing and testing than they do coding. In my experience very few trainees have any real experience of analysis, at best some rudimentary theoretical knowledge. In the real world the bigger the project the greater the proportion of time spent analyzing to understand exactly what you need to do. Often up to 25% of the total project time-scale. (Of course, those early activities have lower team sizes so the proportion of budget is more like 10%) Fred Brooks made the same point in his classic essay "No Silver Bullet". He talked about the inherent and intrinsic difficulty of software and understanding the essence of the solution as the major challenges. And no toolset (or training course) can shield you from that. At best they can free you to concentrate on it. Design is only covered at a shallow depth - usually restricted to some UML class diagrams and maybe a message sequence chart. Testing is covered at unit test level but rarely at integration or system test levels. And virtually never at UX or performance level. > On the other hand, should training in Python, and more specifically, the > likes of matplotlib, involve any forlorn attempt at universal coverage > of 'the programmer's tool-set'? My personal thought is that the key word there is 'forlorn' You could teach them to be experts in one toolset and in their very first job they discover they have to learn a complete new toolset. The principles will be similar but the practice very different. -- 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 mhysnm1964 at gmail.com Wed Jun 26 06:34:15 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Wed, 26 Jun 2019 20:34:15 +1000 Subject: [Tutor] tree or link-list questions. In-Reply-To: References: <008b01d52b50$e68a0280$b39e0780$@gmail.com> Message-ID: <029201d52c0a$b515d660$1f418320$@gmail.com> Allan, Once again, thanks for the help. I need to read more on OOPS it is all new and that was my first attempt. I looked at nesting Dictionaries and it got quite complex fast with the length of the strings and the path as you have outlined. I have parked that structure style for now. All these answers are really helping me to improve my programming knowledge and narrowing my definition. The reason why I am using the tree structure like a file system. Is I am going to attempt to write some code to walk down the tree based upon certain conditions which I am still working on. Based upon the test conditions will determine how I move up and down the tree. The high picture is I am trying to build a dynamic search functionality based upon keywords in the text. Like a store name or organisation name. The text has been cleaned up before applying it to the tree based upon text or punctuation and digits that are not required. Such as receipt numbers, postcodes, braces, etc. This is mostly done. I am hoping the tree structure will help in finishing the search capability. I will only know once I have tried it. If not, then I will look for another method. I am enjoying what I am doing because I am learning. I am pushing myself beyond my normal level of skills. Playing in the dark as I say. LOL Sean -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Wednesday, 26 June 2019 2:48 AM To: tutor at python.org Subject: Re: [Tutor] tree or link-list questions. On 25/06/2019 13:24, mhysnm1964 at gmail.com wrote: ... the tree I am trying to create is: > > * A single root node > * A node can only have a single parent. > * A parent can have multiple children. > * Each node is unique. A tree with multiple children (where multiple is more than 2) is entirely possible but does make the code more complex. Have you considered a binary tree where items go either left or right depending on whether they are "greater than" or "less than" (for some arbitrary definition of greater and less) than the parent node? This is much simpler to code, and you will find lots of examples online. ------ monospace font needed -------
Root
   the
      brown
         fox
         cow
         car
      yellow
         car
      green
         house
      quick
         brown
            fox
   yellow
      flowers
------ end of monospace font ------- The immediate questions you must answer (because only you understand the requirements) are: 1) How do you know which tree branch to navigate down? For example you have two car nodes. If searching for car which is the correct answer? Similarly for fox? 2) How do you know to go down an extra level. For example there are 4 nodes below 'the' What determines whether a new node sits alongside the existing nodes or at a level below. 3) Having decided you need to go a level lower how do you know which subnode to go down? How do you insert the node car into the correct subnode of the?(either brown or yellow) It looks like you are doing this based on the textual context. That means you insertion code has to be stateful since it must remember the previous word inserted (or have access to the original data). That gets complicated really quickly. Similarly, when you try to retrieve the data how do yu know which node to fetch. For example if I search for fox? Surely you will need to provide the full 'path' to fox topick the right one? But if you know the path why search a tree? In that case nested dictionaries would seem a better solution? Something like(missing all the quotes!): data = { the: [ {brown: [ {fox:[]}, {cow:[]}, {car:[]}, ]}, {yellow: [ {car: []} ]}, {green: [ {house: []} ]}, {quick: [ {brown: [ {fox: []} ]}, ]}, ]}, {yellow: [ {flowers: []} ]} ]} Then you can access the appropriate path: myFox = data[the][quick][brown][fox] Just a thought. Since I don't really know how you intend to use this I'm not sure if that would work for you. -- 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 mhysnm1964 at gmail.com Wed Jun 26 06:40:50 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Wed, 26 Jun 2019 20:40:50 +1000 Subject: [Tutor] data structures general query Message-ID: <029b01d52c0b$a08598b0$e190ca10$@gmail.com> All, General computer science question for data structures. When would you use the below structures and why? If you can provide a real life example on when they would be used in a program This would be great. I am not after code, just explanation. Link lists Double link-lists Binary trees What other trees are their other than hierarchical tree (file systems) Link lists I would guess be useful in an index for a database? Binary trees useful for searches? Sean From alan.gauld at yahoo.co.uk Wed Jun 26 07:12:11 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 26 Jun 2019 12:12:11 +0100 Subject: [Tutor] tree or link-list questions. In-Reply-To: <029201d52c0a$b515d660$1f418320$@gmail.com> References: <008b01d52b50$e68a0280$b39e0780$@gmail.com> <029201d52c0a$b515d660$1f418320$@gmail.com> Message-ID: On 26/06/2019 11:34, mhysnm1964 at gmail.com wrote: > The reason why I am using the tree structure like a file system. Is I am > going to attempt to write some code to walk down the tree based upon certain > conditions which I am still working on. Based upon the test conditions will > determine how I move up and down the tree. Defining the test conditions is the hard part of building and searching any tree. That's why binary trees are so much easier, the usual comparison (<,=,>) apply. When you have multiple subtrees your test function must return a selection index telling you which subtree to go down. > The high picture is I am trying to build a dynamic search functionality > based upon keywords in the text. Like a store name or organisation name. The > text has been cleaned up before applying it to the tree based upon text or > punctuation and digits that are not required. Such as receipt numbers, > postcodes, braces, etc. This is mostly done. I am hoping the tree structure > will help in finishing the search capability. I will only know once I have > tried it. If not, then I will look for another method. One thing to consider is an LDAP directory. It is a tree structure that acts like nested dictionaries and is specifically designed for handling hierarchies. Specifically organisational hierarchies, but any kind will do. It what's at the heart of most corporate Directory systems - such an Microsoft Exchange. And the good news is that there are open source options available and Python modules to work with them. but it might be overkill for your project. But worth a quick Google and Wikipedia browse... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Wed Jun 26 07:28:53 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 26 Jun 2019 12:28:53 +0100 Subject: [Tutor] data structures general query In-Reply-To: <029b01d52c0b$a08598b0$e190ca10$@gmail.com> References: <029b01d52c0b$a08598b0$e190ca10$@gmail.com> Message-ID: On 26/06/2019 11:40, mhysnm1964 at gmail.com wrote: > When would you use the below structures and why? If you can provide a real > life example on when they would be used in a program This would be great. > Link lists Link lists are very flexible and ideal for when you have a varying amount of data and don;t want to reserve space for a maximum amount when you only use a fraction of it. Examples are process lists in an OS. Jobs in a queue (although technically queues are data structures in their own right!) Think of situations where you use a list in everyday life - a shopping list. You add items in an ad-hoc way and don't know in advance how any items you will eventually have. The advantages are that they have a low memory footprint, easy to move things around (including deletion), insertion is quick if adding to the top. Searching not so much. > Double link-lists Same as single linked lists except you can access both ends so a sorted list becomes much faster to search but at the cost of making inserts and moves etc slightly more complex(but only slightly). Real world uses? Hmmm, for me not so many. I'll let somebody else suggest something! > Binary trees Good for searching. Not so good for insertions/moves. Always a danger of the tree becoming unbalanced in which case it tends towards becoming a linked list. Real world use - heirarchical or ordered data arriving at random. Trees are inherently ordered so inserting the data puts it "in order" Code tends to be more complex than for lists though and is often recursive in nature which can be an issue for big trees. > What other trees are their other than hierarchical tree (file systems) Thee are whole families of trees including multi child trees such as yours. AVL trees (if memory serves) try to force the tree to be balanced. Non binary trees need selector functions etc (often hash based). Real world use - database caches, network management heirarchies parsing structures. -- 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 oscar.j.benjamin at gmail.com Wed Jun 26 07:47:01 2019 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 26 Jun 2019 12:47:01 +0100 Subject: [Tutor] Basic Question about Visualization for enduser In-Reply-To: References: <1b81eba6-387a-6431-7bd1-cc9475210c99@wichmann.us> Message-ID: On Tue, 25 Jun 2019 at 17:08, Sinardy Xing wrote: > > My question is, how currently all of this great technology glue together > and as a final product for the enduser. Because I cant imagine that we > install Anaconda Jupyter Notebook at frontend for the enduser to use it, > and give end user bunch of *.py It really depends who the end user is and what you want to give to them. To you it might seem obvious what work you are doing or what you mean by a "final product" or an "end user" but to me those can mean all sorts of things. Jupyter notebooks are great for sharing the results with other people who might want to tweak the code and try test variations of it. It's popular for data visualisation because you can encapsulate both the code and the output plots in a single file that you can send to someone or share on the web etc. On the other hand if your end user is not someone who will want to run the code but just wants to see the output plots etc then you can turn the notebook into a PDF file and give that to them. I often use matplotlib to make plots that I save as PDFs and then incorporate into a larger PDF document that I create using LaTeX. That's a common pattern for matplotlib which aims to create "publication quality figures" using vector graphics. If you actually wanted to create an interactive piece of software for other people to use on their computers then that's very different. -- Oscar From mats at wichmann.us Wed Jun 26 13:01:19 2019 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 26 Jun 2019 11:01:19 -0600 Subject: [Tutor] data structures general query In-Reply-To: <029b01d52c0b$a08598b0$e190ca10$@gmail.com> References: <029b01d52c0b$a08598b0$e190ca10$@gmail.com> Message-ID: On 6/26/19 4:40 AM, mhysnm1964 at gmail.com wrote: > All, > > > > General computer science question for data structures. > > When would you use the below structures and why? If you can provide a real > life example on when they would be used in a program This would be great. I > am not after code, just explanation. > > > > Link lists > > Double link-lists This becomes a question of what kind of data you have and what you need to do with it. Python has "lists", which are what most people think of as arrays - you reference them by index. Lists have fast access: if you know you want the 7th element, you just ask for that. Then if you want to insert a new element following that, the whole array past that point has to move to make room - the grunt work is normally abstracted away by languages that have array datatypes, but it is still happening, same for deleting, so lots of random inserts/deletes (except at the end) are "expensive". In linked lists, insert is cheap. You just take the existing element's "next" field and stick that into the new element's "next" field, and then stick the new element into the existing element's "next" field. However, deleting an still isn't cheap: unless your traversal mechansm saves it on the way to finding a certain element, you have to start from the beginning and walk through to find the previous element, because it's this one which has to have its "next" field changed. A doubly linked list however makes removal trivial - you already have a reference to that element via the "previous" field. For both kinds of linked lists, searching is expensive as you have to walk the chain. The fastest data structure for searching is when the lookup is hashed - this is what the Python dict type allows for, as the keys are hashed as they're stored (leading to the sometimes surprising rule for Python dicts that "key must be hashable"). Other popular data structures are queues and stacks. Note there are also circular lists, which only means the tail hooks back into the head. So your choices depend on what the usage pattern of your data is. The fact that Python doesn't have link lists, neither natively or in the standard library, suggests the designers didn't think they represented a common enough usage pattern for users of the language. > Binary trees > > What other trees are their other than hierarchical tree (file systems) Can let this go without mentioning the Merkle tree, darling of anyone who's ever dabbled in blockchain :) > Link lists I would guess be useful in an index for a database? I believe the "classic" use case is memory management - keeping track of chunks of memory. > Binary trees useful for searches? it's useful for searches if the "is A less than B" comparison is a useful property of the data you are representing. If not, it isn't useful at all as you have no way to construct the tree :) From wrw at mac.com Wed Jun 26 13:59:10 2019 From: wrw at mac.com (William Ray Wing) Date: Wed, 26 Jun 2019 13:59:10 -0400 Subject: [Tutor] data structures general query In-Reply-To: <029b01d52c0b$a08598b0$e190ca10$@gmail.com> References: <029b01d52c0b$a08598b0$e190ca10$@gmail.com> Message-ID: > On Jun 26, 2019, at 6:40 AM, mhysnm1964 at gmail.com wrote: > > All, > > > > General computer science question for data structures. > > When would you use the below structures and why? If you can provide a real > life example on when they would be used in a program This would be great. I > am not after code, just explanation. > One of the most useful (to me) structures is the double-ended queue ("from collections import deque?). It creates a queue that can quickly remove an item from one end and add an item to the other. Particularly useful for displaying a sliding window into time series data, or a moving track of the most recent n observations of a physical measurement. Bill From mats at wichmann.us Wed Jun 26 14:46:11 2019 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 26 Jun 2019 12:46:11 -0600 Subject: [Tutor] data structures general query In-Reply-To: References: <029b01d52c0b$a08598b0$e190ca10$@gmail.com> Message-ID: <5d29a2d8-ceab-d31c-b8b6-2d9b2f7deb17@wichmann.us> On 6/26/19 11:59 AM, William Ray Wing via Tutor wrote: > One of the most useful (to me) structures is the double-ended queue ("from collections import deque?). It creates a queue that can quickly remove an item from one end and add an item to the other. Particularly useful for displaying a sliding window into time series data, or a moving track of the most recent n observations of a physical measurement. > > Bill Indeed. deques are often implemented using double-linked lists, the Python version is (though you can't get at the details easily). I forgot to add the snide-comment part of "what are these good for": (a) binary search trees are excellent for Computer Science professors who want to introduce recursion into their classes. (b) all the classic data structures are candidates for being job interview questions ("implement a linked list in Python on the whiteboard") From alan.gauld at yahoo.co.uk Wed Jun 26 17:50:28 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 26 Jun 2019 22:50:28 +0100 Subject: [Tutor] data structures general query In-Reply-To: <5d29a2d8-ceab-d31c-b8b6-2d9b2f7deb17@wichmann.us> References: <029b01d52c0b$a08598b0$e190ca10$@gmail.com> <5d29a2d8-ceab-d31c-b8b6-2d9b2f7deb17@wichmann.us> Message-ID: On 26/06/2019 19:46, Mats Wichmann wrote: > I forgot to add the snide-comment part of "what are these good for": > > (a) binary search trees are excellent for Computer Science professors > who want to introduce recursion into their classes. > > (b) all the classic data structures are candidates for being job > interview questions ("implement a linked list in Python on the whiteboard") That's a good point that I intended to make in my response but forgot. In practice, Python's data structure can mimic these classic data structures and do so more efficiently than trying to write them from scratch in Python. So while they all have Computer Science theoretic pros and cons, in practice, Python programmers tend to ignore them and use lists, sets, tuples and dictionaries. And if they need anything more complex classes or pre-built modules. It's a very rare problem that needs a traditional data structure in Python. -- 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 brick.howse at yahoo.com Wed Jun 26 19:07:01 2019 From: brick.howse at yahoo.com (Brick Howse) Date: Wed, 26 Jun 2019 17:07:01 -0600 Subject: [Tutor] Range command Message-ID: Hello all, New to programming and I noticed the range command did not function like it does in the tutorial. For example, I type >>>> range(5, 10) And the output is range(5, 10) In section 4.3 It shows it should display >>>> range(5, 10) 5, 6, 7, 8, 9 I'm using windows 10 Python 3.7.3 Any suggestions is greatly appreciated. Thank you in advance. Derek Sent from my iPhone From cs at cskk.id.au Wed Jun 26 19:20:51 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 27 Jun 2019 09:20:51 +1000 Subject: [Tutor] data structures general query In-Reply-To: References: Message-ID: <20190626232051.GA5693@cskk.homeip.net> On 26Jun2019 11:01, Mats Wichmann wrote: >On 6/26/19 4:40 AM, mhysnm1964 at gmail.com wrote: >> Link lists I would guess be useful in an index for a database? > >I believe the "classic" use case is memory management - keeping track of >chunks of memory. Flipping this, your typical ordered database index (unless the tech has advanced further) is a B+ tree possibly with a doubly linked list threaded through the leaf nodes. So a B+ tree is a sorted tree structure which is maintained in a balanced way so that the depth is pretty consistent across the tree, in turn so that there is not particularly long branch equivalent to have particular keys which are very expensive to look up. Unlike a binary tree a B+ tree has many keys at each node because this makes for efficient storage of the nodes in "blocks", whatever size is useful, and is also makes the tree shallower, making lookups cheaper. The leaf nodes point at the associated records (or possibly just hold keys if there are no records), and the doubly linked list of leaf nodes means you can traverse forwards or backwards from one leaf to the next in either order. Which makes find ranges of keys efficient: "SELECT ... WHERE key >= value ORDER BY key DESC" and its many variants. As has been pointed out by others, the various computer science basic structures are often built up into something different or more complex depending on what your data access pattern will be. It is important to understand the basic structures so that you can reason about their trade offs, and in turn understand more complex related structures. This goes both ways: in design you choose a structure to support what you're going to do. In use, you might choose to approach a problem differently depending on what data structure is used to arrange the data, because some actions are cheap in some structures and expensive in others, so you choose the efficient action where possible. Cheers, Cameron Simpson From mats at wichmann.us Wed Jun 26 19:29:08 2019 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 26 Jun 2019 17:29:08 -0600 Subject: [Tutor] Range command In-Reply-To: References: Message-ID: <2523ac04-0f49-4301-9761-51e795fd6c54@wichmann.us> On 6/26/19 5:07 PM, Brick Howse via Tutor wrote: > Hello all, > > New to programming and I noticed the range command did not function like it does in the tutorial. > For example, > I type >>>>> range(5, 10) > And the output is > range(5, 10) > > In section 4.3 > It shows it should display >>>>> range(5, 10) > 5, 6, 7, 8, 9 > > I'm using windows 10 > Python 3.7.3 > > Any suggestions is greatly appreciated. Things change. You have a Python2-oriented tutorial. Neither version is wrong. In Python2, range returns a list. In Python3, range returns a range object, which you can iterate over. So for a typical use, which is to iterate over the range, the results are identical: >>> for i in range(5,10): ... print(i) ... 5 6 7 8 9 From alan.gauld at yahoo.co.uk Wed Jun 26 19:44:07 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 27 Jun 2019 00:44:07 +0100 Subject: [Tutor] Range command In-Reply-To: References: Message-ID: On 27/06/2019 00:07, Brick Howse via Tutor wrote: > Hello all, > > New to programming and I noticed the range command did not function like it does in the tutorial. > For example, > I type >>>>> range(5, 10) > And the output is > range(5, 10) You have a Python 2 tutorial but are using Python 3. In Python 3 range has changed and returns a fancy kind of object (a range) that you don't really need to know about. If you really want the values convert it to a list: >>> list(range(5,10)) > Any suggestions is greatly appreciated. To avoid any similar confusion either find a Python 3 tutorial or download Python 2 and use it. Once you know Python 2 converting to Python 3 is not hard. But it is probably best just to learn Python 3 from the start! Thee are quite a few of these kinds of changes between Python 2 and 3. For experienced programmers they are "A Good Thing" because they make Python more efficient and more consistent in the way it works. But for a beginner they just seem bizarre and unintuitive. For now, just accept them and eventually you will understand why they exist. -- 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 braveheartmovement at yahoo.com Thu Jun 27 17:20:08 2019 From: braveheartmovement at yahoo.com (Brave Heart) Date: Thu, 27 Jun 2019 23:20:08 +0200 Subject: [Tutor] Python and DB Message-ID: <45ZXsd0YT6zp4CF@mail.python.org> I have a little RSS program , I current the out are basically outputted on my screen, but I would like python to write to DB so I can from DB write on a webpage with PHP... Kinda like I want to run a news website .. Sent from Mail for Windows 10 From mats at wichmann.us Thu Jun 27 18:50:21 2019 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 27 Jun 2019 16:50:21 -0600 Subject: [Tutor] Python and DB In-Reply-To: <45ZXsd0YT6zp4CF@mail.python.org> References: <45ZXsd0YT6zp4CF@mail.python.org> Message-ID: <976246ad-a66b-56ae-d8df-dd6f58a50b68@wichmann.us> On 6/27/19 3:20 PM, Brave Heart via Tutor wrote: > I have a little RSS program , I current the out are basically outputted on my screen, but I would like python to write to DB so I can from DB write on a webpage with PHP... > > Kinda like I want to run a news website .. There doesn't seem to be a question here. Does this help in any way? https://docs.python.org/3.7/library/sqlite3.html From cs at cskk.id.au Thu Jun 27 18:57:46 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Fri, 28 Jun 2019 08:57:46 +1000 Subject: [Tutor] Python and DB In-Reply-To: <45ZXsd0YT6zp4CF@mail.python.org> References: <45ZXsd0YT6zp4CF@mail.python.org> Message-ID: <20190627225746.GA48659@cskk.homeip.net> On 27Jun2019 23:20, Brave Heart wrote: >I have a little RSS program , I current the out are basically outputted on my screen, but I would like python to write to DB so I can from DB write on a webpage with PHP... >Kinda like I want to run a news website .. See the "sqlite3" module with ships with Python. It creates a database in a local file and provides an SQL interface: https://docs.python.org/3/library/sqlite3.html#module-sqlite3 SQLite3 also has a command line tool for interactive access to the database; you might need to install that separately. For the web side you'd need an SQLite library for PHP, but I cannot believe that one does not exist. (Or you could also write your web application in Python instead of PHP.) Cheers, Cameron Simpson From alan.gauld at yahoo.co.uk Thu Jun 27 19:03:00 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 28 Jun 2019 00:03:00 +0100 Subject: [Tutor] Python and DB In-Reply-To: <45ZXsd0YT6zp4CF@mail.python.org> References: <45ZXsd0YT6zp4CF@mail.python.org> Message-ID: On 27/06/2019 22:20, Brave Heart via Tutor wrote: > I would like python to write to DB so I can from DB write on a webpage with PHP... Yes, that's easy enough. Python supports access to many databases, do you have one in mind? If not the SQLite module that comes in the standard library is probably more than adequate for your needs. PHP has modules for reading SQLite too. If you are familiar with using databases in any other language the Python DBAPI should be easy to pick up. If you don;t know SQL then you might like the database topic in my tutorial(see below) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mayoadams at gmail.com Fri Jun 28 01:24:04 2019 From: mayoadams at gmail.com (Mayo Adams) Date: Fri, 28 Jun 2019 01:24:04 -0400 Subject: [Tutor] Environment variables and Flask Message-ID: I have for some time been flummoxed as to the significance of setting environment variables, for example in order to run a Flask application. What are these environment variables, exactly, and why is it necessary to set them? "Googling" here simply leads me into more arcana, and doesn't really help. -- Mayo Adams From mhysnm1964 at gmail.com Fri Jun 28 02:10:15 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Fri, 28 Jun 2019 16:10:15 +1000 Subject: [Tutor] double nodes being enter into tree structure Message-ID: <02a701d52d78$289bc400$79d34c00$@gmail.com> Hello All, I am back!!! I forget which movie that comes from. Anyway, my issue is I am getting the same node being added to the parent node of my tree below. I have traced the code and do not understand why. This occurs when the loop which calls the addNode function has only looped once. I have had the return statement present and commented out with the same result. The length of the children list is 2 when it should be 1. AddNode is using recursive functionality. I suspect the function should be in the object itself, but I have it outside for now to work out my logic. I am sure there is a logic issue here in the area of OOPS, but I cannot see it. The aim is two only ever have one word in the children list regardless how many times it appears in the original source. Thus if the string has ?the brown fox? and ?the brown car?. The following has to occur: * Children list in the root node will only have the ?the? node once. * Children list in the ?the? node will only have the ?brown? node once. * Children in the ?brown? node will have two nodes ?cow? and ?car?. Below is the code: def addNode(words, tree): # creates the branch of words for the tree if words: wordExists = False for child in tree.children: if words[0] == child.name: wordExists = True # end if # end for if not wordExists: tree = tree.add(words[0], 0) addNode(words[1:], tree) # end if # note, the below has been uncommented with the same issue. #return tree class Node(object): # I am not 100% sure the purpose of (object) does. def __init__(self, name, value): self.parent = None self.children = [] self.name = name self.value = value def add(self, name, value): node1=Node(name, value) self.children.append(node1) node1.parent=self return node1 ?. Bunch a code to load the files. for account_no, date, line, amount in records: tree.children.append(addNode(line.split(), tree)) note: I have tried Allan?s suggestion as shown below and could not get it to work. I am currently reading his book on object programming to learn more about OOPS. This is how I worked out the recursive function. ? def add_child(self, newNode): if newNode.name != self.name self.children.append(newNode) newNode.parent = self myNode.add_child(Node(name, value)) From alan.gauld at yahoo.co.uk Fri Jun 28 04:34:40 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 28 Jun 2019 09:34:40 +0100 Subject: [Tutor] Environment variables and Flask In-Reply-To: References: Message-ID: On 28/06/2019 06:24, Mayo Adams wrote: > What are these environment variables, exactly, and why is it necessary to > set them? When you run a program under an operating system the OS sets up an "environment" (or context) for the program to run in. (This includes the OS shell that the user interacts with - each user gets their own environment.) Environment variables are variables that are accessible within that environment but not from other environments. Thus two users may have different values for the same variable, such as the HOME variable which dictates the users home directory. Or PATH which tells the OS where the users programs can be found. When you start a new program the OS creates a copy of the current environment(your user environment) and runs the program within that copy. Thus if the program modifies any of the environment variables it does not affect the parent process environment since it is modifying its own copy. (So if it changes HOME to give itself its own default directory that doesn't change the user's HOME - or any other program environment for that matter) Some applications define their own environment variables as a way of setting user specific values. So a web server might define the root directory for the server as an environment variable then each user can set a different root without having to pass that in each time they run the program. Environment variable have fallen out of favour for user settings and config files are now preferred. But some things are a bit easier via en environment variable - especially where you spawn new sub-processes and don't want the sub-process to have to re-read the config file each time. -- 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 Fri Jun 28 04:48:38 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 28 Jun 2019 09:48:38 +0100 Subject: [Tutor] double nodes being enter into tree structure In-Reply-To: <02a701d52d78$289bc400$79d34c00$@gmail.com> References: <02a701d52d78$289bc400$79d34c00$@gmail.com> Message-ID: On 28/06/2019 07:10, mhysnm1964 at gmail.com wrote: > > Anyway, my issue is I am getting the same node being added to the parent node of my tree below. I'm not sure about that part but.... > def addNode(words, tree): > if words: > wordExists = False > for child in tree.children: > if words[0] == child.name: > wordExists = True > if not wordExists: > tree = tree.add(words[0], 0) > > addNode(words[1:], tree) Notice that the recursive call is adding the subsequent words to the same tree that was passed to addNode originally. In other words you are not building a tee you are just building a list of children under the top level tree node. I suspect you want to add the subsequent words to the children of the node you just added? Or the existing one of the same value... So you need something like(untested!) for child in tree.children: if words[0] == child.name nextNode = child else: nextNode = tree.addNode(words[0],0) addNode(words[1:], nextNode) -- 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 Jun 28 07:45:22 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Fri, 28 Jun 2019 21:45:22 +1000 Subject: [Tutor] Environment variables and Flask In-Reply-To: References: Message-ID: <20190628114522.GA13945@cskk.homeip.net> On 28Jun2019 09:34, Alan Gauld wrote: >Environment variable have fallen out of favour for user settings >and config files are now preferred. But some things are a bit >easier via en environment variable - especially where you spawn >new sub-processes and don't want the sub-process to have >to re-read the config file each time. This is something of a simplification. Most programmes consult a few places for configuration information. A programme may want to run in different ways (different places to write files, different language settings or timezones, etc). Environment variables are a convenient and inheritable way to indicate a specific way to run, because they get inherited (as a copy) from parent programme to child programmes and so on. So a flask application will usually be invoked from within a web server, and various things about how it should run _may_ be indicated by environment variables set by the web server. A flexible programme may decide how to run from several places, in a specific order (to ensure predictable controllable behaviour). A normal order would be: command line arguments, environment variables, personal config file ($HOME/.thingrc), system configfile (/etc/thingrc), inbuilt defaults within the programme. The idea here is that this is a simple hierachy of defaults. Anything can be overridden by a command line option. If not supplied, an environment variable may be consulted. Otherwise the personal config file. Otherwise the system default. Otherwise some default within the programme. Programmatically you don't go: for each setting, look at these things in the order above. Instead you has some "settings" structure in the programme, initially filled out with some internal defaults. You read the system file to override the builtin defaults. Then you read the personal file to override that. Then you consult the environment to override that. Then you process the command line and have it override various things. A single pass across all this stuff. Any of it may be missing. Returning to Flask and the environment: because a Flask app is often invoked from within a web server instead of directly, it isn't feasible to pass it "command line" arguments to control it. So the environment becomes the most convenient place for ad hoc special settings. Cheers, Cameron Simpson "How do you know I'm Mad?" asked Alice. "You must be," said the Cat, "or you wouldn't have come here." From mats at wichmann.us Fri Jun 28 09:22:07 2019 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 28 Jun 2019 07:22:07 -0600 Subject: [Tutor] Environment variables and Flask In-Reply-To: References: Message-ID: <42f52d70-d77b-e85b-4ecf-8a49d1930d8e@wichmann.us> On 6/27/19 11:24 PM, Mayo Adams wrote: > I have for some time been flummoxed as to the significance of setting > environment variables, for example in order to run a Flask application. > What are these environment variables, exactly, and why is it necessary to > set them? "Googling" here simply leads me into more arcana, and doesn't > really help. As others have noted, it's a way to pass information from one process to another at startup time. Since this is a Python list, I thought it might be instructive to show how it works. In Python, you access these environment variables through a dictionary in the os module, called environ, which "survives" across the call-another-process boundary, unlike any normal variables you might set in your program. Here's a trivial Python app that is able to recognize those environment variables that begin with MYENV_. That points up one issue with environment variables right away: it's a namespace you share with everybody, and there's a chance someone accidentally is using a variable you think is important - because it's important to them in their context, not yours. So tricks like special naming conventions may be useful. In this snip, we build a dictionary from os.environ, using only the keys that seem to be "for us": === child.py === import os myenv = { k: v for k, v in os.environ.items() if "MYENV_" in k } print("child found these settings:", myenv) ====== Now write another script which sets a value, then calls the child script; then sets a different value, then calls the child script. === parent.py === import os import subprocess print("Calling with MYENV_foo set") os.environ['MYENV_foo'] = "Yes" subprocess.run(["python", "child.py"]) print("Calling with MYENV_bar set") os.environ['MYENV_bar'] = "1" subprocess.run(["python", "child.py"]) ====== From mats at wichmann.us Fri Jun 28 09:34:11 2019 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 28 Jun 2019 07:34:11 -0600 Subject: [Tutor] double nodes being enter into tree structure In-Reply-To: <02a701d52d78$289bc400$79d34c00$@gmail.com> References: <02a701d52d78$289bc400$79d34c00$@gmail.com> Message-ID: <2c784eb0-5c1c-674f-5d52-18893255e60e@wichmann.us> On 6/28/19 12:10 AM, mhysnm1964 at gmail.com wrote: > class Node(object): > > # I am not 100% sure the purpose of (object) does. as to this bit: having your class inherit from object means it's a "new-style class". That's only significant if you're writing code that is expected to run under Python 2; in Python 3 all classes are new-style and you don't need to inherit from object to get that - and if you run a code checker on your program in a Python 3 context, it will likely tell you so. From mayoadams at gmail.com Fri Jun 28 09:34:55 2019 From: mayoadams at gmail.com (Mayo Adams) Date: Fri, 28 Jun 2019 09:34:55 -0400 Subject: [Tutor] Environment variables and Flask In-Reply-To: <42f52d70-d77b-e85b-4ecf-8a49d1930d8e@wichmann.us> References: <42f52d70-d77b-e85b-4ecf-8a49d1930d8e@wichmann.us> Message-ID: Many thanks to some very bright and helpful gentlemen. On Fri, Jun 28, 2019 at 9:24 AM Mats Wichmann wrote: > On 6/27/19 11:24 PM, Mayo Adams wrote: > > I have for some time been flummoxed as to the significance of setting > > environment variables, for example in order to run a Flask application. > > What are these environment variables, exactly, and why is it necessary to > > set them? "Googling" here simply leads me into more arcana, and doesn't > > really help. > > As others have noted, it's a way to pass information from one process to > another at startup time. Since this is a Python list, I thought it > might be instructive to show how it works. In Python, you access these > environment variables through a dictionary in the os module, called > environ, which "survives" across the call-another-process boundary, > unlike any normal variables you might set in your program. Here's a > trivial Python app that is able to recognize those environment variables > that begin with MYENV_. That points up one issue with environment > variables right away: it's a namespace you share with everybody, and > there's a chance someone accidentally is using a variable you think is > important - because it's important to them in their context, not yours. > So tricks like special naming conventions may be useful. > > In this snip, we build a dictionary from os.environ, using only the keys > that seem to be "for us": > > > === child.py === > import os > > myenv = { k: v for k, v in os.environ.items() if "MYENV_" in k } > > print("child found these settings:", myenv) > ====== > > Now write another script which sets a value, then calls the child > script; then sets a different value, then calls the child script. > > === parent.py === > import os > import subprocess > > print("Calling with MYENV_foo set") > os.environ['MYENV_foo'] = "Yes" > subprocess.run(["python", "child.py"]) > > print("Calling with MYENV_bar set") > os.environ['MYENV_bar'] = "1" > subprocess.run(["python", "child.py"]) > ====== > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Mayo Adams 287 Erwin Rd. Chapel Hill, NC 27514 (919)-780-3917 mayoadams at gmail.com From sjeik_appie at hotmail.com Fri Jun 28 16:32:30 2019 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Fri, 28 Jun 2019 20:32:30 +0000 Subject: [Tutor] Fwd: Re: Unexpected result when running flask application. In-Reply-To: Message-ID: Ooops, forgot to 'reply all' ---------- Forwarded message ---------- From: Albert-Jan Roskam Date: 28 Jun 2019 21:31 Subject: Re: [Tutor] Unexpected result when running flask application. To: Cameron Simpson Cc: On 20 Jun 2019 00:56, Cameron Simpson wrote: On 19Jun2019 09:54, Alan Gauld wrote: >On 19/06/2019 05:18, Cravan wrote: >> I am experiencing an unexpected result when I try to >> run my flask application. >> The movie.html page prints out nothing except those in the

. This appears on my webpage: > >Note that the mail server does not allow (for security reasons) >binary attachments so we lost your image. Cravan, you might find it useful to "View Source" of that page in your browser. You can also use command line tools like "curl" or "wget" to directly fetch the page content. >However, your html files are not in HTML. >I'm not a Flask expert but every time I've used Flask the >html pages have been real HTML. Yours appear to be in some >strange pseudo markup language. It is very common in Flask to write HTML pages using Jinja templates, which is what his examples look like. Of course this adds more complexity, if he forgets to use Jinja to render the content to HTML before returning it. >If this is something unique to Flask then I suspect you will >need to ask on a Flask support page or list. It doesn't seem >to be a Python language related issue at this point. ==? I haven't seen the templates, but your view function should probably retun flas.render_template('some.html', **kwargs), where kwargs will be e.g. your database values that are going to be displayed/{{interpolated}}. From merrickdav at gmail.com Sat Jun 29 02:12:46 2019 From: merrickdav at gmail.com (David Merrick) Date: Sat, 29 Jun 2019 18:12:46 +1200 Subject: [Tutor] Python 3.7 Grids Message-ID: Hi Looking for a way to use the determine the position of a card in a grid using the mouse click event in Python. Code is attached. Unfortunately using Tinkter grids / frames can't determine between the two demo cards. The Relevant code is below def showCardInitial(cardList): cardsToPlay = [] length = len(cardList) fileFaceDown = 'extracards/FACEDOWN.gif' cardFaceDownCard = fileFaceDown.split('/') cardFaceDownCard = cardFaceDownCard[1].split('.')[0] photo = PhotoImage(file=fileFaceDown) number = random.randrange(0,length) fileFaceUp = cardList[number][2] card = fileFaceUp.split('/') card = card[1].split('.')[0] w = Label(image=photo) w.photo = photo w.grid(row = 0,column = 0,padx = 10) #print('cardListis ',cardList) cardFile = cardList[1][2] #print cardFile photo = PhotoImage(file=cardFile) cardToDisplay = Label(image=photo) cardToDisplay.photo = photo cardToDisplay.grid(row=0,column = 1,padx = 10) #w.grid(row = row,column = count) return def determineCard(): global x global y print( "clicked at", x, y) if(2 <= x and x <= 83) and (0 <= y and y <= 130): print('Card One is Selected') return Any suggestions are welcome -- Dave Merrick TutorInvercargill http://tutorinvercargill.co.nz Daves Web Designs Website http://www.daveswebdesigns.co.nz Email merrickdav at gmail.com Ph 03 216 2053 Cell 027 3089 169 From merrickdav at gmail.com Sat Jun 29 01:57:41 2019 From: merrickdav at gmail.com (David Merrick) Date: Sat, 29 Jun 2019 17:57:41 +1200 Subject: [Tutor] Python 3.7 Grids Message-ID: Hi Looking for a way to use the determine the position of a card in a grid using the mouse click event in Python. Code is attached. There are no viruses. Unfortunately using Tinkter grids / frames can't determine between the two demo cards. Any suggestions are welcome -- Dave Merrick TutorInvercargill http://tutorinvercargill.co.nz Daves Web Designs Website http://www.daveswebdesigns.co.nz Email merrickdav at gmail.com Ph 03 216 2053 Cell 027 3089 169 From ingo at ingoogni.nl Sat Jun 29 08:46:53 2019 From: ingo at ingoogni.nl (ingo) Date: Sat, 29 Jun 2019 14:46:53 +0200 Subject: [Tutor] path Message-ID: <97c61f68-1cda-1060-720f-390a584fd064@ingoogni.nl> A user has to type a path in the commandline on Win 10, so just a string. This has to become a path / directory in the file system. Later in the process subdirectories and files are 'appended' by the script. In these files there are also paths, derived from the input path and they have to use forward slashes. The tool that uses these files requires that. # create structure: # /---fullpath <-- input() cmd # | # /--- data # /--- db # | +--- basename.db3 <-- pathlib.Path(...).touch() # | +--- basename.read <-- forward slashes inside # /--- ddl # /--- doc # /--- sql # +--- basename.sublime-project <-- forward slashes inside (json) # +--- _FOSSIL_ And here the misery begins... A short excerpt: Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> inp = "c:\test\drive\this" >>> import pathlib >>> p=pathlib.PurePath(inp) >>> p PureWindowsPath('c:\test/drive\this') >>> print(pathlib.PurePosixPath(p)) c:/ est/drive his >>> inp 'c:\test\\drive\this' >>> import os >>> print(os.path.normpath(inp)) c: est\drive his >>> print(pathlib.Path(inp)) c: est\drive his >>> how to go from a string to a path, how to append to a path (os.path.join or / with Path), how to turn it into 'posix' TIA, Ingo From bgailer at gmail.com Sat Jun 29 09:24:53 2019 From: bgailer at gmail.com (Bob Gailer) Date: Sat, 29 Jun 2019 09:24:53 -0400 Subject: [Tutor] Python 3.7 Grids In-Reply-To: References: Message-ID: On Jun 29, 2019 3:01 AM, "David Merrick" wrote: > > Hi Looking for a way to use the determine the position of a card in a grid > using the mouse click event in Python. Code is attached. Unfortunately this list does not forward attachments. Either give us a link to the code or even better if it's not terribly complicated paste it into the reply. be sure to reply all so everyone on the tutor list will see your reply. > There are no viruses. That's kind of like a Salesman saying "trust me". Bob Gailer From bgailer at gmail.com Sat Jun 29 09:37:19 2019 From: bgailer at gmail.com (Bob Gailer) Date: Sat, 29 Jun 2019 09:37:19 -0400 Subject: [Tutor] [Python-Help] Writing hello world In-Reply-To: References: <64d3f69a-b900-d17d-679e-aa748d0a23ab@python.org> <20190526112415.33e4a02d@fsol> <20190623174820.105019af@fsol> <1561566014.66.708@mint-julep.mondoinfo.com> Message-ID: On Jun 28, 2019 9:26 AM, "Erastus muriithi" wrote: > > Iam a student..iam interested in learning python,,I don't know how to study this python.kindly help me how to go about it..Thankyou First make sure you have python installed on your computer. If you need help with that let us know what kind of computer and operating system you are using. At the python. Org website you will find links to tutorials. Try some of these out. Direct future emails to tutor at python.org. Always reply all so a copy goes back to the list. From mats at wichmann.us Sat Jun 29 09:42:00 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 29 Jun 2019 07:42:00 -0600 Subject: [Tutor] path In-Reply-To: <97c61f68-1cda-1060-720f-390a584fd064@ingoogni.nl> References: <97c61f68-1cda-1060-720f-390a584fd064@ingoogni.nl> Message-ID: <9a1b591a-c4d8-a436-7c86-3403f6b4ec30@wichmann.us> On 6/29/19 6:46 AM, ingo wrote: > A user has to type a path in the commandline on Win 10, so just a > string. > A short excerpt: > > Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 > bit (AMD64)] on win32 > Type "copyright", "credits" or "license()" for more information. >>>> inp = "c:\test\drive\this" >>>> import pathlib >>>> p=pathlib.PurePath(inp) >>>> p > PureWindowsPath('c:\test/drive\this') >>>> print(pathlib.PurePosixPath(p)) > c:/ est/drive his >>>> inp > 'c:\test\\drive\this' >>>> import os >>>> print(os.path.normpath(inp)) > c: est\drive his >>>> print(pathlib.Path(inp)) > c: est\drive his >>>> > > how to go from a string to a path, how to append to a path (os.path.join > or / with Path), how to turn it into 'posix' Most people don't use pathlib, and that's kind of sad, since it tries to mitigate the kinds of questions you just asked. Kudos for trying. For your example, when you define inp as a string, it needs to be a raw string because otherwise Python will interpret the backslash sequences. \t means tab, which is why the the results look mangled. inp = "c:\test\drive\this" If you're going to use pathlib, then may as well use the / operator for joining, From mats at wichmann.us Sat Jun 29 09:52:32 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 29 Jun 2019 07:52:32 -0600 Subject: [Tutor] path In-Reply-To: <9a1b591a-c4d8-a436-7c86-3403f6b4ec30@wichmann.us> References: <97c61f68-1cda-1060-720f-390a584fd064@ingoogni.nl> <9a1b591a-c4d8-a436-7c86-3403f6b4ec30@wichmann.us> Message-ID: <1877a4d9-0f3e-4e9c-b14d-e2e319412c49@www.fastmail.com> Sigh... something dropped my raw string, so that was a really bad sample :( inp = r"c:\test\drive\this" On Sat, Jun 29, 2019, at 07:44, Mats Wichmann wrote: > > For your example, when you define inp as a string, it needs to be a raw > string because otherwise Python will interpret the backslash sequences. > \t means tab, which is why the the results look mangled. > > inp = "c:\test\drive\this" > > From ingo at ingoogni.nl Sat Jun 29 10:33:44 2019 From: ingo at ingoogni.nl (ingo) Date: Sat, 29 Jun 2019 16:33:44 +0200 Subject: [Tutor] path In-Reply-To: <1877a4d9-0f3e-4e9c-b14d-e2e319412c49@www.fastmail.com> References: <97c61f68-1cda-1060-720f-390a584fd064@ingoogni.nl> <9a1b591a-c4d8-a436-7c86-3403f6b4ec30@wichmann.us> <1877a4d9-0f3e-4e9c-b14d-e2e319412c49@www.fastmail.com> Message-ID: <4a12b1c3-d282-a1a9-1c4d-854724ef7447@ingoogni.nl> On 29-6-2019 15:52, Mats Wichmann wrote: > Sigh... something dropped my raw string, so that was a really bad sample :( > > inp = r"c:\test\drive\this" > > > On Sat, Jun 29, 2019, at 07:44, Mats Wichmann wrote: >> >> For your example, when you define inp as a string, it needs to be a raw >> string because otherwise Python will interpret the backslash sequences. >> \t means tab, which is why the the results look mangled. >> >> inp = "c:\test\drive\this" >> import pathlib print('input') a=input() b=r"{}".format(a) #does this make sense to create a r'string'? wa = pathlib.PureWindowsPath(a) wb = pathlib.PureWindowsPath(b) pa = pathlib.PurePosixPath(a) pb = pathlib.PurePosixPath(b) ppa = pathlib.PurePosixPath(wa) ppb = pathlib.PurePosixPath(wb) input c:\test\this\path\ >>> print(a) c:\test\this\path\ >>> print(b) c:\test\this\path\ >>> print(wa) c:\test\this\path >>> print(wb) c:\test\this\path >>> print(pa) c:\test\this\path\ >>> print(pb) c:\test\this\path\ >>> print(ppa) c:\/test/this/path >>> print(ppb) c:\/test/this/path What I'm looking for is c:/test/this/path From ingo at ingoogni.nl Sat Jun 29 10:55:15 2019 From: ingo at ingoogni.nl (ingo) Date: Sat, 29 Jun 2019 16:55:15 +0200 Subject: [Tutor] path In-Reply-To: <4a12b1c3-d282-a1a9-1c4d-854724ef7447@ingoogni.nl> References: <97c61f68-1cda-1060-720f-390a584fd064@ingoogni.nl> <9a1b591a-c4d8-a436-7c86-3403f6b4ec30@wichmann.us> <1877a4d9-0f3e-4e9c-b14d-e2e319412c49@www.fastmail.com> <4a12b1c3-d282-a1a9-1c4d-854724ef7447@ingoogni.nl> Message-ID: On 29-6-2019 16:33, ingo wrote: > > What I'm looking for is c:/test/this/path After further testing, the other tools in the chain accept paths like c:\\test\\dir c:\/test/dir c:/test/dir anything except standard windows, the top two I can generate. Ingo From robertvstepp at gmail.com Sun Jun 30 00:35:55 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 29 Jun 2019 23:35:55 -0500 Subject: [Tutor] Python 3.7 Grids In-Reply-To: References: Message-ID: On Sat, Jun 29, 2019 at 2:02 AM David Merrick wrote: > > Hi Looking for a way to use the determine the position of a card in a grid > using the mouse click event in Python. Code is attached. There are no > viruses. > > Unfortunately using Tinkter grids / frames can't determine between the two > demo cards. As Bob Gailer mentioned this is a text only list which does not allow attachments, so I cannot see what you are actually attempting with your code. But with what you said, two thoughts come to my mind: 1) You can embed each card image on a button widget and if such a button is clicked have the button's callback function respond accordingly. 2) tkinter allows you to know the size of your window and can report back what the cursor's current position is. If you ensure that your cards take up a known coordinate space in the window, when a mouse click event happens you can write code to determine if the cursor fell within either card's known coordinate space. Hope this gives you some idea of how to tackle your problem. -- boB From ingo at ingoogni.nl Sun Jun 30 02:01:15 2019 From: ingo at ingoogni.nl (ingo) Date: Sun, 30 Jun 2019 08:01:15 +0200 Subject: [Tutor] path In-Reply-To: <9a1b591a-c4d8-a436-7c86-3403f6b4ec30@wichmann.us> References: <97c61f68-1cda-1060-720f-390a584fd064@ingoogni.nl> <9a1b591a-c4d8-a436-7c86-3403f6b4ec30@wichmann.us> Message-ID: On 29-6-2019 15:42, Mats Wichmann wrote: > > Most people don't use pathlib, and that's kind of sad, since it tries to > mitigate the kinds of questions you just asked. Kudos for trying. In the end, it works, Ingo ---%<------%<------%<--- # set up some default directories and files # for starting a new project with SQLite # using Sublime + SQLTools. # # /---fullpath # | # /--- data # /--- db # | +--- basename.db3 # | +--- basename.read # /--- ddl # /--- doc # /--- sql # +--- basename.sublime-project # +--- _FOSSIL_ import pathlib import sys # the last bit of the full path is used as the name for the database # c:\newdatabase\test will create the databse # c:\newdatabase\test\db\test.db3 print('enter full path for new db:') fp = pathlib.Path(input()) fn = fp.name # = os.path.basename() dirs = {} for sub in ['', 'data', 'db', 'ddl', 'doc', 'sql']: dirs[sub] = fp / sub try: dirs[sub].mkdir(parents=False, exist_ok=False) except FileExistsError: print(f'Directory already exists: {dirs[sub]}') sys.exit(1) fdb = dirs['db'] / (fn+'.db3') fdb.touch() fr = dirs['db'] / (fn+'.read') fr.write_text(f""" -- template to build db from tables etc -- using dot commands PRAGMA foreign_keys = OFF; --DROP TABLE IF EXISTS sometable; BEGIN TRANSACTION; --.read {(dirs['ddl'] / 'someddl.ddl').as_posix()} COMMIT; PRAGMA temp_store = 2; PRAGMA foreign_keys = ON; PRAGMA journal_mode = WAL; BEGIN TRANSACTION; --.read {(dirs['sql'] / 'somequery.sql').as_posix()} COMMIT; """ ) fsub = dirs[''] / (fn+'.sublime-project') fsub.write_text(f''' {{ "folders":[ {{ "path": "." }} ], "connections":{{ "Connection SQLite":{{ "database": "{fdb.as_posix()}", "encoding": "utf-8", "type": "sqlite" }} }}, "default": "Connection SQLite" }}''' ) # TODO set up fossil in the fp dir From mats at wichmann.us Sun Jun 30 11:01:49 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 30 Jun 2019 09:01:49 -0600 Subject: [Tutor] path In-Reply-To: References: <97c61f68-1cda-1060-720f-390a584fd064@ingoogni.nl> <9a1b591a-c4d8-a436-7c86-3403f6b4ec30@wichmann.us> Message-ID: <1d74dd98-1831-e861-64aa-56b6e80c18b6@wichmann.us> On 6/30/19 12:01 AM, ingo wrote: > > > On 29-6-2019 15:42, Mats Wichmann wrote: >> >> Most people don't use pathlib, and that's kind of sad, since it tries to >> mitigate the kinds of questions you just asked. Kudos for trying. > > In the end, it works, Sounds good. One suggestion - a sort of general programming suggestion really - whenever you take user input, do some kind of validation on it before using it. The context may not be one where anything malicious could happen, but still, just to catch errors.