[Tutor] getting python 3 to run from the command line
Hi, for some reason I haven't been able to get python 3 to work from the the command line. I've added it to the the path in the the system variables but still no luck. when I try to run python 2.6 it works flawlessly, though, bearing in mind that it wasn't added to the system variables! i'm running windows vista and have python 30 ,31 and 26 installed. all of this , by the way, was just to get easy install to work so i can get nose working to do some practice of unit testing! can anyone shed some light on the issue? thanks ___ 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" wrote I've been working on a way to parse an XML document and convert it into a python dictionary. I want to maintain the hierarchy of the XML. Here is the sample XML I have been working on: Neil Gaiman Glyn Dillon Charles Vess This is my first stab at this: #!/usr/bin/env python from lxml import etree def generateKey(element): if element.attrib: key = (element.tag, element.attrib) else: key = element.tag return key So how are you handling multiple identical tags? It looks from your code that you will replace the content of the previous tag with the content of the last found tag? I would expect your keys to have some reference to either the parse depth or a sequuence count. In your sample XML the problem never arises and maybe in your real data it will never happen either, but in the general case it is quite common for the same tag and attribute pair to be used multiple times in a document. class parseXML(object): def __init__(self, xmlFile = 'test.xml'): self.xmlFile = xmlFile def parse(self): doc = etree.parse(self.xmlFile) root = doc.getroot() key = generateKey(root) dictA = {} for r in root.getchildren(): keyR = generateKey(r) if r.text: dictA[keyR] = r.text if r.getchildren(): dictA[keyR] = r.getchildren() The script doesn't descend all of the way down because I'm not sure how to hand a XML document that may have multiple layers. Advice anyone? Would this be a job for recursion? Recursion is the classic way to deal with tree structures so yes you could use there. provided your tree never exceeds Pythons recursion depth limit (I think its still 1000 levels). I'm not sure how converting etree's tree structure into a dictionary will help you however. It seems like a lot of work for a small gain. 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
Re: [Tutor] getting python 3 to run from the command line
"Khalid Al-Ghamdi" wrote in message news:dfac564f0911140040y27e0bee5ub02aa2de2b02f...@mail.gmail.com... Hi, for some reason I haven't been able to get python 3 to work from the the command line. I've added it to the the path in the the system variables but still no luck. when I try to run python 2.6 it works flawlessly, though, bearing in mind that it wasn't added to the system variables! i'm running windows vista and have python 30 ,31 and 26 installed. all of this , by the way, was just to get easy install to work so i can get nose working to do some practice of unit testing! can anyone shed some light on the issue? How are you starting python? If you type 'python' and C:\Python31 is the first thing in your path, you should see Python 3.1 execute. If you type 'somefile.py' the Windows command line uses the registered program for the '.py' extension to run the script. It doesn't use the PATH. You can see the default program for an extension from the command line with the 'assoc' and 'ftype' commands: C:\>assoc .py .py=Python.File C:\>ftype Python.File Python.File="C:\Python26\python.exe" "%1" %* -Mark ___ 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
On Sat, Nov 14, 2009 at 1:14 AM, Christopher Spears wrote: > I've been working on a way to parse an XML document and convert it into a > python dictionary. I want to maintain the hierarchy of the XML. Here is the > sample XML I have been working on: > > > > Neil Gaiman > Glyn Dillon > Charles Vess > > > This is the output: > 163>./parseXML.py > {'collection': {('comic', {'number': '62', 'title': 'Sandman'}): [ writer at -482193f4>, , -482193a4>]}} This seems an odd format. How are you going to use it? How is this better than the native ElementTree structure? > The script doesn't descend all of the way down because I'm not sure how to > hand a XML document that may have multiple layers. Advice anyone? Would > this be a job for recursion? Yes. Here is an example that might be helpful: http://code.activestate.com/recipes/410469/ Kent ___ 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
On Sat, Nov 14, 2009 at 3:40 AM, Khalid Al-Ghamdi wrote: > Hi, > for some reason I haven't been able to get python 3 to work from the the > command line. I've added it to the the path in the the system variables but > still no luck. when I try to run python 2.6 it works flawlessly, though, > bearing in mind that it wasn't added to the system variables! > i'm running windows vista and have python 30 ,31 and 26 installed. > all of this , by the way, was just to get easy install to work so i can get > nose working to do some practice of unit testing! I don't think setuptools supports Python 3. There is a fork of setuptools called distribute that does support Python 3. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
Hi, >> for log in logs: >> l = log.getline() >> print l >> >> This gives me three loglines. How do I get more? Other than while True: >> > I presume that what you want is to get all lines from each log. Well... what I want to do is create a single, sorted list by merging a number of other sorted lists. > for log in logs: > for line in log.getlines(): > print l This gives me three lines. S. ___ 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
Khalid Al-Ghamdi wrote: Hi, for some reason I haven't been able to get python 3 to work from the the command line. I've added it to the the path in the the system variables but still no luck. when I try to run python 2.6 it works flawlessly, though, bearing in mind that it wasn't added to the system variables! i'm running windows vista and have python 30 ,31 and 26 installed. all of this , by the way, was just to get easy install to work so i can get nose working to do some practice of unit testing! can anyone shed some light on the issue? thanks We need more details to really diagnose the problem, although you've given enough for us to guess. If you really want to help us help you, explain what you mean by "haven't been able to get..."Just what do you try, and what happens when you try it? What does your PATH look like, and where is the python installation you're trying to run. I'll make some guesses anyway. You've got Vista, and you're running from a command prompt. When you try the following: c:\programming\sources\project\ > python myscript.py the system runs Python 2.6 on your script (which is in the project directory). Now you want it to run Python 3.1 instead. I have no clue what you've tried. There are four variables I know of which help determine whether you can run a python script in various ways, and using various installations of python. Two of these are in the registry, and two are in the environment (environment variables). Keywords are assoc, ftype, PATHEXT, and PATH I'll start with the environment variable PATH. This determines how the system interprets word "python" on the command above. If you have multiple python.exe files, you can choose between them in two different ways: you can change the PATH, or you can change what you type. My preference is as follows: Make a batch directory (Mine is called m:\t\bat ) and use it to put all your batch files for various purposes. Add that directory to your PATH to point there (by using Control panel, so it will still work on reboots). Close any existing command lines (DOS boxes), because they won't have the new PATH value. Start a new one, and check PATH to make sure it's there, and spelled right. For each installation of python.exe, create a pythonNN.bat which knows how to run that particular version. So I might have a python26.bat which has the following two lines in it: Echo Running Python version 2.6 c:\python26\python.exe %* When everything's working, you can take out the Echo line, and add a '@' symbol in front of the other line. The other three things I mentioned only affect you if you're trying to run a python script "directly", without typing "python" in front of its name. One other thing: It's useful to have a simple script called version.py, which looks something like: import sys print (sys.version) (but don't indent the lines, naturally. For that matter, don't indent them in the bat files above either.) HTH, DaveA ___ 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
"Khalid Al-Ghamdi" wrote for some reason I haven't been able to get python 3 to work from the the command line. I've added it to the the path in the the system variables but still no luck. when I try to run python 2.6 it works flawlessly, though, bearing in mind that it wasn't added to the system variables! Can you print the output from PATH? That should tell you what is included in your path, so your Python30 or python31 folder should show up. i'm running windows vista and have python 30 ,31 and 26 installed. How are you calling Python from the command line? I assume you start a CMD window and then just type python? If so it will execute whichever python it finds first. You might want to create aliases for python30, python31 and python26 that execute the right python.exe In fact if you dreate batch files you an also set the PYTHONPATH to the right folders too if there are different folders for pytghon 3 and python 2 files - which is usually necessary! HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
On Sat, Nov 14, 2009 at 7:34 AM, Stephen Nelson-Smith wrote: > > Well... what I want to do is create a single, sorted list by merging a > number of other sorted lists. > > Just write your own merge: (simplified and probably inefficient and first thing off the top of my head) newlist = [] for x, y, z in zip(list1, list2, list3): if y > x < z: newlist.append(x) elif x > y < z: newlist.append(y) elif x > z < y: newlist.append(z) I'm pretty sure that should work although it's untested. 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] Iterable Understanding
Gah! Failed to reply to all again! On Sat, Nov 14, 2009 at 1:43 PM, Stephen Nelson-Smith wrote: > Hi, >> I'm not 100% sure to understand your needs and intention; just have a try. >> Maybe what you want actually is rather: >> >> for log in logs: >> for line in log: >> print l > > Assuming you meant print line. This also gives me just three lines. > >> Meaning your log objects need be iterable. To do this, you must have an >> __iter__ method that would surely simply return the object's getline (or >> maybe replace it alltogether). > > I'm not sure I fully understand how that works, but yes, I created an > __iter__ method: > > def __iter__(self): > self.line=self.logfile.readline() > stamp=self.timestamp(self.line) > heapq.heappush(self.heap, (stamp, self.line)) > pop = heapq.heappop(self.heap) > yield pop > > But I still don't see how I can iterate over it... I must be missing > something. > > I thought that perhaps I could make a generator function: > > singly = ((x.stamp, x.line) for x in logs) > for l in singly: > print > > But this doesn't seem to help either. > > >> Then when walking the log with for...in, python will silently call getline >> until error. This means getline must raise StopIteration when the log is >> "empty" and __iter__ must "reset" it. > > Yes, but for how long? Having added the __iter__ method, if I now do: > > for log in logs: > for line in log: > print line > > I still get only three results. > >> Another solution may be to subtype "file", for a file is precisely an >> iterator over lines; and you really get your data from a file. > > I'm very sorry - I'm not sure I understand. I get that a file is > iterable by definition, but I'm not sure how subtyping it helps. > >> Simply (sic), there must some job done about this issue of time stamps >> (haven't studied in details). Still, i guess this track may be worth an >> little study. > > Sorry for not understanding :( > >> Once you get logs iterable, you may subtype list for your overall log >> collection and set it an __iter__ method like: >> >> for log in self: >> for line in log: >> yield line >> >> (The trick is not from me.) > > OK - I make the logs iterable by giving them an __iter__ method - I > get that. I just don't know what you mean by 'subtype list'. > >> Then you can write: >> for line in my_log_collection > > That sounds useful > > S. > -- Stephen Nelson-Smith Technical Director Atalanta Systems Ltd www.atalanta-systems.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
Hi Wayne, > Just write your own merge: > (simplified and probably inefficient and first thing off the top of my head) > newlist = [] > for x, y, z in zip(list1, list2, list3): I think I need something like izip_longest don't I, since the list wil be of varied length? Also, where do these lists come from? They can't go in memory - they're much too big. This is why I felt using some kind if generator was the right way - I can produce 3 (or 12) sets of tuples... i just need to work out how to merge them. > if y > x < z: > newlist.append(x) > elif x > y < z: > newlist.append(y) > elif x > z < y: > newlist.append(z) > I'm pretty sure that should work although it's untested. Well, no it won't work. The lists are in time order, but they won't match up. One log may have entries at the same numerical position (ie the 10th log entry) but earlier than the entries on the previous lines. To give a simple example: List 1List 2List 3 (1, cat) (2, fish) (1, cabbage) (4, dog) (5, pig) (2, ferret) (5, phone) (6, horse) (3, sausage) Won't this result in the lowest number *per row* being added to the new list? Or am I misunderstanding how it works? S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
On Sat, Nov 14, 2009 at 8:49 AM, Stephen Nelson-Smith wrote: > Hi Wayne, > > > Just write your own merge: > > (simplified and probably inefficient and first thing off the top of my > head) > > newlist = [] > > for x, y, z in zip(list1, list2, list3): > > I think I need something like izip_longest don't I, since the list wil > be of varied length? > Yes, probably > > Also, where do these lists come from? They can't go in memory - > they're much too big. This is why I felt using some kind if generator > was the right way - I can produce 3 (or 12) sets of tuples... i just > need to work out how to merge them. > you can zip generators, too: In [10]: for x, y in zip(xrange(10), xrange(5)): : print x, y : : 0 0 1 1 2 2 3 3 4 4 In [38]: for x,y in itertools.izip_longest(xrange(10), xrange(5)): : print x,y : : 0 0 1 1 2 2 3 3 4 4 5 None 6 None 7 None 8 None 9 None > if y > x < z: > > newlist.append(x) > > elif x > y < z: > > newlist.append(y) > > elif x > z < y: > > newlist.append(z) > > I'm pretty sure that should work although it's untested. > > Well, no it won't work. The lists are in time order, but they won't > match up. One log may have entries at the same numerical position (ie > the 10th log entry) but earlier than the entries on the previous > lines. To give a simple example: > > List 1List 2List 3 > (1, cat) (2, fish) (1, cabbage) > (4, dog) (5, pig) (2, ferret) > (5, phone) (6, horse) (3, sausage) > > Won't this result in the lowest number *per row* being added to the > new list? Or am I misunderstanding how it works? I forgot to add the rest of them. But it appears that you'll have to come up with some better logic. I would use some type of priority queue-style implementation. I don't know if the heapq datatype would be sufficient, or if you'd need to roll your own. The way I would do it is check the top value of each, whichever has the smallest value, pop it off and add that to the new list (which could also be a generator[there's some other term for it but I forget ATM] writing out to a file) then check across the top of all 3 again. Make sense? HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] getting python 3 to run from the command line (version 2)
hi all, I realize my question was previous question was phrased, well, vaguely, as I learn from your responses so here's version 2 of my question: i'm running windows vista and have python 30 ,31 and 26 installed.! when I try to run python form the command line by printing *python *to whichever path i have there (in my case:C:\users\KE>) it runs python 30, but 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) when I tried to change the path to the directory that contains python 31 and enter *python (c:\python31>)*, it just crashes and gives me the following message: *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 teh application's support team for more Information.* * * When i change the directory to c:\python26 and then enter it works ok. so can anyone tell me why this is happening? PS: I learn from Alan Gauld's response that windows defaults to python30 when i enter python because in the system variables python30 was entered first and I confirmed that that was true, but still python 30 doesn't process my code and python 31 crashes. thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] OT: Writing code while tired, counterproductive?
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- ___ 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
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. Basically, the user would submit a query like "Neil Gaiman" and then the program would return the name of the files in which the words "Neil Gaiman" appears. I thought I might be able to use the tags to speed up the search. For example, maybe the program will only look at the "writer" tags, or I can ask the program to show me everything under the "comic" tag. --- On Sat, 11/14/09, Kent Johnson wrote: > From: Kent Johnson > Subject: Re: [Tutor] parsing XML into a python dictionary > To: "Christopher Spears" > Cc: tutor@python.org > Date: Saturday, November 14, 2009, 5:03 AM > On Sat, Nov 14, 2009 at 1:14 AM, > Christopher Spears > > wrote: > > I've been working on a way to parse an XML document > and convert it into a python dictionary. I want to > maintain the hierarchy of the XML. Here is the sample XML > I have been working on: > > > > > > > > Neil Gaiman > > Glyn > Dillon > > Charles > Vess > > > > > > > This is the output: > > 163>./parseXML.py > > {'collection': {('comic', {'number': '62', 'title': > 'Sandman'}): [, > , penciller at -482193a4>]}} > > This seems an odd format. How are you going to use it? How > is this > better than the native ElementTree structure? > > > The script doesn't descend all of the way down because > I'm not sure how to hand a XML document that may have > multiple layers. Advice anyone? Would this be a job for > recursion? > > Yes. Here is an example that might be helpful: > http://code.activestate.com/recipes/410469/ > > Kent > ___ 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 List 1List 2List 3 (1, cat) (2, fish) (1, cabbage) (4, dog) (5, pig) (2, ferret) (5, phone) (6, horse) (3, sausage) Won't this result in the lowest number *per row* being added to the new list? Or am I misunderstanding how it works? You are right. I think the algorithm you need goes something like - read the first item from each flie - find the lowest item add it to the result - keep taking items from that file until you find one greater than - the next lowest item that you first found, put that cvalue in place of the original lowest value - repeat for the new lowest value until it matches the next lowest value and so on until all files are empty To taker the trivial case of 2 files containing the odd and every other even numbers respectively A = [1, 3, 5, 7,... B = [2, 6, 10... lowA = 1 low2 = 2 load lowA into result, get next from A -> 3 lowA becomes 3 load next lowest (lowB=2) into result, get next from B -> 6 lowB becomes 6 load next lowest (lowA=3) into result, get next from A -> 5 load 5 in result and get next from A -> 7 load 7 in lowA load next lowest (lowB=6) into result, get next from B -> 10 load next lowest(lowA=7) into result, get next from A -> 9 etc until all files are empty Obviously you can create a dictionary or list of your lowest values and the matching file. You can simplify the algorithm by always loading the latest value into lowX and alwayss checking for the lowest value before storing it, but for high volumes the extra checks will mount up. 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
Re: [Tutor] getting python 3 to run from the command line (version 2)
"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. 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
Re: [Tutor] Writing code while tired, counterproductive?
"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
Re: [Tutor] getting python 3 to run from the command line (version 2)
"Khalid Al-Ghamdi" wrote in message news:dfac564f0911140854v42fa4e0ehe5868517a50ef...@mail.gmail.com... hi all, I realize my question was previous question was phrased, well, vaguely, as I learn from your responses so here's version 2 of my question: i'm running windows vista and have python 30 ,31 and 26 installed.! when I try to run python form the command line by printing *python *to whichever path i have there (in my case:C:\users\KE>) it runs python 30, but 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) when I tried to change the path to the directory that contains python 31 and enter *python (c:\python31>)*, it just crashes and gives me the following message: *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 teh application's support team for more Information.* * * When i change the directory to c:\python26 and then enter it works ok. so can anyone tell me why this is happening? PS: I learn from Alan Gauld's response that windows defaults to python30 when i enter python because in the system variables python30 was entered first and I confirmed that that was true, but still python 30 doesn't process my code and python 31 crashes. My version of Python and Windows doesn't understand cp720. According to http://en.wikipedia.org/wiki/Code_page_720, it is a OEM Arabic code page. The Windows-equivalent is 'cp1256'. I have Python 2.6 and it doesn't have a codec for cp720, but does for cp1256. Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. u'abc'.encode('cp720') Traceback (most recent call last): File "", line 1, in LookupError: unknown encoding: cp720 u'abc'.encode('cp1256') 'abc' If you are running on an Arabic version of Windows, you might change the console code page to 1256 and see if that works. Run 'chcp 1256' before running 'python'. -Mark ___ 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?
On Sat, Nov 14, 2009 at 12:43 PM, Modulok wrote: > Does anyone else find, writing code while tired to be counterproductive? I definitely find that there is a point of diminishing returns, where my productivity and the quality of code decline to the point where it is no longer worth it to continue. Kent ___ 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?
When I get really tired, I sort of slip into an half awaken state. I still have my eyes open, I still look at the code, I still think about the problem, but suddenly the lines come alive and start arguing with each other (and some times arguing with me!), or setting up dates, or singing in choir, all sorts of crazy things. That's definitely the time to STOP!, and get some real sleep. Cheers, Jose Amoreira ___ 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
Ahoy! On Sa, 2009-11-14 at 20:49 +, Stephen Nelson-Smith wrote: > 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. It's too early for Python 3.x. He should probably learn Python 2.6 for now, possibly with future imports (though they probably take a bit longer to explain). 3.x will probably cause too many problems for him for now. A lot of the reference material he can find on the web expects 2.x and many of the "cool" libraries aren't quite ported to 3.x yet. That's just confusing. He'll learn about the differences between 2.x and 3.x eventually, but he shouldn't have to worry about it for the time being. Cheers Alan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Should a beginner learn Python 3.x
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. -- Stephen Nelson-Smith Technical Director Atalanta Systems Ltd www.atalanta-systems.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
"Stephen Nelson-Smith" wrote 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 Version 3 support is getting there but I still don't think it's fully suitable for beginners yet. I'd say stick with version 2.6 (or 2.7!) [Actually v3 is fine for beginners the problem is as soon as they try to do anything beyond beginner they will likely run into support issues...] That having been said my tutorial for V3 is making slow but steady progress and the basics section is complete and I'm slowly ticking off the advanced topics so he could try that. (see this sig's url) But personally I'd still go with v2 -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] opening a file directly from memory
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 COFIDIS Maxicredito. Ate' €10.000 sem burocracias. Resposta on-line! Clique aqui para saber mais http://www.iol.pt/correio/rodape.php?dst=0802273 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Find Integer co-ordinates lying on a circle
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)]}) 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? ___ 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: 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 ___ 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)]}) 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? Your "all possible results" isn't even close to all the points that match the 0<= r <= 5. And I don't know how either spec justifies the set logic you quoted. So I have to start over. I think you're trying to find all integer co-ordinates which lie on or within a circle of given radius (sample 5). Taking only the first quadrant to keep the number of values smaller, I see: ( 0 , 0 ) ( 0 , 1 ) ( 0 , 2 ) ( 0 , 3 ) ( 0 , 4 ) ( 0 , 5 ) ( 1 , 0 ) ( 1 , 1 ) ( 1 , 2 ) ( 1 , 3 ) ( 1 , 4 ) ( 2 , 0 ) ( 2 , 1 ) ( 2 , 2 ) ( 2 , 3 ) ( 2 , 4 ) ( 3 , 0 ) ( 3 , 1 ) ( 3 , 2 ) ( 3 , 3 ) ( 3 , 4 ) ( 4 , 0 ) ( 4 , 1 ) ( 4 , 2 ) ( 4 , 3 ) ( 5 , 0 ) To find them, just write a doubly nested loop, each with range from -radius to radius, checking x*x + y*y <= radius*radius. Add to the result set, each value that meets the comparison. There are some optimizations you can do, but first get it working in the form you want. DaveA ___ 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
> > Your "all possible results" isn't even close to all the points that match > the 0<= r <= 5. And I don't know how either spec justifies the set logic > you quoted. > > So I have to start over. I think you're trying to find all integer > co-ordinates which lie on or within a circle of given radius (sample 5). > > 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. Taking only the first quadrant to keep the number of values smaller, I see: > > ( 0 , 0 ) ( 0 , 1 ) ( 0 , 2 ) ( 0 , 3 ) ( 0 , 4 ) ( 0 , 5 ) > ( 1 , 0 ) ( 1 , 1 ) ( 1 , 2 ) ( 1 , 3 ) ( 1 , 4 ) > ( 2 , 0 ) ( 2 , 1 ) ( 2 , 2 ) ( 2 , 3 ) ( 2 , 4 ) > ( 3 , 0 ) ( 3 , 1 ) ( 3 , 2 ) ( 3 , 3 ) ( 3 , 4 ) > ( 4 , 0 ) ( 4 , 1 ) ( 4 , 2 ) ( 4 , 3 ) > ( 5 , 0 ) > > To find them, just write a doubly nested loop, each with range from -radius > to radius, checking x*x + y*y <= radius*radius. Add to the result set, each > value that meets the comparison. > > > There are some optimizations you can do, but first get it working in the > form you want. > > > The bruteforce code is working for me but that's an unacceptable option. So I tried pythagorean triplets. But I'm not sure as to where am I doing mistake. >>> d = collections.defaultdict(list) I created a dictionary. >>> 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)])) for u = (0,9) and v > u, the triplets are u**2+v**2, v**2 - u**2, 2*u*v. So, I stored them. However it looks for only half of positive quadrant. set logic is to remove duplicate elements. Now for each value (p, q) there will be seven more possible values i.e. (q, p), (q, -p), (-q, p), (-q, -p), (p, -q), (-p, q), (-p, -q). In case of either of p or q value = 0, the values are reduced to 3. >>> [d[k].append(v) for k,v in s] I stored all the values. Hope I did not made this post really messy. I think I'm unable to apply my logic. How to implement / Can there be better logic, optimizations and implementations ? ___ 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
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. Txs Dave, Miguel COFIDIS Maxicredito. Ate' €10.000 sem burocracias. Resposta on-line! Clique aqui para saber mais http://www.iol.pt/correio/rodape.php?dst=0802273 ___ 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: Your "all possible results" isn't even close to all the points that match the 0<= r <= 5. And I don't know how either spec justifies the set logic you quoted. So I have to start over. I think you're trying to find all integer co-ordinates which lie on or within a circle of given radius (sample 5). 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.) In other words r = n, not 0 <= r <= n as you originally said. You want all points for which x*x + y*y = n*n Upon reading your new message, it appears that you are not using r in the usual sense of polar coordinates (where r = sqrt( x*x + y*y) and theta = arctan(x/y)or maybe it's y/x ) Instead you're using r as the radius of one circle you're trying to do, in a loop that is trying different radii up to n (or R) 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 Care to define r and R ? I think the outer loop here belongs elsewhere. Let's solve it for one circle. Then you can run that inside a loop which iterates through possible radii. The outer loop isn't our concern. And the innermost loop can be replaced by a calculation. Why not simply y = math.sqrt(r*r - x*x) and check if y is an int. ?? However the complexity reaches O(n**3) and it's not optimized at all. So I though of using pythagorean triplets. Taking only the first quadrant to keep the number of values smaller, I see: ( 0 , 0 ) ( 0 , 1 ) ( 0 , 2 ) ( 0 , 3 ) ( 0 , 4 ) ( 0 , 5 ) ( 1 , 0 ) ( 1 , 1 ) ( 1 , 2 ) ( 1 , 3 ) ( 1 , 4 ) ( 2 , 0 ) ( 2 , 1 ) ( 2 , 2 ) ( 2 , 3 ) ( 2 , 4 ) ( 3 , 0 ) ( 3 , 1 ) ( 3 , 2 ) ( 3 , 3 ) ( 3 , 4 ) ( 4 , 0 ) ( 4 , 1 ) ( 4 , 2 ) ( 4 , 3 ) ( 5 , 0 ) To find them, just write a doubly nested loop, each with range from -radius to radius, checking x*x + y*y <= radius*radius. Add to the result set, each value that meets the comparison. There are some optimizations you can do, but first get it working in the form you want. The bruteforce code is working for me but that's an unacceptable option. So I tried pythagorean triplets. But I'm not sure as to where am I doing mistake. d = collections.defaultdict(list) I created a dictionary. 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)])) for u = (0,9) and v > u, the triplets are u**2+v**2, v**2 - u**2, 2*u*v. So, I stored them. However it looks for only half of positive quadrant. set logic is to remove duplicate elements. Now for each value (p, q) there will be seven more possible values i.e. (q, p), (q, -p), (-q, p), (-q, -p), (p, -q), (-p, q), (-p, -q). In case of either of p or q value = 0, the values are reduced to 3. [d[k].append(v) for k,v in s] I stored all the values. Hope I did not made this post really messy. I think I'm unable to apply my logic. How to implement / Can there be better logic, optimizations and implementations ? ___ 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
(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? ___ 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 started as an absolute beginner to with programming with Python 3. I remain a beginner but I've now installed 2.6 as well, because I found that some of the modules I wanted to use weren't available for 3.x. My personal experience was that the available literature/tutorials wasn't really a problem. I quickly figured out that I should copy the list on the python site of all the 2x-3x changes, and I picked up a couple other lists written with different wording saying the same thing, and kept it as a quick reference on my computer. But honestly, as a beginner you only run into a handful of differences. Other than print(), input(), <>, xrange(), the dictionary has_key, interkeys(), a bit of other dictionary stuff, tkinter, and renamed http modules, you're not going to run into much that varies between the versions. I just learned the very basic stuff to watch out for - compared to all the other new knowledge you're putting in your brain, it's nothing. I personally really prefer print() to print - it just made more sense to me when learning, and I like that you get a set literal like you get with lists and dictionaries (it felt like the same logic was being applied in similar situations, which is good when you're just starting out). But these are small conveniences and I switched to 2.x because of compatibility issues. I don't think it's a problem to initially learn on 3.1, but I do think it's inevitable that he will have to learn both - and not just for compatibility with the cool toys. If he's going to be using random online tutorials and reading references from everywhere, he's going to run into stuff written for both 3.x and 2.x and he's going to have to know the little differences to compensate for when trying out the practice code. For instance, 2.x users that grab the new Head First Programming book by O'Reilly that's coming out in Dec (teaching beginning programming using Python 3.1) will have issues converting backwards.. so it's not all one way. Just my mostly ignorant 2 cents. -Kris On Sat, Nov 14, 2009 at 12:49 PM, Stephen Nelson-Smith wrote: > 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. > > -- > Stephen Nelson-Smith > Technical Director > Atalanta Systems Ltd > www.atalanta-systems.com > ___ > 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] opening a file directly from memory
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 COFIDIS Maxicredito. Ate' €10.000 sem burocracias. Resposta on-line! Clique aqui para saber mais http://www.iol.pt/correio/rodape.php?dst=0802273 ___ 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
>> 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 good question, and already well-answered by most. i'll chime in with a few remarks too. basically, if he is really starting from scratch, i.e., no preexisting codebase, not using it for work, etc., then there's no harm in starting using 3.x as long as you give the caveat that most tutorials and source out there is still 2.x. 3.x has not gained widespread adoption yet because not all of the lower-level (nor higher-level) libraries, packages, and modules have been ported to 3.x yet. i gave a talk recently about this very topic ( http://siliconvalley-codecamp.com/Sessions.aspx?OnlyOne=true&id=227 ) and will repeat it again at PyCon 2010 in Atlanta ( http://us.pycon.org/2010/conference/talks -- see session #48 ). 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. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.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] Should a beginner learn Python 3.x
On Sun, Nov 15, 2009 at 10:37 AM, wesley chun wrote: > >> 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 > > > good question, and already well-answered by most. i'll chime in with a > few remarks too. basically, if he is really starting from scratch, > i.e., no preexisting codebase, not using it for work, etc., then > there's no harm in starting using 3.x as long as you give the caveat > that most tutorials and source out there is still 2.x. 3.x has not > gained widespread adoption yet because not all of the lower-level (nor > higher-level) libraries, packages, and modules have been ported to 3.x > yet. > > i gave a talk recently about this very topic ( > http://siliconvalley-codecamp.com/Sessions.aspx?OnlyOne=true&id=227 ) > and will repeat it again at PyCon 2010 in Atlanta ( > http://us.pycon.org/2010/conference/talks -- see session #48 ). > > 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. > > hope this helps! > -- wesley > 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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor