Re: [Tutor] array of different datatypes
On Sep 22, 2008, at 11:50 PM, Steve Willoughby wrote: Dinesh B Vadhia wrote: Thanks Steve. How do you sort on the second element of each list to get: a' = [[42, 'fish'], [1, 'hello'] [2, 'world'] ] something like this would do the trick: a_prime = sorted(a, key=(lambda i: i[1])) sorted(a) returns a new list consisting of the elements in a but in sorted order. the key= parameter says how to derive the sort key from any given element; in this case, the elements being sorted are themselves lists, and element #1 in the sub-list (a.k.a. row) is the key. try itemgetter: In [1]: a = [[42, 'fish'], ...: [2, 'world'], ...: [1, 'hello']] In [2]: from operator import itemgetter In [3]: sorted(a, key=itemgetter(1)) Out[3]: [[42, 'fish'], [1, 'hello'], [2, 'world']] From: Steve Willoughby Sent: Monday, September 22, 2008 8:16 PM To: Dinesh B Vadhia Cc: tutor@python.org Subject: Re: [Tutor] array of different datatypes Dinesh B Vadhia wrote: I have looked (honestly!) and cannot see an array structure to allow different datatypes per column. I need a 2 column array with column 1 = an integer and column 2 = chars, and after populating the array, sort on column 2 with column 1 sorted relatively. If by array you mean a regular Python list, the data type of every single element may be different. So it's just how lists always work. a = [[1, 'hello'], [2, 'world'], [42, 'fish'], ] Thanks! Dinesh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] A question about how python handles numbers larger than it's 32 bit limit
Greetings, The book I have says when you anticipate that you will be working with numbers larger than what python can handle, you place an L after the number to signal python to treat it as a large number. Does this treating of the number only mean that Python won't try to represent the number internally as a 32bit integer? Python still appears to be representing the number only with an L behind it so what is happening to the number then. Is the L behind the number telling python to handle this large number in HEX instead which would fit into the 32 bit limit? thanks in advance, John T ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A question about how python handles numbers larger than it's 32 bit limit
2008/9/23 John Toliver [EMAIL PROTECTED] Greetings, The book I have says when you anticipate that you will be working with numbers larger than what python can handle, you place an L after the number to signal python to treat it as a large number. Does this treating of the number only mean that Python won't try to represent the number internally as a 32bit integer? Python still appears to be representing the number only with an L behind it so what is happening to the number then. Is the L behind the number telling python to handle this large number in HEX instead which would fit into the 32 bit limit? thanks in advance, John T The L stands for long integer and means that it will usually use twice the space of a regular integer so in this case 64bits. HTH Adam. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A question about how python handles numbers larger than it's 32 bit limit
On Tue, Sep 23, 2008 at 04:24:48PM +0100, Adam Bark wrote: 2008/9/23 John Toliver [EMAIL PROTECTED] Greetings, The book I have says when you anticipate that you will be working with numbers larger than what python can handle, you place an L after the number to signal python to treat it as a large number. Does this treating of the number only mean that Python won't try to represent the number internally as a 32bit integer? Python still appears to be representing the number only with an L behind it so what is happening to the number then. Is the L behind the number telling python to handle this large number in HEX instead which would fit into the 32 bit limit? thanks in advance, John T The L stands for long integer and means that it will usually use twice the space of a regular integer so in this case 64bits. s/64bits/infinite/ Python is not C :) In Python, long integers are unlimited precision values, so you can accurately store a number like 32432471704327419865487605452750436198382489276811235713483294872389573259823495174875098750298475019874230984710985743980572840957432098578029107923471 if you want to. They aren't handled as *fast* as regular native integer values (which are implemented as the C long int type internally in CPython, so they may be 32 bits or possibly(?) longer), but they are unlimited in size. Python will automatically promote an integer to a long when it gets too big, so you don't *have* to put the L on the end or use long() to construct one explicitly, unless you really want it to be long type from the beginning. -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A question about how python handles numbers larger than it's 32 bit limit
On Tue, Sep 23, 2008 at 11:16 AM, John Toliver [EMAIL PROTECTED] wrote: Greetings, The book I have says when you anticipate that you will be working with numbers larger than what python can handle, you place an L after the number to signal python to treat it as a large number. Your book is a little old, the L has not been required since Python 2.4. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] array of different datatypes
Reed O'Brien wrote: On Sep 22, 2008, at 11:50 PM, Steve Willoughby wrote: Dinesh B Vadhia wrote: Thanks Steve. How do you sort on the second element of each list to get: a' = [[42, 'fish'], [1, 'hello'] [2, 'world'] ] something like this would do the trick: a_prime = sorted(a, key=(lambda i: i[1])) sorted(a) returns a new list consisting of the elements in a but in sorted order. the key= parameter says how to derive the sort key from any given element; in this case, the elements being sorted are themselves lists, and element #1 in the sub-list (a.k.a. row) is the key. try itemgetter: In [1]: a = [[42, 'fish'], ...: [2, 'world'], ...: [1, 'hello']] In [2]: from operator import itemgetter In [3]: sorted(a, key=itemgetter(1)) Out[3]: [[42, 'fish'], [1, 'hello'], [2, 'world']] From: Steve Willoughby Sent: Monday, September 22, 2008 8:16 PM To: Dinesh B Vadhia Cc: tutor@python.org Subject: Re: [Tutor] array of different datatypes Dinesh B Vadhia wrote: I have looked (honestly!) and cannot see an array structure to allow different datatypes per column. I need a 2 column array with column 1 = an integer and column 2 = chars, and after populating the array, sort on column 2 with column 1 sorted relatively. itemgetter also allows you to do something like this (2.5 and later)... In [1]: a = [[42, 'fish'], [1, 'hello'], [2, 'world'], [41, 'fish']] In [2]: sorted(a, key=itemgetter(1, 0)) Out[2]: [[41, 'fish'], [42, 'fish'], [1, 'hello'], [2, 'world']] ... which would give you the relative sort you asked about for column 1. But at that point, if I had control over the input data structure -- I would probably reverse the order, and then just use a vanilla sorted call without any key arg. If by array you mean a regular Python list, the data type of every single element may be different. So it's just how lists always work. a = [[1, 'hello'], [2, 'world'], [42, 'fish'], ] Thanks! Dinesh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] image processing
I'm trying to do simple image manipulation but am getting an error. any ideas what i am doing wrong here? Thanks JJ #!/usr/bin/python import ImageFilter,Image name=test.jpg file=open(./+name,w) pic=Image.open(file) pic.rotate(45) pic.save(new_ + name) pic.show() the error: Traceback (most recent call last): File image_filter.py, line 7, in module pic=Image.open(file) File /usr/lib/python2.5/site-packages/PIL/Image.py, line 1893, in open prefix = fp.read(16) IOError: [Errno 9] Bad file descriptor Disclaimer: The information contained in this transmission, including any attachments, may contain confidential information of Panasonic Avionics Corporation. This transmission is intended only for the use of the addressee(s) listed above. Unauthorized review, dissemination or other use of the information contained in this transmission is strictly prohibited. If you have received this transmission in error or have reason to believe you are not authorized to receive it, please notify the sender by return email and promptly delete the transmission. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] image processing
On Tue, Sep 23, 2008 at 5:38 PM, jeremiah [EMAIL PROTECTED] wrote: I'm trying to do simple image manipulation but am getting an error. any ideas what i am doing wrong here? file=open(./+name,w) pic=Image.open(file) You're opening the file for writing, then asking PIL to read it. That's not going to work. In fact, there's no reason for you to open the file yourself. Just pass the filename, like this: pic=Image.open(name) -- Jerry ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] please explain this error
Hi all, New to this python thing.. I am trying to run a python script to convert sccs data to svn data (sccs2svn.py). Downloaded this off the web, and it sounds like it should do what I need it to, but I can not seem to get it to run. I keep getting the following error message: Traceback (most recent call last): File /home/hkroh/bin/python_scripts/sccs_te.py, line 481, in ? core.run_app(run) File /usr/src/build/554290-x86_64/install/usr/lib/python2.3/site-packages/sv n/core.py, line 33, in run_app File /home/hkroh/bin/python_scripts/sccs_te.py, line 418, in run interface.add(i) File /home/hkroh/bin/python_scripts/sccs_te.py, line 242, in add print self._commit(revision, delta.getDate(), transaction, subpool) File /home/hkroh/bin/python_scripts/sccs_te.py, line 162, in _commit return repos.svn_repos_fs_commit_txn(self.repos_ptr, txn, subpool) libsvn._core.SubversionException: (Can't remove 'SVN_skill/db/transactions/0-1.txn': Directory not empty, 39) I go to the offending directory and I see a .nfsblah file. When I touch the file, it disappears. I do not know enough about python, or the add on module that is causing this failure. Can someone please point me in the right direction in solving my problem. Please let me know what else you need to know to help with this. Thanks in advance for any help, Herold ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] please explain this error
On Tue, Sep 23, 2008 at 06:13:31PM -0400, Herold Kroh wrote: libsvn._core.SubversionException: (Can't remove 'SVN_skill/db/transactions/0-1.txn': Directory not empty, 39) I go to the offending directory and I see a .nfsblah file. When I touch the file, it disappears. The problem here is with NFS, really. The .nfsblah files are how the NFS system handles the case where files are deleted on the fileserver but still open on a client. (Under Unix, a file can still be open and all its data accessed even if deleted from the filesystem... it won't *really* go away until it's closed, too. But NFS doesn't represent that case well so a temporary filename is used.) Unless your Python program is what's holding the offending file(s) open... any idea what's keeping the file in use? -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] image processing
jeremiah wrote: I'm trying to do simple image manipulation but am getting an error. any ideas what i am doing wrong here? Thanks JJ #!/usr/bin/python import ImageFilter,Image name=test.jpg file=open(./+name,w) Right here you're opening the file in write mode, in effect creating a new file ready to be written to. You probably want to use mode 'rb' HTH, Emile pic=Image.open(file) pic.rotate(45) pic.save(new_ + name) pic.show() the error: Traceback (most recent call last): File image_filter.py, line 7, in module pic=Image.open(file) File /usr/lib/python2.5/site-packages/PIL/Image.py, line 1893, in open prefix = fp.read(16) IOError: [Errno 9] Bad file descriptor Disclaimer: The information contained in this transmission, including any attachments, may contain confidential information of Panasonic Avionics Corporation. This transmission is intended only for the use of the addressee(s) listed above. Unauthorized review, dissemination or other use of the information contained in this transmission is strictly prohibited. If you have received this transmission in error or have reason to believe you are not authorized to receive it, please notify the sender by return email and promptly delete the transmission. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Sorting Dictionary of Dictionary by certain Value
Hi Pythonistas, I have a large dictionary of dictionary (50,000+ keys) which has a structure as follows: DoD = { 'flintstones' : { 'husband' : fred, 'pal' : barney, 'income': 500, }, 'jetsons' : { 'husband' : george, 'wife' : jane, 'his boy' : elroy, 'income': 700, }, 'simpsons' : { 'husband' : homer, 'wife' : marge, 'kid' : bart, 'income': 600, }, }; I want to sort the dictionary by 'income' Is there an efficient way to do the same. Thanks in advance. - Jo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting Dictionary of Dictionary by certain Value
2008/9/24 Joe Python [EMAIL PROTECTED]: Hi Pythonistas, I have a large dictionary of dictionary (50,000+ keys) which has a structure as follows: [snip] I want to sort the dictionary by 'income' Is there an efficient way to do the same. Note that you cannot sort a dictionary. The best you can do is build a list containing the dictionary keys in the appropriate order and use the dictionary in combination with that list. You could try this: 1. Build a dictionary mapping income to list of families. 2. Sort keys to this dictionary. 3. Iterate through this sorted list, emitting family names. e.g. from collections import defaultdict familiesByIncome = defaultdict(list) for family in DoD: familiesByIncome[DoD[family]['income']].append(family) incomes = familiesByIncome.keys() incomes.sort() # sorts from lowest to highest familiesSorted = [] for inc in incomes: familiesSorted.extend(familiesByIncome[inc]) ## HTH! -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting Dictionary of Dictionary by certain Value
On Wed, Sep 24, 2008, John Fouhy wrote: 2008/9/24 Joe Python [EMAIL PROTECTED]: Hi Pythonistas, I have a large dictionary of dictionary (50,000+ keys) which has a structure as follows: [snip] I want to sort the dictionary by 'income' Is there an efficient way to do the same. Note that you cannot sort a dictionary. The best you can do is build a list containing the dictionary keys in the appropriate order and use the dictionary in combination with that list. What you can do is create a class for the objects in the top level dictionary, and create a __cmp__ method in that class using cmp to compare the objects in the dictionary, finally sorting the values. class MyStuff(object): def __init__(self, name, income): self.name = name self.income = int(income) def cmp(self, other): return(cmp((-self.income, self.name), -other.income, other.name)) d = dict( key1 = MyStuff('john', 1), key2 = MyStuff('bill', 2), ) vals = d.values() vals.sort() # vals should be sorted by income in descending order and name. Bill -- INTERNET: [EMAIL PROTECTED] Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax:(206) 232-9186 People who relieve others of their money with guns are called robbers. It does not alter the immorality of the act when the income transfer is carried out by government. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting Dictionary of Dictionary by certain Value
On Tue, Sep 23, 2008, Bill Campbell wrote: On Wed, Sep 24, 2008, John Fouhy wrote: 2008/9/24 Joe Python [EMAIL PROTECTED]: Hi Pythonistas, I have a large dictionary of dictionary (50,000+ keys) which has a structure as follows: [snip] I want to sort the dictionary by 'income' Is there an efficient way to do the same. Note that you cannot sort a dictionary. The best you can do is build a list containing the dictionary keys in the appropriate order and use the dictionary in combination with that list. What you can do is create a class for the objects in the top level dictionary, and create a __cmp__ method in that class using cmp to compare the objects in the dictionary, finally sorting the values. class MyStuff(object): def __init__(self, name, income): self.name = name self.income = int(income) Whoops The method below should be def __cmp__ def cmp(self, other): return(cmp((-self.income, self.name), -other.income, other.name)) d = dict( key1 = MyStuff('john', 1), key2 = MyStuff('bill', 2), ) vals = d.values() vals.sort() # vals should be sorted by income in descending order and name. Bill -- INTERNET: [EMAIL PROTECTED] Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax:(206) 232-9186 People who relieve others of their money with guns are called robbers. It does not alter the immorality of the act when the income transfer is carried out by government. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bill -- INTERNET: [EMAIL PROTECTED] Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax:(206) 232-9186 Microsoft is to computers what Phillip Morris is to lungs. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting Dictionary of Dictionary by certain Value
This might help you. http://blog.modp.com/2008/09/sorting-python-dictionary-by-value-take.html Robert Joe Python wrote: Hi Pythonistas, I have a large dictionary of dictionary (50,000+ keys) which has a structure as follows: DoD = { 'flintstones' : { 'husband' : "fred", 'pal' : "barney", 'income' : 500, }, 'jetsons' : { 'husband' : "george", 'wife' : "jane", 'his boy' : "elroy", 'income' : 700, }, 'simpsons' : { 'husband' : "homer", 'wife' : "marge", 'kid' : "bart", 'income' : 600, }, }; I want to sort the dictionary by 'income' Is there an efficient way to do the same. Thanks in advance. - Jo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How Match a Dot?
Title: Signature.html How do I match a dot in, for example, abc.txt? I want to match it exactly. There must be some sort of escape. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) "Truth is mighty and will prevail. There is nothing wrong with this, except that it ain't so." -- Mark Twain Web Page: www.speckledwithstars.net/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How Match a Dot?
On Tue, Sep 23, 2008 at 10:02 PM, Wayne Watson [EMAIL PROTECTED] wrote: How do I match a dot in, for example, abc.txt? I want to match it exactly. There must be some sort of escape. Assuming you want to match in a regular expression, use \ as an escape and use raw strings. In [40]: import re In [41]: m=re.search(r'\.', 'abc.de') In [43]: m.start() Out[43]: 3 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting Dictionary of Dictionary by certain Value
On Tue, Sep 23, 2008 at 8:41 PM, Joe Python [EMAIL PROTECTED] wrote: Hi Pythonistas, I have a large dictionary of dictionary (50,000+ keys) which has a structure as follows: DoD = { 'flintstones' : { 'husband' : fred, 'pal' : barney, 'income': 500, }, 'jetsons' : { 'husband' : george, 'wife' : jane, 'his boy' : elroy, 'income': 700, }, 'simpsons' : { 'husband' : homer, 'wife' : marge, 'kid' : bart, 'income': 600, }, }; I want to sort the dictionary by 'income' Is there an efficient way to do the same. As has been pointed out, you can't sort a dictionary, it is unordered. You can sort the list of key, value pairs. The simplest way is to make a key function that extracts the value on which to sort. The key, value pairs will look like ('flintstones', { 'husband' : fred, 'pal' : barney, 'income': 500, ) You want to sort on the 'income' element of the value; this key function will work: def key(item): return item[1]['income'] Then sort with sorted(DoD.iteritems(), key=key) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How Match a Dot?
Title: Signature.html I tried that in the re test program (Perl style-re tester) provided with Pyton 2.4 and ... Ah, I used /. It works. But I also used \. Well, I see the culprit. I had one too many \d in: dateRe = re.compile(r'v\d\d\d\d\d\d\d\d_\d\d\d\d\d\d\.\d\d.*') Got it. Thanks. Kent Johnson wrote: On Tue, Sep 23, 2008 at 10:02 PM, Wayne Watson [EMAIL PROTECTED] wrote: How do I match a dot in, for example, abc.txt? I want to match it exactly. There must be some sort of escape. Assuming you want to match in a regular _expression_, use \ as an escape and use raw strings. In [40]: import re In [41]: m=re.search(r'\.', 'abc.de') In [43]: m.start() Out[43]: 3 Kent -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) "Truth is mighty and will prevail. There is nothing wrong with this, except that it ain't so." -- Mark Twain Web Page: www.speckledwithstars.net/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor