Re: [Tutor] reading binary files
On Mon, 2009-02-02 at 11:31 +, etrade.griffi...@dsl.pipex.com wrote: Hi I am trying to read data from a file that has format item_name num_items item_type items eg TIME 1 0.0 DISTANCE 10 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 TIME 1 1.0 DISTANCE 10 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 I can read this if the data are in ASCII format using in_file = open(my_file.dat,r) data1 = in_file.read() tokens = data1.split() and then stepping through the resulting list but the data also appear in the same format in a binary file. I tried converting the binary file to an ASCII file using ifile = open(my_file.dat,rb) ofile = open(new_file.dat,w) base64.decode(ifile, ofile) but that gave the error Error: Incorrect padding. I imagine that there is a straightforward way of doing this but haven't found it so far. Would be grateful for any suggestions! Thanks Alun Griffiths Honestly I'm not sure what you're asking for but in general for reading binary data the I use the struct module. Check it out in the documentation. John Purser ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The better Python approach
a = '1234 5678 1 233 476' a.split() ['1234', '5678', '1', '233', '476'] Where the '' are the command prompt from python. Don't type those. A space is the default split delimiter. If you wish to use a '-' or new line feed them as strings to the split method. John On Wed, 2009-01-21 at 08:33 -0500, Robert Berman wrote: Good Morning, Given a string consisting of numbers separated by spaces such as '1234 5678 1 233 476'. I can see I have two obvious choices to extract or parse out the numbers. The first relying on iteration so that as I search for a blank, I build a substring of all characters found before the space and then, once the space is found, I can then use the int(n) function to determine the number. From my C++ background, that is the approach that seems not only most natural but also most efficient..butthe rules of Python are different and I easily see that I can also search for the first blank, then using the character count, I can use the slice operation to get the characters. Of even further interest I see a string built-in function called split which, I think, will return all the distinct character sub strings for me. My question is what is the most correct python oriented solution for extracting those substrings? Thanks, Robert Berman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Volunteer opportunities
On Wed, 2009-01-21 at 23:31 -0500, bob gailer wrote: Depends on your knowledge of Python and (if any) CMS Pipelines. Testing Design critique (devil's advocate) Cleaning up and fine-tuning the parser Coding built-in stages Documentation Financial support Let me know what sparks your interest. And say a bit about your background and why this interests you. -- Bob Gailer Chapel Hill NC 919-636-4239 Bob, Honestly the first thing that caught my eye was that you were in Chapel Hill. I grew up in Durham NC. I'm in Tacoma WA now. I'm a disabled vet who got into computers and programming from an accounting career. Most of what I've done has been database oriented. I started playing with Python a few years ago and just enjoy the language and would like to find some practical application to use it with. I'm more of a consumer than producer on the tutor list. I read a lot, mostly technical stuff, but nothing that complex. I took a peek at the 889 manual for Pipelines from the wiki page you linked to. It seems to assume a LOT of knowledge I don't have so I'm not sure I'm suitable at all for the project. John Purser ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calculator will not multiply
On Sun, 2009-01-18 at 10:37 -0500, David wrote: Everything else works + - / but not * why? thanks -david #!/usr/bin/python from __future__ import division import sys def add(x, y): return x + y def sub(x, y): return x - y def dev(x, y): return x / y def mul(x, y): return x * y def compute(arg1, arg2, arg3): if sys.argv[2] == +: total = add(int(sys.argv[1]), int(sys.argv[3])) print total elif sys.argv[2] == -: total = sub(int(sys.argv[1]), int(sys.argv[3])) print total elif sys.argv[2] == /: total = dev(int(sys.argv[1]), int(sys.argv[3])) print total elif sys.argv[2] == *: total = mul(int(sys.argv[1]), int(sys.argv[3])) print total else: print oops compute(sys.argv[1], sys.argv[2], sys.argv[3]) It worked fine for me. Don't forget to put quotes around the * sign or the shell will substitute. so: ./calc.py 2 '*' 9 or: ./calc.py 2 * 9 or: ./calc.py 2 \* 9 but not: ./calc.py 2 * 9 John Purser ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] strings and int()
Try eval(2*3) On Thu, 2009-01-15 at 10:14 +1000, Mr Gerard Kelly wrote: If you have a string 6, and you do int(6), you get the number 6. But if you have a string 2*3 and you do int(2*3) you get a name error. How do you take an expression in a string, and evaluate the expression to get a number? I want to be able to turn the string 2*3 into the number 6. thanks ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] eval and floating point
On Thu, 2009-01-15 at 12:19 +1000, Mr Gerard Kelly wrote: Thanks very much I've noticed that the eval() function gives an integer, so eval(3/2) gives back 1. float(eval(3/2)) doesn't seem to work, any way to get a floating point number back with eval()? I know you can just do (3./2.), but is there any way to do it with just (3/2)? That's not the eval function returning that integer so much as it is Python itself. You might try: eval (1.0 * 3/2) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help
On Sun, 2009-01-11 at 14:01 -0800, rev pacce wrote: I have no expierence using python. I was following a tutorial and i kept getting a syntax error. it wasprint hello world! hello world was not coming up underneath it.. i tried to run the module but that didnt work either. ___ Hello, Don't type the into python. Just type: print hello world! into the interactive prompt and I think it will work for you. John ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python debugger
Despite the name I believe Winpdb is platform independent I haven't used it myself but I looked into it while following this thread. http://winpdb.org/ John Purser On Sat, 2009-01-10 at 12:56 +0100, Michael Bernhard Arp Sørensen wrote: It might and I'll keep it in mind. However, I'm not going to ditch my linux just to get a gui debugger. After all, I do have WingIDE installed in Linux, but I wanted to be free of the GUI limitations I have when I code on a host that I connect to through a minicom on another ssh host. I do that from time to time. Thanks anyway. I got what I needed. :-) /Michael On Fri, Jan 9, 2009 at 3:14 PM, Kent Johnson ken...@tds.net wrote: On Fri, Jan 9, 2009 at 7:36 AM, Michael Bernhard Arp Sørensen mich...@arpsorensen.dk wrote: I can't use a graphical debugger because i mostly code python over ssh on remote servers in my company. Winpdb opens a socket connection between the debugger and debuggee. Perhaps it would run over an ssh tunnel... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] simple array access
On Wed, 2009-01-07 at 18:12 -0800, Artie Ziff wrote: Hello, I used python list comprehension to create a grid (list of lists) of Objects (instances of MyItem class). Can anyone make recommendations to achieve a simple access to the elements. My attempt at array access (like this: array[1,2] ) does not work. What am I overlooking? Thanks in advance! :) Hello, You might want to take a look at NumPy http://numpy.scipy.org/ John Purser ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Usage of for loop
On Mon, 2009-01-05 at 18:49 +0530, vanam wrote: Hi all, i am beginner to this python language and slowing learning the language by referring docs.I am trying to understand the for loop i.e., usage of for loop in python,unlike c where i can give condition in python it is simple iterating over sequence. I am trying tounderstand the below lines of code but of no avail. a = [cat, window,defenestrate] for x in a: print x, len(x) i cant understand what x is doing here and what is the purpose of it can anyone help me out here? -- Raghavendra Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor x is a new variable and when used this way it holds one of the members of a for each trip through the loop. After your for loop is done there will be two new names in a dir() listing if you're using the interactive python shell. There will be a name a and a name x. If you type print x it will output defenestrate because it still holds the last value of a from the for loop. John Purser ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Distinction between tuples and lists
On Thu, 2009-01-01 at 15:07 +0100, Christopher Mutel wrote: Hello all- I stumbled across some discussion of why the fundamental difference between lists and tuples is not mutability, but hetero- versus homogeneous data, e.g. http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/ http://pyre.third-bit.com/blog/archives/000450.html However, after reading the cited discussions, my python books, etc., I have to admit I don't really understand this idea. What does it mean that lists are intended for homogeneous sequences? What is different about lists that would make them more suited for homogeneous sequences than heterogeneous sequences (or vice-versa for tuples)? In the end, from what I understand, the idea of homo/heterogeneity seems orthogonal to mutability, which is the main distinction emphasized by e.g. Learning Python. I would greatly appreciate any help provided, -Chris Mutel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor I read it and don't get their emphasis or perhaps level of concern. As explanatory pieces these two blogs aren't very helpful. They provide a few assertions with no apparent (to me) attempt to support or explain them. Both data types are indexed and both can contain homogeneous (same as) or heterogeneous (different than) data. I get that lists are analogous to a C linked lists as tuples are to C structs. I get that the flexibility of one and the stability of the other makes them useful in different situations. Being able to use struct notation (employee.age instead of employee[4]) would be nice but also not that difficult to implement as a class either. On re-re-re-reading it (I hate it when I don't get the point) I think they're arguing for a more structured You must use this properly approach to be built into the language. And that makes some sense. Who would want a database call (say to an employee database table) that returned a record as a list? On the other hand who would want a record set (group of records) returned as a tuple? There's no reason to assume the third record will always be that of John Smith but the 3rd field of John Smiths's record better be his phone number and not his social security number or else wackiness will ensue. Still, aint it great to work with a language that lets us bounce back and forth between structures as we please? John Purser ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Top posters to tutor list for 2008
On Thu, 2009-01-01 at 09:34 -0500, Kent Johnson wrote: For several years I have been using a simple script to find the top 20 posters to the tutor list by web-scraping the archive pages. I thought others might be interested so here is the list for 2008 and the script that generates it. The lists for previous years (back to 2003) are at the end so everyone on the list doesn't hit the archives to find out :-) The script gives a simple example of datetime, urllib2 and BeautifulSoup. It consolidates names that vary by case but other variations are not detected. Kent, Thank you for this. I've been thinking about a web scraping script but didn't have a clue how to go about it. Seeing someone else's practical implementation is a huge help! A little serendipity to start 2009 off with. Happy New Year to all. John ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor