Re: Getting started with IDLE and Python - no highlighting and no execution
On 06/08/2012 01:58, MRAB wrote: On 06/08/2012 01:09, Rotwang wrote: On 06/08/2012 00:46, PeterSo wrote: I am just starting to learn Python, and I like to use the editor instead of the interactive shell. So I wrote the following little program in IDLE # calculating the mean data1=[49, 66, 24, 98, 37, 64, 98, 27, 56, 93, 68, 78, 22, 25, 11] def mean(data): return sum(data)/len(data) mean(data1) There is no syntax highlighting and when I ran it F5, I got the following in the shell window. >>> RESTART Any ideas? I don't know what editor you're using or how it works, but I'm guessing that pressing f5 runs what you've written as a script, right? In that case the interpreter doesn't automatically print the result of expressions in the same way that the interactive interpreter does; you didn't tell it to print anything, so it didn't. It looks like it's IDLE. Actually, he does say that it's IDLE at the start. [snip] -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter not working
On 01/08/2022 13:17, Daniel Lee wrote: Hello, I my code with tkinter was working before, and now, it has many errors in it. I’m not sure what has happened. The results after running are below: "D:\Python Projects\tes\venv\Scripts\python.exe" "D:/Python Projects/tes/main.py" Traceback (most recent call last): File "D:\Python Projects\tes\main.py", line 1, in import tkinter as tk File "C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 3, in import tkinter.messagebox File "C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\messagebox.py", line 25, in from tkinter.commondialog import Dialog File "C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\commondialog.py", line 13, in from tkinter import Frame, _get_temp_root, _destroy_temp_root ImportError: cannot import name 'Frame' from partially initialized module 'tkinter' (most likely due to a circular import) (C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py) Process finished with exit code 1 The entry: File "C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 3, in import tkinter.messagebox in the traceback does not look right to me. On my PC that file starts with a long docstring. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On 2016-04-19 23:38, Chris Angelico wrote: On Wed, Apr 20, 2016 at 8:29 AM, Seymore4Head wrote: handle = open("\\Winmx\New$\q.txt") for line in handle: line=line.strip() print line Traceback (most recent call last): File "\\Winmx\New$\add viewed.py", line 2, in handle = open("\\Winmx\New$\q.txt") IOError: [Errno 2] No such file or directory: '\\Winmx\\New$\\q.txt' What I would like to do is read a plain text file from a hidden network drive and append a space and the * character to the end of each line. Start with this: print("\\Winmx\New$\q.txt") If that doesn't do what you expect, it's possibly because you want to use a raw string literal to prevent the backslashes from being parsed. handle = open(r"\\Winmx\New$\q.txt") That might help you. (One clue that this is happening is that some of your backslashes got doubled in the error message.) when printed out: >>> print "\\Winmx\New$\q.txt" \Winmx\New$\q.txt That's a file 2 directories down on the current drive. -- https://mail.python.org/mailman/listinfo/python-list
Re: [OT] fortran lib which provide python like data type
On 2015-02-02 02:04, Chris Angelico wrote: On Mon, Feb 2, 2015 at 12:59 PM, Steven D'Aprano wrote: And there are underspecified rules too. What is the plural of octopus? No fair looking it up in the dictionary. Standard and well-known piece of trivia, and there are several options. "Octopodes" is one of the most rigorously formal, but "octopuses" is perfectly acceptable. "Octopi" is technically incorrect, as the -us ending does not derive from the Latin. And the plural of "virus" is "viruses", not "viri" (that's the plural of "vir") or "virii" (that would be the plural of "virius", if it existed). -- https://mail.python.org/mailman/listinfo/python-list
Re: Proper deletion of selected items during map iteration in for loop
On 2014-04-25 18:53, Charles Hixson wrote: What is the proper way to delete selected items during iteration of a map? What I want to do is: for (k, v) in m.items(): if f(k): # do some processing of v and save result elsewhere del m[k] But this gives (as should be expected): RuntimeError: dictionary changed size during iteration In the past I've accumulated the keys to be deleted in a separate list, but this time there are likely to be a large number of them, so is there some better way? The other way is to build a new dictionary. Actually, there's a third way. Iterate over a snapshot: for (k, v) in list(m.items()): if f(k): # do some processing of v and save result elsewhere del m[k] -- https://mail.python.org/mailman/listinfo/python-list
Re: Great Math Mystery
Do you mean Pythonesque or Pythonic?-- https://mail.python.org/mailman/listinfo/python-list
Re: Fwd: Problem with writing a long line in a text file
Steve Holden wrote: Mohsen Akbari wrote: Dear guys, I'm a newbie in python and I have this problem with the code that I'm writing. There is a very long line which I wish to output it to a text file.But when I do this, in the output file, the result appears in two lines. I thought maybe that's because the maximum possible length of the text file has been reached so I opened the file and I tried to put some space at the end of the first line. It wasn't possible to do that for the first line and the space was placed before the first character of the second line. I kept pressing space bar to add more spaces and so the whole second line was moved to the write till it exceeded the first line. I mean the length of the second line became longer than the first line which means the text file can accept longer lines. As a second guess, I thought maybe I had '\n' somewhere in my input string so I tried to check that by doing: temp_ = out_string.find('\n') print temp_ The result was -1 which I think means there is not '\n' in my output string. Now I'm really confused and I don't know what to do. I really appreciate it if somebody can help me out. BTW, I'm using windows. Then, later: --- I use notepad to view my txt file. It appears that way in this tool. --- Here is the output of the debug lines that you mentioned: print *line*.find('\n') -1 print len(*line*) 1528 print repr(line) '0.0\t3.0\t10.0\t24.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t33.0\t40.0\t40.0\t40.0\t40.0\t32.0\t30.0\t32.0\t26.0\t23.0\t30.0\t32.0\t40.0\t40.0\t40.0\t40.0\t40.0\t40.0\t38.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t24.0\t10.0\t11.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t18.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t17.0\t20.0\t23.0\t30.0\t30.0\t30.0\t27.0\t20.0\t20.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t30.0\t23.0\t20.0\t20.0\t20.0\t20.0\t10.0\t10.0\t14.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t27.0\t30.0\t30.0\t27.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t19.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t12.0\t10.0\t13.0\t13.0\t10.0\t2.0\t0.0\t3.0\t5.0\t10.0\t5.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t5.0\t0.0\t2.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t10.0\t16.0\t 20 .0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t10.0\t18.0\t20.0\t20.0\t20.0\t20.0\t20.0\t20.0\t27.0\t30.0\t30.0\t35.0\t40.0\t40.0\t31.0\t20.0\t20.0\t21.0\t30.0\t33.0\t46.0\t50.0\t50.0\t50.0\t48.0\t40.0\t43.0\t50.0\t50.0\t50.0\t56.0\t60.0\t60.0\t60.0\t61.0\t70.0\t70.0\t70.0\t70.0\t75.0\t80.0\t83.0\t90.0\t90.0\t92.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t93.0\t86.0\t80.0\t91.0\t100.0\t100.0\t100.0\t100.0\t98.0\t90.0\t90.0\t90.0\t94.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t100.0\t90.0\t83.0\t87.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t90.0\t' thanks Mohsen There is only one line in your file. Notepad just can't display lines beyond a certain length, is all, so it inserts a line break in the window (but not in the file). Some text editors can display very long lines, others can't and just wrap them when displaying. Notepad can't. -- http://mail.python.org/mailman/listinfo/python-list
Re: KeyboardInterrupt
mattia wrote: Il Thu, 10 Dec 2009 04:56:33 +, Brad Harms ha scritto: On Thu, 10 Dec 2009 00:29:45 +, mattia wrote: Il Wed, 09 Dec 2009 16:19:24 -0800, Jon Clements ha scritto: On Dec 9, 11:53 pm, mattia wrote: Hi all, can you provide me a simple code snippet to interrupt the execution of my program catching the KeyboardInterrupt signal? Thanks, Mattia Errr, normally you can just catch the KeyboardInterrupt exception -- is that what you mean? Jon. Ouch, so the simplest solution is just insert in the 'main' function a try/catch? I believed there was the necessity to create a signal and than attach the KeyboardInterrupt to it... KeyboardInterrupt is just an exception that gets raised when CTLR+C (or the OS's equivalent keyboard combo) gets pressed. It can occur at any point in a script since you never know when the user will press it, which is why you put the try: except KeyboardInterrupt: around as much of your script as possible. The signal that the OS sends to the Python interpreter is irrelevant. Ok, so can you tell me why this simple script doesn't work (i.e. I'm not able to catch the keyboard interrupt)? import time import sys from threading import Thread def do_work(): for _ in range(1000): try: time.sleep(1) print(".", end="") sys.stdout.flush() except KeyboardInterrupt: sys.exit() def go(): threads = [Thread(target=do_work, args=()) for _ in range(2)] for t in threads: t.start() for t in threads: t.join() go() Only the main thread can receive the keyboard interrupt. -- http://mail.python.org/mailman/listinfo/python-list
Re: 3 byte network ordered int, How To ?
mudit tuli wrote: For a single byte, struct.pack(') For two bytes, struct.pack(') what if I want three bytes ? Four bytes and then discard the most-significant byte: struct.pack(')[ : -1] -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
Jeremy wrote: I just profiled one of my Python scripts and discovered that >99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? Thanks, Jeremy -- http://mail.python.org/mailman/listinfo/python-list
Re: syntax
marlowe wrote: I wrote this program, but i have a feeling like there might be a more practical way of writing it. Can someone give me an idea of how to simplify this? Here is an example of the csv file i am using. This program calculates the exponential moving average of the 20 day range. USOtable.csv (full table found at http://ichart.finance.yahoo.com/table.csv?s=USO) Date,Open,High,Low,Close,Volume,Adj Close 2010-01-15,38.97,39.02,38.28,38.40,12615300,38.40 2010-01-14,39.30,39.44,38.88,39.06,8575900,39.06 2010-01-13,39.40,39.71,38.63,39.21,15502700,39.21 2010-01-12,40.07,40.36,39.53,39.63,11960100,39.63 2010-01-11,41.09,41.19,40.46,40.54,8902200,40.54 2010-01-08,40.63,41.17,40.45,40.93,9393500,40.93 2010-01-07,40.87,41.08,40.68,40.72,10012000,40.72 2010-01-06,40.32,41.19,39.89,40.97,19789800,40.97 2010-01-05,40.25,40.45,39.93,40.41,10450200,40.41 test.py import csv reader = open('/prog/USOtable.csv','rb') data = [row for row in csv.reader(reader)] > data = list(csv.reader(reader)) Nvals= [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] > Nvals = [1] * 40 H=float(data[40][2])+float(data[41][2])+float(data[42][2])+float(data [43][2])\ +float(data[44][2])+float(data[45][2])+float(data[46][2])+float(data [47][2])\ +float(data[48][2])+float(data[49][2])+float(data[50][2])+float(data [51][2])\ +float(data[52][2])+float(data[53][2])+float(data[54][2])+float(data [55][2])\ +float(data[56][2])+float(data[57][2])+float(data[58][2])+float(data [59][2])\ +float(data[60][2]) H = sum(float(d[2]) for d in data[40 : 61]) L=float(data[40][3])+float(data[41][3])+float(data[42][3])+float(data [43][3])\ +float(data[44][3])+float(data[45][3])+float(data[46][3])+float(data [47][3])\ +float(data[48][3])+float(data[49][3])+float(data[50][3])+float(data [51][3])\ +float(data[52][3])+float(data[53][3])+float(data[54][3])+float(data [55][3])\ +float(data[56][3])+float(data[57][3])+float(data[58][3])+float(data [59][3])\ +float(data[60][3]) L = sum(float(d[3]) for d in data[40 : 61]) Nvals[39]=(H-L)/2 i=1 while i <=38: high=float(data[39-i][2]) low=float(data[39-i][3]) TR=high-low Nvals[39-i]=(19*Nvals[40-i]+TR)/20 i+=1 Instead of: i=1 while i <=38: ... i+=1 you could use: for i in range(1, 39): ... Note that the start value is inclusive and the end value is exclusive. for value in Nvals: print value -- http://mail.python.org/mailman/listinfo/python-list
Re: EAFP gone wrong
Arnaud Delobelle wrote: Hi all, Hi have a set of classes that represent mathematical objects which can be represented as a string using a 'latex' method (after Knuth's famous typesetting system). As I want to be able to typeset some builtin types as well, I have a generic function, latex(), as follows: def latex(val): try: return val.latex() except AttributeError: if isinstance(val, (tuple, list)): return ", ".join(map(latex, val)) elif isinstance(val, dict): return ", ".join( "%s=%s" % (latex(k), latex(v)) for k, v in sorted(val.iteritems()) ) else: return str(val) It's EAFP and I have used this for a while with no problem. Recently I added a new class for 'n choose r' objects, as follows: class Choose(Expression): def __init__(self, n, r): self.subexprs = n, r self.n = n self.r = r def calc(self, ns=None, calc=calc): return choose(calc(self.n, ns), calc(self.r, ns)) def latex(self): return "{%s \\choose %s}" % (latex(self.n), latex(self.k)) When I create a Choose object and try to get its latex representation, this happens: c = Choose(5, 3) latex(c) '' This puzzled me for a bit: why is it not trying to use the latex() method of the Choose object? I read and reread the definition of the latex() method for a while until I found that there was a typo. Where it says: latex(self.k) it should say: latex(self.r) Thus it triggers an AttributeError, which is exactly the kind of exception that I am catching in the latex() function after trying val.latex(). (Of course I could have caught this by calling c.latex() directly but it's such a short method definition that I couldn't imagine missing the typo!). This means that EAFP made me hide a typo which would have been obviously detected had I LBYLed, i.e. instead of try: return val.latex() except AttributeError: ... do if hasattr(val, 'latex'): return val.latex() else: ... So was it wrong to say it's EAFP in this case? Should I have known to LBYL from the start? How do you decide which one to use? Up to now, I thought it was more or less a matter of taste but now this makes me think that at least LBYL is better than catching AttributeError. Thanks for any guidance. In addition to the other replies, you should've tested the Choose class. :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: obfuscate
Paul Rubin wrote: Gregory Ewing writes: Actually I gather it had a lot to do with the fact that the Germans made some blunders in the way they used the Enigma that seriously compromised its security. There was reportedly a branch of the German forces that used their Enigmas differently, avoiding those mistakes, and the British never managed to crack any of their messages. I think you are thinking of the Kriegsmarine (naval) Enigma. Yes they were more careful with procedures, but the machine was also harder to crack because it had four rotors instead of three. IIRC, the Brits were eventually (1942?) able to capture one by shooting up a German submarine and boarding it to get the machine while the sub was sinking; a British sailor wasn't able to get out in time and drowned during that operation. Getting the rotor settings off the captured unit (they may have had to do it more than once) was enough to get a foothold into the code. My memory is hazy on this by now so I may have some parts wrong, but David Kahn's book "Seizing the Enigma" tells the story (I read it many years ago). A fictionalized version appears in Neil Stephenson's novel "Cryptonomicon". U-559? I think that's the one where Hollywood made a film about it, but portraying it as a purely American action. That didn't go down too well in the UK! -- http://mail.python.org/mailman/listinfo/python-list
Re: Download unnamed web image?
galileo228 wrote: On Feb 16, 9:40 pm, galileo228 wrote: On Feb 16, 8:48 pm, John Bokma wrote: galileo228 writes: Using BeautifulSoup, mechanize, and urllib, I've constructed the following: br.open("http://www.school.edu/students/facebook/";) br.select_form(nr = 1) br.form['fulltextsearch'] = 'msb83' # this searches the facebook for me br.submit() results = br.response().read() soup = BeautifulSoup(results) foo2 = soup.find('td', attrs={'width':'95'}) foo3 = foo2.find('a') foo4 = foo3.find('img', attrs={'src':'deliverImage.cfm?netid=msb83'}) # this just drills down to the line and until this point the program does not return an error save_as = os.path.join('./', msb83 + '.jpg') urllib.urlretrieve(foo4, save_as)> I get the following error msg after running this code: AttributeError: 'NoneType' object has no attribute 'strip' Wild guess, since you didn't provide line numbers, etc. foo4 is None (I also would like to suggest to use more meaningful names) -- John Bokma j3b I thought it was too, and I just doublechecked. It's actually foo3 = foo2.find('a') that is causing the NoneType error. Thoughts? I've now fixed the foo3 issue, and I now know that the problem is with the urllib.urlretrieve line (see above). This is the error msg I get in IDLE: Traceback (most recent call last): File "/Users/Matt/Documents/python/dtest.py", line 59, in urllib.urlretrieve(foo4, save_as) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/urllib.py", line 94, in urlretrieve return _urlopener.retrieve(url, filename, reporthook, data) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/urllib.py", line 226, in retrieve url = unwrap(toBytes(url)) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/urllib.py", line 1033, in unwrap url = url.strip() TypeError: 'NoneType' object is not callable Is this msg being generated because I'm trying to retrieve a url that's not really a file? It's because the URL you're passing in, namely foo4, is None. This is presumably because foo3.find() returns None if it can't find the entry. You checked the value of foo3, but did you check the value of foo4? -- http://mail.python.org/mailman/listinfo/python-list
Re: off topic but please forgive me me and answer
superpollo wrote: Steve Holden ha scritto: superpollo wrote: Patrick Maupin ha scritto: On Apr 2, 2:41 pm, Andreas Waldenburger wrote: While everyone else is mocking you: Can you please elaborate on why you want to know and what kind of problem you're trying to solve with this? Also, don't you think you should have picked a maths forum for this kind of question? Methinks the OP is fluent in the way of choosing newsgroups. According to google, he has posted 6855 messages in 213 groups. http://groups.google.com/groups/profile?enc_user=ul3SQhIYmLD0Oj5Yxp-liP3Vw9uApbyajUBv9M9XLUB2gqkZmQ And I can't speak for anybody else, but I just assumed it was an April Fool's question. I meant to be laughing with the OP, not at him, so sorry if I misunderstood. no no you understood prfectly *but* the thing is i am a regular in an italian language math ng which is haunted by a crackpot who insists that 1/2 * 1/2 cannot be 1/4, "because multiplication means getting bigger", so i took a semi-serious stance and made a few posts as a statistical tentative to "convince" said crackpot that the world is not going crazy (but maybe he is) thanks ps: note that my nickname is not unique, and there are a few people whith the same one... and i didn't ever post using googlegroups If you think you will persuade a crackpot to drop his lunacy by logical argument you are clearly an optimist of the first water. But since I like a challenge (and bearing in mind this is OT so I don't claim to be an expert) you might try first of all persuading him to agree to the commutativity of multiplication (i.e. x * y == y * x for any x and y). If he agrees to that, then get him to agree that x * 1 == x for any x. If he agrees to that he does not, since "you cannot multiply something, and not getting some more of it" ... he is stuck with the latin etimology of "multiply" ("multiplicare" means "increase quantity", like in the fish and bread miracle) Do he also think that division always makes it smaller? What about division by a half? -- http://mail.python.org/mailman/listinfo/python-list
Re: Einstein summation notation
Steven D'Aprano wrote: On Fri, 17 Jul 2009 16:09:03 +0100, MRAB wrote: Python did always have True and False. Oops! I meant "didn't", of course. $ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam True, False Traceback (innermost last): File "", line 1, in ? NameError: True -- http://mail.python.org/mailman/listinfo/python-list
Re: An assessment of the Unicode standard
Kurt Mueller wrote: Am 01.09.2009 um 09:39 schrieb Terry Reedy: But this same problem also extends into monies, nation states, units of measure, etc. There is, of course, an international system of measure. The US is the only major holdout. (I recall Burma, or somesuch, is another.) An interesting proposition would be for the US to adopt the metric system in exchange for the rest of the world adopting simplified basic English as a common language. The SI-system is nearly universally employed. Three principal exceptions are Burma (Myanmar), Liberia, and the United States. The United Kingdom has officially adopted the International System of Units but not with the intention of replacing customary measures entirely. The intention in the UK was to switch to SI over a period of 10 years, starting in 1971, so from then only SI was taught in schools. Earlier this year the EU decided that it wouldn't force the UK to abandon the few remaining uses of the Imperial system; SI is preferred, but Imperial is permitted. The roads are still Imperial, and milk delivered to the door can still use the existing pint bottles, but milk sold in shops is in SI. When I was a student, they told us, that in a couple of years there will be the SI-system only, because most countries accepted it in their laws. So we should adopt it. That was in the early 70ties. Only this year we have to deliver results of technical processes to british and US companies. They still want them in their "crazy outdated" units. The other thing would be the US to adopt a "simplified basic English". I would not be astonished, that british people would state, that they already do :-) -- http://mail.python.org/mailman/listinfo/python-list