Re: [Tutor] regarding checksum
Clayton Kirkwood wrote: > Small problem: > Import zlib > For file in files: > checksum = zlib.adler32(file) > > traceback > checksum = zlib.adler32(file) > TypeError: a bytes-like object is required, not 'str' > > Obvious question, how do I make a bytes-like object. I've read through the > documentation and didn't find a way to do this. A checksum is calculated for a sequence of bytes (numbers in the range 0...255), but there are many ways to translate a string into such a byte sequence. As an example let's convert "mañana" first using utf-8, >>> list("mañana".encode("utf-8")) [109, 97, 195, 177, 97, 110, 97] then latin1: >>> list("mañana".encode("latin-1")) [109, 97, 241, 97, 110, 97] So which sequence should the checksum algorithm choose? Instead of picking one at random it insists on getting bytes and requires the user to decide: >>> zlib.adler32("mañana".encode("utf-8")) 238748531 >>> zlib.adler32("mañana".encode("latin1")) 178062064 However, your for loop > For file in files: > checksum = zlib.adler32(file) suggests that you are interested in the checksum of the files' contents. To get the bytes in the file you have to read the file in binary mode: >>> files = "one", "two" >>> for file in files: ... with open(file, "rb") as f: ... print(zlib.adler32(f.read())) ... 238748531 178062064 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is wrong with my Python program that causes it to run but not give results?
I am pretty sure I installed python3. And, also, matplotlib, scipy, and numpy. If I enter either python or python3, I get the >>> prompt, so I may have both installed. How do I verify which versions of python and numpy, matplotlib and scipy I have installed? I am pretty sure I have matplotlib, scipy, and numpy installed under python3, especially since I don't get an error message when I run the program using python3, but, I don't get any output, either, so something is wrong. Would it be a help if I actually list the python program that I am trying to run? On 10/25/2016 7:50 PM, Alan Gauld via Tutor wrote: On 25/10/16 20:24, Ed Troy wrote: my Ubuntu machine. I created the diode IV curve data as per the article, but I can't seem to get it to run. edward@ubuntu:~$ python LED_model_utf8.py LED_IV.txt Traceback (most recent call last): File "LED_model_utf8.py", line 4, in import matplotlib.pyplot as plt ImportError: No module named matplotlib.pyplot Plain 'python' on Ubuntu usually runs Python v2. It looks like you don;t have Matplotlib inastalled for Python 2. Matplotlib is part of the 3rd party SciPy libraries and not part of the standard python install. Having looked at the web page it seems you need to use python3 and have scipy, numpy and matplotlib packages installed. You should have them in your software centre as python3-scipy, python3-numpy and python3-matplotlib If I type python3 LED_model_utf8.py LED_IV.txt, there is a pause and then I am back to the $ prompt. So, it seems like the program is running, but I am not getting any results That's correct. But without seeing the code and data it's hard to guess but you could at least verify that it runs by editing the python file to include print lines. Go to the end of the file and modify the last segment to look like: if __name__ == "__main__": print("Starting") main() print("Stopping...") The page that contains the file and a description of how it works is: https://leicesterraspberrypi.wordpress.com/projects/modelling-a-diode-for-use-in-spice-simulations/ I see the code but I don't see a path that doesn't print anything... You should see some kind of output. -- RF, Microwave, Antenna, and Analog Design, Development,Simulation, and Research Consulting http://aeroconsult.com Aerospace Consulting LLC P.O. Box 536 Buckingham, Pa. 18912 (215) 345-7184 (215) 345-1309 FAX ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is wrong with my Python program that causes it to run but not give results?
On 26/10/16 04:19, Ed Troy wrote: > I am pretty sure I installed python3. And, also, matplotlib, scipy, and > numpy. If I enter either python or python3, I get the >>> prompt, so I > may have both installed. Yes, that's normal. Ubuntu uses python 2 for some of its utilities. > How do I verify which versions of python and > numpy, matplotlib and scipy I have installed? It looks like you have the libraries for v3 but not for v2. Thats OK because the script you want to run is only suitable for v3. > Would it be a help if I actually list the python program that I am > trying to run? Yes. I'm assuming you just cut n paste the code from the web site but something could have gone wrong with the fpormatting and Python is sensitive to that, so seeing your actual code would be a good idea. Meanwhile... >> Go to the end of the file and modify the last segment >> to look like: >> >> if __name__ == "__main__": >>print("Starting") >>main() >>print("Stopping...") Did you try that? And if so did you see either/both messages when you ran the program under python3? -- 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] What is wrong with my Python program that causes it to run but not give results?
Ed Troy writes: > I am pretty sure I installed python3. And, also, matplotlib, scipy, > and numpy. > How do I verify which versions of python and numpy, matplotlib and > scipy I have installed? The following commandline should list the version of installed python packages required by the script – assuming you have used Ubuntu's deb based package manager to install the scripts dependencies: $ dpkg-query --showformat='${db:Status-Abbrev} ${binary:Package} ${Version}\n' \ --show 'python*' \ | awk '$1 ~ /.i.?/ && $2 ~ /^python3?(-((scipy)|(numpy)|(matplotlib)))?$/' \ | column -t Short explanation: dpkg-query lists packages matching a glob-pattern; awk filters for packages actually installed and limits the by the glob-pattern matched package names further; column prettifies the output. > I am pretty sure I have matplotlib, scipy, and numpy installed under > python3, especially since I don't get an error message when I run the > program using python […] As you have noted yourself the absence of ImportErrors strongly implies that you have the required modules installed – even so: double checking won't hurt. As an aside: because the script starts with a shebang line ("#!") you do not, after you have made it executable, need to specify the interpreter to use, which safes you the trouble of choosing Version 2 or 3 of python (or mistyping the interpreter name) and would also allow the script to be easily called from a directory in your PATH environment variable: $ chmod u+x LED_model_utf8.py $ ./LED_model_utf8.py LED_model_utf8.py LED_IV.txt -- Felix Dietrich ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is wrong with my Python program that causes it to run but not give results?
> Alan Gauld via Tutor writes: >> On 26/10/16 04:19, Ed Troy wrote: >> Would it be a help if I actually list the python program that I am >> trying to run? > > Yes. I'm assuming you just cut n paste the code from the web site but > something could have gone wrong with the formatting and Python is > sensitive to that, so seeing your actual code would be a good idea. Could you also provide a small set of sample data that fails, maybe $ head LED_IV.txt > sample.txt will be enough. -- Felix Dietrich ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is wrong with my Python program that causes it to run but not give results?
I found the problem. Originally, I was getting errors that were related to formatting, apparently. It looked fine to me, but because I had copied it from the web, there were, apparently, hidden characters. In an effort to get rid of errors, I was eliminating some lines. I finally added a line, # -#- coding: utf-8 -*- near the beginning of the file. This is when I got to where I was with it seeming to run if I started the command line with python3 but getting errors if I started with python. I was pretty sure I had all of the required modules installed. But then, when instructed to add some lines at the end for debugging, I found that I had accidentally removed two lines at the end and forgot to add them back in. They were: if __name__=="__main__": main() Once I added them, it runs fine whether I say python LED_model_utf8.py LED_IV.txt or python3 LED_model_utf8.py LED_IV.txt Thanks to everyone. Sorry for the mistake on my part. I think that if I had added the # -#- coding: utf-8 -*- right from the start, the problem never would have happened. Ed On 10/26/2016 10:18 AM, Felix Dietrich wrote: Alan Gauld via Tutor writes: On 26/10/16 04:19, Ed Troy wrote: Would it be a help if I actually list the python program that I am trying to run? Yes. I'm assuming you just cut n paste the code from the web site but something could have gone wrong with the formatting and Python is sensitive to that, so seeing your actual code would be a good idea. Could you also provide a small set of sample data that fails, maybe $ head LED_IV.txt > sample.txt will be enough. -- Felix Dietrich ___ 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
[Tutor] run local script on a remote machine
I've got three files as follows: 1: #!/usr/bin/env python3 # # file: experiment.py # # A simple python program that takes parameters. import sys info = sys.argv[1:] print(info) with open("/home/alex/junk.txt", 'w') as file_object: for item in info: file_object.write(''.join((item,'\n'))) 2: #!/bin/bash # # file: call.sh # Demonstrates running a local python script on another host # with command line arguments specified locally. ssh -p22 alex@10.10.10.10 python3 -u - one two three < /home/alex/Py/BackUp/Sandbox/Scripted/experiment.py 3: #!/usr/bin/env python3 # # file: call.py import os import shlex import subprocess script = "/home/alex/Py/BackUp/Sandbox/Scripted/experiment.py" if os.path.isfile(script): print("File exists on local machine.") else: print("No such file.") command = ( "ssh -p22 alex@10.10.10.10 python3 -u - one two three < {}" .format(script)) ret = subprocess.call(shlex.split(command)) if ret: print("Command failed!!") else: print("All's well.") Running the shell script (2) executes a single shell command and leaves the junk.txt file at 10.10.10.10 as desired. Calling the same shell command using the subprocess module from with in a python script (3) does not work: alex@X301n3:~/Py/BackUp/Sandbox/Scripted$ ./call.py File exists on local machine. bash: /home/alex/Py/BackUp/Sandbox/Scripted/experiment.py: No such file or directory Command failed!! I'm doing this using Ubuntu14.04LTS. Thanks in advance for any suggestions as to how to proceed. Alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] String within a string solution (newbie question)
Hello, I am currently writing a basic program to calculate and display the size of folders with a drive/directory. To do this I am storing each directory in a dict as the key, with the value being the sum of the size of all files in that directories (but not directories). For example: { "C:\\docs" : 10, "C:\\docs123" : 200, "C:\\docs\\code\\snippets" : 5, "C:\\docs\\code" : 20, "C:\\docs\\pics" : 200, "C:\\docs\\code\\python" : 10 } Then to return the total size of a directory I am searching for a string in the key: For example: for "C:\\docs\\code" in key: Which works fine and will return "C:\\docs\\code" : 20, "C:\\docs\\code\\snippets" : 5, "C:\\docs\\code\\python" : 10 = (35) However it fails when I try to calculate the size of a directory such as "C:\\docs", as it also returns "C:\\docs123". I'd be very grateful if anyone could offer any advice on how to correct this. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String within a string solution (newbie question)
On 26/10/16 19:06, Wish Dokta wrote: > folders with a drive/directory. To do this I am storing each directory in a > dict as the key, with the value being the sum of the size of all files in > that directories (but not directories). > > For example: > > for "C:\\docs\\code" in key: > > Which works fine and will return "C:\\docs\\code" : 20, > "C:\\docs\\code\\snippets" : 5, "C:\\docs\\code\\python" : 10 = (35) > > However it fails when I try to calculate the size of a directory such as > "C:\\docs", as it also returns "C:\\docs123". > > I'd be very grateful if anyone could offer any advice on how to correct > this. We can't guess what your code looks like, you need to show us. Post the code and we can maybe 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String within a string solution (newbie question)
On Oct 26, 2016 2:07 PM, "Wish Dokta" wrote: > > Hello, > > I am currently writing a basic program to calculate and display the size of > folders with a drive/directory. To do this I am storing each directory in a > dict as the key, with the value being the sum of the size of all files in > that directories (but not directories). > > For example: > > { "C:\\docs" : 10, "C:\\docs123" : 200, "C:\\docs\\code\\snippets" : 5, > "C:\\docs\\code" : 20, "C:\\docs\\pics" : 200, "C:\\docs\\code\\python" : > 10 } > > Then to return the total size of a directory I am searching for a string in > the key: > > For example: > > for "C:\\docs\\code" in key: Put "\\" at the end of the search string. > > Which works fine and will return "C:\\docs\\code" : 20, > "C:\\docs\\code\\snippets" : 5, "C:\\docs\\code\\python" : 10 = (35) > > However it fails when I try to calculate the size of a directory such as > "C:\\docs", as it also returns "C:\\docs123". > > I'd be very grateful if anyone could offer any advice on how to correct > this. > ___ > 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