Re: Comparing lists
To take the heat out of the discussion: sets are blazingly fast. -- http://mail.python.org/mailman/listinfo/python-list
Re: array subset could be improved? -repost ;)
Jim O'D wrote: > Hi all > > I have an array a=array([2,3,-1]). > > I want to extract an array with all the elements of a that are less than 0. > > Method 1. > new = array([i for i in a if i < 0]) > > Method 2. > new = a[nonzero(a<0)] > > I'm using Numeric arrays but can't seem to find a function that does this. > > Am I missing a more obvious way to do it quickly? > > Thanks > > Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: array subset could be improved? -repost ;)
Jim O'D wrote: > Hi all > > I have an array a=array([2,3,-1]). > > I want to extract an array with all the elements of a that are less than 0. > > Method 1. > new = array([i for i in a if i < 0]) > > Method 2. > new = a[nonzero(a<0)] > > I'm using Numeric arrays but can't seem to find a function that does this. > > Am I missing a more obvious way to do it quickly? > > Thanks > > Jim a2=Numeric.compress(a<0,a) -- http://mail.python.org/mailman/listinfo/python-list
Automatic language translation
Does python have a module that will translate between different spoken languages? My python program displays all of its messages in English currently and my boss wants it to default to Korean now. Any ideas how to go about doing this? -- http://mail.python.org/mailman/listinfo/python-list
[Newby] question about modules
Hi, The following four lines of code: import sys, os, re sentence = raw_input("Enter a sentence: ") capwords (sentence) print sentence gives me the following error: NameError: name 'capwords' is not defined As far as I can tell from the online docs, "capwords" should be defined in the built-in "regex" module. Why is it telling me that capwords is not defined? I am completely new to Python so my apologies for such a basic question! Thanks, Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: question about modules
Hi Jeff, That makes sense -- thanks. However now when I use "re.capwords (sentence)" I get a different error message: AttributeError: 'module' object has no attribute 'capwords' Each of the other two suggested implimentations produce a similar error message. Is there something even more basic that I am failing to do? I'm using the IDLE GUI in WinXP, Python release 2.4... Thanks! Jon "Jeffrey Maitland" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Jon writes: > > > Hi, > > > > The following four lines of code: > > > > import sys, os, re > > sentence = raw_input("Enter a sentence: ") > > capwords (sentence) > > print sentence > > > > gives me the following error: NameError: name 'capwords' is not defined > > > > As far as I can tell from the online docs, "capwords" should be defined in > > the built-in "regex" module. Why is it telling me that capwords is not > > defined? > > > > I am completely new to Python so my apologies for such a basic question! > > > > Thanks, > > Jon > > > > > > -- > > http://mail.python.org/mailman/listinfo/python-list > > Hello Jon, > > The reason for that is you only imported the module(class/object) now to use > its methodes you need to call the object. > > in this case to fix the problem you are having you would have to do it > either one of these methodes (unless there are more that I am not aware of) > 1/ > import sys, os, re > sentence = raw_input("Enter a sentence: ") > re.capwords (sentence) # <-- notice the re.capwords this > # calling the capword method of the re module. > print sentence > > 2/ > import sys, os > from re import * # < import all methods of re and allow them to be > # used locally > sentence = raw_input("Enter a sentence: ") > capwords (sentence) > print sentence > # this imports all the methodes in the re module so they can be used > # localy. > > or > import sys, os > from re import capwords # < import only capwords method of re > sentence = raw_input("Enter a sentence: ") > capwords (sentence) > print sentence > # this import only imports the capwords methode of the re module > > > I hope this helps you some and if i used the incorrect terminology I am > sorry and I hope someone points it out to me. > > Jeff Maitland -- http://mail.python.org/mailman/listinfo/python-list
Re: exec a string in an embedded environment
Tommy, same question to you... :-) -Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: void * C array to a Numpy array using Swig
Krish, In case you find a good solution, I am also looking for one! For now I essentially use helper functions on the c side which wrap in SWIG to return the data as a string in python. That string can then be converted to a numpy array using the fromstring function. This is inefficient as it does an unnecessary copy but avoids dependence on numeric versus numarray etc. It uses the cstring thing in SWIG (see the manual). The library I am wrapping does not have an image struct, but returns the data into memory that the user has to malloc. In the swig file I have something like this, which I've simplified to try to get to the point. It assumes you have two c functions which take a pointer to your struct as argument, the first returns the size of the data (what to malloc), the second copies the data into your memory where a pointer to the memory location was second arg. Doubtless I've introduced typos below, but hopefully you get the idea? Good luck, Jon --- typedef struct { stuff/* I don't know or care what is in here */ } imagefilestruct; %extend imagefilestruct { [... snip constructor destructor other functions etc] %cstring_output_allocate_size( char ** s, int *slen, free(*$1)) get_data ; void get_data(char **s, int *slen){ void * array; size_t size; size = libraryfunction_get_size(self); array=malloc(size)); libraryfunc_get_data(self, array); *slen = size; *s = (char *) array; } } -- http://mail.python.org/mailman/listinfo/python-list
Re: Installing a Windows Printer
Hi D, I would suggest that you look here http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/prntspol_7mgj.asp at AddPort and and AddPrinter. Though I have not tried to use them in python, I would assume that using win32com [http://www.python.net/crew/mhammond/win32/Downloads.html] or the like you'd be able to make use of those functions. Jon D wrote: > I would like to create a script for Windows 2000 that will create a > Standard TCP/IP printer port and install a printer (I have the > applicable printer drivers needed for the install on a network share). > My plan is to use py2exe and distribute (also via network share) the > script so that administrators, or even users, can easily install the > printer. Is this possible? If so, I would appreciate it if someone > could steer me in the right direction in terms of how to begin (i.e. > libraries needed, sample code). Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: variable creation
Hi, I'm not sure if this is exactly what you had in mind but what about something like this: elements = [] Els = "" pt = {'H': 1.00794, 'He': 4.002602, 'Li': 6.941, 'Be': 9.012182, 'B':10.811} while Els != 'No': Els = raw_input("""Are there any further elements you would like to include? if so type the element, eg, Pd. If not type No:""") if pt.has_key(Els): elements.append({Els:pt[Els]}) print elements Alistair King wrote: > Hei all, > > im trying to create a list of variables for further use: > > > Els = raw_input("Are there any further elements you would like to > include? if so type the element, eg, Pd. If not type No: ") > > if Els != 'No': > el = Els in pt > while el == 1 and Els != 'Next': > elements = [] > elements.insert(-1, Els) > > Els = raw_input("Which further element would you like to > include, eg, Pd, if not type Next: ") > el = Els in pt > > while el == 0 and Els != 'Next': > Els = raw_input("This is not an element in the periodic > table, please try again, eg Pd, If you dont wish to include any more, > type Next: ") > if el == 1: > elements.insert(-1, Els) > > while el == 0: > Els = raw_input("This is not an element in the periodic > table, please try again, eg Pd. If you dont wish to include any more, > type Next: ") > > print elements > > > this works to a certain extent but gets stuck on some loop. Im a > beginner and am not sure where im going wrong. > > here is a portion of the dictionary containing typical elements: > > pt = {'H': 1.00794, 'He': 4.002602, 'Li': 6.941, 'Be': 9.012182, 'B': > 10.811} > > > Ali > > > -- > Dr. Alistair King > Research Chemist, > Laboratory of Organic Chemistry, > Department of Chemistry, > Faculty of Science > P.O. Box 55 (A.I. Virtasen aukio 1) > FIN-00014 University of Helsinki > Tel. +358 9 191 50429, Mobile +358 (0)50 5279446 > Fax +358 9 191 50366 -- http://mail.python.org/mailman/listinfo/python-list
Re: Open file handles?
Perhaps using os you could work with lsof [http://www.linuxcommand.org/man_pages/lsof8.html] Jon Thomas Bartkus wrote: > This may be more of a Linux question, but I'm doing this from Python. . > > How can I know if anything (I don't care who or what!) is in the middle of > using a particular file? > > This comes in context of needing to copy a file BUT only if I can verify > that something else doesn't have an open write handle to that file. IOW - I > need to decline the operation if something else hasn't finished writing to > the file. > > How can I know? > Thomas Bartkus -- http://mail.python.org/mailman/listinfo/python-list
import confused by contents of working directory
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made the mistake of running a test program from the same directory where I built and installed my package. Python uses the package from the current directory, which misses the pyd files, instead of the site-packages package which has them installed. Would somebody please explain how to know what is going on when an import statement is ambiguous? If the interpreter has several choices about what to import then which one is chosen? I apologise if this is FAQ, but it seems that if I say "import mymodule" and the interpreter finds more than one option for mymodule it could raise an Exception asking "which one do you want??" instead of silently picking up the first thing it finds? This would be perhaps a major change - but I expect "import" to go look in the python installation and would look for a command like "load" to go looking for specific filenames or find things that are hanging around in the current working directory. Is there any plan for a statement like: "from future import PleaseDontJustGuessWhatToImport" ? Thanks in advance for any useful advice, perhaps I am just missing the out on the "right way to do it"? It is normal to remove '.' from sys.path at the start of a script? Jon - Some examples of my confusion are outlined below. I use Numeric only because I assume they are doing a proper installation. I had thought for a long time that I had this problem because I was screwing up with distutils or .pth files etc etc. c:\> mkdir import c:\> cd import c:\import> python -c "import Numeric; print 'OK!'" OK! c:\import> echo raise Exception("Not OK! Local py file") > Numeric.py c:\import> python -c "import Numeric;print 'OK!'" Traceback (most recent call last): File "", line 1, in ? File "Numeric.py", line 1, in ? raise Exception("Not OK! Local py file") Exception: Not OK! Local py file c:\import> del Numeric.py c:\import> mkdir Numeric c:\import> python -c "import Numeric; print 'OK!'" OK! c:\import> echo raise Exception("Not OK! Local version") > Numeric\__init__.py c:\import> python -c "import Numeric;print 'OK!'" Traceback (most recent call last): File "", line 1, in ? File "Numeric\__init__.py", line 1, in ? raise Exception("Not OK! Local version") Exception: Not OK! Local version c:\import> del Numeric\__init__.py c:\import>python -c "import Numeric;print 'OK!'" Traceback (most recent call last): File "", line 1, in ? File "c:\python24\lib\site-packages\PIL\__init__.py", line 1, in ? # Exception: Not OK! Local version [authors note] What??? This was imported from Numeric\__init__.pyc c:\import> del Numeric\__init__.pyc c:\import> python -c "import Numeric;print 'OK!'" OK! The the actual example is more like this: c:\ImageD11> python setup.py build --compiler=mingw32 install ... c:\ImageD11>c:\python24\scripts\ImageD11_gui.py ... ImportError: can't import name closest c:\ImageD11> cd .. c:\> c:\python24\scripts\ImageD11_gui.py ...and the gui runs!! ... Given that the script resides in $python\scripts I sort of expect it to avoid tripping over some local and irrelevant file. -- http://mail.python.org/mailman/listinfo/python-list
pyserial port connection problem
Hi, I wrote some code to read in info from a port using pyserial. the code reads info sent by a box that is connected to my computer by an rs232-to usb adapter. When I was writing the code and testing it on my computer it worked fine. I ran py2exe on the program (which uses wxpython for its gui) and sent the output from the py2exe to another computer. now when I try to run it on this other computer it fails to open the port. it gives the error that it cannot createfile. the code I'm using to connect is the same as it is when it worked on my computer. I do notice that pyserial sees this port on the other computer as .\\COM14 - while windows device manager displays it as COM14. On the other computer the data is properly displayed in Hyperterminal when opened on COM14. how can I resolve this issue? thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Linear regression in NumPy
> I have a question about the linear_least_squares in Numpy. Not quite sure what is going on, it looks like there could be some confusion as to linear_least_squares is expecting as an argument of some Numeric arrays and what you are supplying (a Matrix) is perhaps not close enough to being the same thing. Up-to-date versions for all this are all in "numpy" nowadays and the numpy mailing list is perhaps a better place to ask: http://projects.scipy.org/mailman/listinfo/numpy-discussion Anyway, I've pasted in an example using Numeric and LinearAlgebra, which you seem to have on your system. They still work fine. Not sure what the "Matrix" package is that you are using? HTH, Jon example: C:\>python Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from Numeric import * >>> from LinearAlgebra import linear_least_squares >>> a=array( [[1, 1], [1, 2], [1, 3]] ) >>> y=array( [ 1,2,4] ) # note 1 D >>> a array([[1, 1], [1, 2], [1, 3]]) >>> y array([1, 2, 4]) >>> linear_least_squares(a,y) (array([-0.6667, 1.5 ]), array([ 0.1667]), 2, array([ 4.07914333, 0.60049122])) >>> # Is this what you expect as output??? Jianzhong Liu wrote: > Hello, Guys, > > I have a question about the linear_least_squares in Numpy. > > My linear_least_squares cannot give me the results. > > I use Numpy1.0. The newest version. So I checked online and get your > guys some examples. > > I did like this. > > [EMAIL PROTECTED] 77] ~ >> py > Python 2.4.3 (#1, May 18 2006, 07:40:45) > [GCC 3.3.3 (cygwin special)] on cygwin > Type "help", "copyright", "credits" or "license" for more information. > >>> from Numeric import * > >>> from LinearAlgebra import linear_least_squares > >>> from Matrix import * > >>> y = Matrix([[1], [2], [4]]) > >>> x = Matrix([[1, 1], [1, 2], [1, 3]]) > >>> print y > Matrix([[1], >[2], >[4]]) > >>> x > Matrix([[1, 1], >[1, 2], >[1, 3]]) > >>> print linear_least_squares(x, y) > > Here my Numpy stops. and never give me a result. THis is the problem of > Numpy or sth wrong. > > Can you guys give me a LinearAlgebra.py so that I can have a try? > > Thanks, > > John -- http://mail.python.org/mailman/listinfo/python-list
Re: plot dendrogram with python
> does anyone know if there is a way to plot a dendrogram with python. > Pylab or matplotlib do not provide such a function. This makes a datafile for gnuplot using output from pycluster. I'd be interested to see something like this added to pylab/matplotlib, although I don't have time myself. Not very elegant, but someone can probably transform it to the three line recursion which escapes me. Best, Jon import Numeric from Pycluster import treecluster dist = Numeric.zeros((10,10),Numeric.Float) for i in range(dist.shape[0]): dist[i:,i:]=i tree , dist = treecluster(distancematrix=dist,method='a') tree=tree.tolist() base = [] line = [] names = range(dist.shape[0]+1) def f(i,tree,names,spos): height=dist[spos] if i>=0: try: base.append(names[i]) except: print i x=len(base) line.append((x,0)) line.append((x,height)) line.append(("#","#")) else: cluster = tree[-i-1] h1,x1=f(cluster[0],tree,names,-i-1) h2,x2=f(cluster[1],tree,names,-i-1) x=(x1+x2)/2.0 if h1==h2: # tie line.append((x1,h1)) line.append((x2,h2)) line.append(("#","#")) line.append((x,height)) line.append((x,h1)) line.append(("#","#")) else: raise Exception("Whoops") tree[-i-1].append((x,h1)) return height,x h1,x1 = f(tree[-1][0],tree,names,len(tree)-1) h2,x2 = f(tree[-1][1],tree,names,len(tree)-1) x=(x1+x2)/2.0 height = dist[-1] tree[-1].append((x,h1)) if h1==h2: # tie line.append((x1,h1)) line.append((x2,h2)) line.append(("#","#")) line.append((x,height)) line.append((x,h1)) line.append(("#","#")) else: raise Exception("Whoops") # print base d=open("dend.dat","w") # make a tree diagram for point in line: if point[0]!="#": print >> d, point[0],point[1] else: print >> d print >> d d.close() # # os.system("gnuplot") # """plot "dend.dat" u 1:2 w l""" -- http://mail.python.org/mailman/listinfo/python-list
Cheeseshop needs mirrors
I'm a frequent helper in the IRC channel for the Pylons web framework. Pylons is installed from eggs using easy_install, and when Cheeseshop is down (or so slow it might as well be down), it gives a bad impression of our framework and Python in general. It took us half an hour to figure out how to bootstrap setuptools onto one person's machine, since ez_install.py sources from the cheeseshop, and even when we got that installed, bootstrapping the rest of Pylons was very troublesome. If something as widely used as eggs is going to have a single point of failure like the Cheeseshop, we can't have it going down. I'm sure there are other solutions, but my suggestion is simply to have mirrors. It works for Debian, after all. -- http://mail.python.org/mailman/listinfo/python-list
Re: Requirements For A Visualization Software System For 2010
Go for it! I can see immediate application in displaying and exploring multivariate projections. e.g., factor analyses and components analysis, multivariate groupings superimposed on projected hyperspaces... This is stuff some of us have been dreaming about for a couple of decades, and getting there from primitives has been (ahem) "fun." To be able to express this and see results using a higher level language would be really important to a lot of research exploration. -- jondr -- http://mail.python.org/mailman/listinfo/python-list
HTTPS GET request.
Hi, I'm testing an application that sends an HTTPS GET request in the form of: https://localhost/cgi-bin/parse_eas.cgi?vers=2+"&msg=This+is+a+simple+%26+short+test. I need to get a hold of that entire request for comparison / verification purposes. The closet thing I found is Sebastien Martini's Simple HTTP server supporting SSL recipe (http://aspn.activestate.com/ASPN/Cookbook/ Python/Recipe/442473). In this recipe Sebastien created a SecureHTTPRequestHandler by extending the SimpleHTTPRequestHandler class. The SimpleHTTPRequestHandler extends the BaseHTTPRequestHandler that references an internal instance "request" variable. If getting access to that request variable is the right way to go to get hold of the entire GET request, can someone suggest a way (or a web resource for me to learn how) to do that (since I'm relatively new to Python). Or is there a better way? Many thanks! - Jon -- http://mail.python.org/mailman/listinfo/python-list
idiom to ask if you are on 32 or 64 bit linux platform?
Hello everyone, I've got a ctypes wrapper to some code which seems to be different when compiled on 32 bit linux compared to 64 bit linux. For the windows version I can use sys.platform == 'win32' versus 'linux2' to decide whether to get the .dll or .so library to load. Having narrowed it down to linux I still need to choose between libKLT32.so and libKLT64.so Can someone tell me an idiom to choose the right one? Thanks! Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: idiom to ask if you are on 32 or 64 bit linux platform?
Christian Heimes wrote: > You can check the size of a void pointer with ctypes: >>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 And Matt Nordhoff wrote: >>> import platform >>> platform.architecture() Thanks guys! Exactly what I was after. -Jon -- http://mail.python.org/mailman/listinfo/python-list
How to use buildout with a scripts directory?
Hello, I am trying to use buildout and am having trouble to figure out how to get it to install the scripts that come with a package. They are present inside the egg which is installed by buildout, but they don't end up in the bin directory. After running buildout my bin directory is empty, but I can find: .\eggs\imaged11-1.2.3-py2.5-win32.egg\EGG-INFO\scripts\peaksearch.py In the package I have a directory (ImageD11) with the library stuff in it and a directory named "scripts" with the scripts. When I use distutils or setuptools it does what I expected: setup.py: ... setup(name='ImageD11', ..., packages = ["ImageD11"], package_dir = {"ImageD11":"ImageD11"}, scripts = [ "scripts/peaksearch.py", "scripts/bgmaker.py", ...] ) c:\testImageD11distutils\> python setup.py install ... Installing peaksearch.py script to c:\python25\Scripts Installing bgmaker.py script to c:\python25\Scripts Now I am wondering how to persuade "buildout" to do the same thing, but place those scripts into it's own little area. In buildout.cfg I have: == [buildout] parts = ImageD11 [ImageD11] recipe = zc.recipe.egg:scripts scripts = scripts/peaksearch.py eggs = ImageD11 version = 1.2.3 It seems to acknowledge that scripts/peaksearch.py is requested, but doesn't seem to do anything about it. My scripts mostly just map command line arguments to function calls, something like this: if __name__=="__main__": import ImageD11.something, sys ImageD11.something.do_something_interesting( sys.argv[1], sys.argv [2] ) I've read the documentation at http://pypi.python.org/pypi/zc.buildout several times but I still can't figure out how to make these scripts part of the build. There seems to be a lot of talk about entry_points, but I'm blocked on those as to what is the entry point for an if __name__=="__main__": idiom? Thanks in advance for any help! Jon -- http://mail.python.org/mailman/listinfo/python-list
Progressive download in urllib2
I would like to be able to control the (stop/resume) the download of a large http object when using urllib2:urlopen() in Linux My understanding is that the open reads data until the socket buffers are filled, then waits until data is consumed (urllib2.read()) and fetches more. Is there a way to set the size of the buffer pool and/or effect flow control? -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing a serial stream too slowly
On Jan 23, 9:48 pm, "M.Pekala" wrote: > Hello, I am having some trouble with a serial stream on a project I am > working on. I have an external board that is attached to a set of > sensors. The board polls the sensors, filters them, formats the > values, and sends the formatted values over a serial bus. The serial > stream comes out like $A1234$$B-10$$C987$, where "$A.*$" is a sensor > value, "$B.*$" is a sensor value, "$C.*$" is a sensor value, ect... > > When one sensor is running my python script grabs the data just fine, > removes the formatting, and throws it into a text control box. However > when 3 or more sensors are running, I get output like the following: > > Sensor 1: 373 > Sensor 2: 112$$M-160$G373 > Sensor 3: 763$$A892$ > > I am fairly certain this means that my code is running too slow to > catch all the '$' markers. Below is the snippet of code I believe is > the cause of this problem... > > def OnSerialRead(self, event): > text = event.data > self.sensorabuffer = self.sensorabuffer + text > self.sensorbbuffer = self.sensorbbuffer + text > self.sensorcbuffer = self.sensorcbuffer + text > > if sensoraenable: > sensorresult = re.search(r'\$A.*\$.*', self.sensorabuffer ) > if sensorresult: > s = sensorresult.group(0) > s = s[2:-1] > if self.sensor_enable_chkbox.GetValue(): > self.SensorAValue = s > self.sensorabuffer = '' > > if sensorbenable: > sensorresult = re.search(r'\$A.*\$.*', self.sensorbenable) > if sensorresult: > s = sensorresult.group(0) > s = s[2:-1] > if self.sensor_enable_chkbox.GetValue(): > self.SensorBValue = s > self.sensorbenable= '' > > if sensorcenable: > sensorresult = re.search(r'\$A.*\$.*', self.sensorcenable) > if sensorresult: > s = sensorresult.group(0) > s = s[2:-1] > if self.sensor_enable_chkbox.GetValue(): > self.SensorCValue = s > self.sensorcenable= '' > > self.DisplaySensorReadings() > > I think that regex is too slow for this operation, but I'm uncertain > of another method in python that could be faster. A little help would > be appreciated. You sure that's your code? Your re.search()'s are all the same. -- http://mail.python.org/mailman/listinfo/python-list
Re: Find the mime type of a file.
On Jan 25, 5:04 pm, Olive wrote: > I want to have a list of all the images in a directory. To do so I want > to have a function that find the mime type of a file. I have found > mimetypes.guess_type but it only works by examining the extension. In > GNU/Linux the "file" utility do much better by actually looking at the > file. Is there an equivalent function in python (as a last resort I can > always use the external file utility). > > Olive You could also try using PIL.(I hardly use it, but...) from PIL import Image for fname in [some list of filenames here]: img = Image.open(fname) print img.format Might be more expensive than the file utility, but that's up to you to determine (open might be lazy, or it might load it - there is a separate load function though, so who knows). hth, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Constraints -//- first release -//- Flexible abstract class based validation for attributes, functions and code blocks
On Jan 27, 6:38 am, Nathan Rice wrote: > > May I suggest a look at languages such as ATS and Epigram? They use > > types that constrain values specifically to prove things about your > > program. Haskell is a step, but as far as proving goes, it's less > > powerful than it could be. ATS allows you to, at compile-time, declare > > that isinstance(x, 0 <= Symbol() < len(L)) for some list L. So it > > might align well with your ideas. > > Thanks for the tip. > > >>> Probably deserves a better name than "constraintslib", that makes one > >>> think of constraint satisfaction. > > >> As you can probably tell from my other projects, I'm bad at coming up > >> with snappy names. > > > I'm bad at doing research on previous projects ;) > > I guess I'm not plugging my other projects enough... You should check > out elementwise. > > Thanks, > > Nathan I love elementwise and this one - thanks. If I can be so bold, I would call it 'contracts'. Or, if you want to be more imaginative and esoteric - 'judge'/'barrister'/'solicitor'. Thanks again, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: os.stat last accessed attribute updating last accessed value
On Feb 4, 9:33 pm, Python_Junkie wrote: > I am trying to obtain the last accessed date. About 50% of the files' > attributes were updated such that the file was last accessed when this > script touches the file. > I was not opening the files > > Anyone have a thought of why this happened. > > Python 2.6 on windows xp Read up on NTFS - but on some file systems - to check a file access time is, well umm, is accessing it. Also possible that listing a directory is considered an access. It's the least useful of all records - I've only ever possibly wanted modification or creation times. hth, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding MIME type for a data stream
On Thursday, 8 March 2012 23:40:13 UTC, Tobiah wrote: > > I have to assume you're talking python 2, since in python 3, strings > > cannot generally contain image data. In python 2, characters are pretty > > much interchangeable with bytes. > > Yeah, python 2 > > > > if you're looking for a specific, small list of file formats, you could > > make yourself a signature list. Most (not all) formats distinguish > > themselves in the first few bytes. > > Yeah, maybe I'll just do that. I'm alowing users to paste > images into a rich-text editor, so I'm pretty much looking > at .png, .gif, or .jpg. Those should be pretty easy to > distinguish by looking at the first few bytes. > > Pasting images may sound weird, but I'm using a jquery > widget called cleditor that takes image data from the > clipboard and replaces it with inline base64 data. > The html from the editor ends up as an email, and the > inline images cause the emails to be tossed in the > spam folder for most people. So I'm parsing the > emails, storing the image data, and replacing the > inline images with an img tag that points to a > web2py app that takes arguments that tell it which > image to pull from the database. > > Now that I think of it, I could use php to detect the > image type, and store that in the database. Not quite > as clean, but that would work. > > Tobiah Something like the following might be worth a go: (untested) from PIL import Image img = Image.open(StringIO(blob)) print img.format HTH Jon. PIL: http://www.pythonware.com/library/pil/handbook/image.htm -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast file data retrieval?
On Monday, 12 March 2012 20:31:35 UTC, MRAB wrote: > On 12/03/2012 19:39, Virgil Stokes wrote: > > I have a rather large ASCII file that is structured as follows > > > > header line > > 9 nonblank lines with alphanumeric data > > header line > > 9 nonblank lines with alphanumeric data > > ... > > ... > > ... > > header line > > 9 nonblank lines with alphanumeric data > > EOF > > > > where, a data set contains 10 lines (header + 9 nonblank) and there can > > be several thousand > > data sets in a single file. In addition,*each header has a* *unique ID > > code*. > > > > Is there a fast method for the retrieval of a data set from this large > > file given its ID code? > > > Probably the best solution is to put it into a database. Have a look at > the sqlite3 module. > > Alternatively, you could scan the file, recording the ID and the file > offset in a dict so that, given an ID, you can seek directly to that > file position. I would have a look at either bsddb, Tokyo (or Kyoto) Cabinet or hamsterdb. If it's really going to get large and needs a full blown server, maybe MongoDB/redis/hadoop... -- http://mail.python.org/mailman/listinfo/python-list
Re: How to decide if a object is instancemethod?
On Wednesday, 14 March 2012 13:28:58 UTC, Cosmia Luna wrote: > class Foo(object): > def bar(self): > return 'Something' > > func = Foo().bar > > if type(func) == : # This should be always true > pass # do something here > > What should type at ? > > Thanks > Cosmia import inspect if inspect.ismethod(foo): # ... Will return True if foo is a bound method. hth Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: Jinja2 + jQuery tabs widget
On Wednesday, 14 March 2012 14:16:35 UTC, JoeM wrote: > Hi All, > > I'm having issues including a {block} of content from Jinja2 > template into a jQueryUI tab. Does anyone know if such a thing is > possible? An example is below, which gives me a 500 error when loading > the page. > > Thanks, > Joe > > > > > $(function() { > $( "#tabs" ).tabs(); > }); > > > > > > Summary > Maps > Tables > Animations > Definitions > > > > {% block map_content %} {% endblock %} > > Firstly, this isn't really a Python language question - although jinja2 is a commonly used module for web frameworks. Secondly, the code looks fine, except we don't know what's in the map_content block. Thirdly, 500 is an internal server error - so it's possible it's nothing to do with any of this anyway -- could you provide a more comprehensive error message? Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Global join function?
On Wednesday, 14 March 2012 18:41:27 UTC, Darrel Grant wrote: > In the virtualenv example bootstrap code, a global join function is used. > > http://pypi.python.org/pypi/virtualenv > > subprocess.call([join(home_dir, 'bin', 'easy_install'), > 'BlogApplication']) > > > In interpeter, I tried this: > > >>> [join([], 'bin', 'easy_install')] > Traceback (most recent call last): > File "", line 1, in > NameError: name 'join' is not defined > > I think I've seen this used elsewhere, but googling only seems to show > results about the string method join, not whatever this is. > > To be clear, I understand how to use "".join(list), but have not found > any information about this other, seemingly global, join function > which takes multiple arguments. It's been bugging me. os.path.join Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: Style question (Poll)
On Wednesday, 14 March 2012 21:16:05 UTC, Terry Reedy wrote: > On 3/14/2012 4:49 PM, Arnaud Delobelle wrote: > > On 14 March 2012 20:37, Croepha wrote: > >> Which is preferred: > >> > >> for value in list: > >> if not value is another_value: > >> value.do_something() > >> break > > Do you really mean 'is' or '=='? > > If you mean x is not y, write it that way. > 'not x is y' can be misread and misunderstood, depending on whether > the 'is' is true or not. > > >>> not 1 is 1 > False > >>> not (1 is 1) > False > >>> (not 1) is 1 > False > > Does not matter how read. > > >>> not (1 is 0) > True > >>> (not 1) is 0 > False > >>> not 1 is 0 > True > > Does matter how read. > > >> if list and not list[0] is another_value: > >> list[0].do_something() > > Or > try: >value = mylist[0] >if value is not another_value: value.dosomething > except IndexError: >pass > > I would not do this in this case of index 0, but if the index were a > complicated expression or expensive function call, making 'if list' an > inadequate test, I might. > > > Hard to say, since they don't do the same thing :) > > > > I suspect you meant: > > > > for value in list: > > if not value is another_value: > > value.do_something() > > break > > > > I always feel uncomfortable with this because it's misleading: a loop > > that never loops. > > I agree. Please do not do this in public ;-). > > -- > Terry Jan Reedy I'm not sure it's efficient or even if I like it, but it avoids try/except and the use of a for loop. if next( iter(mylist), object() ) is not another_value: # ... Just my 2p, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: urllib.urlretrieve never returns???
On Monday, 19 March 2012 19:32:03 UTC, Laszlo Nagy wrote: > The pythonw.exe may not have the rights to access network resources. > >> Have you set a default timeout for sockets? > >> > >> import socket > >> socket.setdefaulttimeout(10) # 10 seconds > I have added pythonw.exe to allowed exceptions. Disabled firewall > completely. Set socket timeout to 10 seconds. Still nothing. > > urllib.urlretrieve does not return from call > > any other ideas? Maybe try using the reporthook option for urlretrieve, just to see if that does anything... If it constantly calls the hook or never calls it, that's one thing. Alternately, tcpdump/wireshark whatever, to see what the heck is going on with traffic - if any. hth Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: Python is readable (OT)
programming, I say, let a thousand voices shout out. > Instead of imagining a single language so wonderful that every other > language is overshadowed and forgotten, imagine that the single language > is the next Java, or C, or even for that matter Python, but whatever it > is, it's not ideal for the problems you care about, or the way you think > about them. Not so attractive now, is it? > > > > The optimistic view is that there will be some kind of inflection point > > around 2020 or so. I could imagine a perfect storm of good things > > happening, like convergence on a single browser platform, > > You call that a perfect storm of good things. I call that sort of > intellectual and software monoculture a nightmare. > > I want a dozen browsers, not one of which is so common that web designers > can design for it and ignore the rest, not one browser so common that > nobody dares try anything new. > > > > nearly > > complete migration to Python 3, further maturity of JVM-based languages, > > etc., where the bar gets a little higher from what people expect from > > languages. Instead of fighting semicolons and braces, we start thinking > > bigger. It could also be some sort of hardware advance, like screen > > resolutions that are so amazing they let us completely rethink our views > > on terseness, punctuation, code organization, etc. > > And what of those with poor eyesight, or the blind? Are they to be > excluded from your "bigger" brave new world? > > > > -- > Steven Completely not related to this discussion, but, I just have to say to Steven, I could not have expressed that better. +1 QOTW (albeit a long one) Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Data mining/pattern recogniton software in Python?
On Friday, 23 March 2012 16:43:40 UTC, Grzegorz Staniak wrote: > Hello, > > I've been asked by a colleague for help in a small educational > project, which would involve the recognition of patterns in a live > feed of data points (readings from a measuring appliance), and then > a more general search for patterns on archival data. The language > of preference is Python, since the lab uses software written in > Python already. I can see there are packages like Open CV, > scikit-learn, Orange that could perhaps be of use for the mining > phase -- and even if they are slanted towards image pattern > recognition, I think I'd be able to find an appropriate package > for the timeseries analyses. But I'm wondering about the "live" > phase -- what approach would you suggest? I wouldn't want to > force an open door, perhaps there are already packages/modules that > could be used to read data in a loop i.e. every 10 seconds, > maintain a a buffer of 15 readings and ring a bell when the data > in buffer form a specific pattern (a spike, a trough, whatever)? > > I'll be grateful for a push in the right direction. Thanks, > > GS > -- > Grzegorz Staniak It might also be worth checking out pandas[1] and scikits.statsmodels[2]. In terms of reading data in a loop I would probably go for a producer-consumer model (possibly using a Queue[3]). Have the consumer constantly try to get another reading, and notify the consumer which can then determine if it's got enough data to calculate a peak/trough. This article is also a fairly good read[4]. That's some pointers anyway, hth, Jon. [1] http://pandas.pydata.org/ [2] http://statsmodels.sourceforge.net/ [3] http://docs.python.org/library/queue.html [4] http://www.laurentluce.com/posts/python-threads-synchronization-locks-rlocks-semaphores-conditions-events-and-queues/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Fetching data from a HTML file
On Friday, 23 March 2012 13:52:05 UTC, Sangeet wrote: > Hi, > > I've got to fetch data from the snippet below and have been trying to match > the digits in this to specifically to specific groups. But I can't seem to > figure how to go about stripping the tags! :( > > Sum class="green">24511 align='center'>02561.496 > [min] > > > Actually, I'm working on ROBOT Framework, and haven't been able to figure out > how to read data from HTML tables. Reading from the source, is the best (read > rudimentary) way I could come up with. Any suggestions are welcome! > > Thanks, > Sangeet I would personally use lxml - a quick example: # -*- coding: utf-8 -*- import lxml.html text = """ Sum2451102561.496 [min] """ table = lxml.html.fromstring(text) for tr in table.xpath('//tr'): print [ (el.get('class', ''), el.text_content()) for el in tr.iterfind('td') ] [('', 'Sum'), ('', ''), ('green', '245'), ('red', '11'), ('', '0'), ('', '256'), ('', '1.496 [min]')] It does a reasonable job, but if it doesn't work quite right, then there's a .fromstring(parser=...) option, and you should be able to pass in ElementSoup and try your luck from there. hth, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Best way to structure data for efficient searching
On Wednesday, 28 March 2012 19:39:54 UTC+1, larry@gmail.com wrote: > I have the following use case: > > I have a set of data that is contains 3 fields, K1, K2 and a > timestamp. There are duplicates in the data set, and they all have to > processed. > > Then I have another set of data with 4 fields: K3, K4, K5, and a > timestamp. There are also duplicates in that data set, and they also > all have to be processed. > > I need to find all the items in the second data set where K1==K3 and > K2==K4 and the 2 timestamps are within 20 seconds of each other. > > I have this working, but the way I did it seems very inefficient - I > simply put the data in 2 arrays (as tuples) and then walked through > the entire second data set once for each item in the first data set, > looking for matches. > > Is there a better, more efficient way I could have done this? It might not be more *efficient* but others might find it more readable, and it'd be easier to change later. Try an in-memory SQL DB (such as sqlite3) and query as (untested) select t2.* from t1 join t2 on k1=k3 and k2=k4 where abs(t1.timestamp - t2.timestamp) < 20 Failing that, two (default)dicts with a tuple as the pair, then use that as your base. Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: help with subclassing problem
On Thursday, 29 March 2012 21:23:20 UTC+1, Peter wrote: > I am attempting to subclass the date class from the datetime package. > Basically I want a subclass that can take the date as a string (in multiple > formats), parse the string and derive the year,month and day information to > create a date instance i.e. > > class MyDate(datetime.date): > def __init__(self, the_date): > # magic happens here to derives year, month and day from the_date > datetime.date.__init__(self, year, month, day) > > But no matter what I do, when I attempt to create an instance of the new > class, I get the error message: > > Traceback (most recent call last): > File "", line 1, in > TypeError: Required argument 'year' (pos 1) not found > > > I have even created a class which doesn't include the argument I want to use > but with default arguments i.e. > > class MyDate (datetime.date): > def __init__(self, year = 1, month = 1, day = 1): > datetime.date.__init__(self, year, month, day) > > and get the same error message. > > What am I doing wrong here? > > Thanks for any help, > Peter Details here: http://stackoverflow.com/questions/399022/why-cant-i-subclass-datetime-date Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Async IO Server with Blocking DB
On Tuesday, 3 April 2012 23:13:24 UTC+1, looking for wrote: > Hi > > We are thinking about building a webservice server and considering > python event-driven servers i.e. Gevent/Tornado/ Twisted or some > combination thereof etc. > > We are having doubts about the db io part. Even with connection > pooling and cache, there is a strong chance that server will block on > db. Blocking for even few ms is bad. > > can someone suggest some solutions or is async-io is not at the prime- > time yet. > > Thanks Maybe look at Cyclone (a Tornado variation built on Twisted), and various modules that will offer synch and events - GIYF! It's doable! Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Gotcha's?
On Wednesday, 4 April 2012 23:34:20 UTC+1, Miki Tebeka wrote: > Greetings, > > I'm going to give a "Python Gotcha's" talk at work. > If you have an interesting/common "Gotcha" (warts/dark corners ...) please > share. > > (Note that I want over http://wiki.python.org/moin/PythonWarts already). > > Thanks, > -- > Miki One I've had to debug... >>> text = 'abcdef' >>> if text.find('abc'): print 'found it!' # Nothing prints as bool(0) is False >>> if text.find('bob'): print 'found it!' found it! Someone new who hasn't read the docs might try this, but then I guess it's not really a gotcha if they haven't bothered doing that. -- http://mail.python.org/mailman/listinfo/python-list
ordering with duck typing in 3.1
Any reason you can't derive from int instead of object? You may also want to check out functions.total_ordering on 2.7+ -- http://mail.python.org/mailman/listinfo/python-list
Re: ordering with duck typing in 3.1
On Monday, 9 April 2012 12:33:25 UTC+1, Neil Cerutti wrote: > On 2012-04-07, Jon Clements wrote: > > Any reason you can't derive from int instead of object? You may > > also want to check out functions.total_ordering on 2.7+ > > functools.total_ordering > > I was temporarily tripped up by the aforementioned documentation, > myself. > > -- > Neil Cerutti Oops. I sent it from a mobile tablet device - I got auto-corrected. But yes, it is functools.total_ordering - TY you Neil. Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: escaping
On Monday, 16 April 2012 11:03:31 UTC+1, Kiuhnm wrote: > On 4/16/2012 4:42, Steven D'Aprano wrote: > > On Sun, 15 Apr 2012 23:07:36 +0200, Kiuhnm wrote: > > > >> This is the behavior I need: > >> path = path.replace('\\', '') > >> msg = ". {} .. '{}' .. {} .".format(a, path, b) > >> Is there a better way? > > > > > > This works for me: > > > a = "spam" > b = "ham" > path = r"C:\a\b\c\d\e.txt" > msg = ". %s .. %r .. %s ." % (a, path, b) > print msg > > . spam .. 'C:\\a\\b\\c\\d\\e.txt' .. ham . > > I like this one. Since I read somewhere that 'format' is preferred over > '%', I was focusing on 'format' and I didn't think of '%'. > Anyway, it's odd that 'format' doesn't offer something similar. > > Kiuhnm If you look at http://docs.python.org/library/string.html#format-string-syntax you'll notice the equiv. of %r is {!r} -- http://mail.python.org/mailman/listinfo/python-list
Re: Regular expressions, help?
On Thursday, 19 April 2012 07:11:54 UTC+1, Sania wrote: > Hi, > So I am trying to get the number of casualties in a text. After 'death > toll' in the text the number I need is presented as you can see from > the variable called text. Here is my code > I'm pretty sure my regex is correct, I think it's the group part > that's the problem. > I am using nltk by python. Group grabs the string in parenthesis and > stores it in deadnum and I make deadnum into a list. > > text="accounts put the death toll at 637 and those missing at > 653 , but the total number is likely to be much bigger" > dead=re.match(r".*death toll.*(\d[,\d\.]*)", text) > deadnum=dead.group(1) > deaths.append(deadnum) > print deaths > > Any help would be appreciated, > Thank you, > Sania Or just don't fully rely on a regex. I would, for time, and the little sanity I believe I have left, would just do something like: death_toll = re.search(r'death toll.*\d+', text).group().rsplit(' ', 1)[1] hth, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: How do you refer to an iterator in docs?
On Thursday, 19 April 2012 13:21:20 UTC+1, Roy Smith wrote: > Let's say I have a function which takes a list of words. I might write > the docstring for it something like: > > def foo(words): >"Foo-ify words (which must be a list)" > > What if I want words to be the more general case of something you can > iterate over? How do people talk about that in docstrings? Do you say > "something which can be iterated over to yield words", "an iterable over > words", or what? > > I can think of lots of ways to describe the concept, but most of them > seem rather verbose and awkward compared to "a list of words", "a > dictionary whose keys are words", etc. I would just write the function signature as (very similar to how itertools does it): def func(iterable, ..): pass IMHO that documents itself. If you need explicit, look at the itertools documentation. hth Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using arguments in a decorator
On Friday, 20 April 2012 16:57:06 UTC+1, Rotwang wrote: > Hi all, here's a problem I don't know how to solve. I'm using Python 2.7.2. > > I'm doing some stuff in Python which means I have cause to call > functions that take a while to return. Since I often want to call such a > function more than once with the same arguments, I've written a > decorator to eliminate repeated calls by storing a dictionary whose > items are arguments and their results: > > def memo(func): > def memofunc(*args, **kwargs): > twargs = tuple(kwargs.items()) > if (args, twargs) in memofunc.d: > return copy(memofunc.d[(args, twargs)]) > memofunc.d[(args, twargs)] = func(*args, **kwargs) > return copy(memofunc.d[(args, twargs)]) > memofunc.__name__ = func.__name__ > memofunc.d = {} > return memofunc > > > If a function f is decorated by memo, whenever f is called with > positional arguments args and keyword arguments kwargs, the decorated > function defines twargs as a hashable representation of kwargs, checks > whether the tuple (args, twargs) is in f's dictionary d, and if so > returns the previously calculated value; otherwise it calculates the > value and adds it to the dictionary (copy() is a function that returns > an object that compares equal to its argument, but whose identity is > different - this is useful if the return value is mutable). > > As far as I know, the decorated function will always return the same > value as the original function. The problem is that the dictionary key > stored depends on how the function was called, even if two calls should > be equivalent; hence the original function gets called more often than > necessary. For example, there's this: > > >>> @memo > def f(x, y = None, *a, **k): > return x, y, a, k > > >>> f(1, 2) > (1, 2, (), {}) > >>> f.d > {((1, 2), ()): (1, 2, (), {})} > >>> f(y = 2, x = 1) > (1, 2, (), {}) > >>> f.d > {((1, 2), ()): (1, 2, (), {}), ((), (('y', 2), ('x', 1))): (1, 2, (), {})} > > > What I'd like to be able to do is something like this: > > def memo(func): > def memofunc(*args, **kwargs): > # > # define a tuple consisting of values for all named positional > # arguments occurring in the definition of func, including > # default arguments if values are not given by the call, call > # it named > # > # define another tuple consisting of any positional arguments > # that do not correspond to named arguments in the definition > # of func, call it anon > # > # define a third tuple consisting of pairs of names and values > # for those items in kwargs whose keys are not named in the > # definition of func, call it key > # > if (named, anon, key) in memofunc.d: > return copy(memofunc.d[(named, anon, key)]) > memofunc.d[(named, anon, key)] = func(*args, **kwargs) > return copy(memofunc.d[(named, anon, key)]) > memofunc.__name__ = func.__name__ > memofunc.d = {} > return memofunc > > > But I don't know how. I know that I can see the default arguments of the > original function using func.__defaults__, but without knowing the > number and names of func's positional arguments (which I don't know how > to find out) this doesn't help me. Any suggestions? > > > -- > Hate music? Then you'll hate this: > > http://tinyurl.com/psymix Possibly take a look at functools.lru_cache (which is Python 3.2+), and use the code from that (at it's part of the stdlib, someone must have done design and testing on it!). http://hg.python.org/cpython/file/default/Lib/functools.py Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: Using arguments in a decorator
On Saturday, 21 April 2012 09:25:40 UTC+1, Steven D'Aprano wrote: > On Fri, 20 Apr 2012 09:10:15 -0700, Jon Clements wrote: > > >> But I don't know how. I know that I can see the default arguments of > >> the original function using func.__defaults__, but without knowing the > >> number and names of func's positional arguments (which I don't know how > >> to find out) this doesn't help me. Any suggestions? > > > > Possibly take a look at functools.lru_cache (which is Python 3.2+), and > > use the code from that (at it's part of the stdlib, someone must have > > done design and testing on it!). > > With respect Jon, did you read the Original Poster's question closely? > Using a LRU cache doesn't even come close to fixing his problem, which > occurs *before* you do the lookup in the cache. I did indeed Steven - what I was suggesting was that functools.lru_cache would be a good starting point. Although I will completely admit that I didn't read the code for functools.lru_cache thoroughly enough to realise it wouldn't be suitable for the OP (ie, it sounded right, looked okay at a glance, and I figured it wasn't a totally unreasonable assumption of a suggestion - so guess I fell into the old 'assume' trap! [not the first time, and won't be the last for sure!]) > > Rotwang's problem is that if you have a function with default arguments: > > def func(spam=42): > return result_of_time_consuming_calculation() > > then these three function calls are identical and should (but don't) > share a single cache entry: > > func() > func(42) > func(spam=42) > > The OP would like all three to share a single cache entry without needing > two redundant calculations, which take a long time. > > The problem is that the three calls give three different patterns of args > and kwargs: > > (), {} > (42,) {} > (), {'spam': 42} > > hence three different cache entries, two of which are unnecessary. I'm wondering if it wouldn't be unreasonable for lru_cache to handle this. Cheers, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie, homework help, please.
On Saturday, 21 April 2012 18:35:26 UTC+1, someone wrote: > On Saturday, April 21, 2012 12:28:33 PM UTC-5, someone wrote: > > Ok, this is my dillema, not only am I new to this programming buisness, > > before the last few days, I did not even know what python was, and besides > > opening up the internet or word documents, that is most of what I know. > > Yet, I have a professor who should be on Psych medication for giving us 3 > > projects, 2 of which I have not listed here to do. I was able to do > > research over the last 3 days, and I have spent 3 days on this project, by > > borrowing others ideas on this project. Below, you will find my professors > > assignment (oh, and due in one week right before finals, so I am stressing > > out so much, cause I don't know why he is crazy enough to assign crap like > > this a week before finals when I have Calculus final,chem final, etc. I > > have figured out most of the assignment, and below, it will be posted after > > the teacher's post of the assignment. What I need help with, and I have > > tried relentlessly to find, is how to put freaking stars(asterisks) as > > border around a list without installing any other program to a portable > > python, of course, this is where my problem lies. Below, you will see what > > I have done, please, help!!! > > You are required to complete and submit the following programming projects > > in Python by the indicated deadline: > > > > Standard Header Information project (5 pts): > > Write a program that will: > > 1) Ask the user for the following information: > > - name of file to be created for storing SHI > > - user’s name (as part of SHI) > > - user’s course and section (as part of SHI) > > - user’s semester and year (as part of SHI) > > - user’s assignment title (as part of SHI) > > 2) Write the above SHI data to a text (.txt) file with the name chosen by > > the user (above) > > 3) Close the file that the SHI data was written to > > 4) Open the file with the SHI data (again) > > 5) Read the data into different (from part 1) variable names > > 6) Display the SHI data read from the file in the interpreter with a border > > around the SHI data (include a buffer of 1 line/space between the border > > and SHI data). An example might look like: > > > > *** > > * * > > * First Name and Last * > > * ENGR 109-X * > > * Fall 2999 * > > * Format Example * > > * * > > *** > > > > > > textfile=input('Hello, we are about to create a text file. An example would > > be: (sample.txt) without the parenthesis. What ever you do name it, it > > needs to end in (.txt). What would you like to name your textfile?') > > userinput=[input('What is your name?'),input('What is your Course Section > > and Course number?'),input('What is the Semester and year?'),input('What is > > the title of this class assignment?')] > > for item in userinput: > > openfile=open(textfile,'w');openfile.writelines("%s\n" % item for item > > in userinput);openfile.close() > > x=textfile;indat=open(x,'r');SHI=indat.read() > > def border(Sullivan): > > string=SHI > > stringlength=len(string) > > stringlength=stringlength("%s\n" % item for item in stringlength) + 2 * > > (3 + 3) > > hBorder=stringlength//2*"* "+"*"[:stringlength%2] > > spacer="*"+" "*(stringlength - 2)+"*" > > fancyText="* "+string+" *" > > return(hBorder,spacer,fancyText,hBorder) > > > > textTuple = border(SHI) > > for lines in textTuple: > > print (lines) > > almost forgot, it has to have a 1 inch border around the top, bottom, left, > and right, with it being aligned to the left. In the picture above, that is > not how it actually looks, the stars to the right are aligned on the right, > not right next to each other. Thanks. Honestly phrased question - well done. Look at the textwrap module - I have no idea how you'll got an inch outputting in just text, as I might have a slightly different font setting and logical and physical inches are different. Good luck, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Web Scraping - Output File
comcast.net> writes: > > Hello, > > I am having some difficulty generating the output I want from web > scraping. Specifically, the script I wrote, while it runs without any > errors, is not writing to the output file correctly. It runs, and > creates the output .txt file; however, the file is blank (ideally it > should be populated with a list of names). > > I took the base of a program that I had before for a different data > gathering task, which worked beautifully, and edited it for my > purposes here. Any insight as to what I might be doing wrote would be > highly appreciated. Code is included below. Thanks! I would approach it like this... import lxml.html QUERY = '//tr[@bgcolor="#F1F3F4"][td[starts-with(@class, "body_cols")]]' url = 'http://www.skadden.com/Index.cfm?contentID=44&alphaSearch=A' tree = lxml.html.parse(url).getroot() trs = tree.xpath(QUERY) for tr in trs: tds = [el.text_content() for el in tr.iterfind('td')] print tds hth Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: HTML Code - Line Number
comcast.net> writes: > > Hello, > [snip] > Any thoughts as to how to define a function to do this, or do this > some other way? All insight is much appreciated! Thanks. > Did you not see my reply to your previous thread? And why do you want the line number? Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: HTML Code - Line Number
On Friday, 27 April 2012 18:09:57 UTC+1, smac...@comcast.net wrote: > Hello, > > For scrapping purposes, I am having a bit of trouble writing a block > of code to define, and find, the relative position (line number) of a > string of HTML code. I can pull out one string that I want, and then > there is always a line of code, directly beneath the one I can pull > out, that begins with the following: > > > However, because this string of HTML code above is not unique to just > the information I need (which I cannot currently pull out), I was > hoping there is a way to effectively say "if you find the html string > _ in the line of HTML code above, and the string valign="top" class="body_co comcast.net> writes: > > Hello, > > I am having some difficulty generating the output I want from web > scraping. Specifically, the script I wrote, while it runs without any > errors, is not writing to the output file correctly. It runs, and > creates the output .txt file; however, the file is blank (ideally it > should be populated with a list of names). > > I took the base of a program that I had before for a different data > gathering task, which worked beautifully, and edited it for my > purposes here. Any insight as to what I might be doing wrote would be > highly appreciated. Code is included below. Thanks! [quoting reply to first thread] I would approach it like this... import lxml.html QUERY = '//tr[@bgcolor="#F1F3F4"][td[starts-with(@class, "body_cols")]]' url = 'http://www.skadden.com/Index.cfm?contentID=44&alphaSearch=A' tree = lxml.html.parse(url).getroot() trs = tree.xpath(QUERY) for tr in trs: tds = [el.text_content() for el in tr.iterfind('td')] print tds hth Jon. [/quote] > following, then pull everything that follows this second string. > > Any thoughts as to how to define a function to do this, or do this > some other way? All insight is much appreciated! Thanks. comcast.net> writes: > > Hello, > [snip] > Any thoughts as to how to define a function to do this, or do this > some other way? All insight is much appreciated! Thanks. > [quote in reply to second thread] Did you not see my reply to your previous thread? And why do you want the line number? [/quote] I'm trying this on GG, as the mailing list gateway one or t'other does nee seem to work (mea culpa no doubt). So may have obscured the issue more with my quoting and snipping, or what not. Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: key/value store optimized for disk storage
On Friday, 4 May 2012 16:27:54 UTC+1, Steve Howell wrote: > On May 3, 6:10 pm, Miki Tebeka wrote: > > > I'm looking for a fairly lightweight key/value store that works for > > > this type of problem: > > > > I'd start with a benchmark and try some of the things that are already in > > the standard library: > > - bsddb > > - sqlite3 (table of key, value, index key) > > - shelve (though I doubt this one) > > > > Thanks. I think I'm ruling out bsddb, since it's recently deprecated: > > http://www.gossamer-threads.com/lists/python/python/106494 > > I'll give sqlite3 a spin. Has anybody out there wrapped sqlite3 > behind a hash interface already? I know it's simple to do > conceptually, but there are some minor details to work out for large > amounts of data (like creating the index after all the inserts), so if > somebody's already tackled this, it would be useful to see their > code. > > > You might find that for a little effort you get enough out of one of these. > > > > Another module which is not in the standard library is hdf5/PyTables and in > > my experience very fast. > > Thanks. Could also look at Tokyo cabinet or Kyoto cabinet (but I believe that has slightly different licensing conditions for commercial use). -- http://mail.python.org/mailman/listinfo/python-list
Re: A question of style (finding item in list of tuples)
On Monday, 21 May 2012 13:37:29 UTC+1, Roy Smith wrote: > I've got this code in a django app: > > CHOICES = [ > ('NONE', 'No experience required'), > ('SAIL', 'Sailing experience, new to racing'), > ('RACE', 'General racing experience'), > ('GOOD', 'Experienced racer'), > ('ROCK', 'Rock star'), > ] > > def experience_text(self): > for code, text in self.CHOICES: > if code == self.level: > return text > return "" > > Calling experience_text("ROCK") should return "Rock star". Annoyingly, > django handles this for you automatically inside a form, but if you also > need it in your application code, you have to roll your own. > > The above code works, but it occurs to me that I could use the much > shorter: > > def experience_text(self): > return dict(CHOICES).get("self.level", "???") > > So, the question is, purely as a matter of readability, which would you > find easier to understand when reading some new code? Assume the list > of choices is short enough that the cost of building a temporary dict on > each call is negligible. I'm just after style and readability here. Haven't used django in a while, but doesn't the model provide a get_experience_display() method which you could use... Failing that, if order isn't important, you can not bother with tuples and have CHOICES be a dict, then pass choices=CHOICES.iteritems() as I believe it takes any iterable, and maybe plug an ordereddict if order is important. hth Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Email Id Verification
On Friday, 25 May 2012 14:36:18 UTC+1, Grant Edwards wrote: > On 2012-05-25, Steven D'Aprano wrote: > > On Thu, 24 May 2012 05:32:16 -0700, niks wrote: > > > >> Hello everyone.. > >> I am new to asp.net... > >> I want to use Regular Expression validator in Email id verification.. > > > > Why do you want to write buggy code that makes your users hate your > > program? Don't do it! Write good code, useful code! Validating email > > addresses is the wrong thing to do. > > I have to agree with Steven. Nothing will make your users swear at > you as certainly as when you refuse to accept the e-mail address at > which the reeive e-mail all day every day. > > -- > Grant Edwards grant.b.edwardsYow! I appoint you > at ambassador to Fantasy > gmail.comIsland!!! Ditto. This would be my public email, but (like most I believe) also have 'private' and work email addresses. For the OP, just trying to check an email is syntactically correct is okay-ish if done properly. Normally as mentioned you just send a confirmation email to said address with some id and link that confirms (normally with an expiry period). Some mail servers support the "does this mailbox exist?" request, but I fear these days due to spam, most will just say no -- so the only option is to send and handle a bounce (and some don't even send back bounces). And a pretty good way for malicious people to make mail servers think you're trying a DoS. Although, what I'm finding useful is an option of "auth'ing" with twitter, facebook, google etc... Doesn't require a huge amount of work, and adds a bit of validity to the request. Jon (who still didn't get any bloody Olympic tickets). -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic comparison operators
> > Any time you find yourself thinking that you want to use eval to solve a > problem, take a long, cold shower until the urge goes away. > > If you have to ask why eval is dangerous, then you don't know enough > about programming to use it safely. Scrub it out of your life until you > have learned about code injection attacks, data sanitation, trusted and > untrusted input. Then you can come back to eval and use it safely and > appropriately. I would +1 QOTW - but fear might have to cheat and say +1 to 2 paragraphs of the week :) Jon. -- http://mail.python.org/mailman/listinfo/python-list
usenet reading
Hi All, Normally use Google Groups but it's becoming absolutely frustrating - not only has the interface changed to be frankly impractical, the posts are somewhat random of what appears, is posted and whatnot. (Ironically posted from GG) Is there a server out there where I can get my news groups? I use to be with an ISP that hosted usenet servers, but alas, it's no longer around... Only really interested in Python groups and C++. Any advice appreciated, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: sqlite INSERT performance
On Thursday, 31 May 2012 16:25:10 UTC+1, duncan smith wrote: > On 31/05/12 06:15, John Nagle wrote: > > On 5/30/2012 6:57 PM, duncan smith wrote: > >> Hello, > >> I have been attempting to speed up some code by using an sqlite > >> database, but I'm not getting the performance gains I expected. > > > > SQLite is a "lite" database. It's good for data that's read a > > lot and not changed much. It's good for small data files. It's > > so-so for large database loads. It's terrible for a heavy load of > > simultaneous updates from multiple processes. > > > > Once the table is created the data will not be changed at all. > Corresponding integer codes will have to be generated for columns. (I > want to do this lazily because some columns might never be needed for > output files, and processing all columns was relatively expensive for my > initial solution.) After that it's a series of 'SELECT a, b, ... FROM > table WHERE f="g" ORDER by a, b, ...' style queries dumped to space > separated text files. > > > However, wrapping the inserts into a transaction with BEGIN > > and COMMIT may help. > > > > Unfortunately there's no discernible difference. > > > If you have 67 columns in a table, you may be approaching the > > problem incorrectly. > > > > Quite possibly. I have defined start and end points. The data are > contained in text files. I need to do the mapping to integer codes and > generate output files for subsets of variables conditional on the levels > of other variables. (I was doing the subsequent sorting separately, but > if I'm using SQL I guess I might as well include that in the query.) The > output files are inputs for other (C++) code that I have no control over. > > Any approach that doesn't consume large amounts of memory will do. Cheers. > > Duncan It might be worth checking out https://sdm.lbl.gov/fastbit/ which has Python bindings (nb: the library itself takes a while to compile), but I'm not I00% sure it would meet all your requirements. Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: DBF records API
On 01/06/12 23:13, Tim Chase wrote: On 06/01/12 15:05, Ethan Furman wrote: MRAB wrote: I'd probably think of a record as being more like a dict (or an OrderedDict) with the fields accessed by key: record["name"] but: record.deleted Record fields are accessible both by key and by attribute -- by key primarily for those cases when the field name is in a variable: for field in ('full_name','nick_name','pet_name'): print record[field] and since dbf record names cannot start with _ and are at most 10 characters long I've used longer than that method names... but if I want to support dbf version 7 that won't work. It seems to me that, since you provide both the indexing notation and the dotted notation, just ensure that the methods such as dbf.scatter_fields *always* trump and refer to the method. This allows for convenience of using the .field_name notation for the vast majority of cases, but ensures that it's still possible for the user (of your API) to use the indexing method to do things like value = dbf["scatter_fields"] if they have a thusly-named field name and want its value. -tkc I did think about *trumping* one way or the other, but both *ugh*. Ethan: I think offering both is over-complicating the design for no gain, and possible complications later. For instance, what if you introduce a method/property called "last" to get the last row of a table, it'll cause some head-scratching as someone will suddenly have to make sure your API changes didn't conflict with their column names (or if they've used yours as a base and introduce methods, doesn't interfere with their users of their version of the library...) To most developers, I think blah["whatever"] is perfectly clear as looking up a value via key is mostly done that way. I suppose you could use __getitem__ to grab certain fields in one go ( as per your example - from any iterable that isn't a basestring? - and users would probably enjoy not keep re-typing "record.xxx" and would save you having to invent another possibly conflicting name) such as: print record['full_name', 'nick_name', 'pet_name'] # looks clean to me In short I totally agree with MRAB here. Just my 2p, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: file pointer array
On 06/06/12 18:54, Prasad, Ramit wrote: data= [] for index in range(N, 1): # see Chris Rebert's comment with open('data%d.txt' % index,'r') as f: data.append( f.readlines() ) I think "data.extend(f)" would be a better choice. Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: file pointer array
On 06/06/12 19:51, MRAB wrote: On 06/06/2012 19:28, Jon Clements wrote: On 06/06/12 18:54, Prasad, Ramit wrote: data= [] for index in range(N, 1): # see Chris Rebert's comment with open('data%d.txt' % index,'r') as f: data.append( f.readlines() ) I think "data.extend(f)" would be a better choice. .extend does something different, and "range(N, 1)" is an empty range if N > 0. Mea culpa - I had it in my head the OP wanted to treat the files as one contiguous one. So yeah: # something equiv to... (unless it is definitely a fixed range in which # case (x)range can be used) data = [ list(open(fname)) for fname in iglob('/home/jon/data*.txt') ] # then if they ever need to treat it as a contiguous sequence... all_data = list(chain.from_iterable(data)) Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Compare 2 times
On 06/06/12 14:39, Christian Heimes wrote: Am 06.06.2012 14:50, schrieb loial: I have a requirement to test the creation time of a file with the current time and raise a message if the file is more than 15 minutes old. Platform is Unix. I have looked at using os.path.getctime for the file creation time and time.time() for the current time, but is this the best approach? Lots of people are confused by ctime because they think 'c' stands for change. That's wrong. st_ctime is status change time. The ctime is updated when you change (for example) owner or group of a file, create a hard link etc. POSIX has no concept of creation time stamp. Christian I haven't thought this through too much, but perhaps an ugly "work-around" would be to use inotify (in some kind of daemon) to watch for the IN_CREATE events and store the crtime in a personal DB. Then possibly look at some sort of scheduling to fulfil what happens after 15 minutes. I'm sure there's subtleties I'm missing, but just thought it could be useful. Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is that safe to use ramdom.random() for key to encrypt?
On Sun, 17 Jun 2012 12:31:04 +1000, Chris Angelico wrote: > On Sun, Jun 17, 2012 at 12:15 PM, Yesterday Paid > wrote: >> I'm making cipher program with random.seed(), random.random() as the >> key table of encryption. >> I'm not good at security things and don't know much about the algorithm >> used by random module. > > For security, you don't want any algorithm, you want something like > /dev/random (on Unix-like platforms). > > I'm pretty sure Python includes crypto facilities. Unless it (most > oddly) lacks these batteries, I would recommend using one of them > instead. > > ChrisA Cryptography is a complex subject - I've had the (mis)fortune to study it briefly. Whatever you do - *do not* attempt to write your own algorithm. Python includes hashlib (forms of SHA and MD5) and uuid modules, but I take it a symmetric or possibly public/private key system is required - depending on what you want to secure, where it's stored and who needs access. I generally find a separate partition with an encrypted file-system (which is fairly straight forward on *nix systems or I think there's a product out there that works with Windows), is a lot easier and puts the load on the filesystem/OS instead of having to be handled in your application is a lot simpler. Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: Is that safe to use ramdom.random() for key to encrypt?
On Sun, 17 Jun 2012 23:17:37 +, Steven D'Aprano wrote: > On Mon, 18 Jun 2012 08:41:57 +1000, Chris Angelico wrote: > >> On Mon, Jun 18, 2012 at 3:06 AM, Rafael Durán Castañeda >> wrote: >>> The language Python includes a SystemRandom class that obtains >>> cryptographic grade random bits from /dev/urandom on a Unix-like >>> system, including Linux and Mac OS X, while on Windows it uses >>> CryptGenRandom. >> >> /dev/urandom isn't actually cryptographically secure; it promises not >> to block, even if it has insufficient entropy. But in your instance... > > Correct. /dev/random is meant to be used for long-lasting > cryptographically-significant uses, such as keys. urandom is not. > > http://en.wikipedia.org/wiki//dev/random > > >>> Do you think is secure enough for token generation? (40 chars long >>> tokens are used for password reset links in a website, there isn't any >>> special security concern for the web). >> >> ... it probably is fine, since password reset tokens don't need to be >> as secure as encryption keys (if anyone _does_ figure out how to >> predict your password resets, all they'll be able to do is lock people >> out of their accounts one by one, not snoop on them all unbeknownst, >> and you'll be able to see log entries showing the resets - you DO log >> them, right?). In fact, you could probably get away with something >> pretty trivial there, like a SHA1 of the current timestamp, the user >> name, and the user's current password hash. The chances that anybody >> would be able to exploit that are fairly low, given that you're not a >> bank or other high-profile target. > > If I were an identity thief, I would *love* low-profile targets. Even > though the payoff would be reduced, the cost would be reduced even more: > > - they tend to be complacent, even more so than high-profile targets; > > - they tend to be smaller, with fewer resources for security; > > - mandatory disclosure laws tend not to apply to them; > > - they don't tend to have the resources to look for anomalous usage > patterns, if they even cared enough to want to. > > > If there was a Facebook-like website that wasn't Facebook[1], but still > with multiple tens of thousands of users, I reckon a cracker who didn't > vandalise people's accounts could steal private data from it for *years* > before anyone noticed, and months or years more before they did > something about it. > > > > [1] And very likely a Facebook-like website that *was* Facebook. I > reckon the odds are about 50:50 that FB would prefer to keep a breach > secret than risk the bad publicity by fixing it. > > > -- > Steven I'm reminded of: http://xkcd.com/936/ http://xkcd.com/792/ There's also one where it's pointed out it's easier to brute force a person who has the code, than brute force the computer. [but can't find that one at the moment] -- http://mail.python.org/mailman/listinfo/python-list
Re: How does this work?
On Jun 5, 4:37 am, Ben Finney wrote: > writes: > > I was surfing around looking for a way to split a list into equal > > sections. I came upon this algorithm: > > > >>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc > > >>> f("Hallo Welt", 3) > > ['Hal', 'lo ', 'Wel', 't'] > > > (http://stackoverflow.com/questions/312443/how-do-you-split-a-list-int...) > > This is an excellent example of why “clever” code is to be shunned. > Whoever wrote this needs to spend more time trying to get their code > past a peer review; the above would be rejected until it was re-written > to be clear. > > Here is my attempt to write the above to be clear (and fixing a couple > of bugs too): > > def split_slices(seq, slicesize, accumulator=None): > """ Return a list of slices from `seq` each of size `slicesize`. > > :param seq: The sequence to split. > :param slicesize: The maximum size of each slice. > :param accumulator: A sequence of existing slices to which > ours should be appended. > :return: A list of the slices. Each item will be a slice > from the original `seq` of `slicesize` length; the last > item may be shorter if there were fewer than `slicesize` > items remaining. > > """ > if accumulator is None: > accumulator = [] > if seq: > slice = seq[:slicesize] > result = split_slices( > seq[slicesize:], slicesize, accumulator + [slice]) > else: > result = accumulator > return result > > > It doesn't work with a huge list, but looks like it could be handy in > > certain circumstances. I'm trying to understand this code, but am > > totally lost. I know a little bit about lambda, as well as the ternary > > operator > > In Python, ‘lambda’ is merely an alternative syntax for creating > function objects. The resulting object *is* a function, so I've written > the above using the ‘def’ syntax for clarity. > > The ternary operator is often useful for very simple expressions, but > quickly becomes too costly to read when the expression is complex. The > above is one where the writer is so much in love with the ternary > operator that they have crammed far too much complexity into a single > expression. > > > Just curious if anyone could explain how this works or maybe share a link > > to a website that might explain this? > > Does the above help? > > -- > \ “We must find our way to a time when faith, without evidence, | > `\ disgraces anyone who would claim it.” —Sam Harris, _The End of | > _o__) Faith_, 2004 | > Ben Finney Just my 2p, but isn't the itertools "grouper" recipe prudent? -- http://mail.python.org/mailman/listinfo/python-list
PyPad 2.7.1 Update 4
Hi All, I'm pleased to announce that PyPad 2.7.1 (Update 4), a simple python environment for the iPad / iPhone, is now available in the iTunes store. It can be found at: http://itunes.apple.com/au/app/pypad/id428928902?mt=8 Update 4 adds the ability to create multiple modules and import them into one another. The documentation is lagging a bit but the functionality can be found in the action menu of the accessory bar. Further updates planed include: Updated documentation Improving cursor interaction in interactive mode Providing access to interactive mode command history Adding iOS specific modules Syntax highlighting and more... Regards, Jon Dowdall -- http://mail.python.org/mailman/listinfo/python-list
PyPad 2.7.1 Update 4 (Python on iPad and iPhone)
Hi All, I'm pleased to announce that PyPad (Python environment for iOS) 2.7.1 Update 4 is now available in the iTunes App Store. New in this version is the ability to create custom modules. Modules can be independent or can include other user modules to build larger frame works. Plans for future versions include: Improved cursor handling in interactive mode. Access to the interactive command history. Modules to access iOS specific functionality. Additional documentation. Syntax highlighting. Improved script debugging support. Regards, Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: Need help with simple OOP Python question
On Sep 5, 3:43 pm, Peter Otten <__pete...@web.de> wrote: > Kristofer Tengström wrote: > > Thanks everyone, moving the declaration to the class's __init__ method > > did the trick. Now there's just one little problem left. I'm trying to > > create a list that holds the parents for each instance in the > > hierarchy. This is what my code looks like now: > > > - > > > class A: > > def __init__(self, parents=None): > > self.sub = dict() > > if parents: > > You should explicitly test for None here; otherwise in a call like > > ancestors = [] > a = A(anchestors) > > the list passed as an argument will not be used, which makes fore confusing > behaviour. > > > self.parents = parents > > else: > > self.parents = [] > > def sub_add(self, cls): > > hierarchy = self.parents > > hierarchy.append(self) > > Here you are adding self to the parents (that should be called ancestors) > and pass it on to cls(...). Then -- because it's non-empty -- it will be > used by the child, too, and you end up with a single parents list. > > > obj = cls(hierarchy) > > self.sub[obj.id] = obj > > While the minimal fix is to pass a copy > > def sub_add(self, cls): > obj = cls(self.parents + [self]) > self.sub[obj.id] = obj > > I suggest that you modify your node class to keep track only of the direct > parent instead of all ancestors. That makes the implementation more robust > when you move a node to another parent. I may not be understanding the OP correctly, but going by what you've put here, I might be tempted to take this kind of stuff out of the class's and using a graph library (such as networkx) - that way if traversal is necessary, it might be a lot easier. But once again, I must say I'm not 100% sure what the OP wants to achieve... Jon. -- http://mail.python.org/mailman/listinfo/python-list
One line command line filter
It seems unreasonably hard to write simple one-line unix command line filters in python: eg: ls | python -c " print x.upper()" to get at sys.stdin or similar needs an import, which makes a subsequent for-loop illegal. python -c "import sys; for x in sys.stdin(): print x" <<- SyntaxError Am I missing something obvious? The best I've come up with is to use sitecustomize.py to add to __builtin__ def stdin(): import sys for x in sys.stdin(): if not x: return yield x.strip() import __builtin__ __builtin__.stdin = stdin This allows ls | python -c "for x in stdin(): print x.upper()" Is there a better solution - if not is this worth a PEP? -- http://mail.python.org/mailman/listinfo/python-list
Re: One line command line filter
> > Am I missing something obvious? > > ls | python -c "for line in __import__('sys').stdin: print (line.upper())" Ah, so I am missing something - it is possible - but 'obvious'? Do people think it should be more accessible -- http://mail.python.org/mailman/listinfo/python-list
Re: pattern matching
On Feb 24, 2:11 am, monkeys paw wrote: > if I have a string such as '01/12/2011' and i want > to reformat it as '20110112', how do i pull out the components > of the string and reformat them into a DDMM format? > > I have: > > import re > > test = re.compile('\d\d\/') > f = open('test.html') # This file contains the html dates > for line in f: > if test.search(line): > # I need to pull the date components here I second using an html parser to extact the content of the TD's, but I would also go one step further reformatting and do something such as: >>> from time import strptime, strftime >>> d = '01/12/2011' >>> strftime('%Y%m%d', strptime(d, '%m/%d/%Y')) '20110112' That way you get some validation about the data, ie, if you get '13/12/2011' you've probably got mixed data formats. hth Jon. -- http://mail.python.org/mailman/listinfo/python-list
Numerical representation
Hello all, I am new to the Python language and writing a Runge-Kutta-Fellberg 7(8) integrator in Python, which requires an extreme numerical precision for my particular application. Unfortunately, I can not seem to attain it. The interesting part is if I take my exact code and translate it to Matlab code (so I use the exact same process and numbers), I get a far superior precision (the one I am expecting, in fact). This leads me to think I need to call a certain command in my Python script in order to make sure no truncation errors are building up over my integration. Has anyone had similar problems? Is there a difference between how Matlab and Python store numbers, and if so how do I make Python more accurate? I know there is a lot of packages out there, but this in fact overwhelmed me a little bit and seems to prevent me from finding the answer to my question, so I'm hoping someone with more experience will be able to enlighten me! Best regards, Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: Numerical representation
Actually, I import numpy in my code for array creation...in the documentation I did not manage to find anything that would solve this precision problem I mentioned however. If you're familiar with it, would you happen to know what capability of numpy might solve my problem? On Fri, Mar 4, 2011 at 4:49 PM, Santoso Wijaya wrote: > Have you taken a look at numpy? [1] It was written for exactly this kind of > usage. > > ~/santa > > [1] http://numpy.scipy.org/ > > > On Fri, Mar 4, 2011 at 2:32 PM, Jon Herman wrote: > >> Hello all, >> >> I am new to the Python language and writing a Runge-Kutta-Fellberg 7(8) >> integrator in Python, which requires an extreme numerical precision for my >> particular application. Unfortunately, I can not seem to attain it. >> The interesting part is if I take my exact code and translate it to Matlab >> code (so I use the exact same process and numbers), I get a far superior >> precision (the one I am expecting, in fact). This leads me to think I need >> to call a certain command in my Python script in order to make sure no >> truncation errors are building up over my integration. >> >> Has anyone had similar problems? Is there a difference between how Matlab >> and Python store numbers, and if so how do I make Python more accurate? >> >> I know there is a lot of packages out there, but this in fact overwhelmed >> me a little bit and seems to prevent me from finding the answer to my >> question, so I'm hoping someone with more experience will be able to >> enlighten me! >> >> Best regards, >> >> Jon >> >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > -- http://mail.python.org/mailman/listinfo/python-list
Re: Numerical representation
I'm starting to run out of ideas of what to do...I've imported the true division (I'm using Python 2.7) to make sure I wasn't accidentally using any integers but the results remain identical, so it's not a division problem. I've copied the loop I'm running below, is there any mathematical operation I am making here that may have an accuracy problem? Thanks for any advice you can give! Xold=X told=t if abs(dt) > abs(tf-t): dt = tf-t k[:,0]=F(mu, Xold, told) for n in range(1,13): nn=n-1 Xtemp1=zeros(neq) for j in range(nn): Xtemp1 = Xtemp1 + b[n,j] * k[:,j] Xtemp1 X=Xold+ dt * Xtemp1 t=told+a[n]*dt k[:,n]=F(mu, X, 0) Xtemp2=zeros(neq) for l in range(13): Xtemp2=Xtemp2+c[l]*k[:,l] X=Xold + dt * Xtemp2 t=told+dt Xstore=vstack((Xstore,X)) tstore=vstack((tstore,t)) if abs(tf-t)< 1e-14: print('At tf') break On Fri, Mar 4, 2011 at 6:46 PM, Jon Herman wrote: > Actually, I import numpy in my code for array creation...in the > documentation I did not manage to find anything that would solve this > precision problem I mentioned however. If you're familiar with it, would you > happen to know what capability of numpy might solve my problem? > > > > > On Fri, Mar 4, 2011 at 4:49 PM, Santoso Wijaya > wrote: > >> Have you taken a look at numpy? [1] It was written for exactly this kind >> of usage. >> >> ~/santa >> >> [1] http://numpy.scipy.org/ >> >> >> On Fri, Mar 4, 2011 at 2:32 PM, Jon Herman wrote: >> >>> Hello all, >>> >>> I am new to the Python language and writing a Runge-Kutta-Fellberg 7(8) >>> integrator in Python, which requires an extreme numerical precision for my >>> particular application. Unfortunately, I can not seem to attain it. >>> The interesting part is if I take my exact code and translate it to >>> Matlab code (so I use the exact same process and numbers), I get a far >>> superior precision (the one I am expecting, in fact). This leads me to think >>> I need to call a certain command in my Python script in order to make sure >>> no truncation errors are building up over my integration. >>> >>> Has anyone had similar problems? Is there a difference between how Matlab >>> and Python store numbers, and if so how do I make Python more accurate? >>> >>> I know there is a lot of packages out there, but this in fact overwhelmed >>> me a little bit and seems to prevent me from finding the answer to my >>> question, so I'm hoping someone with more experience will be able to >>> enlighten me! >>> >>> Best regards, >>> >>> Jon >>> >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >>> >>> >> > -- http://mail.python.org/mailman/listinfo/python-list
Numerical representation
Sorry Robert, I'd missed your post when I just made my last one. The output I am getting in Python looks as follows: array([ 9.91565050e-01, 1.55680112e-05, -1.53258602e-05, -5.75847623e-05, -9.64290960e-03, -8.26333458e-08]) This is the final state vector, consisting of 6 states (postion and velocity), although this isn't really relevant right now. In MATLAB, using the same process for the RKF78, I get the following output: Xend = Columns 1 through 3 9.915755153307796e-0013.592556838089922e-016 -1.523933534321440e-005 Columns 4 through 6 -7.175069559491303e-015 -9.624755221500220e-003 1.289789641929434e-017 As you can see, the results are close but there is a big difference in precision (the 2nd, 4th and 6th entries are supposed to be zero under the intial and final conditions I am running). See also the post I just made where you can see the Python code I am using (MATLAB code is identical but translated) This is for a fixed timestep in both Python and Matlab. If I let the code run with an identical method for timestep correction (based on a tolerance), Python will use a timestep approximately 10^5 SMALLER than Matlab uses. So I'm really sure it's not a representation issue, but actually a precision issue. Thanks for any advice! -- http://mail.python.org/mailman/listinfo/python-list
Re: Numerical representation
And for the sake of completeness, the derivative function I am calling from my integrator (this is the 3 body problem in astrodynamics): def F(mu, X, ti): r1= pow((pow(X[0]+mu,2)+pow(X[1],2)+pow(X[2],2)),0.5) r2= pow((pow(X[0]+mu-1,2)+pow(X[1],2)+pow(X[2],2)),0.5) Ax= X[0]+2*X[4]-(1-mu)*(X[0]+mu)/r1**3-mu*(X[0]-(1-mu))/r2**3 Ay= X[1]-2*X[3]-(1-mu)*X[1]/r1**3-mu*X[1]/r2**3 Az= -(1-mu)*X[2]/r1**3-mu*X[2]/r2**3 XDelta=array([X[3], X[4], X[5], Ax, Ay, Az]) return XDelta On Mon, Mar 7, 2011 at 11:50 AM, Jon Herman wrote: > Sorry Robert, I'd missed your post when I just made my last one. The output > I am getting in Python looks as follows: > > array([ 9.91565050e-01, 1.55680112e-05, -1.53258602e-05, > -5.75847623e-05, -9.64290960e-03, -8.26333458e-08]) > > This is the final state vector, consisting of 6 states (postion and > velocity), although this isn't really relevant right now. > > In MATLAB, using the same process for the RKF78, I get the following > output: > > Xend = > > Columns 1 through 3 > > 9.915755153307796e-0013.592556838089922e-016 > -1.523933534321440e-005 > > Columns 4 through 6 > >-7.175069559491303e-015 -9.624755221500220e-003 > 1.289789641929434e-017 > > As you can see, the results are close but there is a big difference in > precision (the 2nd, 4th and 6th entries are supposed to be zero under the > intial and final conditions I am running). > See also the post I just made where you can see the Python code I am using > (MATLAB code is identical but translated) > > This is for a fixed timestep in both Python and Matlab. If I let the code > run with an identical method for timestep correction (based on a tolerance), > Python will use a timestep approximately 10^5 SMALLER than Matlab uses. So > I'm really sure it's not a representation issue, but actually a precision > issue. > > Thanks for any advice! > -- http://mail.python.org/mailman/listinfo/python-list
Re: Numerical representation
And for the sake of additional completeness (I'm sorry I didn't think of all this in one go): my derivative function in Python produces results that agree with MATLAB to order e-16 (machine precision), so the error is definitely building up in my integrator. On Mon, Mar 7, 2011 at 11:59 AM, Jon Herman wrote: > And for the sake of completeness, the derivative function I am calling from > my integrator (this is the 3 body problem in astrodynamics): > > def F(mu, X, ti): > > r1= pow((pow(X[0]+mu,2)+pow(X[1],2)+pow(X[2],2)),0.5) > r2= pow((pow(X[0]+mu-1,2)+pow(X[1],2)+pow(X[2],2)),0.5) > > Ax= X[0]+2*X[4]-(1-mu)*(X[0]+mu)/r1**3-mu*(X[0]-(1-mu))/r2**3 > Ay= X[1]-2*X[3]-(1-mu)*X[1]/r1**3-mu*X[1]/r2**3 > Az= -(1-mu)*X[2]/r1**3-mu*X[2]/r2**3 > > XDelta=array([X[3], X[4], X[5], Ax, Ay, Az]) > > return XDelta > > > > > > On Mon, Mar 7, 2011 at 11:50 AM, Jon Herman wrote: > >> Sorry Robert, I'd missed your post when I just made my last one. The >> output I am getting in Python looks as follows: >> >> array([ 9.91565050e-01, 1.55680112e-05, -1.53258602e-05, >> -5.75847623e-05, -9.64290960e-03, -8.26333458e-08]) >> >> This is the final state vector, consisting of 6 states (postion and >> velocity), although this isn't really relevant right now. >> >> In MATLAB, using the same process for the RKF78, I get the following >> output: >> >> Xend = >> >> Columns 1 through 3 >> >> 9.915755153307796e-0013.592556838089922e-016 >> -1.523933534321440e-005 >> >> Columns 4 through 6 >> >>-7.175069559491303e-015 -9.624755221500220e-003 >> 1.289789641929434e-017 >> >> As you can see, the results are close but there is a big difference in >> precision (the 2nd, 4th and 6th entries are supposed to be zero under the >> intial and final conditions I am running). >> See also the post I just made where you can see the Python code I am using >> (MATLAB code is identical but translated) >> >> This is for a fixed timestep in both Python and Matlab. If I let the code >> run with an identical method for timestep correction (based on a tolerance), >> Python will use a timestep approximately 10^5 SMALLER than Matlab uses. So >> I'm really sure it's not a representation issue, but actually a precision >> issue. >> >> Thanks for any advice! >> > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Numerical representation
It really is exactly the same process, but sure. Below is my Matlab translation of the python code I posted earlier, it functions at the increased accuracy I've shown above. k(:,1)=feval(deq, ti, x, mu); for n = 2:1:13 nn=n-1; Xtemp1 = 0.0; for j = 1:1:nn Xtemp1 = Xtemp1 + beta(n,j) * k(:,j); end x=xwrk+ dt * Xtemp1; ti=twrk+alph(n)*dt; k(:,n)=feval(deq, ti, x, mu); end Xtemp2=0.0; for l = 1:1:13 Xtemp2=Xtemp2+ch(l)*k(:,l); end x=xwrk + dt * Xtemp2; t=twrk+dt; On Mon, Mar 7, 2011 at 1:50 PM, Chris Rebert wrote: > >>> On Fri, Mar 4, 2011 at 2:32 PM, Jon Herman > wrote: > >>>> > >>>> I am new to the Python language and writing a Runge-Kutta-Fellberg > 7(8) > >>>> integrator in Python, which requires an extreme numerical precision > for my > >>>> particular application. Unfortunately, I can not seem to attain it. > >>>> The interesting part is if I take my exact code and translate it to > >>>> Matlab code (so I use the exact same process and numbers), I get a far > >>>> superior precision (the one I am expecting, in fact). This leads me to > think > >>>> I need to call a certain command in my Python script in order to make > sure > >>>> no truncation errors are building up over my integration. > >>>> > >>>> Has anyone had similar problems? Is there a difference between how > >>>> Matlab and Python store numbers, and if so how do I make Python more > >>>> accurate? > >>>> > > On Mon, Mar 7, 2011 at 10:36 AM, Jon Herman wrote: > > I'm starting to run out of ideas of what to do...I've imported the true > > division (I'm using Python 2.7) to make sure I wasn't accidentally using > any > > integers but the results remain identical, so it's not a division > problem. > > I've copied the loop I'm running below, is there any mathematical > operation > > I am making here that may have an accuracy problem? Thanks for any advice > > you can give! > > > > > Since the problem is quite possibly due to an imperfection in the > translation process, I think also posting your MATLAB integrator code > for comparison would be advisable. > > Cheers, > Chris > -- > Please avoid top-posting: > http://en.wikipedia.org/wiki/Top-posting > -- http://mail.python.org/mailman/listinfo/python-list
Re: Numerical representation
Thanks Terry! Of course, speed is not my main concern at this point and I'm more worried about precision...would you have some input on this discussion? :) Jon On Mon, Mar 7, 2011 at 2:35 PM, Terry Reedy wrote: > On 3/7/2011 1:59 PM, Jon Herman wrote: > >> And for the sake of completeness, the derivative function I am calling >> from my integrator (this is the 3 body problem in astrodynamics): >> >> def F(mu, X, ti): >> >> r1= pow((pow(X[0]+mu,2)+pow(X[1],2)+pow(X[2],2)),0.5) >> > >x0 = X[0]; x1 = X[1]; x2 = X[2] >r1 = sqrt((x0+mu)**2) + x1*x1 + x2*x2) >etc... > might be faster. Certainly, repeated lookups of pow is slow > and above is easier to read. > > r2= pow((pow(X[0]+mu-1,2)+pow(X[1],2)+pow(X[2],2)),0.5) >> >> Ax= X[0]+2*X[4]-(1-mu)*(X[0]+mu)/r1**3-mu*(X[0]-(1-mu))/r2**3 >> Ay= X[1]-2*X[3]-(1-mu)*X[1]/r1**3-mu*X[1]/r2**3 >> Az= -(1-mu)*X[2]/r1**3-mu*X[2]/r2**3 >> >> XDelta=array([X[3], X[4], X[5], Ax, Ay, Az]) >> >> return XDelta >> \ >> > > > -- > Terry Jan Reedy > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Numerical representation
Thanks all for the input, the remark about printing intermediate steps was a very good one (and so obvious I can't believe it took me this long to get there...) The error was in my loop where I multiply by the "b" or "beta" coefficients. The range for this loop (marked by j) is set up properly in Matlab (1 to nn) but in Python running this over range(nn) drops the values of the column 'nn'. That means the coefficients were all wrong. This quickly became apparent as I was printing a number of internal values. Thank you for the assistance, I apologize for asking your time for such a beginner's oversight...I'll be fluent in Python some day ;-) On Mon, Mar 7, 2011 at 5:34 PM, Robert Kern wrote: > On 3/7/11 2:52 PM, Jon Herman wrote: > >> It really is exactly the same process, but sure. Below is my Matlab >> translation >> of the python code I posted earlier, it functions at the increased >> accuracy I've >> shown above. >> >>k(:,1)=feval(deq, ti, x, mu); >> >> for n = 2:1:13 >> nn=n-1; >> Xtemp1 = 0.0; >> for j = 1:1:nn >> Xtemp1 = Xtemp1 + beta(n,j) * k(:,j); >> end >> x=xwrk+ dt * Xtemp1; >> ti=twrk+alph(n)*dt; >> k(:,n)=feval(deq, ti, x, mu); >> end >> >> Xtemp2=0.0; >> for l = 1:1:13 >> Xtemp2=Xtemp2+ch(l)*k(:,l); >> end >> >> >> x=xwrk + dt * Xtemp2; >> t=twrk+dt; >> > > You may want to try printing out values in both implementations to see > where they start to diverge. > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless > enigma > that is made terrible by our own mad attempt to interpret it as though it > had > an underlying truth." > -- Umberto Eco > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Reading/Writing files
Hello all, I am pretty new to Python and am trying to write data to a file. However, I seem to be misunderstanding how to do so. For starters, I'm not even sure where Python is looking for these files or storing them. The directories I have added to my PYTHONPATH variable (where I import modules from succesfully) does not appear to be it. So my question is: How do I tell Python where to look for opening files, and where to store new files? Thanks, Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading/Writing files
Jack, thanks. Alright, so what I did is create a file called hello.txt with a single line of text in there. I then did the following: f="fulldirectory\hello.txt" (where fulldirectory is of course the actual full directory on my computer) open("f", "w") And I get the following error: IOError: [Errno 13] Permission denied: 'f' If I open to read, I get: IOError: [Errno 2] No such file or directory: 'f' Can anyone explain to me why this happens? On Fri, Mar 18, 2011 at 3:50 PM, Jack Trades wrote: > > On Fri, Mar 18, 2011 at 4:33 PM, Jon Herman wrote: > >> Hello all, >> >> I am pretty new to Python and am trying to write data to a file. However, >> I seem to be misunderstanding how to do so. For starters, I'm not even sure >> where Python is looking for these files or storing them. The directories I >> have added to my PYTHONPATH variable (where I import modules from >> succesfully) does not appear to be it. >> >> So my question is: How do I tell Python where to look for opening files, >> and where to store new files? >> >> Thanks, >> >> Jon >> >> > By default Python will read and write files from the directory that your > program is run from. This cannot always be relied upon though (for instance > if your program was imported as a module from another program). > > To find out what directory your program is currently in use os.getcwd(). > Here's an example I just ran... > > >>> import os > >>> os.getcwd() > '/media/DATA/code/lispy/liSpy' > > The folder that is returned from os.getcwd() is the folder that "open" will > use. You can specify another folder by giving the full path. > > open("/full/path/to/file.txt", "w") > > PYTHONPATH is for importing modules, which is a separate concern. > > -- > Jack Trades > Pointless Programming Blog <http://pointlessprogramming.wordpress.com> > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading/Writing files
Folks, thanks for the many responses! Specifying the full file name (and not using parentheses when inappropriate, thanks Jack :)) I am now happily reading/writing files. My next question: what is the best way for me to write an array I generated to a file? And what is the best way for me to load that array again, the next time I start up my computer? Basically I am doing very large computations and want to store the results. Thanks a lot guys! Jon On Fri, Mar 18, 2011 at 4:18 PM, Dan Stromberg wrote: > > Are you on windows? > > You probably should use / as your directory separator in Python, not \. In > Python, and most other programming languages, \ starts an escape sequence, > so to introduce a literal \, you either need to prefix your string with r > (r"\foo\bar") or double your backslashes ("\\foo\\bar"). > > / works fine on windows, and doesn't require escaping ("/foo/bar"). > > On Fri, Mar 18, 2011 at 2:56 PM, Jon Herman wrote: > >> Jack, >> >> thanks. >> >> Alright, so what I did is create a file called hello.txt with a single >> line of text in there. I then did the following: >> >> f="fulldirectory\hello.txt" (where fulldirectory is of course the actual >> full directory on my computer) >> open("f", "w") >> >> And I get the following error: IOError: [Errno 13] Permission denied: 'f' >> If I open to read, I get: IOError: [Errno 2] No such file or directory: >> 'f' >> >> Can anyone explain to me why this happens? >> >> >> >> >> >> On Fri, Mar 18, 2011 at 3:50 PM, Jack Trades >> wrote: >> >>> >>> On Fri, Mar 18, 2011 at 4:33 PM, Jon Herman wrote: >>> >>>> Hello all, >>>> >>>> I am pretty new to Python and am trying to write data to a file. >>>> However, I seem to be misunderstanding how to do so. For starters, I'm not >>>> even sure where Python is looking for these files or storing them. The >>>> directories I have added to my PYTHONPATH variable (where I import modules >>>> from succesfully) does not appear to be it. >>>> >>>> So my question is: How do I tell Python where to look for opening files, >>>> and where to store new files? >>>> >>>> Thanks, >>>> >>>> Jon >>>> >>>> >>> By default Python will read and write files from the directory that your >>> program is run from. This cannot always be relied upon though (for instance >>> if your program was imported as a module from another program). >>> >>> To find out what directory your program is currently in use os.getcwd(). >>> Here's an example I just ran... >>> >>> >>> import os >>> >>> os.getcwd() >>> '/media/DATA/code/lispy/liSpy' >>> >>> The folder that is returned from os.getcwd() is the folder that "open" >>> will use. You can specify another folder by giving the full path. >>> >>> open("/full/path/to/file.txt", "w") >>> >>> PYTHONPATH is for importing modules, which is a separate concern. >>> >>> -- >>> Jack Trades >>> Pointless Programming Blog <http://pointlessprogramming.wordpress.com> >>> >>> >> >> -- >> >> http://mail.python.org/mailman/listinfo/python-list >> >> > -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading/Writing files
Wow, Jack, that is one awesome and simple module...thank you so much! I am happily storing and accessing all the arrays I could ever want :) Thanks to all for the quick assistance! On Fri, Mar 18, 2011 at 4:24 PM, Jack Trades wrote: > > On Fri, Mar 18, 2011 at 5:21 PM, Jon Herman wrote: > >> Folks, >> >> thanks for the many responses! Specifying the full file name (and not >> using parentheses when inappropriate, thanks Jack :)) I am now happily >> reading/writing files. >> >> My next question: what is the best way for me to write an array I >> generated to a file? >> And what is the best way for me to load that array again, the next time I >> start up my computer? >> >> Basically I am doing very large computations and want to store the >> results. >> >> Thanks a lot guys! >> >> Jon >> >> > To save a data structure like an array (called a list in Python), you can > use the pickle module. Here's the documentation on pickle. > > http://docs.python.org/library/pickle.html > > http://www.developertutorials.com/tutorials/python/python-persistence-management-050405-1306/ > > > > -- > Jack Trades > Pointless Programming Blog <http://pointlessprogramming.wordpress.com> > > -- http://mail.python.org/mailman/listinfo/python-list
python on iPad (PyPad)
Hi All, Sorry for the blatant advertising but hope some of you may be interested to know that I've created an iPad application containing the python interpreter and a simple execution environment. It's available in iTunes at http://itunes.apple.com/us/app/pypad/id428928902?mt=8# I wanted to have python available 'on the go' without carrying a laptop. The current implementation is based on my need to test simple python functions in an isolated environment. I hope to add more iOS specific capabilities if there is enough interest. Enjoy... Jon Dowdall -- http://mail.python.org/mailman/listinfo/python-list
Re: python on iPad (PyPad)
On Sat, 09 Apr 2011 00:36:58 -0700, Raymond Hettinger wrote: > On Apr 8, 10:13 pm, Jon Dowdall wrote: >> Hi All, >> >> Sorry for the blatant advertising but hope some of you may be >> interested to know that I've created an iPad application containing the >> python interpreter and a simple execution environment. It's available >> in iTunes athttp://itunes.apple.com/us/app/pypad/id428928902?mt=8# >> >> I wanted to have python available 'on the go' without carrying a >> laptop. The current implementation is based on my need to test simple >> python functions in an isolated environment. I hope to add more iOS >> specific capabilities if there is enough interest. > > I'm definitely interested in your adding more capabilities. > > Nice work so far. I'm surprised at the number of things that you got > work in the first version: long integers, simple classes, a number of > modules successfully import (math and itertools for example). > > I don't now how difficult the work is, but it would be nice to have a > way to save multiple scripts, have an interactive prompt, and get more > modules to work (random, collections, functools). > > Thanks again for your work. > > > Raymond > twitter: @raymondh Thanks for the great feedback. An updated version, that has some minor GUI improvements and allows the python code to be transferred via iTunes, is awaiting approval. Hope to get to some of the other items you mention as time permits :-) Cheers, Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: TextWrangler "run" command not working properly
On Apr 14, 9:52 pm, Fabio wrote: > Hi to all, > I have troubles with TextWrangler "run" command in the "shebang" (#!) > menu. > I am on MacOSX 10.6.7. > I have the "built-in" Python2.5 which comes installed by "mother Apple". > Then I installed Python2.6, and left 2.5 untouched (I was suggested to > leave it on the system, since "something might need it"). > > I ran the "Update Shell Profile.command", and now if I launch "python" > in the terminal it happily launches the 2.6 version. > Then I installed some libraries (scipy and matplotlib) on this newer 2.6 > version. > They work, and everything is fine. > > Then, I started to use TexWrangler, and I wanted to use the "shebang" > menu, and "run" command. > I have the "#! first line" pointing to the 2.6 version. > It works fine, as long as I don't import the libraries, in which case it > casts an error saying: > > ImportError: No module named scipy > > Maybe for some reason it points to the old 2.5 version. > But I might be wrong and the problem is another... > > I copy here the first lines in the terminal window if i give the "run in > terminal" command > > Last login: Thu Apr 14 22:38:26 on ttys000 > Fabio-Mac:~ fabio$ > /var/folders/BS/BSS71XvjFKiJPH3Wqtx90k+++TM/-Tmp-/Cleanup\ At\ > Startup/untitled\ text-324506443.860.command ; exit; > Traceback (most recent call last): > File "/Users/fabio/Desktop/test.py", line 3, in > import scipy as sp > ImportError: No module named scipy > logout > > [Process completed] > > where the source (test.py) contains just: > > #!/usr/bin/python2.6 > > import scipy as sp > > print "hello world" > > Any clue? > > Thanks > > Fabio http://www.velocityreviews.com/forums/t570137-textwrangler-and-new-python-version-mac.html ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding empty columns. Is there a faster way?
On Apr 21, 5:40 pm, nn wrote: > time head -100 myfile >/dev/null > > real 0m4.57s > user 0m3.81s > sys 0m0.74s > > time ./repnullsalt.py '|' myfile > 0 1 Null columns: > 11, 20, 21, 22, 23, 24, 25, 26, 27, 30, 31, 33, 45, 50, 68 > > real 1m28.94s > user 1m28.11s > sys 0m0.72s > > import sys > def main(): > with open(sys.argv[2],'rb') as inf: > limit = sys.argv[3] if len(sys.argv)>3 else 1 > dlm = sys.argv[1].encode('latin1') > nulls = [x==b'' for x in next(inf)[:-1].split(dlm)] > enum = enumerate > split = bytes.split > out = sys.stdout > prn = print > for j, r in enum(inf): > if j%100==0: > prn(j//100,end=' ') > out.flush() > if j//100>=limit: > break > for i, cur in enum(split(r[:-1],dlm)): > nulls[i] |= cur==b'' > print('Null columns:') > print(', '.join(str(i+1) for i,val in enumerate(nulls) if val)) > > if not (len(sys.argv)>2): > sys.exit("Usage: "+sys.argv[0]+ > " ") > > main() What's with the aliasing enumerate and print??? And on heavy disk IO I can hardly see that name lookups are going to be any problem at all? And why the time stats with /dev/null ??? I'd probably go for something like: import csv with open('somefile') as fin: nulls = set() for row in csv.reader(fin, delimiter='|'): nulls.update(idx for idx,val in enumerate(row, start=1) if not val) print 'nulls =', sorted(nulls) hth Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: checking if a list is empty
On May 7, 12:51 am, Ian Kelly wrote: > On Fri, May 6, 2011 at 4:21 PM, Philip Semanchuk wrote: > > What if it's not a list but a tuple or a numpy array? Often I just want to > > iterate through an element's items and I don't care if it's a list, set, > > etc. For instance, given this function definition -- > > > def print_items(an_iterable): > > if not an_iterable: > > print "The iterable is empty" > > else: > > for item in an_iterable: > > print item > > > I get the output I want with all of these calls: > > print_items( list() ) > > print_items( tuple() ) > > print_items( set() ) > > print_items( numpy.array([]) ) > > But sadly it fails on iterators: > print_items(xrange(0)) > print_items(-x for x in []) > print_items({}.iteritems()) My stab: from itertools import chain def print_it(iterable): it = iter(iterable) try: head = next(it) except StopIteration: print 'Empty' return for el in chain( (head,), it ): print el Not sure if I'm truly happy with that though. Jon Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Lies in education [was Re: The "loop and a half"]
On 2017-10-12, Ben Bacarisse wrote: > Chris Angelico writes: >> Normally, with a Python-based framework, you don't need _any_ web >> server configuration. You simply define your URL routing within the >> Python code. The only thing the web server needs to know is where to >> find the web app, and that's sufficiently standard that it can be done >> off-the-shelf; for instance, you push your code to Heroku, and they >> set everything up to pass requests to your app. Not possible with PHP, >> since you need *custom* web server config to manage your rewrite >> rules. > > That's at odds with what I've read online which admittedly may be all > junk. I wanted to try Flask so I installed the Ubuntu packages but then > got stuck on a huge document that suggested I needed to install things > called Nginx and Gunicorn. You've now mentioned another: Heroku. I'm > sure the complex instructions I found are not really required -- it was > probably just the usual "this is what I did so this is how it's done" > document, but I'm having trouble finding the simpler way to do it. > > Since no web server configuration is needed (I have a working Apache > installation that mirrors, as closely as possible, what my hosting > provider uses) it should be relatively easy. Can you tell me, or can > you point me to a resource that tells me, where to put the app? I don't > yet know what "push your code to Heroku" means. "don't need _any_ web server configuration" is rather, er, optimistic. For Apache you'd need the mod_proxy_uwsgi module installed, and the config would be something like this: DocumentRoot /srv/www/appname/appname ProxyPass uwsgi://127.0.0.1:3031/ ProxyPass ! and you need an app container listening on the port defined above, e.g. uwsgi with config like: /etc/uwsgi/apps-available/appname.ini: [uwsgi] plugin = python3 socket = 127.0.0.1:3031 threads = 4 master = 1 chdir = /srv/www/appname module = appname:app # https://github.com/unbit/uwsgi/issues/1126 wsgi-disable-file-wrapper = true and you'll need something to run uwsgi on system startup. -- https://mail.python.org/mailman/listinfo/python-list
Re: Lies in education [was Re: The "loop and a half"]
On 2017-10-12, Ben Bacarisse wrote: > I see. If I'm reading this right, the app requests are passed through > to another server -- uWSGI. Yes. It doesn't have to be uWSGI; it could be gunicorn, or you could probably use Apache's mod_fcgid. As a last resort you could use CGI, which wouldn't involve any long-running processes, which has the benefit of not requiring any special support from your host but the disadvantage of most likely being very slow indeed. > How does this typically work on low-cost hosting? I may be able to set > up the ProxyPass locally (i.e. in .htaccess) but I won't be able to > write /etc/uwsgi/apps-available/appname.ini. Maybe there are a locally > defined .ini files that uwsgi reads? You need to choose a host that supports one of the relevant systems mentioned above. If you already have a host then it's possible they already do, otherwise you may need to choose another. -- https://mail.python.org/mailman/listinfo/python-list
Re: Report on non-breaking spaces in posts (was: How to join elements at the beginning and end of the list)
On 2017-10-31, Stefan Ram wrote: > Ned Batchelder writes: >> Â Â Â def wrapped_join(values, sep): > > Ok, here's a report on me seing non-breaking spaces in > posts in this NG. I have written this report so that you > can see that it's not my newsreader that is converting > something, because there is no newsreader involved. Yes Ned's post definitely had non-breaking spaces in; it looks like Thunderbird is turning strings of spaces into ' \xa0+ ', presumably because things that expect HTML will squash runs of spaces (even though the post is not HTML...) By the way I would commend 'od -tx1z' to you as it is *far* easier to read: 0012460 20 0a 65 6e 63 61 70 73 75 6c 61 74 65 20 69 74 > .encapsulate it< 0012500 3a 0a 0a 20 c2 a0 c2 a0 c2 a0 20 64 65 66 20 77 >:.. .. def w< 0012520 72 61 70 70 65 64 5f 6a 6f 69 6e 28 76 61 6c 75 >rapped_join(valu< -- https://mail.python.org/mailman/listinfo/python-list
Re: replacing `else` with `then` in `for` and `try`
On 2017-11-01, Alexey Muranov wrote: > what do you think about the idea of replacing "`else`" with "`then`" in > the contexts of `for` and `try`? > > It seems clear that it should be rather "then" than "else." Compare > also "try ... then ... finally" with "try ... else ... finally". > > Currently, with "else", it is almost impossible to guess the meaning > without looking into the documentation. Why would we want to make the language worse? It is fairly obvious what 'else' means, whereas 'then' has an obvious meaning that is in fact the opposite of what it would actually do. It seems clear that 'else' is the correct word (or at least, far better than 'then'). Maybe the change should be that it is a syntax error to use a 'for/while...else' with no 'break'. -- https://mail.python.org/mailman/listinfo/python-list
Re: replacing `else` with `then` in `for` and `try`
On 2017-11-02, Steve D'Aprano wrote: > On Fri, 3 Nov 2017 12:39 am, Jon Ribbens wrote: >> Why would we want to make the language worse? It is fairly obvious >> what 'else' means, > > Yes, obvious and WRONG. Nope, obvious and right. > for x in seq: > do_something() > else: > print("seq was empty") > > is an obvious, common and wrong interpretation. No, it's an obvious bug. You have a 'for...else' with no 'break'. Like I said, that should probably be a syntax error. >> whereas 'then' has an obvious meaning that is in >> fact the opposite of what it would actually do. > > Er... is today opposite day? Because 'then' describes precisely what it > actually does. No, 'then' describes the opposite of what it does. The word 'then' implies something that always happens next, whereas 'else' conveys the correct meaning, which is something that happens if the course of the preceding piece of code did not go as expected. > Perhaps before we continue, we should ask what you think for...else > and while...else statements do. Just to be sure we are all on the > same page here. I think they do what the Python Language Reference sections 8.2 and 8.3 say they do. >> It seems clear that 'else' is the correct word (or at least, far >> better than 'then'). > > Not clear at all. The 'for...else' block is a common source of > confusion, if it was clear what it did, people wouldn't so often get > it wrong. That might be an argument that it is imperfect. It doesn't even begin to constitute an argument that 'then' would be an improvement. >> Maybe the change should be that it is a syntax error to use a >> 'for/while...else' with no 'break'. > > Only if you want to make the experience of using Python in the interactive > interpreter worse. See my recent post: > > "A use-case for for...else with no break" Yes, I saw that. It's possible you are the only person in the world ever to have done that. It would not make the interactive interpreter 'worse' in the slightest for that silly trick to be lost. -- https://mail.python.org/mailman/listinfo/python-list
Re: replacing `else` with `then` in `for` and `try`
On 2017-11-03, Alexey Muranov wrote: > 'Then' describes what happens next indeed, unless some extraordinary > situation prevents it from happening, for example: > > try: > go_to_the_bakery() > then: > buy_croissants(2) > except BakeryClosed: > go_to_the_grociery() > buy_baguette(1) > finally: > come_back() > > I know this is a poor program example (why not to use a boolean return > value instead of an exception, etc.), and i know that currently in > Python `except` must precede `else`, it is just to illustrate the > choice of terms. It looks like you're suggesting not just changing the 'else' keyword to 'then', but changing the syntax completely. The above is certainly not an improvement on the status quo, as it is completely counter-intuitive that exceptions in the 'then' clause will not be caught by the 'except' clauses. -- https://mail.python.org/mailman/listinfo/python-list
Re: replacing `else` with `then` in `for` and `try`
On 2017-11-03, Steve D'Aprano wrote: > On Fri, 3 Nov 2017 03:31 am, Jon Ribbens wrote: >> No, it's an obvious bug. You have a 'for...else' with no 'break'. >> Like I said, that should probably be a syntax error. > > It should absolutely not be a syntax error. There's no reason for it > to be a syntax error, except to satisfy some arrogant and foolish > idea of purity. It'd be nice if you could be a little less rude. It's not an "arrogant and foolish idea of purity", it's to help people catch bugs in their code, and to aid their understanding of the language. > There are uncountable ways of writing code which is seemingly > "pointless", and we don't make it a syntax error. And there's uncountable ways of writing code which we *do* make a syntax error. I'm not sure what your point is there. >> No, 'then' describes the opposite of what it does. The word 'then' >> implies something that always happens next, > > Right, which is what happens with the for...else block. No. Ok, so look. It's obvious that you and I have different mental models of the situation here. You're thinking of 'for...else' as two arbitrary clauses that run consecutively unless the whole thing is aborted by a 'break', whereas I'm thinking of the 'for' clause as being a search for a situation that matches a condition and the 'else' clause being what happens if the condition is not matched (i.e. exactly the same as 'if...else'). Now there's nothing inherently *wrong* with your choice of mental model, except it's leading you into confusion because my model means the meaning of the 'else' keyword is intuitive and obvious, and yours means it's counter-intuitive and confusing. Your suggestion is that the fix is to change the language, my suggestion is to fix your model. I'd suggest that changing your mind is easier than changing the language ;-) Also, my model has the advantage that if what the 'for' clause is doing doesn't match the concept of 'searching for a match' then it's obvious that you shouldn't be using 'for...else' in the first place. Again, sure, the language doesn't currently strictly enforce the requirement for a 'break', but nevertheless if you don't have one you almost certainly have a bug. >> Yes, I saw that. It's possible you are the only person in the world >> ever to have done that. It would not make the interactive interpreter >> 'worse' in the slightest for that silly trick to be lost. > > Just because you personally haven't used this technique doesn't make it > a "silly trick". It's an incredibly obscure work-around for a different problem, i.e. an infelicity in the way the interactive prompt parses input blocks. If the parsing is a genuine issue then the answer is to fix that, not to look for hacks that almost never help anyway. > And while I thank you for the complement that I am the cleverest and most > insightful Python coder in the world, I didn't say anything even remotely resembling that. -- https://mail.python.org/mailman/listinfo/python-list
Re: replacing `else` with `then` in `for` and `try`
On 2017-11-03, Chris Angelico wrote: > On Fri, Nov 3, 2017 at 10:49 PM, Jon Ribbens > wrote: >>> It should absolutely not be a syntax error. There's no reason for it >>> to be a syntax error, except to satisfy some arrogant and foolish >>> idea of purity. >> >> It'd be nice if you could be a little less rude. It's not an "arrogant >> and foolish idea of purity", it's to help people catch bugs in their >> code, and to aid their understanding of the language. > > That wasn't rudeness. Yes, it was. >> No. Ok, so look. It's obvious that you and I have different mental >> models of the situation here. You're thinking of 'for...else' as two >> arbitrary clauses that run consecutively unless the whole thing is >> aborted by a 'break', whereas I'm thinking of the 'for' clause as >> being a search for a situation that matches a condition and the >> 'else' clause being what happens if the condition is not matched >> (i.e. exactly the same as 'if...else'). >> >> Now there's nothing inherently *wrong* with your choice of mental >> model, except it's leading you into confusion because my model means >> the meaning of the 'else' keyword is intuitive and obvious, and yours >> means it's counter-intuitive and confusing. Your suggestion is that >> the fix is to change the language, my suggestion is to fix your model. >> I'd suggest that changing your mind is easier than changing the >> language ;-) > > If anything, I would say that Steven's model is at a lower abstraction > layer than yours Yes, absolutely. > - though IMO your model is more of an example use-case than a > description of what is actually happening. It's a high-level way of thinking about it that avoids confusion and leads to correct code. Unless you can come up with a *sensible* use-case that it doesn't cover, it's also a comprehensive way of thinking about it. > TBH I prefer the "if 1:" trick to gather code into a block. But that > requires pre-planning, Or pressing up-arrow ;-) > whereas slapping an "else:" after the loop can be done after the event. -- https://mail.python.org/mailman/listinfo/python-list
Re: replacing `else` with `then` in `for` and `try`
On 2017-11-03, Steve D'Aprano wrote: > The for loop does not necessarily perform a search: > > count = 1 > for obj in sequence: > if count > MAX_OBJECTS: > print("too many objects, halting") > break > process(obj) > else: > print("finished") > > According to your mental model, this code is... what? Illegal? Silly? > Impossible? A syntax error? That conforms to my model. It's searching for the condition 'count > MAX_OBJECTS'. > Even when there is a search, the sense of the search may be reversed > from your mental model: > > for request in sequence: > if 'please' not in request: > break > process(request) > else: > print('done') > return > raise SysExit('failure to say please') That's searching for the condition "'please' not in request". > It doesn't matter if you can think of alternative ways of writing these code > snippets. They're legal, and they work, and your mental model doesn't cover > them, at all or easily. That means your mental model is at best incomplete. You've yet to come up with one that it doesn't cover. > In other words, it was only when I realised that `else` would be better > written as `then` that I understood the behaviour of the statement. If I read > it as "else", then the natural interpretation is that the block runs only if > the loop doesn't run: You're repeating yourself. > You'll probably dismiss them as "silly" because they don't meet your mental > model, and insist that I would be wrong to use them. As above, you're mistaken. > Your response to code that doesn't match your mental model is to say > that it is obviously wrong and probably buggy and should be made > into a syntax error if possible. No, it isn't. Try reading again what I actually wrote. > And my mental model is to treat "else" in this concept as some foreign word, > perhaps Dutch, a false-friend that actually means "next" but due to some > awful coincidence happens to look exactly like the English word for "else". And that's leading you into confusion, as you've demonstrated. > Ah yes, because it is inconceivable that anyone might have thought of a use > for for...else without a break. It's not inconceivable, but nobody has thought of a sensible use so far (by which I mean one that shows it's a useful feature). > What matters is that only code that matches your model is allowed. You do seem to enjoy telling other people what their opinions are. Frankly, I don't know why I even bother having opinions when you're here to tell me what they are. In future, I'll just come to you to find out what I think. >> It's an incredibly obscure work-around for a different problem, > > You mean a different problem to "searching"? Yes indeed it is. No, I mean a problem that has nothing to do with 'for...else' clauses. If your door is stuck then you might climb in through a window, but that doesn't mean that the purpose of windows is to be entrances, or that windows should be designed to make them better entrances, or that the problem isn't the stuck door. >>> And while I thank you for the complement that I am the cleverest and most >>> insightful Python coder in the world, >> >> I didn't say anything even remotely resembling that. > > That was sarcasm. Yes dear. -- https://mail.python.org/mailman/listinfo/python-list
Re: replacing `else` with `then` in `for` and `try`
On 2017-11-04, Michael Torrie wrote: > On 11/03/2017 09:06 PM, Chris Angelico wrote: >> On Sat, Nov 4, 2017 at 1:57 PM, Michael Torrie wrote: >>> On 11/03/2017 07:09 PM, Steve D'Aprano wrote: That's incorrect. There are multiple ways to exit a loop that will prevent the `else` block from executing, `break` is only one. >>> >>> Such as? >> >> There are many. But other than break, I don't know of any that WOULD >> execute the next line of code immediately _after_ the loop. > > Can you be more specific? What are some of these "many" ways of aborting > a loop? Help a guy out here. > > I know, for example, that we have exceptions. But those hardly matter in > this discussion because they wouldn't execute the else clause either. > They'd either be caught elsewhere, or end the program. sys.exit() can > also terminate a for loop, but it terminates the whole program without > running the else statement. Yes, those are the sort of thing that Steve was referring to. He was being unhelpfully pedantic. A giant meteor destroying the computer the program was running on would prevent the 'else' block from executing too. -- https://mail.python.org/mailman/listinfo/python-list
Re: Ideas about how software should behave (was: replacing `else` with `then` in `for` and `try`)
On 2017-11-04, Ben Finney wrote: > To respond to the criticism of an idea – criticism containing no mention > of the person – as though it “clearly refers to the [person]”, is of > significant concern on a software dicussion forum such as this. No, the thing that is "of significant conern on a software discussion forum such as this" is people such as yourself defending the abuse of other contributors. -- https://mail.python.org/mailman/listinfo/python-list
Re: replacing `else` with `then` in `for` and `try`
On 2017-11-05, Steve D'Aprano wrote: > On Sat, 4 Nov 2017 04:44 am, Jon Ribbens wrote: >> That conforms to my model. It's searching for the condition >> 'count > MAX_OBJECTS'. > > That's sounds to me that you are willing to call just about any test of a > condition inside a loop a "search". I don't think that's helpful. I think it > is mangling the word to the point it is meaningless. You're entitled to your opinion, of course. I've provided you with a way of thinking about 'for...else' that makes its purpose and meaning intuitively obvious. You'd apparently prefer to think about it a different way that makes it counter-intuitive, and would like to change the language to match your thinking instead. I'd regretfully suggest that that is not going to happen. > How about a loop that exits at some random time? Is that a search? > > for i in range(100, 0, -1): > if flip_coin() == 'Heads': Of course, that's searching for the condition "flip_coin() == 'Heads'". > What you actually wrote, in various posts: > > You have a 'for...else' with no 'break'. Like I said, that should > probably be a syntax error. > > if what the 'for' clause is doing doesn't match the concept > of 'searching for a match' then it's obvious that you shouldn't > be using 'for...else' in the first place. > > the language doesn't currently strictly enforce the requirement > for a 'break', but nevertheless if you don't have one you almost > certainly have a bug. > > I stand by my comment as an accurate description of your response. You're conflating two completely different points: * whether to allow 'for...else' with no 'break' * how to think about 'for...else' with a 'break' My comment about syntax errors only has any relevance at all to the former point. > I find the code useful. I shouldn't have to justify why it is useful to me, > but for the record it especially comes in handy when I've already typed out a > multi-line loop in the REPL, and only then realised that I'll need some way > to add an extra line at the end. Just press up arrow to go back and edit the first line of your current input and insert an 'if 1:' as someone else suggested. > And yes, a better REPL would also solve that problem. But I have to use the > REPL that exists, not the imaginary one that I'd like. If you want to call > this a hackish work around for a limitation of the REPL, I'll say... okay. > What's your point? It is still useful. The question isn't whether it's useful, but whether it's *more* useful than preventing bugs in peoples' code. You're entitled to your opinion on that, and I'm entitled to mine, and neither opinion is 'arrogant'. We're all just making guesses in the dark, unless anybody has any statistics about how much time is wasted each year by bugs caused by 'for...else' with no 'break' and how much time would be wasted each year by people being unable to use your REPL trick. -- https://mail.python.org/mailman/listinfo/python-list