Re: [Tutor] Python 3 Combine 1 D Arrays
Stephen P. Molnar wrote: > I'm beginning to think that I don't know how to ask the question as > Google and Stack Overflow have resulted in nothing. All of the results > seem to deal with integers. > > I have a number of single column floating point arrays each containing > 300 entries that I want to combine into a n by 300 array where n is the > number of single column arrays. > > I have tried zip, np.concatenate etc with only failure. > > Thanks in advance. > Googling for numpy combine 1d arrays into 2d array I get http://stackoverflow.com/questions/21322564/numpy-list-of-1d-arrays-to-2d-array as the first hit. Does that help? The second hit http://stackoverflow.com/questions/17710672/create-2-dimensional-array-with-2-one-dimensional-array should also work, and the best approach is probably #3: https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.column_stack.html >>> import numpy as np >>> a = np.array([1.2, 3.4, 5.6]) >>> b = np.array([10.11, 12.13, 14.15]) >>> np.column_stack((a, b)) array([[ 1.2 , 10.11], [ 3.4 , 12.13], [ 5.6 , 14.15]]) No rocket science ;) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help - What is the best package to learn programming in Python?
for IDE, you have pycharm which works well i use Wing IDE personal. Nice for me. Don't forget to check the in-built IDLE tutorialspoint for python 2 or 3 is a nice quick reference. Learning how to do things the python way helps to ease jobs. else, python has become more than an average general purpose lang. It even has an astrophysics library (astropy), as well as data science and image processing libraries. It is used in industries for internal workings and production as well . So, investing in python really pays off ! P.s. Learn it from the begining well. e.g. the print in python also takes optional arguments as well. . . ..what to print _sep _end _std output _flush print('to print', sep=' ', end=' ', file=open('file.txt','w'), flush=true) so, better learn python well ! Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 18 May 2017 03:09, "Alan Gauld via Tutor" wrote: > On 17/05/17 19:17, keith quach wrote: > > > I hope you could help. I am new to the Python community. I am looking > > for your recommendation for a Windows 10 (64 bit) Python 3.6 > > distribution package that covers all the necessary installtions/files. > > It depends on what you want to do. There is no single package I > know of that includes *all* the Python modules available - there > are too many and many are out of sync with different versions. > > If you do scientific/numeric computations you probably want > something like Anaconda or Enthought. If you do media/video > work you might want some of the distros targetting Maya or > similar. > > Of the "standard" distributions I usually recommend the > ActiveState.com distro because it includes some extra > Windows goodies and integrates with the help system better. > > If you need an IDE you will need to check those out separately, > there are at least half a dozen, some free, some commercial. > IDEs are a very personal choice, many Python programmers > prefer not to use one but work with multiple open windows > instead. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do I display a picture?
If all PIL does with .show() is to invoke the system default image viewer, why would it pop an empty console window? the existence of this window causes the script to hang, so i have to so far manually close it before the script would continue to run. On Wed, May 17, 2017 at 6:03 PM, Alan Gauld via Tutor wrote: > On 18/05/17 00:24, Michael C wrote: > > > or to find another way to display the picture without using python image > > library. > > > There are lots of ways it depends on what you actually want > to do with the image. For example you can run your favourite > image viewer program(that looks like what PIL is doing) > > Or you could create a temporary html file with an img tag > pointing at the image, then open your browser on that file. > > Or you can create a simple GUI and put the image into a > canvas component. > > Or you could use a different image editing module like > the ImageMagik module(forgotten the name) or Blender. > > It just depends what you need it for which solution suits. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image Library
when I run this, while it's called test.pyw, this pops up from PIL import Image im = Image.open('1.bmp') im.show() [image: Inline image 1] On Wed, May 17, 2017 at 6:51 PM, eryk sun wrote: > On Wed, May 17, 2017 at 10:33 PM, Michael C > wrote: > > On Wed, May 17, 2017 at 3:30 PM, eryk sun wrote: > > > >> You're probably running a .py script that's associated with py.exe or > >> python.exe. These executables create a new console (i.e. an instance > >> of the console host process, conhost.exe), if they don't inherit one > >> from their parent process. The new console defaults to creating a > >> visible window. Change the file extension to .pyw. This file extension > >> should be associated with pyw.exe or pythonw.exe, which will not > >> create a console. > >> > >> FYI, the cmd shell is unrelated to that console window. Windows users > >> often confuse the cmd shell with the console window that it uses. I > >> suppose it's less confusing on Linux. Like cmd, bash will inherit the > >> parent's terminal/console; however, it doesn't automatically spawn a > >> terminal on Linux if the parent doesn't have one. (Getting that > >> behavior on Windows requires the DETACHED_PROCESS creation flag.) > >> Since Windows users typically run cmd.exe to get a command-line shell > >> for running programs, they associate cmd.exe with the console window. > >> It isn't obvious that other programs create consoles without any > >> associated instance of cmd.exe. > > > > Actually, that is the whole script! I didn't get used to have the cmd.exe > > window pop up at all, could it be something I did? > > Changing the script's extension to .pyw didn't prevent the console > from appearing? Or did you not try it? > > Also, to reiterate, the cmd shell doesn't create or own any window, > and unless something really weird is going on, there's no cmd.exe > instance associated with the console window that you're seeing. cmd > can use a console via standard I/O File handles, and usually does, but > not always. It's no different from python.exe, powershell.exe, or any > other console application. Really, there is no such thing as a "cmd > window", "python window", or "PowerShell window". Because these are > long-running shell processes (e.g. Python's REPL), people are inclined > to think in those terms, and that's fine, but would you call it a > "where.exe window", "chcp.com window", "doskey.exe window", > "whoami.exe window", "findstr.exe window"? I think not. It's clear > that these programs simply use the console; they don't own it. Neither > do shells, but in the case of shells and other long-running console > processes, we're loose with language for convenience -- as long as it > doesn't confuse our understanding of how things really work. > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image Library
On 18/05/17 02:58, Michael C wrote: > when I run this, while it's called test.pyw, this pops up > > from PIL import Image > > im = Image.open('1.bmp') > im.show() One suggestion is to use Pillow instead of PIL. So far as I know PIL is frozen and all new development is on Pillow. It is backwardly compatible although for this case that hardly matters! But it's just possible that this is fixed in Pillow? But if this is all you are using PIL for then its overkill and you would be much better off sing a different approach. But that depends on what you are trying to achieve. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pyuthon 3 Combine 1 D Arrays
On 05/17/2017 06:15 PM, Luis JM Amoreira wrote: Hi, If your arrays have shape (300,1) (single column, 300 rows right?) then M = numpy.hstack((array1, array2)) is an array with shape (300, 2) (300 rows, 2 columns) and M.T is 2 by 300 [shape (2, 300)]. Take an example: In [11]: import numpy as np In [12]: a1=np.random.rand(3,1) In [13]: a1 Out[13]: array([[ 0.09042866], [ 0.63008665], [ 0.99106757]]) In [14]: a2=np.random.rand(3,1) In [15]: np.hstack((a1,a2)) Out[15]: array([[ 0.09042866, 0.99965848], [ 0.63008665, 0.12334957], [ 0.99106757, 0.43502637]]) In [16]: np.hstack((a1,a2)).T Out[16]: array([[ 0.09042866, 0.63008665, 0.99106757], [ 0.99965848, 0.12334957, 0.43502637]]) Hope this helps! ze On 05/17/2017 08:50 PM, Stephen P. Molnar wrote: I'm beginning to think that I don't know how to ask the question as Google and Stack Overflow have resulted in nothing. All of the results seem to deal with integers. I have a number of single column floating point arrays each containing 300 entries that I want to combine into a n by 300 array where n is the number of single column arrays. I have tried zip, np.concatenate etc with only failure. Thanks in advance. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor Thanks for your reply. Here's what I get: Traceback (most recent call last): File "", line 1, in runfile('/home/comp/Apps/Python/Molecular_Transforms/Basic/MolT_app_1.py', wdir='/home/comp/Apps/Python/Molecular_Transforms/Basic') File "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile execfile(filename, namespace) File "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "/home/comp/Apps/Python/Molecular_Transforms/Basic/MolT_app_1.py", line 289, in f.write("\n".join("".join(map(str, x)) for x in (name_I_app))) File "/home/comp/Apps/Python/Molecular_Transforms/Basic/MolT_app_1.py", line 289, in f.write("\n".join("".join(map(str, x)) for x in (name_I_app))) TypeError: 'numpy.float64' object is not iterable However, numpy.column_stack works. Problem solved. -- Stephen P. Molnar, Ph.D.Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image Library
Oh I have been using Pillow 4.0 the whole time alright, sorry I forgot to mention it. On Thu, May 18, 2017 at 1:55 AM, Alan Gauld via Tutor wrote: > On 18/05/17 02:58, Michael C wrote: > > when I run this, while it's called test.pyw, this pops up > > > > from PIL import Image > > > > im = Image.open('1.bmp') > > im.show() > > One suggestion is to use Pillow instead of PIL. > So far as I know PIL is frozen and all new development > is on Pillow. It is backwardly compatible although for > this case that hardly matters! But it's just possible > that this is fixed in Pillow? > > But if this is all you are using PIL for then its > overkill and you would be much better off sing a > different approach. But that depends on what you > are trying to achieve. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image Library
Did you go into the source code of PIL/Pillow? Awesome!!! On Wed, May 17, 2017 at 7:52 PM, eryk sun wrote: > On Thu, May 18, 2017 at 1:58 AM, Michael C > wrote: > > when I run this, while it's called test.pyw, this pops up > > > > from PIL import Image > > > > im = Image.open('1.bmp') > > im.show() > > Ok, I stand corrected. It's something weird, and most likely due to > Windows support here being an afterthought throw in just for coverage, > instead of something anyone actually cared about. The code does indeed > use os.system to show the file, which is just about the worst thing > one can do here for a Windows viewer. > > >>> print(inspect.getsource(ImageShow.WindowsViewer)) > class WindowsViewer(Viewer): > format = "BMP" > > def get_command(self, file, **options): > return ('start "Pillow" /WAIT "%s" ' > '&& ping -n 2 127.0.0.1 >NUL ' > '&& del /f "%s"' % (file, file)) > > >>> print(inspect.getsource(ImageShow.Viewer.show_file)) > def show_file(self, file, **options): > """Display given file""" > os.system(self.get_command(file, **options)) > return 1 > > The WindowsViewer class preferrably should override show_file to call > ShellExecuteExW directly via ctypes, and wait (if possible) to delete > the temporary file. Or at least use subprocess.call with shell=True, > which will hide the console window. Note that being able to wait on > the image viewer is not guaranteed. For example, the image viewer in > Windows 10 is an app that cannot be waited on. > > Since you already have a file on disk, you could skip PIL completely. > Just use os.startfile('1.bmp'). > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image Library
os.startfile('1.bmp') works like a charm! Now I need to figure out how to close this window once I finish with it! On Thu, May 18, 2017 at 8:14 AM, Michael C wrote: > Oh I have been using Pillow 4.0 the whole time alright, sorry I forgot to > mention it. > > On Thu, May 18, 2017 at 1:55 AM, Alan Gauld via Tutor > wrote: > >> On 18/05/17 02:58, Michael C wrote: >> > when I run this, while it's called test.pyw, this pops up >> > >> > from PIL import Image >> > >> > im = Image.open('1.bmp') >> > im.show() >> >> One suggestion is to use Pillow instead of PIL. >> So far as I know PIL is frozen and all new development >> is on Pillow. It is backwardly compatible although for >> this case that hardly matters! But it's just possible >> that this is fixed in Pillow? >> >> But if this is all you are using PIL for then its >> overkill and you would be much better off sing a >> different approach. But that depends on what you >> are trying to achieve. >> >> -- >> Alan G >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> http://www.amazon.com/author/alan_gauld >> Follow my photo-blog on Flickr at: >> http://www.flickr.com/photos/alangauldphotos >> >> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image Library
On 18/05/17 16:43, Michael C wrote: > os.startfile('1.bmp') > > works like a charm! > > Now I need to figure out how to close this window once I finish with it! Sadly that is manual. Unless you care to write code to search for the process and use the Windows API to kill it off. If you really want the image under your programs control you need to build a GUI, even if a very basic one and display the image in a window. A very simplistic Tkinter app will do for simply splatting an image on screen then closing it later. But it all depends what else you need the app to do how complex it gets after that. Here is some untested Tkinter code to display an image for 2 seconds: import tkinter as tk top = tk.Tk() tk.Label(top, image='1.bmp').pack() top.after(2000, top.quit) top.mainloop() If not that, something quite like it... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] deleting elements of a dictionary
I am trying to remove incorrect entries of my dictionary. I have multiple keys for the same value, ex, [111]:[5] [222]:[5] [333]:[5} and I have found out that some key:value pairs are incorrect, and the best thing to do is to delete all entries who value is 5. So this is what I am doing: import numpy read_dictionary = numpy.load('loc_string_dictionary.npy').item() for n in read_dictionary: print(read_dictionary[n]) if read_dictionary[n] == '5': del read_dictionary[n] However, I get this error: RuntimeError: dictionary changed size during iteration and I can see why. What's the better thing to do? thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image Library
I'll use it when I get to it! Thanks! For now, I use this, as suggested by eryk sun: os.startfile('1.bmp') it doesn't pop the window. Thanks Alan! On Thu, May 18, 2017 at 10:06 AM, Alan Gauld via Tutor wrote: > On 18/05/17 16:43, Michael C wrote: > > os.startfile('1.bmp') > > > > works like a charm! > > > > Now I need to figure out how to close this window once I finish with it! > > Sadly that is manual. Unless you care to write code to search for the > process and use the Windows API to kill it off. > > If you really want the image under your programs control you > need to build a GUI, even if a very basic one and display > the image in a window. A very simplistic Tkinter app will > do for simply splatting an image on screen then closing > it later. But it all depends what else you need the app to > do how complex it gets after that. > > Here is some untested Tkinter code to display an image > for 2 seconds: > > import tkinter as tk > > top = tk.Tk() > tk.Label(top, image='1.bmp').pack() > top.after(2000, top.quit) > > top.mainloop() > > If not that, something quite like it... > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
Michael C wrote: > I am trying to remove incorrect entries of my dictionary. > I have multiple keys for the same value, > > ex, > [111]:[5] > [222]:[5] > [333]:[5} > > and I have found out that some key:value pairs are incorrect, and the best > thing to do > is to delete all entries who value is 5. So this is what I am doing: > > import numpy > read_dictionary = numpy.load('loc_string_dictionary.npy').item() > > for n in read_dictionary: > print(read_dictionary[n]) > if read_dictionary[n] == '5': > del read_dictionary[n] > > > However, I get this error: > RuntimeError: dictionary changed size during iteration > > and I can see why. > > What's the better thing to do? You can copy the keys into a list: for n in list(read_dictionary): > print(read_dictionary[n]) > if read_dictionary[n] == '5': > del read_dictionary[n] As the list doesn't change size during iteration there'll be no error or skipped key aka list item. If there are only a few items to delete build a list of keys and delete the dict entries in a secend pass: delenda = [k for k, v in read_dictionary.items() if v == "5"] for k in delenda: del read_dictionary[k] If there are many items to delete or you just want to default to the most idiomatic solution put the pairs you want to keep into a new dict: read_dictionary = {k: v for k, v in read_dictionary.items() if v != "5"} ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Collecting output from Python scripts executed via Cron
I have written a several Python scripts to collect data from external sources (an email account and an sftp site). In development I run the scripts from IDLE or the command line and can view the output of various print statements in the scripts which helps me to monitor progress and confirm correct operation. However, in production I will be running the scripts via Cron. Is there a recommended/ elegant way to collect this output on the fly for later review/ processing. Previously I have written bash scripts simply redirecting the standard output to a text file and emailed this back to myself but I would like to do this directly within Python rather than having to wrap the Python script in a bash script. Thanks, Leo. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor