Re: [Tutor] Should a beginner learn Python 3.x
>> Should I advise him to >> stick with 2.6 for a bit, since most of the material out there will >> be for 2.x? Or since he's learning from scratch, should he jump >> straight to 3.x In which case what can you recommend for him to work >> through - I must stress he has absolutely no clue at all about >> programming, no education beyond 16 yrs old, but is keen to learn. > > (While I'm aware this answer may launch sharp replies from some of you, I'll > be bold and say what I mean;-) > > I guess python is no more a language especially suited for absolute beginners > for a while already. It has grown too many features, too much complication > and several layers of abstraction. So, my answers would not be py2.6 or > py3.1, rather py1.5. very bold indeed. if learning purely for being introduced to programming, etc., not work-related, etc., you have an interesting idea. but even so, you don't have to learn *every* feature of 2.x or 3.x to learn programming. you can just learn the 1.5 syntax. basically, i'm not going to give a sharp reply, however, it is an unusual suggestion, but it isn't mind-blowing as there are some systems out there that are built using 1.5, e.g., the Red Hat installer, http://www.scons.org ... etc. it also helps keep the 1st edition of my book "Core Python Programming" alive -- that edition was written against 1.5.2, 1.6, and 2.0 back in 2000-2001. :-) cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Python Web Development with Django", Addison Wesley, (c) 2009 http://withdjango.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] I love python / you guys :)
hi, Although not a question, i just want to tell you guys how awesome you are! I am not a programmer, i can do a bit of bash. I have never officially learnt programming, but numerous times looked at some perl, c, java and never really gotten past the beginning stages of it. That all changed when i picked up python. Although my style and use of python is probably barbaric at best, I really enjoy it when you can make things work. Python was just amazing from a readability / logical point of view. If i can think of something, there is a way to do it in python. After learning the simple data structures it seems i can really do anything i want to. I really appreciate all the effort the knowledgeable guys put into this list (you know who you are! you post answers all the time!). I've asked a couple of questions and the answers have always been excellent and i've learnt so much. So i guess i just wanted to say thanks for supporting a great language, and helping me as i learn to shape my thinking into better and better ways to do things. stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing code while tired, counterproductive?
>From first-hand experience, i would concur :) A refreshed mind will perform much better than an over-exerted one. On Sat, Nov 14, 2009 at 8:57 PM, Alan Gauld wrote: > > "Modulok" wrote > > Does anyone else find, writing code while tired to be counterproductive? >> > > Yes. Doing anything that is mentally taxing is usually a bad idea when > tired! > > Alan G > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Regards, Lloyd ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unexpected iterator
Le Sun, 15 Nov 2009 19:23:33 -, "Alan Gauld" s'exprima ainsi: > What does 'unpack' mean? I've seen a few Python errors about packing > and unpacking. What does it mean? Unpacking is rarely needed. It matches some kind of problems. Imagine you parse "codes" each made of name-sep-number. Then when walking through the result you can write: for code in codes: (name,sep,number) = code (parens not needed) It's just an elegant manner to avoid indexing -- right? Denis * la vita e estrany * http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
Marc Tompkins wrote: On Sun, Nov 15, 2009 at 4:11 PM, Stephen Nelson-Smith wrote: import gzip from heapq import heappush, heappop, merge Is this a preferred method, rather than just 'import heapq'? It has a couple of advantages: - convenience: if you "import heapq", then to do a push you need to type "heapq.heappush"; if you do "from heapq import heappush", then you can simply type "heappush" to use it. - efficiency: you import only what you actually need to use. Importing all from xx import yy, zz gets the whole module xx, not just the functions yy and zz. It just doesn't add the module name to the present global namespace. That changes the way you reference yy and zz, but it doesn't decrease the load time or the memory used. And if you want to cheat, you can reference the rest of the module with something like: sys.modules["heapq"].heapify() or even heapq = sys.modules["heapq"] which then gets you where you would have been had you just imported the module the first place. of a gigantic package to use one or two methods is wasteful; on the other hand, if a package contains five methods and you're using four of them, this might not be such a big deal. Now you switch from talking about modules (heapq) to packages. I don't believe packages are loaded monolithically, but just modules. Remember, this is Python... there's always going to be more than one way to do it, and you'll get an argument on this list for every one of them. What you should NEVER do, though: "from package import *" Definite agreement on that one. Consider the following two lines: from hashlib import md5 q3 = sys.modules["hashlib"].sha1 Without the first line, the second one gets an error. So the import is indeed getting more than just the md5 constructor. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
On Sun, Nov 15, 2009 at 8:57 PM, Marc Tompkins wrote: > On Sun, Nov 15, 2009 at 4:11 PM, Stephen Nelson-Smith > wrote: >> >> > import gzip >> > from heapq import heappush, heappop, merge >> >> Is this a preferred method, rather than just 'import heapq'? >> > It has a couple of advantages: ... > - efficiency: you import only what you actually need to use. Importing all > of a gigantic package to use one or two methods is wasteful; I can't think of any way in which import heapq is "wasteful" compared to from heapq import heappush, heappop, merge other than a tiny efficiency in name lookup (it's faster to lookup just heappush than heapq.heappush). In either case, the entire module is loaded. The only difference is in which names are introduced into the importing module's namespace. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find Integer co-ordinates lying on a circle
regarding Integer co-ordinates within a circle got a nice link : http://mathworld.wolfram.com/GausssCircleProblem.html ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find Integer co-ordinates lying on a circle
@DaveA: thanks for pointing it out. For a origin-centre circle x**2 + y**2 = r**2, I assumed r to be integer, however it was r**2 which was integer. A mistake on my part. On Mon, Nov 16, 2009 at 6:41 AM, Dave Angel wrote: > spir wrote: > >> Le Sun, 15 Nov 2009 09:11:16 +0530, >> Shashwat Anand s'exprima ainsi: >> >> >> >>> No, I'm trying to find all integer co-ordinates which lies on a circle. >>> Say for a circle of radius 5 the co-ordinates are [(5, 0), (0, 5), (-5, >>> 0), >>> (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3, >>> 4), (-4, 3), (-3, -4), (-4, -3)] which lies on circle x**2 + y**2 =**2 >>> (as >>> >>> Origin is the centre of the circle.) >>> Now I want a table of these points for say r = to upper bound. >>> So for r =, the only point lying on it is (0,0) >>> For r =, the points lying on them (the circle x**2 + y**2 = 1**2) is [(1, >>> >>> 0), (0, 1), (-1, 0), (0, -1)] >>> And so forth. >>> >>> Thus the table shows the points (x,y) which are lying on the circle of >>> radius 'r'. >>> >>> radius 'r' - (x, y) >>> 0 - (0, 0) >>> 1 - (1, 0), (0, 1), (-1, 0), (0, -1) >>> 2 - (2, 0), (0, 2), (-2, 0), (0, -2) >>> 3 - (3, 0), (0, 3), (-3, 0), (0, -3) >>> 4 - (4, 0), (0, 4), (-4, 0), (0, -4) >>> 5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), >>> (-3, >>> 4), (-4, 3), (-3, -4), (-4, -3) >>> >>> Which is correct. I'm not trying to find all integer co-ordinates within >>> a >>> circle but trying to create a table of points lying on the circle of >>> radius >>> 'r', varying the radius from '0' to upper bound 'r'. >>> The bruteforce can be done as: >>> >>> #R =pper bound >>> >>> for r in range(0, R+1): >>>for x in range(0, R+1): >>>for y in range(0, R+1): >>>if x**2 + y**2 =r**2: >>>#store them >>> >>> However the complexity reaches O(n**3) and it's not optimized at all. So >>> I >>> though of using pythagorean triplets. >>> >>> >> >> Interesting! As some said previously, you only need to find point for a >> quadrant, then extrapolate. >> I can see two approaches to find _integer_ coordinate pairs on a circle of >> given radius: >> >> * Walk the circle itself. I mean start with a point on it (0,r), then find >> an algorithm to walk step by step (unit by unit) while keeping as close as >> possible to the circle. This means rounding to next int. Eg for r= you would >> step on (0,2), (1,1), (2,0), (1,-1),... For each pair, simply check whether >> x² + y² = 2². >> >> Got an algorithm used for antialiasing and plotting circle known as bresanham's algorithm ( http://en.wikipedia.org/wiki/Midpoint_circle_algorithm) but hadn't actually worked on it. > >> * Solve the above equation in integer domain, again by an approaching >> algorithm, maybe taking profit of know points (where either x or y is 0). >> >> I guess such approaches can be interesting, compared to brute force, only >> in case of very big number of radii or very big radii. >> >> Denis >> >> >> > What you don't know is that the OP's original unstated (and probably at > that time unknown) requirement included circles of non-integer radius, as > long as three of the points on such a circle land on integer vertices. For > example, the points (8, 1), (1, -8), (-4, 7). That little tidbit never > made it into the thread. > > DaveA > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] getting python 3 to run from the command line (version2)
Forgive the top-posting, but when in Rome... Running 'chcp' at the command line will show the default code page. Judging from the OP's name it is probably an Arabic version of Windows. Since Python 2.6 works it probably is falling back to something besides cp720. Try: import sys print sys.stdout.encoding to find out what. Python 3.0 probably has a bug if it runs but doesn't work correctly as described by the OP below. Python 3.1 "refuses to guess" and displays an error. Since Python 3.X uses Unicode for strings it really needs to know the encoding of the terminal to decode stdin and encode to stdout. Implementing a cp720 codec would likely fix the problem. -Mark "ALAN GAULD" wrote in message news:339296.66934...@web86707.mail.ird.yahoo.com... Forwarding to the tutor list with cut n paste sessions. It looks to me like the code page issue somebody else referred to is the problem but the behaviour seems a bit extreme, I'd have thought it might have chosen a default value or something... But I'm not sure what causes it to select cp720 in the first place. Where does Py_Initialize get its values? Are they set in the environment somewhere? And why is 2.6 OK? I know 3.0 did some changes around locale handling but I can't recall what they were. I assume this is a side effect of those changes? Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ - Forwarded Message From: Khalid Al-Ghamdi To: ALAN GAULD Sent: Sunday, 15 November, 2009 18:02:25 Subject: Re: [Tutor] getting python 3 to run from the command line (version 2) Hi, I'm really sorry for this hassle! Python31 (gives error): C:\Users\KE>python Fatal Python error: Py_Initialize: can't initialize sys standard streams LookupError: unknown encoding: cp720 This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. C:\Users\KE>cd c:\python31 c:\Python31>python Fatal Python error: Py_Initialize: can't initialize sys standard streams LookupError: unknown encoding: cp720 This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. Python30 (doesn't respond): c:\Python31>cd c:\python30 c:\Python30>python Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. 2+2 Python26(works ok): C:\Users\KE>cd c:\python26 c:\Python26>python Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. 2+2 4 Thanks very much On Sun, Nov 15, 2009 at 8:16 PM, ALAN GAULD wrote: Unfortunately the image didn't get to me. To copy/paste from a cmd window click the small icon at the left hand end of the title bar. From that select Edit->Mark Use the mouse to select the text you want to copy Use the menu again to do Edit->Copy Now paste into your mail program. You can make this easier by opening the properties dialog from the same menu, and on the Options tag tick QuickEdit. This will allow you to select with the mouse and copy by hitting return after selecting. This is very useful when sending python examples to the tutor list! :-) Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ From: Khalid Al-Ghamdi To: Alan Gauld Sent: Sunday, 15 November, 2009 7:11:06 Subject: Re: [Tutor] getting python 3 to run from the command line (version 2) Hi Alan, this is how the problem looks from my work pc (which is xp. at home i use vista). and yes i don't know how copy and paste from the cmd so here is an image: As you can see when I try to access python31 it gives me the error above. When I use python 26 it works fine. Now, I don't have python30 installed at my work PC, but what happens is when I enter c:\python30\python it initiates and gives me a blank prompt(>>>) then when i enter something (2+2 for example) it returns (>>>) as if i just pressed enter without entering any code. It just doesn't process the code for me. I hope you can be of help. thanks On Sat, Nov 14, 2009 at 9:56 PM, Alan Gauld wrote: "Khalid Al-Ghamdi" wrote when i try to code something it just gives me a new line without any processing of the code. (for example:2+2 returns a new line) You mean you get to the >>> prompt? And you type 2+2 you get this: 2+2 With just a newline between your input and the next >>> prompt? when I tried to change the path to the directory that contains python 31 and enter *python (c:\python31>)*, I'm not surprised it doesn't work, that should have python trying to start executing a folder. But *fatal python error: Py_Initialize: can't initialize sys standard streams
Re: [Tutor] Iterable Understanding
On Sun, Nov 15, 2009 at 4:11 PM, Stephen Nelson-Smith wrote: > > import gzip > > from heapq import heappush, heappop, merge > > Is this a preferred method, rather than just 'import heapq'? > > It has a couple of advantages: - convenience: if you "import heapq", then to do a push you need to type "heapq.heappush"; if you do "from heapq import heappush", then you can simply type "heappush" to use it. - efficiency: you import only what you actually need to use. Importing all of a gigantic package to use one or two methods is wasteful; on the other hand, if a package contains five methods and you're using four of them, this might not be such a big deal. Remember, this is Python... there's always going to be more than one way to do it, and you'll get an argument on this list for every one of them. What you should NEVER do, though: "from package import *" -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find Integer co-ordinates lying on a circle
spir wrote: Le Sun, 15 Nov 2009 09:11:16 +0530, Shashwat Anand s'exprima ainsi: No, I'm trying to find all integer co-ordinates which lies on a circle. Say for a circle of radius 5 the co-ordinates are [(5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3, 4), (-4, 3), (-3, -4), (-4, -3)] which lies on circle x**2 + y**2 =**2 (as Origin is the centre of the circle.) Now I want a table of these points for say r = to upper bound. So for r =, the only point lying on it is (0,0) For r =, the points lying on them (the circle x**2 + y**2 = 1**2) is [(1, 0), (0, 1), (-1, 0), (0, -1)] And so forth. Thus the table shows the points (x,y) which are lying on the circle of radius 'r'. radius 'r' - (x, y) 0 - (0, 0) 1 - (1, 0), (0, 1), (-1, 0), (0, -1) 2 - (2, 0), (0, 2), (-2, 0), (0, -2) 3 - (3, 0), (0, 3), (-3, 0), (0, -3) 4 - (4, 0), (0, 4), (-4, 0), (0, -4) 5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3, 4), (-4, 3), (-3, -4), (-4, -3) Which is correct. I'm not trying to find all integer co-ordinates within a circle but trying to create a table of points lying on the circle of radius 'r', varying the radius from '0' to upper bound 'r'. The bruteforce can be done as: #R =pper bound for r in range(0, R+1): for x in range(0, R+1): for y in range(0, R+1): if x**2 + y**2 =r**2: #store them However the complexity reaches O(n**3) and it's not optimized at all. So I though of using pythagorean triplets. Interesting! As some said previously, you only need to find point for a quadrant, then extrapolate. I can see two approaches to find _integer_ coordinate pairs on a circle of given radius: * Walk the circle itself. I mean start with a point on it (0,r), then find an algorithm to walk step by step (unit by unit) while keeping as close as possible to the circle. This means rounding to next int. Eg for r= you would step on (0,2), (1,1), (2,0), (1,-1),... For each pair, simply check whether x² + y² = 2². * Solve the above equation in integer domain, again by an approaching algorithm, maybe taking profit of know points (where either x or y is 0). I guess such approaches can be interesting, compared to brute force, only in case of very big number of radii or very big radii. Denis What you don't know is that the OP's original unstated (and probably at that time unknown) requirement included circles of non-integer radius, as long as three of the points on such a circle land on integer vertices. For example, the points (8, 1), (1, -8), (-4, 7). That little tidbit never made it into the thread. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
Hi Marty, Thanks for a very lucid reply! > Well, you haven't described the unreliable behavior of unix sort so I > can only guess, but I assume you know about the --month-sort (-M) flag? Nope - but I can look it up. The problem I have is that the source logs are rotated at 0400 hrs, so I need two days of logs in order to extract 24 hrs from to 2359 (which is the requirement). At present, I preprocess using sort, which works fine as long as the month doesn't change. > import gzip > from heapq import heappush, heappop, merge Is this a preferred method, rather than just 'import heapq'? > def timestamp(line): > # replace with your own timestamp function > # this appears to work with the sample logs I chose > stamp = ' '.join(line.split(' ', 3)[:-1]) > return time.strptime(stamp, '%b %d %H:%M:%S') I have some logfie entries with multiple IP addresses, so I can't split using whitespace. > class LogFile(object): > def __init__(self, filename, jitter=10): > self.logfile = gzip.open(filename, 'r') > self.heap = [] > self.jitter = jitter > > def __iter__(self): > while True: > for logline in self.logfile: > heappush(self.heap, (timestamp(logline), logline)) > if len(self.heap) >= self.jitter: > break Really nice way to handle the batching of the initial heap - thank you! > try: > yield heappop(self.heap) > except IndexError: > raise StopIteration > > logs = [ > LogFile("/home/stephen/qa/ded1353/quick_log.gz"), > LogFile("/home/stephen/qa/ded1408/quick_log.gz"), > LogFile("/home/stephen/qa/ded1409/quick_log.gz") > ] > > merged_log = merge(*logs) > with open('/tmp/merged_log', 'w') as output: > for stamp, line in merged_log: > output.write(line) Oooh, I've never used 'with' before. In fact I am currently restricted to 2.4 on the machine on whch this will run. That wasn't a problem for heapq.merge, as I was just able to copy the code from the 2.6 source. Or I could use Kent's recipe. > ... which probably won't preserve the order of log entries that have the > same timestamp, but if you need it to -- should be easy to accommodate. I don't think that is necessary, but I'm curious to know how... Now... this is brilliant. What it doesn't do that mine does, is handle date - mine checks for whether it starts with the appropriate date, so we can extract 24 hrs of data. I'll need to try to include that. Also, I need to do some filtering and gsubbing, but I think I'm firmly on the right path now, thanks to you. > HTH, Very much indeed. S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unexpected iterator
"Stephen Nelson-Smith" wrote To upack your variables a and b you need an iterable object on the right side, which returns you exactly 2 variables What does 'unpack' mean? I've seen a few Python errors about packing and unpacking. What does it mean? It has a coup[le of uses, the one being referred to here is where we take a sequence of values and assign each value to a corresponding variable on the other side of the assignment. x,y = (val1, val2) which is equivalent to x = val1; y = val2 or j,k = [3,4] j=3; k=4 You can also pack a set of variables into a single value: t = 3,4,5 or s = 4,5,6 But this is really just tuple assignment without the parentheses! :-) Pack/Unpack can also refer to an operation of the struct module, which does a similar thing with binary data. So if we have a set of bytes b we can extract the bytes into a set of variables using a format string and the struct.unpack() operation. Conversely you use struct.pack to encode a sequence of values into a sequence of bytes. But thats a more specialised usage, not seen so often. HTH -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Fw: getting python 3 to run from the command line (version 2)
Forwarding to the tutor list with cut n paste sessions. It looks to me like the code page issue somebody else referred to is the problem but the behaviour seems a bit extreme, I'd have thought it might have chosen a default value or something... But I'm not sure what causes it to select cp720 in the first place. Where does Py_Initialize get its values? Are they set in the environment somewhere? And why is 2.6 OK? I know 3.0 did some changes around locale handling but I can't recall what they were. I assume this is a side effect of those changes? Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ - Forwarded Message From: Khalid Al-Ghamdi To: ALAN GAULD Sent: Sunday, 15 November, 2009 18:02:25 Subject: Re: [Tutor] getting python 3 to run from the command line (version 2) Hi, I'm really sorry for this hassle! Python31 (gives error): C:\Users\KE>python Fatal Python error: Py_Initialize: can't initialize sys standard streams LookupError: unknown encoding: cp720 This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. C:\Users\KE>cd c:\python31 c:\Python31>python Fatal Python error: Py_Initialize: can't initialize sys standard streams LookupError: unknown encoding: cp720 This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. Python30 (doesn't respond): c:\Python31>cd c:\python30 c:\Python30>python Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 2+2 >>> Python26(works ok): C:\Users\KE>cd c:\python26 c:\Python26>python Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 2+2 4 >>> Thanks very much On Sun, Nov 15, 2009 at 8:16 PM, ALAN GAULD wrote: Unfortunately the image didn't get to me. > > >To copy/paste from a cmd window click the small icon >at the left hand end of the title bar. > > >From that select Edit->Mark >Use the mouse to select the text you want to copy >Use the menu again to do Edit->Copy > > >Now paste into your mail program. > > >You can make this easier by opening the properties >dialog from the same menu, and on the Options tag >tick QuickEdit. This will allow you to select with >the mouse and copy by hitting return after selecting. > > >This is very useful when sending python examples >to the > tutor list! :-) > > > >Alan Gauld >Author of the Learn To Program website > >http://www.alan-g.me.uk/ > > > > > > From: Khalid Al-Ghamdi >To: Alan Gauld >Sent: Sunday, 15 November, 2009 7:11:06 >Subject: Re: [Tutor] getting python 3 to run from the command line (version 2) > > > >Hi Alan, > > >this is how the problem looks from > my work pc (which is xp. at home i use vista). and yes i don't know how copy > and paste from the cmd so here is an image: > > > > > > > >As you can see when I try to access python31 it gives me the error above. When >I use python 26 it works fine. >Now, I don't have python30 installed at my work PC, but what happens is when I >enter c:\python30\python it initiates and gives me a blank prompt(>>>) then >when i enter something (2+2 for example) it returns (>>>) as if i just pressed >enter without entering any code. It just doesn't process the code for me. > > >I hope you can be of help. > > >thanks > > >On Sat, Nov 14, 2009 at 9:56 PM, Alan Gauld wrote: > > "Khalid Al-Ghamdi" wrote >> >> >> >when i try to code something it just gives me a new line without any >>processing of the code. (for example:2+2 returns a new line) >>> >> >>You mean you get to the >>> prompt? And you type 2+2 you get this: >> >> >>> > > > >2+2 > >> >>> > > > > With just a newline between your input and the next >>> prompt? >> >> >when I tried to change the path to the directory that contains python 31 >and >>> >>>enter *python (c:\python31>)*, >>> I'm not surprised it doesn't work, that should have python trying to start executing a folder. But >> >> >*fatal python error: Py_Initialize: can't initialize sys standard streams >>> I get a very different message: >> C:\Documents and Settings\Alan Gauld>python (C:\Python31) python: can't open file '(C:\Python31)': [Errno 22] Invalid argument >> >> >> >When i change the directory to c:\python26 and then enter it works ok. >>so can anyone tell me why this is happening? >>> >> >>Nope, sorry, I don't understand how the 2.6 version works if you are passing in a folder as you did for 3.1 >> Do you know how to cut n paste from a cmd window> It would probably help if you pasted in the actuall sessions into your mail. >>
Re: [Tutor] Find Integer co-ordinates lying on a circle
Le Sun, 15 Nov 2009 09:11:16 +0530, Shashwat Anand s'exprima ainsi: > > No, I'm trying to find all integer co-ordinates which lies on a circle. > Say for a circle of radius 5 the co-ordinates are [(5, 0), (0, 5), (-5, 0), > (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3, > 4), (-4, 3), (-3, -4), (-4, -3)] which lies on circle x**2 + y**2 = 5**2 (as > Origin is the centre of the circle.) > Now I want a table of these points for say r = 0 to upper bound. > So for r = 0, the only point lying on it is (0,0) > For r = 1, the points lying on them (the circle x**2 + y**2 = 1**2) is [(1, > 0), (0, 1), (-1, 0), (0, -1)] > And so forth. > > Thus the table shows the points (x,y) which are lying on the circle of > radius 'r'. > > radius 'r' - (x, y) > 0 - (0, 0) > 1 - (1, 0), (0, 1), (-1, 0), (0, -1) > 2 - (2, 0), (0, 2), (-2, 0), (0, -2) > 3 - (3, 0), (0, 3), (-3, 0), (0, -3) > 4 - (4, 0), (0, 4), (-4, 0), (0, -4) > 5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3, > 4), (-4, 3), (-3, -4), (-4, -3) > > Which is correct. I'm not trying to find all integer co-ordinates within a > circle but trying to create a table of points lying on the circle of radius > 'r', varying the radius from '0' to upper bound 'r'. > The bruteforce can be done as: > > #R = upper bound > for r in range(0, R+1): > for x in range(0, R+1): > for y in range(0, R+1): > if x**2 + y**2 == r**2: > #store them > > However the complexity reaches O(n**3) and it's not optimized at all. So I > though of using pythagorean triplets. Interesting! As some said previously, you only need to find point for a quadrant, then extrapolate. I can see two approaches to find _integer_ coordinate pairs on a circle of given radius: * Walk the circle itself. I mean start with a point on it (0,r), then find an algorithm to walk step by step (unit by unit) while keeping as close as possible to the circle. This means rounding to next int. Eg for r=2, you would step on (0,2), (1,1), (2,0), (1,-1),... For each pair, simply check whether x² + y² = 2². * Solve the above equation in integer domain, again by an approaching algorithm, maybe taking profit of know points (where either x or y is 0). I guess such approaches can be interesting, compared to brute force, only in case of very big number of radii or very big radii. Denis * la vita e estrany * http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Should a beginner learn Python 3.x
Le Sat, 14 Nov 2009 20:49:52 +, Stephen Nelson-Smith s'exprima ainsi: > My brother in law is learning python. He's downloaded 3.1 for > Windows, and is having a play. It's already confused him that print > "hello world" gives a syntax error > > He's an absolute beginner with no programming experience at all. I > think he might be following 'Python Programming for the Absolute > Beginner", or perhaps some online guides. Should I advise him to > stick with 2.6 for a bit, since most of the material out there will > be for 2.x? Or since he's learning from scratch, should he jump > straight to 3.x In which case what can you recommend for him to work > through - I must stress he has absolutely no clue at all about > programming, no education beyond 16 yrs old, but is keen to learn. > > S. > (While I'm aware this answer may launch sharp replies from some of you, I'll be bold and say what I mean;-) I guess python is no more a language especially suited for absolute beginners for a while already. It has grown too many features, too much complication and several layers of abstraction. So, my answers would not be py2.6 or py3.1, rather py1.5. Lua is certainly great in comparison to present python in this respect --except if you think builtin OO is a must. But lua is "prepared" for OO anyway (its tables are dict-like objects), & several libraries provide it. Lua + OO framework is close to python 1.5 from my point of view, except less builtin sweeties, and even cleaner syntax (half-way between python and pascal, but unfortunately no indented structure). Denis * la vita e estrany * http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Should a beginner learn Python 3.x
>> i get asked this question a lot, esp. when it pertains to my book, >> "Core Python Programming." which should i learn? is your book >> obsolete? etc. i basically tell them that even though they are >> backwards-incompatible, it's not like Python 2 and 3 are so >> different that you wouldn't recognize the language anymore! as Kris >> has said, there are just a handful of noticeable difference that you >> have to just keep in mind. finally, the next edition of the book will >> definitely be BOTH Python 2 and 3. Python 2 isn't EOL'd and will be >> around for awhile longer -- the most important evidence of this being >> that both 2.x and 3.x are being developed in parallel. > > I just ordered your great book 2nd edition. I dont know if i should get > worried using a dated version. All i want is to learn the language. The > transition process (i think) should just follow normally once you learn the > language. So far I'm just a newbie trying to learn. you see? that's *exactly* what i'm talking about. :-) my stance for all my books and my courses is that i focus on teaching you the core fundamentals of the Python language. i'm less interested in the minor differences between releases that newbies will not likely use anyway. however, i cannot ignore the fact that 3.x is backwards-incompatible with older releases, so those are the opportune times to mention the differences. the next edition of the book will not exist for several years, and in that book, it will be a combo of Python 2 and 3, not purely just 3. in the most recent printing of the 2nd ed., the publishers have let me add 2 new appendices, one on Python 3 as the next generation of the language, and another on Python 2.6 and the remaining 2.x releases and their transition role. these should be able to tie y'all over until 3rd ed. :-) cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Python Web Development with Django", Addison Wesley, (c) 2009 http://withdjango.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
Stephen Nelson-Smith wrote: >> It's unclear from your previous posts (to me at least) -- are the >> individual log files already sorted, in chronological order? > > Sorry if I didn't make this clear. No they're not. They are *nearly* > sorted - ie they're out by a few seconds, every so often, but they are > in order at the level of minutes, or even in the order of a few > seconds. > > It was precisely because of this that I decided, following Alan's > advice, to pre-filter the data. I compiled a unix sort command to do > this, and had a solution I was happy with, based on Kent's iterator > example, fed into heapq.merge. > > However, I've since discovered that the unix sort isn't reliable on > the last and first day of the month. So, I decided I'd need to sort > each logfile first. The code at the start of *this* thread does this > - it uses a heapq per logfile and is able to produce a tuple of > timestamp, logline, which will be in exact chronological order. What > I want to do is merge this output into a file. Well, you haven't described the unreliable behavior of unix sort so I can only guess, but I assume you know about the --month-sort (-M) flag? I did misunderstand your intent for this thread, so thanks for clarifying. The fact remains that if you are interested in using heapq.merge, then you need to pass it iterable objects. And, I don't see any reason to avoid adapting your approach to fit heapq.merge. How about something like the following (completely untested) ... import gzip from heapq import heappush, heappop, merge def timestamp(line): # replace with your own timestamp function # this appears to work with the sample logs I chose stamp = ' '.join(line.split(' ', 3)[:-1]) return time.strptime(stamp, '%b %d %H:%M:%S') class LogFile(object): def __init__(self, filename, jitter=10): self.logfile = gzip.open(filename, 'r') self.heap = [] self.jitter = jitter def __iter__(self): while True: for logline in self.logfile: heappush(self.heap, (timestamp(logline), logline)) if len(self.heap) >= self.jitter: break try: yield heappop(self.heap) except IndexError: raise StopIteration logs = [ LogFile("/home/stephen/qa/ded1353/quick_log.gz"), LogFile("/home/stephen/qa/ded1408/quick_log.gz"), LogFile("/home/stephen/qa/ded1409/quick_log.gz") ] merged_log = merge(*logs) with open('/tmp/merged_log', 'w') as output: for stamp, line in merged_log: output.write(line) ... which probably won't preserve the order of log entries that have the same timestamp, but if you need it to -- should be easy to accommodate. HTH, Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unexpected iterator
On So, 2009-11-15 at 15:12 +, Stephen Nelson-Smith wrote: > > To upack your variables a and b you need an iterable object on the right > > side, which returns you exactly 2 variables > > What does 'unpack' mean? I've seen a few Python errors about packing > and unpacking. What does it mean? Unpacking refers to taking a collection of values and assigning it to a collection of variables. (a,b) = (1,2,3) # this won't work Let's see what it actually means (in pseudo-code): (a,b)[0] = (1,2,3)[0] # a = 1 (a,b)[1] = (1,2,3)[1] # b = 2 (a,b)[2] = (1,2,3)[2] # undefined! = 3 Same goes for the inverse: (a,b,c) = (1,2) # this won't work either in pseudo code: (a,b,c)[0] = (1,2)[0] # a = 1 (a,b,c)[1] = (1,2)[1] # b = 2 (a,b,c)[2] = (1,2)[2] # c = undefined! Basically, just make sure your len(variables) == len(values) before trying to use the unpacking syntax: (a,b,c) = (1,2,3) # this works (a,b) = (1,2) # this works, too Cheers Alan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unexpected iterator
> To upack your variables a and b you need an iterable object on the right > side, which returns you exactly 2 variables What does 'unpack' mean? I've seen a few Python errors about packing and unpacking. What does it mean? S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nul file in Windows
Timo List wrote: For my program I disable the py2exe log feature by routing output to the nul-file. Code: if win32 and py2exe: sys.stdout = open("nul", "w") sys.stderr = open("nul", "w") This always worked fine. Today, I received an email from a user with the following error: IOError: [Errno 2] No such file or directory: 'nul' Now, I thought the nul-file always existed, shouldn't it? Is there another way to disable output, if this one fails? Cheers, Timo All you need is an object that behaves like a file, but does nothing with the data sent to it. That's what duck-typing is all about. I haven't tried it, but I'd start by making a new class: class NullFile(object): def __init__(self, *arg, **kwarg): pass def write(self, data, *arg, **kwarg): pass def close(self, *arg, **kwarg): pass and just say sys.stdout = NullFile() If you get any exceptions, you could add new methods to this file, accordingly. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nul file in Windows
On Sun, Nov 15, 2009 at 6:39 AM, Timo List wrote: > For my program I disable the py2exe log feature by routing output to the > nul-file. > Code: > > if win32 and py2exe: > sys.stdout = open("nul", "w") > sys.stderr = open("nul", "w") > > This always worked fine. > > Today, I received an email from a user with the following error: > IOError: [Errno 2] No such file or directory: 'nul' > > Now, I thought the nul-file always existed, shouldn't it? > Is there another way to disable output, if this one fails? > Just try/except: try: #put your code here except IOError: #don't save, print a message, whatever you want to do #if you want to propagate the error, uncomment next line #raise HTH, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] parsing XML into a python dictionary
Christopher Spears, 14.11.2009 19:47: > Thanks! I have a lot of XML files at work that users search through. I > want to parse the XML into a python dictionary and then read the dictionary > into a database that users can use to search through the thousands of files. I think "database" is the right keyword here. Depending on how large your "thousands of files" are and what the actual content of each file is, a full-text search engine (e.g. pylucene) or an XML database might be the right tool, instead of trying to write something up yourself. If you want to use something that's in Python's standard library, consider parsing the XML files as a stream instead of a document tree (look for the iterparse() function in lxml.etree or the xml.etree.ElementTree package), and safe the extracted data into a sqlite3 database. You can also use such a database as a kind of cache that stores relevant information for each file, and update that information whenever you notice that a file has been modified. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OT: Writing code while tired, counterproductive?
Le Sat, 14 Nov 2009 10:43:47 -0700, Modulok s'exprima ainsi: > List, > > This is kind off topic, but: > > Does anyone else find, writing code while tired to be counterproductive? > > It just seems like when I push myself to stay up late finishing a > project, I sorely regret it the following day. Granted, Python is a > fairly forgiving language and doesn't take the mental attention that > some others require (like C++ ...or someone else's C++ or someone > else's poorly written C++), but for me, writing code while tired > usually ends up being a bad thing - In any language. But then there's > the guilt of taking the day off and not meeting deadlines. *grumble* > Just wondering if this is a common occurrence among the masses. > > Anyone? > -Modulok- 100% agree denis * la vita e estrany * http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] nul file in Windows
For my program I disable the py2exe log feature by routing output to the nul-file. Code: if win32 and py2exe: sys.stdout = open("nul", "w") sys.stderr = open("nul", "w") This always worked fine. Today, I received an email from a user with the following error: IOError: [Errno 2] No such file or directory: 'nul' Now, I thought the nul-file always existed, shouldn't it? Is there another way to disable output, if this one fails? Cheers, Timo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
Hi Martin, Thanks for a very detailed response. I'm about to head out, so I can't put your ideas into practice yet, or get down to studying for a while. However, I had one thing I felt I should respond to. > It's unclear from your previous posts (to me at least) -- are the > individual log files already sorted, in chronological order? Sorry if I didn't make this clear. No they're not. They are *nearly* sorted - ie they're out by a few seconds, every so often, but they are in order at the level of minutes, or even in the order of a few seconds. It was precisely because of this that I decided, following Alan's advice, to pre-filter the data. I compiled a unix sort command to do this, and had a solution I was happy with, based on Kent's iterator example, fed into heapq.merge. However, I've since discovered that the unix sort isn't reliable on the last and first day of the month. So, I decided I'd need to sort each logfile first. The code at the start of *this* thread does this - it uses a heapq per logfile and is able to produce a tuple of timestamp, logline, which will be in exact chronological order. What I want to do is merge this output into a file. I think I probably have enough to be getting on with, but I'll be sure to return if I still have questions after studying the links you provided, and trying the various suggestions people have made. Thanks so very much! S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find Integer co-ordinates lying on a circle
Shashwat Anand wrote: > How to find all possible integer co-ordinates lying on a circle of given radius 'r'. > If given the upper bound of 'r', I want to calculate all given co-ordinates lying for 0 <= r <= n > > Let's say the upper bound of radius is 5 > All possible results are: > radius 'r' - (x, y) > 0 - (0, 0) > 1 - (1, 0), (0, 1), (-1, 0), (0, -1) > 2 - (2, 0), (0, 2), (-2, 0), (0, -2) > 3 - (3, 0), (0, 3), (-3, 0), (0, -3) > 4 - (4, 0), (0, 4), (-4, 0), (0, -4) > 5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3, 4), (-4, 3), (-3, -4), (-4, -3) > > Also for a particular radius, lots of possible combination of (x, y) is possible so best datastructure could be defaultdict for further operations IMHO. > So my desired output is: > defaultdict(, {0 : [(0, 0)], 1: [(1, 0), (0, 1), (-1, 0), (0, -1)], 2: [(2, 0), (0, 2), (-2, 0), (0, -2)], 3: [(3, 0), (0, 3), (-3, 0), (0, -3)], 4: [(4, 0), (0, 4), (-4, 0), (0, -4)], 5: [(5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3, 4), (-4, 3), (-3, -4), (-4, -3)]}) > I think you only need to consider the first quadrant (positive x and y). So if you find one result you can determine the other seven (or three for a point on the axis). One approach might be to use complex numbers: m = complex(0, 1).__mul__ rng4 = range(4) def rotate_and_mirror(z): for _ in rng4: yield z yield z.conjugate() z = m(z) or: def rotate_and_mirror2(x, y): z = complex(x, y) for _ in rng4: yield z yield z.conjugate() z = m(z) for z in rotate_and_mirror2(3, 4): print z (3+4j) (3-4j) (-4+3j) (-4-3j) (-3-4j) (-3+4j) (4-3j) (4+3j) > My approach using pythagorean triplets: > >>> d = collections.defaultdict(list) > >>> s = list(set([((u*u + v*v), (v*v - u*u, 2*u*v)) for u in range(10) for v in range(u+1, 10)])) > >>> [d[k].append(v) for k,v in s] > However it sure is wrong and fails in my case. > > Any suggestions as to how can I reach my desired goal, or are there any other options to tackle this problem? > I think there are formulas for finding triples, eg. def primitive_triples(N): for i in xrange(1, N): m = 2 * i h = m * (i+1) yield m+1, h, h+1 So it seems the problem is finding primitive triples and multiples of those triples. ie. (3, 4) is on the circle with radius 5, so (6, 8) is on the circle with radius 10 etc. That's the best I've got - my "maths brain" is much atrophied! Good luck. Gerard ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] opening a file directly from memory
mj...@iol.pt wrote: da...@ieee.org wrote: (You forgot to send this message to the list, so I'm forwarding it) mj...@iol.pt wrote: da...@ieee.org wrote: mj...@iol.pt wrote: I'm wondering if I must save a file to memory before opening it. By opening I mean displaying it to the user. I have a BLOB field in a db and I have managed to read the blob into a binary fileobject. I've also managed to write it to disk and then I open it by doubleclicking on it. But I was wondering: 1. How to open the file directly from code (and not by double clicking): I'm aware of os.startfile on Win by I'm on a Mac now, so I rather have a cross-platform way of accomplishing this. 2. If there is any Python module that takes care of saving and cleaning temp files in an OS transparent way? Txs, Miguel You don't say what this binary data is. Is there a specific program that should be launched to "display it to the user" ? Or do you have to keep this general? If you know what the necessary program is, you could use subprocess module to launch it. But I don't know enough about the Mac to know how to do the Mac equivalent of os.startfile As for avoiding the use of a file, that depends entirely on the program you're launching. Some programs can be told to get their data from stdin. If that's the case, there's a way to provide stdin directly from Python, using subprocess. As for temporary files, consider tempfile module. I haven't used it, but it looks promising. HTH, DaveA I know what the binary data is from the database. Typically it would be some file the OS knows how to open. Chiefly I'm trying to avoid making the user save the file on some location and then go and double click it to open it. This makes no sense, since the user has already made the decision to open the file. Miguel Txs Dave. You forgot to answer the question. You say "The OS knows how to open". Does *your* *program* know what program is needed, to open this particular binary data? Yes. My program knows. A database column stores the complete file name (including extension), and I can be certain the applications will be available to run the file. Miguel Then just use the subprocess module to invoke the program, passing it the filename as an argument. No need for any double-clicking. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
Stephen Nelson-Smith wrote: > I think I'm having a major understanding failure. Perhaps this will help ... http://www.learningpython.com/2009/02/23/iterators-iterables-and-generators-oh-my/ > So in essence this: > > logs = [ LogFile( "/home/stephen/qa/ded1353/quick_log.gz", "04/Nov/2009" ), > LogFile( "/home/stephen/qa/ded1408/quick_log.gz", "04/Nov/2009" ), > LogFile( "/home/stephen/qa/ded1409/quick_log.gz", "04/Nov/2009" ) ] > > Gives me a list of LogFiles - each of which has a getline() method, > which returns a tuple. > > I thought I could merge iterables using Kent's recipe, or just with > heapq.merge() But, at this point are your LogFile instances even iterable? AFAICT, the answer is no, and I think you should want them to be in order to use heapq.merge. Have a look at the documentation (http://docs.python.org/library/stdtypes.html#iterator-types) and then re-read Kent's advice, in your previous thread ('Logfile multiplexing'), about "using the iterator protocol" (__iter__). And, judging by the heapq docs (http://docs.python.org/library/heapq.html#heapq.merge) ... """ Merge multiple sorted inputs into a single sorted output (for example, merge timestamped entries from multiple log files). Returns an iterator over the sorted values. """ ... using heapq.merge appears to be a reasonable approach. You might also be interested to know, that while heapq.merge is(was) new in 2.6, it's implementation is very similar (read: nearly identical) to the one of the cookbook recipes referenced by Kent. It's unclear from your previous posts (to me at least) -- are the individual log files already sorted, in chronological order? I'd imagine they are, being log files. But, let's say you were to run your hypothetical merge script against only one file -- would the output to be identical to the input? If not, then you'll want to sort the inputs first. > > But how do I get from a method that can produce a tuple, to some > mergable iterables? > I'm going to re-word this question slightly to "How can I modify the LogFile class, for instances to be usable by heapq.merge?" and make an attempt to answer. The following borrows heavily from Kent's iterator example, but removes your additional line filtering (if self.stamp.startswith(date), etc) to, hopefully, make it clearer. import time, gzip, heapq def timestamp(line): # replace with your own timestamp function # this appears to work with the sample logs I chose stamp = ' '.join(line.split(' ', 3)[:-1]) return time.strptime(stamp, '%b %d %H:%M:%S') class LogFile(object): def __init__(self, filename): self.logfile = gzip.open(filename, 'r') def __iter__(self): for logline in self.logfile: yield (timestamp(logline), logline) logs = [ LogFile("/home/stephen/qa/ded1353/quick_log.gz"), LogFile("/home/stephen/qa/ded1408/quick_log.gz"), LogFile("/home/stephen/qa/ded1409/quick_log.gz") ] merged = heapq.merge(*logs) with open('/tmp/merged_log', 'w') as output: for stamp, line in merged: output.write(line) Will it be fast enough? I have no clue. Good luck! Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] opening a file directly from memory
wrote You forgot to answer the question. You say "The OS knows how to open". Does *your* *program* know what program is needed, to open this particular binary data? Yes. My program knows. A database column stores the complete file name (including extension), and I can be certain the applications will be available to run the file. You still haven't answered the question. We have established that a) The OS knows what program is associated with the file b) your program knows the file name and location But does your program - or more specifically you as the author - know what program the OS will use to open the file? If you do you can call it explicitly, but if you do not then you need to find a way of getting the OS to tell you, or to leave it to the OS. For example a txt file could be opened by any of hundreds of text editors, depending on what the user selected, but a Photoshop file will typically only be opened by photoshop. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor