Use Regular Expressions to extract URL's
Hello I am using regular expressions to grab URL's from a string(of HTML code). I am getting on very well I seem to be grabbing the full URL [b]but[/b] I also get a '' character at the end of it. Do you know how I can get rid of the '' char at the end of my URL [b]Example of problem:[/b] [quote] I get this when I extract a url from a string http://google.com; I want to get this http://google.com [/quote] My regular expression: [code] def find_urls(string): Extract all URL's from a string return as a list url_list = re.findall(r'(?:http://|www.).*?[]',string) return url_list [/code] -- http://mail.python.org/mailman/listinfo/python-list
Re: Performance of list.index - how to speed up a silly algorithm?
MRAB wrote: The .index method does a linear search, checking on average 1/2 of the items in the list. That's why it's so slow. In order to avoid that you could build a dict of each value in dimension_values[col_idx] and its index in a single pass so that it becomes a quick lookup. For example: from itertools import count, izip def iterator_factory(): # columns = [color, size, weight, value] rows = [ [Yellow, Big, 2, 4], [Blue, Big, 3, -4], [Blue, Small, 10, 55], #... ] return iter(rows) class Indexer(object): def __init__(self): self.lookup = {} self.counter = count() def __call__(self, value): try: return self.lookup[value] except KeyError: result = self.lookup[value] = next(self.counter) return result def to_list(self): d = self.lookup reverse = dict(izip(d.itervalues(), d.iterkeys())) assert len(reverse) == len(d) return [reverse[i] for i in xrange(len(reverse))] def pairs(rows, dimension_cols, measure_cols, indexers): for row in rows: dv = [indexer(row[col]) for indexer, col in izip(indexers, dimension_cols)] mv = [row[col] for col in measure_cols] yield dv, mv def main(): # dimension_names = [color, size] dimension_cols = [0, 1] # measure_names = [weight, value] measure_cols = [2, 3] indexers = [Indexer() for _ in dimension_cols] facts = pairs(iterator_factory(), dimension_cols, measure_cols, indexers) facts = list(facts) print facts for i, indexer in enumerate(indexers): print %d: %s % (i, indexer.to_list()) if __name__ == __main__: main() -- http://mail.python.org/mailman/listinfo/python-list
Re: matching strings in a large set of strings
Karin Lagesen karin.lage...@bio.uio.no writes: Hello. I have approx 83 million strings, all 14 characters long. I need to be able to take another string and find out whether this one is present within the 83 million strings. Now, I have tried storing these strings as a list, a set and a dictionary. I know that finding things in a set and a dictionary is very much faster than working with a list, so I tried those first. However, I run out of memory building both the set and the dictionary, so what I seem to be left with is the list, and using the in method. I imagine that there should be a faster/better way than this? Shouldn't a set with 83 million 14 character strings be fine in memory on a stock PC these days? I suppose if it's low on ram you might start swapping which will kill performance. Perhaps the method you're using to build the data structures creates lots of garbage? How much ram do you have and how much memory does the python process use as it builds your data structures? A set should give good performance if the target string is also 14 characters. If you do end up with the list then try using bisect http://docs.python.org/library/bisect.html which should be quicker than just using in (which I think just scans linearly through the list testing for equality). There are other algorithms you can use that have better theoretical performance than using bisect for this particular problem, but you get into trade offs between executing things in python as opposed to C if you start to hand code things in python. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get ipRouteTable from Cisco router?
On 29 апр, 19:12, gvozdikov t1k0v.s...@gmail.com wrote: Hello! I want to get route tables from Cisco routers in the network. What i have: import re from pysnmp.entity.rfc3413.oneliner import cmdgen s = r'(%s)' % ('(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.)\ {3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)') pattern = re.compile(s) file = 'routers.txt' s = open(file).read() i = 0 router_list = [] while True: match = pattern.search(s, i) if match: router_list.append(match.group(1)) i = match.end() + 1 else: break class router: def __init__(self, who): self.name = who routetable = {} router1 = router(router_list[0]) cmdGen = cmdgen.CommandGenerator() errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( cmdgen.CommunityData('test-agent', public, 0), cmdgen.UdpTransportTarget((router1.name, 161)), (1,3,6,1,2,1,4,21,1,1)) if errorIndication: print errorIndication else: if errorStatus: print '%s at %s\n' % (errorStatus.prettyPrint(),varBindTable[-1][int(errorIndex)-1]) else: for varBindTableRow in varBindTable: for oid, val in varBindTableRow: print varBindTableRow Result: Code: Select all [(ObjectName('1.3.6.1.2.1.4.21.1.1.0.0.0.0'), IpAddress('0.0.0.0'))] [(ObjectName('1.3.6.1.2.1.4.21.1.1.10.9.0.0'), IpAddress('10.9.0.0'))] [(ObjectName('1.3.6.1.2.1.4.21.1.1.192.168.1.0'), IpAddress('192.168.1.0'))] How can i get IpAddress values from this list and put they in the dictionary? Or may be there is much better solution? Solution is pretty simple: RouteTable = { ipRouteDest: (1, 3, 6, 1, 2, 1, 4, 21, 1, 1), ipRouteIfIndex: (1, 3, 6, 1, 2, 1, 4, 21, 1, 2), ipRouteNextHop: (1, 3, 6, 1, 2, 1, 4, 21, 1, 7), ipRouteType: (1, 3, 6, 1, 2, 1, 4, 21, 1, 8), ipRouteMask: (1, 3, 6, 1, 2, 1, 4, 21, 1, 11), } def hex2dec(mack): return int(mack, 16) def convertIp(hexip): ip = map(hex, map(ord, hexip)) ip = map(hex2dec, ip) ip = re.sub(\,, .,re.sub(\'|\[|\]|\s,, str(ip))) return ip def walk(host, community, oid): cmdGen = cmdgen.CommandGenerator() errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( cmdgen.CommunityData('test-agent', community, 0), cmdgen.UdpTransportTarget((host, 161)), oid) if errorIndication: print errorIndication else: if errorStatus: print '%s at %s\n' % (errorStatus.prettyPrint(),varBindTable[-1][int(errorIndex)-1]) else: val = [] for varBindTableRow in varBindTable: for oid in varBindTableRow: try: val.append(convertIp(varBindTableRow[0][1])) except: val.append(str(varBindTableRow[0][1])) return val ipRouteDest = walk(router1.name, community, RouteTable[ipRouteDest]) ipRouteIfIndex = walk(router1.name, community, RouteTable[ipRouteIfIndex]) ipRouteNextHop = walk(router1.name, community, RouteTable[ipRouteNextHop]) ipRouteType = walk(router1.name, community, RouteTable[ipRouteType]) ipRouteMask = walk(router1.name, community, RouteTable[ipRouteMask]) table = zip(ipRouteIfIndex, ipRouteNextHop, ipRouteType, ipRouteMask) routetable = dict(zip(ipRouteDest, table)) print routetable Result: {'10.9.0.0': ('0', '192.168.1.1', '4', '255.255.0.0'), '0.0.0.0': ('0', '192.168.1.1', '4', '0.0.0.0'), '192.168.1.0': ('1', '192.168.1.254', '3', '255.255.255.0')} But how create class router with routetable as attribute? -- http://mail.python.org/mailman/listinfo/python-list
Re: matching strings in a large set of strings
Paul Rudin paul.nos...@rudin.co.uk wrote: Shouldn't a set with 83 million 14 character strings be fine in memory on a stock PC these days? I suppose if it's low on ram you might start swapping which will kill performance. Perhaps the method you're using to build the data structures creates lots of garbage? How much ram do you have and how much memory does the python process use as it builds your data structures? Some simple experiments should show you that a stock PC running a 32 bit Python will struggle: s = 12345678901234 sys.getsizeof(s) 38 83*38 3154 So more than 3GB just for the strings (and that's for Python 2.x on Python 3.x you'll need nearly 5GB). Running on a 64 bit version of Python should be fine, but for a 32 bit system a naive approach just isn't going to work. Option 1: use a trie. That should reduce storage, maybe it will reduce it enough, maybe not. It depends on the data. Option 2: use a simple database. e.g. shelve. Simple to write and easy to use. Option 3: use a linear search through the file containing the 83 million strings. If the OP really does want to check *one* string then that is comparatively slow but still much faster than posting the question here. If they really wanted to check say 10,000 strings then put those strings in a set and test each line of the 83 million line file against the set instead of doing it the other way round. At some number of test strings this is probably faster than using a database. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: matching strings in a large set of strings
On Fri, 30 Apr 2010 08:23:39 +0100, Paul Rudin wrote: Karin Lagesen karin.lage...@bio.uio.no writes: Hello. I have approx 83 million strings, all 14 characters long. I need to be able to take another string and find out whether this one is present within the 83 million strings. Now, I have tried storing these strings as a list, a set and a dictionary. I know that finding things in a set and a dictionary is very much faster than working with a list, so I tried those first. However, I run out of memory building both the set and the dictionary, so what I seem to be left with is the list, and using the in method. I imagine that there should be a faster/better way than this? Shouldn't a set with 83 million 14 character strings be fine in memory on a stock PC these days? Not even close. Using Python 2.6: s = 12345678901234 assert len(s) == 14 import sys sys.getsizeof(s) 38 So a single 14 char string takes 38 bytes. import random, string chars = list(string.letters + string.digits)*4 def rnd_str(): ... random.shuffle(chars) ... return ''.join(chars[:14]) ... s = set() while len(s) 83000: ... s.add(rnd_str()) ... sys.getsizeof(s) 1048688 So a set with 83000 such strings takes approximately 1 MB. So far fairly trivial. But that's just the memory used by the container (the set), not the contents. 38 bytes * 83,000 strings = another 3 MB. Which of course is trivial for a modern PC, but the OP is using 83 million such strings, not 83 thousand, which gives us a grand total of at least 3 gigabytes. An entry level desktop PC these days is generally 2GB, and entry level notebooks might have half a gig. If the OP is on a 64 bit system, every pointer will be twice the size, leading to even larger memory requirements. And if the strings are unicode, then potentially they could be anything up to four times larger each. Worst case, the OP could need something of the order of 24 GB to store the strings all in memory. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: matching strings in a large set of strings
Duncan Booth duncan.bo...@invalid.invalid writes: Paul Rudin paul.nos...@rudin.co.uk wrote: Shouldn't a set with 83 million 14 character strings be fine in memory on a stock PC these days? I suppose if it's low on ram you might start swapping which will kill performance. Perhaps the method you're using to build the data structures creates lots of garbage? How much ram do you have and how much memory does the python process use as it builds your data structures? Some simple experiments should show you that a stock PC running a 32 bit Python will struggle: s = 12345678901234 sys.getsizeof(s) 38 83*38 3154 So more than 3GB just for the strings (and that's for Python 2.x on Python 3.x you'll need nearly 5GB). Running on a 64 bit version of Python should be fine, but for a 32 bit system a naive approach just isn't going to work. It depends - a few gig of RAM can be cheap compared with programmer time. If you know you can solve a problem by spending a few euros on some extra RAM it can be a good solution! It depends of course where the code is being deployed - if it's something that's intended to be deployed widely then you can't expect thousands of people to go out and buy more RAM - but if it's a one off deployment for a particular environment then it can be the best way to go. -- http://mail.python.org/mailman/listinfo/python-list
Re: Use Regular Expressions to extract URL's
On Thu, 29 Apr 2010 23:53:06 -0700, Jimbo wrote: Hello I am using regular expressions to grab URL's from a string(of HTML code). I am getting on very well I seem to be grabbing the full URL [b]but[/b] I also get a '' character at the end of it. Do you know how I can get rid of the '' char at the end of my URL Live dangerously and just drop the last character from string s no matter what it is: s = s[:-1] Or be a little more cautious and test first: if s.endswith(''): s = s[:-1] Or fix the problem at the source. Using regexes to parse HTML is always problematic. You should consider using a proper HTML parser. Otherwise, try this regex: r'(http://(?:www)?\..*?)' -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: matching strings in a large set of strings
s = 12345678901234 assert len(s) == 14 import sys sys.getsizeof(s) 38 So a single 14 char string takes 38 bytes. Make that at least 40 bytes. You have to take memory alignment into account. So a set with 83000 such strings takes approximately 1 MB. So far fairly trivial. But that's just the memory used by the container (the set), not the contents. 38 bytes * 83,000 strings = another 3 MB. Which of course is trivial for a modern PC, but the OP is using 83 million such strings, not 83 thousand, which gives us a grand total of at least 3 gigabytes. An entry level desktop PC these days is generally 2GB, and entry level notebooks might have half a gig. You are pretty much screwed on a 32bit system here. In my experience 32bit system can't store more than 2.5 to 2.8 GB on the heap. Eventually malloc() will fail since large amounts of the 4 GB address space is reserved for other things like stack, entry point, shared library mappings, error detection etc. Memory fragmentation isn't an issue here. Other ideas: * use a SQL database with an index on the data column. The index could optimize the starting with case. * You need to search for a string inside a large set of texts? Sounds like a job for a fulltext search machine! Grab PyLucene and index your data inside a lucene database. A SSD helps alot here. Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: array matching
Bill Jordan wrote: Hey guys, I am sorry if this is not the right list to post some questions. I have a simple question please and would appreciate some answers as I am new to Python. I have 2 D array: test = [[A,1],[B,2],[A,3][B,4]] I want to arrang this array in different arrays so each one will have what is attached to. For example I want the output: A =[1,3] and B=[2,4] Thanks, Bill Did you try anything before asking ? this is a pretty simple problem. test = [['A',1],['B',2],['A',3], ['B',4]] arranged ={} # dictionary containing the arranged arrays for name, value in test: # you better be sure all arrays contain only 2 elements if name in arranged: arranged[name].append(value) else: arranged[name] = [value] print arranged out: {'A': [1, 3], 'B': [2, 4]} JM -- http://mail.python.org/mailman/listinfo/python-list
Adorable Pancreas
I want to use that as a code name for some software project. Perhaps for a release where a lot of work has gone on behind the scenes, not necessarily visible to the user. It’s from the Jean Kerr quote: “I’m tired of all this nonsense about beauty being skin deep. That’s deep enough. What do you want, an adorable pancreas?” -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing words in HTML file
On 28.04.10 15:02, james_027 wrote: hi, Any idea how I can replace words in a html file? Meaning only the content will get replace while the html tags, javascript, css are remain untouch. You could try XIST (http://www.livinglogic.de/Python/xist/): Example code: from ll.xist import xsc, parsers def p2p(node, converter): if isinstance(node, xsc.Text): node = node.replace(Python, Parrot) node = node.replace(python, parrot) return node node = parsers.parseurl(http://www.python.org/;, tidy=True) node = node.mapped(p2p) node.write(open(parrot_index.html, wb)) Hope that helps! Servus, Walter -- http://mail.python.org/mailman/listinfo/python-list
Re: Cross-platform way to retrieve the current (Operative system) DNS server IP address in python
joamag wrote: It's not my ip address that I want to discover... I want to discover my default dns server ip address. This ip is stored in an operative system basis. Dos anyone know how to get it ? You asked for a cross-platform solution; there probably isn't one. The code below works on Unix and MacOS X. If you can find code that works on Windows, you can cobble together somthing that will work on most platforms. dns_ips = [] for line in file('/etc/resolv.conf', 'r'): columns = line.split() if columns[0] == 'nameserver': dns_ips.extend(columns[1:]) print dns_ips Hope this help, -- HansM -- http://mail.python.org/mailman/listinfo/python-list
Re: Performance of list.index - how to speed up a silly algorithm?
The .index method does a linear search, checking on average 1/2 of the items in the list. That's why it's so slow. In order to avoid that you could build a dict of each value in dimension_values[col_idx] and its index in a single pass so that it becomes a quick lookup. For example: from itertools import count, izip ... def main(): # dimension_names = [color, size] dimension_cols = [0, 1] # measure_names = [weight, value] measure_cols = [2, 3] indexers = [Indexer() for _ in dimension_cols] facts = pairs(iterator_factory(), dimension_cols, measure_cols, indexers) facts = list(facts) print facts for i, indexer in enumerate(indexers): print %d: %s % (i, indexer.to_list()) if __name__ == __main__: main() Whew! :-) Thank you for taking the time. I'm not very familiar to itertools yet, so I need some time to understand this *beautiful* code. :-) L -- http://mail.python.org/mailman/listinfo/python-list
Re: assigning multi-line strings to variables
On 04/30/10 13:21, Steven D'Aprano wrote: On Fri, 30 Apr 2010 05:41:26 +1000, Lie Ryan wrote: On 04/29/10 20:40, Gregory Ewing wrote: Lie Ryan wrote: No, the implicit concatenation is there because Python didn't always have triple quoted string. Nowadays it's an artifact and triple quoted string is much preferred. I don't agree. I often use implicit concatenation when I'm writing a format string that won't fit on one source line, because it allows me to fit it into the surrounding indentation structure without introducing unwanted spaces into the string. Both tecnhiques have their places. That statement should be quantified with for large chunks of text. Format string is typically 2-3 lines at most, not enough to qualify as large chunk. No it shouldn't. It applies equally for two lines or two hundred lines. You seem to have missed that these are NOT equivalent: abcd efgh abcd\ efgh To avoid the ugly backslash, you can use Python's implicit line continuation inside brackets. This is especially handy since you often use brackets for function calls: You *again* missed the point. The purpose here is to *avoid manual preprocessing* of the text chunk string concatenation is not suitable for including large text. Implicit because they force you to preprocess the chunk before python will accept the text into the source code. If you added backslashes inside triple-quotes, it's just as ugly as implicit continuation. When you don't want the newline, then just let the text flow, there is no need to *artificially* break the lines, whether using backslash or implicit concatenation. Triple quoted string is a place where 80-char limits (or whatever number you set) shouldn't apply; if you insist on having string inside triple-quotes to be obey 80-chars, then it's just foolish consistency. With triple quoting, you just need to copy, paste, then add *exactly two* backslashes if preserving exact newline is crucial. Compare that with adding a quote before and after *each line*. That is O(1) vs O(n) difference. Large chunk of text inside source code is usually there because you're too lazy to create a separate file for it. When you're too lazy to even create a file, you're not likely to be that industrious to do any sort of preprocessing to the chunk. And you most likely won't care that the source code looks ugly if it exceeds the oh-so-sacred 80-char limit, because scripts with that kind of nature (i.e. includes large text chunk instead of separate file) is typically one-off throwaway scripts. You want to start working on the chunk immediately instead of spending ten-minutes forcing python to grok the text. -- http://mail.python.org/mailman/listinfo/python-list
Re: assigning multi-line strings to variables
On 04/30/10 12:07, Alf P. Steinbach wrote: On 30.04.2010 01:29, * Carl Banks: On Apr 28, 11:16 am, Alf P. Steinbachal...@start.no wrote: On 28.04.2010 18:54, * Lie Ryan: Python have triple-quoted string when you want to include large amount of text; Yes, that's been mentioned umpteen times in this thread, including the *very first* quoted sentence above. It's IMHO sort of needless to repeat that after quoting it, and providing yet another example right after quoting an example. Probably you didn't notice? I think he repeated it just to let people know that they can get what they want without following your adsurd advice. Perhaps you could quote the advice that you find absurd, and explain how you interpret the quoted text? My previous experience with you is that you immediately called me insane (and worse) for suggesting a solution that could work in principle and did work in practice for the problem posed in that thread; I think you resorted to such characterizations because you had stated that it was impossible. I don't know about your feud with Carl, but for this particular thread, the problem is that your solution involves much more manual work than is necessary. Even worse, you suggested to write a Python script to format it for you. That is the worse piece of advice I've ever heard. Yes, your advices works perfectly if you follow it; except that it adds something to my worklist instead of lifting it. -- http://mail.python.org/mailman/listinfo/python-list
Accessing an instance of a class from within instance of another class
Hello all, I have some knowledge of programing in C++ and I believe that I understand the way one should write programs in Python. But I need some help finding the Right Way for doing a thing. Here is the problem: I have a class (call it Data) that has a number of NumPy arrays and some methods that get useful information from the arrays (math stuff). I have two other classes (called Viewer1 and Viewer2) (they are subclasses of QAbstractTableModel but that's not important). I am working in my code with one instance of each class. Viewer1 and Viewer2 must be able to call methods from the Data instance, but as the instance of Data is constantly updated, I cannot just copy it. I have thought of the following solution: data = Data() class Viewer(***): def __init__(self, ***, data): *** self.D = [data, ] def Data(self): return self.D[0] def SomeOtherFunction(self): self.Data.something() It's an ugly way to implement a pointer or something like it. What is the right way? Cheers Stefan Krastanov P.S. Maybe it's bad idea to use two different view classes, but that is another question. P.P.S. Maybe I'm breaking the encapsulation. What should I do? -- http://mail.python.org/mailman/listinfo/python-list
Re: ooolib, reading writing a spread sheet and keep formatting
On 04/30/10 05:58, News123 wrote: cjw wrote: However: I'd like to read in a spreadsheet, perform only minor modifications and write it back with the exact formatting. this is unfortunately not working. Do you know that Python is one of OpenOffice's macro language? Python macro have the full access to UNO that OpenOffice itself uses internally. -- http://mail.python.org/mailman/listinfo/python-list
Re: Adorable Pancreas
Lawrence D'Oliveiro l...@geek-central.gen.new_zealand writes: It’s from the Jean Kerr quote: “I’m tired of all this nonsense about beauty being skin deep. That’s deep enough. What do you want, an adorable pancreas?” Insulin, glucagon; comin' from the Islets of Langerhans. URL:http://au.youtube.com/watch?v=BtsQxUYHXbw -- \ “Courteous and efficient self-service.” —café, southern France | `\ | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Possible to import from an in-memory zipfile stored in a cString?
Any ideas on whether or not it is possible to import from an in-memory zipfile stored in a cString vs. an on disk zipfile? This seems like it should be possible and perhaps even something that someone's already coded a solution for? Would love to hear your comments before going down a blind path or re-inventing the wheel. Thanks, Malcolm -- http://mail.python.org/mailman/listinfo/python-list
Re: assigning multi-line strings to variables
On 30.04.2010 12:51, * Lie Ryan: On 04/30/10 12:07, Alf P. Steinbach wrote: On 30.04.2010 01:29, * Carl Banks: On Apr 28, 11:16 am, Alf P. Steinbachal...@start.no wrote: On 28.04.2010 18:54, * Lie Ryan: Python have triple-quoted string when you want to include large amount of text; Yes, that's been mentioned umpteen times in this thread, including the *very first* quoted sentence above. It's IMHO sort of needless to repeat that after quoting it, and providing yet another example right after quoting an example. Probably you didn't notice? I think he repeated it just to let people know that they can get what they want without following your adsurd advice. Perhaps you could quote the advice that you find absurd, and explain how you interpret the quoted text? My previous experience with you is that you immediately called me insane (and worse) for suggesting a solution that could work in principle and did work in practice for the problem posed in that thread; I think you resorted to such characterizations because you had stated that it was impossible. I don't know about your feud with Carl, but for this particular thread, the problem is that your solution involves much more manual work than is necessary. Even worse, you suggested to write a Python script to format it for you. That is the worse piece of advice I've ever heard. If you have a 5K string without line breaks, say, then it's entirely reasonable to write a script to split it up, depending on your fav editor's lack of functionality for that. Perhaps you'd like to do it manually. I don't. Yes, your advices works perfectly if you follow it; except that it adds something to my worklist instead of lifting it. No, you simply haven't thought it through. I suspect that you have formed some silly idea in your mind about what I wrote meant, and that that in-your-mind silly idea is what you're arguing against, for otherwise your comments do not make sense (like, you state that saving work adds work). Cheers hth., - Alf -- http://mail.python.org/mailman/listinfo/python-list
Re: Accessing an instance of a class from within instance of another class
Am 30.04.2010 13:05, schrieb Stefan Krastanov: Hello all, [snipp] Here is the problem: I have a class (call it Data) that has a number of NumPy arrays and some methods that get useful information from the arrays (math stuff). I have two other classes (called Viewer1 and Viewer2) (they are subclasses of QAbstractTableModel but that's not important). I am working in my code with one instance of each class. Viewer1 and Viewer2 must be able to call methods from the Data instance, but as the instance of Data is constantly updated, I cannot just copy it. Why do you think the data is copied? Both viewers will hold a reference to the same data object: Type help, copyright, credits or license for more information. class d(object): ... a = 1 ... b = 2 ... c = 3 ... d1 = d() d1.a 1 class view1(object): ... def __init__(self, data): ... self.data = data ... def data(self): ... return self.data ... class view2(object): ... def __init__(self, data): ... self.data = data ... def data(self): ... return self.data ... v1 = view1(d1) v2 = view2(d1) v2.data.b 2 v2.data.b = 4 v2.data.b 4 v1.data.b 4 hth Paul -- http://mail.python.org/mailman/listinfo/python-list
ANN: Wing IDE 3.2.6 released
Hi, Wingware has released version 3.2.6 of Wing IDE, an integrated development environment designed specifically for the Python programming language. This release includes the following minor features and improvements: * Added Copy to Clipboard in Source Assistant * Added ability to clear Python Shell during a session * Added Duplicate Line Above line editing command * Several vi mode fixes (see CHANGELOG.txt for details) * Fixed failure to close files when switching projects * Fixed fragmentation of completion lists in html files * Fixed edit of Include Hidden and Temporary Files project directory property * Many other minor features and bug fixes; See the change log at http://wingware.com/pub/wingide/3.2.6/CHANGELOG.txt for details *Wing 3.2 Highlights* Versions 3.2.x of Wing IDE include the following new features not present in version 3.1: * Support for Python 3.0 and 3.1 * Rewritten version control integration with support for Subversion, CVS, Bazaar, git, Mercurial, and Perforce (*) * Added 64-bit Debian, RPM, and tar file installers for Linux * File management in Project view (**) * Auto-completion in the editor obtains completion data from live runtime when the debugger is active (**) * Perspectives: Create and save named GUI layouts and optionally automatically transition when debugging is started (*) * Improved support for Cython and Pyrex (*.pyx files) * Added key binding documentation to the manual * Added Restart Debugging item in Debug menu and tool bar (**) * Improved OS Commands and Bookmarks tools (*) * Support for debugging 64-bit Python on OS X (*)'d items are available in Wing IDE Professional only. (**)'d items are available in Wing IDE Personal and Professional only. The release also contains many other minor features and bug fixes; see the change log for details: http://wingware.com/pub/wingide/3.2.6/CHANGELOG.txt *Downloads* Wing IDE Professional and Wing IDE Personal are commercial software and require a license to run. A free trial license can be obtained directly from the product when launched. Wing IDE 101 can be used free of charge. Wing IDE Pro 3.2.6http://wingware.com/downloads/wingide/3.2 Wing IDE Personal 3.2.6 http://wingware.com/downloads/wingide-personal/3.2 Wing IDE 101 3.2.6http://wingware.com/downloads/wingide-101/3.2 *About Wing IDE* Wing IDE is an integrated development environment for the Python programming language. It provides powerful debugging, editing, code intelligence, testing, version control, and search capabilities that reduce development and debugging time, cut down on coding errors, and make it easier to understand and navigate Python code. Wing IDE is available in three product levels: Wing IDE Professional is the full-featured Python IDE, Wing IDE Personal offers a reduced feature set at a low price, and Wing IDE 101 is a free simplified version designed for teaching entry level programming courses with Python. System requirements are Windows 2000 or later, OS X 10.3.9 or later for PPC or Intel (requires X11 Server), or a recent Linux system (either 32 or 64 bit). Wing IDE 3.2 supports Python versions 2.0.x through 3.1.x. *Purchasing and Upgrading* Wing 3.2 is a free upgrade for all Wing IDE 3.0 and 3.1 users. Version 2.x licenses cost 1/2 the normal price to upgrade. Upgrade a 2.x license: https://wingware.com/store/upgrade Purchase a 3.x license:https://wingware.com/store/purchase -- The Wingware Team Wingware | Python IDE Advancing Software Development www.wingware.com -- http://mail.python.org/mailman/listinfo/python-list
Re: find integers in f.readline()
elsa wrote: Hi people, I'm having a problem getting the info I need out of a file. I've opened the file with f=open('myFile','r'). Next, I take out the first line with line=f.readline() line looks like this: '83927 300023_25_5_09_FL 9086 9134 F3LQ2BE01AQLXF 1 49 + 80 ZA8Z89HIB7M' I then split it into parts with parts = line.split() ['83927', '300023_25_5_09_FL', '9086', '9134', 'F3LQ2BE01AQLXF', '1', '49', '+', '80', 'ZA8Z89HIB7M'] Now, I need to test whether I can call int(parts[0]) or not. Some of the lines in my file start with a value which represents and integer (as above), while others are just strings of characters. I want to extract just the lines like the one above, that start with an integer. Any suggestions? You can test whether a string contains only digits with the .isdigit() method: '83927'.isdigit() True '-10'.isdigit() False 'something_else'.isdigit() False but usually it's better just to catch the ValueError exception. -- http://mail.python.org/mailman/listinfo/python-list
Re: Possible to import from an in-memory zipfile stored in a cString?
On 04/30/2010 08:55 AM, pyt...@bdurham.com wrote: Any ideas on whether or not it is possible to import from an in-memory zipfile stored in a cString vs. an on disk zipfile? Based on the source-code to zipfile.py on my Debian machine, it looks like the zipfile.ZipFile constructor takes a file parameter. It then checks if it's a string (isinstance() of basestring) and opens that file if it is; otherwise, it assumes it's a file-like object and uses that, so in theory, you should be able to do something like fp = cString(...) zf = zipfile.ZipFile(fp, ...) and it should Just Work(tm). -tkc -- http://mail.python.org/mailman/listinfo/python-list
Python daemonisation with python-daemon
Hello there, I currently have a little project which involve the writing of a server that launch tasks when requested by the user (through a web interface for instance). I would like to turn my server script into a Linux/Unix daemon (launched at boot time by init, dunno if that matter) using the nice python-daemon package by Ben Finley et al (http://pypi.python.org/pypi/ python-daemon/). This package comes with a class DaemonRunner that seems to fit almost exactly my need but I still have some interrogation. My main class looks like the following: class SIGTERM_Received(Exception): pass class MyDaemon(object): def __init__(self): self.pidfile = '/path/to/the/file' self.pidfile_timeout = 5 self.stdin_path = None self.stdout_path = None self.stderr_path = None def run(self): self.init() self.main_loop() self.terminate() def init(self): #doing some initialisation here eg: database connection, logging, connection establishing and such... signal.signal (signal.SIGTERM, lambda: raise SIGTERM_Received()) # used to interrupt the connection listening loop def main_loop(self): #This is the server loop try: while True: rcv = self.some_connection.receive() # this may hang until something was written into the connection by a client. # do something with rcv that involving processes spawning, database interaction and so on... except SIGTERM_Received: return def terminate(self): #some clean up like connection closing and child processes joining. pass And this is the script I intend to drop into /etc/rc.d (for an Archlinux-style initd): #! /usr/bin/python from daemon.runner import DaemonRunner from foo import MyDaemon daemon_runner = DaemonRunner(MyDaemon()) daemon_runner.parse_args() daemon_runner.do_action() The DaemonRunner class expects my class to have stdin_path, stdout_path, stderr_path attributes and after reading the code it seems they have to be valid paths. Is that ok for a Daemon to redirect those stream to /dev/null for example ? I would prefer to alter the DaemonRunner class to accept None as value since DaemonContext, the underlying class, seems to accept None for those parameters. Also, the DaemonRunner use os.kill(pid, signal.SIGTERM) to stop the daemon. I wonder if with my signal handling I'll be able to terminate correctly the daemon (joining children, flushing buffers, closing connections...). If that's relevant, the connection I use is a Listener/Client connection from the standard multiprocessing module. I'm quite neophyte in Unix daemon programming so please forgive me if my question are obvious. Thank for reading, Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: building python 3 -- _dbm necessary bits
James, Thanks for the advice. I looked at the contents of a gdbm-devel package on the web, and compared it, file by file, to my system. I noticed that the online package had three files: /usr/include/gdbm/dbm.h /usr/include/gdbm/gdbm.h /usr/include/gdbm/ndbm.h which on my system are installed in the root of /usr/include: /usr/include/dbm.h /usr/include/gdbm.h /usr/include/ndbm.h So as a test I created a subdirectory /usr/include/gdbm and made symbolic links in the subdirectory back to those root include files. That allowed the Python make script to find the necessary bits for building gdbm support. What's interesting is that I didn't do anything funky when I built the gdbm libraries. Pretty much a configure/make/make install. So I suspect I may not be the only one encountering this problem. Do you know how I can report this issue to the developers so that the Python build script can be modified to look for the gdbm files in /usr/include? - Mark On Fri, 30 Apr 2010 03:44:26 +1000, James Mills prolo...@shortcircuit.net.au wrote: On Fri, Apr 30, 2010 at 3:00 AM, Mark Olbert chairman...@newsgroups.nospam wrote: Okay. But I compiled installed gdbm from source obtained from the gnu archive, so I presume the necessary files would be included (this is on a linux system). Perhaps check where gdbm has installed it's development sources and whether or not the python build scripts can pick it up ? (./configure) Also maybe check ./configure options (maybe you're system is different) ? Without knowing more about your system I can't offer any further useful advise. --james -- http://mail.python.org/mailman/listinfo/python-list
Re: CGI python 3 write RAW BINARY
Le Thu, 29 Apr 2010 23:37:32 +0200, Dodo a écrit : I don't get a thing. Now with the fix : All browsers shows a different thing, but not the image! http://ddclermont.homeip.net/misc/python/ If I save it to computer : * Windows image viewer won't read it * Irfanview can read it without problems Did you set the content-type and content-length in the HTTP headers? Can you post your code? -- http://mail.python.org/mailman/listinfo/python-list
Re: building python 3 -- _dbm necessary bits
Le Fri, 30 Apr 2010 08:14:21 -0700, Mark Olbert a écrit : Do you know how I can report this issue to the developers so that the Python build script can be modified to look for the gdbm files in /usr/include? You can post an issue at http://bugs.python.org Regards Antoine. -- http://mail.python.org/mailman/listinfo/python-list
Re: Issues while building pyfsevents in python 3.1
In article mailman.2389.1272569408.23598.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: On 4/29/2010 2:30 AM, mathan kumar wrote: I m trying port pyfsevents coded in python2.6 to python3.1. Specifying system and compiler/version might help responders. Also, pyobjc-dev is probably a better place for getting help (original article is not in my news spool). -- Aahz (a...@pythoncraft.com) * http://www.pythoncraft.com/ It is easier to optimize correct code than to correct optimized code. --Bill Harlan -- http://mail.python.org/mailman/listinfo/python-list
Python dot-equals (syntax proposal)
At least a few times a day I wish python had the following shortcut syntax: vbl.=func(args) this would be equivalent to vbl = vbl.func(args) example: foo = Hello world foo.=split( ) print foo # ['Hello', 'world'] and I guess you could generalize this to vbl.=[some text] # vbl = vbl.[some text] e.g. temp.=children[0] # temp = temp.children[0] thoughts? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
That's kind of a nifty idea. However, python is currently under a syntax moratorium. No syntax changes will be accepted for at least 24 months starting from the release date of Python 3.1. See more details here: http://www.python.org/dev/peps/pep-3003/ Cheers, Cliff On Fri, 2010-04-30 at 09:04 -0700, Jabapyth wrote: At least a few times a day I wish python had the following shortcut syntax: vbl.=func(args) this would be equivalent to vbl = vbl.func(args) example: foo = Hello world foo.=split( ) print foo # ['Hello', 'world'] and I guess you could generalize this to vbl.=[some text] # vbl = vbl.[some text] e.g. temp.=children[0] # temp = temp.children[0] thoughts? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
J. Cliff Dyer, 30.04.2010 18:20: On Fri, 2010-04-30 at 09:04 -0700, Jabapyth wrote: At least a few times a day I wish python had the following shortcut syntax: vbl.=func(args) this would be equivalent to vbl = vbl.func(args) example: foo = Hello world foo.=split( ) print foo # ['Hello', 'world'] and I guess you could generalize this to vbl.=[some text] # vbl = vbl.[some text] e.g. temp.=children[0] # temp = temp.children[0] thoughts? That's kind of a nifty idea. However, python is currently under a syntax moratorium. No syntax changes will be accepted for at least 24 months starting from the release date of Python 3.1. See more details here: http://www.python.org/dev/peps/pep-3003/ In any case, the right place to discuss this is the python-ideas list. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
On Fri, 30 Apr 2010 09:04:59 -0700 (PDT) Jabapyth jabap...@gmail.com wrote: foo = Hello world foo.=split( ) Isn't; foo = Hello world bar = foo.split() # side note - split() splits on whitespace by default so much clearer? Do you really want to see Python turn into Perl? However, if you really want to propose this you should be clear about which of the following you mean. foo .= split() or foo. = split() I assume you mean the former to be analagous to += and friends but I am not sure since . isn't an operator. -- D'Arcy J.M. Cain da...@druid.net | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
Jabapyth wrote: At least a few times a day I wish python had the following shortcut syntax: vbl.=func(args) this would be equivalent to vbl = vbl.func(args) example: foo = Hello world foo.=split( ) print foo # ['Hello', 'world'] Extending a language comes at a cost, too. A language with 1000 superb features will be much harder to use than one with 10 or 20. In that spirit I suggest a thought experiment: which two features would you kick out of Python to get your new one in? Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
Jabapyth wrote: At least a few times a day I wish python had the following shortcut syntax: vbl.=func(args) this would be equivalent to vbl = vbl.func(args) example: foo = Hello world foo.=split( ) print foo # ['Hello', 'world'] and I guess you could generalize this to vbl.=[some text] # vbl = vbl.[some text] e.g. temp.=children[0] # temp = temp.children[0] thoughts? Useless if you use meaningful names for your variables attributes. It may happen that one object attribute refer to an object of the same type, but it is quite rare that both can share the same name anyway. Possible use cases: 1/ car = Car() car = car.wheel # ??? 2/ wheel = Car() # ??? wheel = wheel.wheel # ??? 3/ currentCar = Car() currentCar = currentCar.nextCar The syntax you prose will be applicable on very little assignements (use case 3). I'm not sure it's worth it. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
D'Arcy J.M. Cain da...@druid.net writes: On Fri, 30 Apr 2010 09:04:59 -0700 (PDT) Jabapyth jabap...@gmail.com wrote: foo = Hello world foo.=split( ) Isn't; foo = Hello world bar = foo.split() # side note - split() splits on whitespace by default so much clearer? Do you really want to see Python turn into Perl? Oh, boy, there we go again. Can you and your buddies please refrain from using Perl as a kind of uber-argument? Just write why you think it's wrong. Bonus points if you can use Python in your arguments. Why I am asking this is that most Oh, like Perl statements I've seen the past months were made by people who either haven't used Perl themselves or have very little skill in the language. This is a Python group. On top of that, it's not possible in Perl (heh, no surprise there). The only thing that comes close is: for ( $a_very_long_variable_name ) { s/foo/bar/g; s/baz/woot/g; s/o+/i/g; } Which makes $_ an alias for $a_very and since s/// defaults to $_ this works. -- John Bokma j3b Hacking Hiking in Mexico - http://johnbokma.com/ http://castleamber.com/ - Perl Python Development -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
On Fri, Apr 30, 2010 at 10:05 AM, John Bokma j...@castleamber.com wrote: D'Arcy J.M. Cain da...@druid.net writes: On Fri, 30 Apr 2010 09:04:59 -0700 (PDT) Jabapyth jabap...@gmail.com wrote: foo = Hello world foo.=split( ) Isn't; foo = Hello world bar = foo.split() # side note - split() splits on whitespace by default so much clearer? Do you really want to see Python turn into Perl? Oh, boy, there we go again. Can you and your buddies please refrain from using Perl as a kind of uber-argument? Just write why you think it's wrong. Bonus points if you can use Python in your arguments. Why I am asking this is that most Oh, like Perl statements I've seen the past months were made by people who either haven't used Perl themselves or have very little skill in the language. This is a Python group. On top of that, it's not possible in Perl (heh, no surprise there). The only thing that comes close is: Actually/ironically, it does appear to be in Perl 6: Mutating method call $obj.=meth The .= operator does inplace modification of the object on the left. -- Synopsis 3: Perl 6 Operators (http://feather.perl6.nl/syn/S03.html) Cheers, Chris -- One phrase: Periodic Table of the Operators http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
On Fri, 30 Apr 2010 12:05:49 -0500 John Bokma j...@castleamber.com wrote: D'Arcy J.M. Cain da...@druid.net writes: so much clearer? Do you really want to see Python turn into Perl? Oh, boy, there we go again. Can you and your buddies please refrain from using Perl as a kind of uber-argument? Just write why you think it's wrong. Bonus points if you can use Python in your arguments. I think I was clear that the problem was obfuscation which Perl is very good at. Haven't you ever had a Perl programmer show you a snippet of code and say I bet you can't guess what this does? On top of that, it's not possible in Perl (heh, no surprise there). The only thing that comes close is: for ( $a_very_long_variable_name ) { s/foo/bar/g; s/baz/woot/g; s/o+/i/g; } Which makes $_ an alias for $a_very and since s/// defaults to $_ I hope that this example wasn't supposed to be a proof of Perl's readability. Anyway, you are right. I just happen to be dealing with some Perl and, even worse, PHP issues on a new server and I am feeling a little battered. I'll be better in a day or two. By the way, to get back to the original topic, I don't really mind if this makes it into the language because I won't be forced to use it. -- D'Arcy J.M. Cain da...@druid.net | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
On 05/01/10 02:50, Jean-Michel Pichavant wrote: Jabapyth wrote: At least a few times a day I wish python had the following shortcut syntax: snip currentCar = Car() currentCar = currentCar.nextCar The syntax you prose will be applicable on very little assignements (use case 3). I'm not sure it's worth it. And for the last use case, isn't iterator better? -- http://mail.python.org/mailman/listinfo/python-list
Re: assigning multi-line strings to variables
On 05/01/10 00:01, Alf P. Steinbach wrote: On 30.04.2010 12:51, * Lie Ryan: On 04/30/10 12:07, Alf P. Steinbach wrote: On 30.04.2010 01:29, * Carl Banks: On Apr 28, 11:16 am, Alf P. Steinbachal...@start.no wrote: On 28.04.2010 18:54, * Lie Ryan: Python have triple-quoted string when you want to include large amount of text; Yes, that's been mentioned umpteen times in this thread, including the *very first* quoted sentence above. It's IMHO sort of needless to repeat that after quoting it, and providing yet another example right after quoting an example. Probably you didn't notice? I think he repeated it just to let people know that they can get what they want without following your adsurd advice. Perhaps you could quote the advice that you find absurd, and explain how you interpret the quoted text? My previous experience with you is that you immediately called me insane (and worse) for suggesting a solution that could work in principle and did work in practice for the problem posed in that thread; I think you resorted to such characterizations because you had stated that it was impossible. I don't know about your feud with Carl, but for this particular thread, the problem is that your solution involves much more manual work than is necessary. Even worse, you suggested to write a Python script to format it for you. That is the worse piece of advice I've ever heard. If you have a 5K string without line breaks, say, then it's entirely reasonable to write a script to split it up, depending on your fav editor's lack of functionality for that. Perhaps you'd like to do it manually. I don't. Use triple-quoted, let them flow, done. I've never heard of any text editor in current use without text wrapping capability, even Notepad has it. And if I've got 5k of text in source code without line breaks I wouldn't want that silly string to disturb my view of the code. You argument aren't even convincing. Perhaps you like to do it the hard way, I don't. Yes, your advices works perfectly if you follow it; except that it adds something to my worklist instead of lifting it. No, you simply haven't thought it through. I suspect that you have formed some silly idea in your mind about what I wrote meant, and that that in-your-mind silly idea is what you're arguing against, for otherwise your comments do not make sense (like, you state that saving work adds work). The point is, what you're suggesting doesn't save work at all as you've shown it. There are other ways to do the same thing, for virtually no work at all. -- http://mail.python.org/mailman/listinfo/python-list
External Hashing [was Re: matching strings in a large set of strings]
I think one could apply an external hashing technique which would require only very few disk accesses per lookup. Unfortunately, I'm now aware of an implementation in Python. Does anybody know about a Python implementation of external hashing? Thanks, Helmut. -- Helmut Jarausch Lehrstuhl fuer Numerische Mathematik RWTH - Aachen University D 52056 Aachen, Germany -- http://mail.python.org/mailman/listinfo/python-list
Re: assigning multi-line strings to variables
On 30.04.2010 19:31, * Lie Ryan: On 05/01/10 00:01, Alf P. Steinbach wrote: On 30.04.2010 12:51, * Lie Ryan: On 04/30/10 12:07, Alf P. Steinbach wrote: On 30.04.2010 01:29, * Carl Banks: On Apr 28, 11:16 am, Alf P. Steinbachal...@start.nowrote: On 28.04.2010 18:54, * Lie Ryan: Python have triple-quoted string when you want to include large amount of text; Yes, that's been mentioned umpteen times in this thread, including the *very first* quoted sentence above. It's IMHO sort of needless to repeat that after quoting it, and providing yet another example right after quoting an example. Probably you didn't notice? I think he repeated it just to let people know that they can get what they want without following your adsurd advice. Perhaps you could quote the advice that you find absurd, and explain how you interpret the quoted text? My previous experience with you is that you immediately called me insane (and worse) for suggesting a solution that could work in principle and did work in practice for the problem posed in that thread; I think you resorted to such characterizations because you had stated that it was impossible. I don't know about your feud with Carl, but for this particular thread, the problem is that your solution involves much more manual work than is necessary. Even worse, you suggested to write a Python script to format it for you. That is the worse piece of advice I've ever heard. If you have a 5K string without line breaks, say, then it's entirely reasonable to write a script to split it up, depending on your fav editor's lack of functionality for that. Perhaps you'd like to do it manually. I don't. Use triple-quoted, let them flow, done. I've never heard of any text editor in current use without text wrapping capability, even Notepad has it. And if I've got 5k of text in source code without line breaks I wouldn't want that silly string to disturb my view of the code. You argument aren't even convincing. You'd put a 5K line in your source code, + you're working with text wrapping in your editor. OK. Cheers hth., - Alf -- http://mail.python.org/mailman/listinfo/python-list
Re: assigning multi-line strings to variables
On 2010-04-30, Lie Ryan lie.1...@gmail.com wrote: Use triple-quoted, let them flow, done. I've never heard of any text editor in current use without text wrapping capability, even Notepad has it. And if I've got 5k of text in source code without line breaks I wouldn't want that silly string to disturb my view of the code. You argument aren't even convincing. Perhaps you like to do it the hard way, I don't. Arguing about how to write 5k of text into your code is about as sensible as arguing about how to stuff a potato into the tailpipe of your Chevrolet. The point is, what you're suggesting doesn't save work at all as you've shown it. There are other ways to do the same thing, for virtually no work at all. Don't put big text dumps in your program. Problem solved! -- Neil Cerutti *** Your child was bitten by a Bat-Lizard. *** -- http://mail.python.org/mailman/listinfo/python-list
Re: Use Regular Expressions to extract URL's
Or perhaps more generically: import re string = 'scatter http://.yahoo.com quotes and text anywhere www.google.com www.bing.com or not' print re.findall(r'(?:http://|www.)[^\s]+',string) ['http://.yahoo.com', 'www.google.com', 'www.bing.com'] -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
Chris Rebert c...@rebertia.com writes: On Fri, Apr 30, 2010 at 10:05 AM, John Bokma j...@castleamber.com wrote: [..] On top of that, it's not possible in Perl (heh, no surprise there). The only thing that comes close is: Actually/ironically, it does appear to be in Perl 6: Mutating method call $obj.=meth The .= operator does inplace modification of the object on the left. -- Synopsis 3: Perl 6 Operators (http://feather.perl6.nl/syn/S03.html) I stand correct, thanks. -- John Bokma j3b Hacking Hiking in Mexico - http://johnbokma.com/ http://castleamber.com/ - Perl Python Development -- http://mail.python.org/mailman/listinfo/python-list
Re: External Hashing
Helmut Jarausch jarau...@igpm.rwth-aachen.de writes: I think one could apply an external hashing technique which would require only very few disk accesses per lookup. Unfortunately, I'm now aware of an implementation in Python. Does anybody know about a Python implementation of external hashing? SQLite? -- John Bokma j3b Hacking Hiking in Mexico - http://johnbokma.com/ http://castleamber.com/ - Perl Python Development -- http://mail.python.org/mailman/listinfo/python-list
Re: http://pypi.python.org/pypi
On Apr 29, 10:49 pm, James Mills prolo...@shortcircuit.net.au wrote: On Fri, Apr 30, 2010 at 5:53 AM, gert gert.cuyk...@gmail.com wrote: How do you upload a plain text .py file as a source file? http://lmgtfy.com/?q=python+distutils+tutorial http://lmgtfy.com/?q=does+not+work :) I have only access to the webpage form too upload my one file. pkginfo is ok, just want to at a single .py file instead of a complete site-package tar directory, because it is not a site-package, its more like a single exe file. Maybe it is just the name of the file that need to be changed. -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for registration package
In article hqnrgs$sm...@reader1.panix.com, kj no.em...@please.post wrote: I'm looking for a Python-based, small, self-contained package to hand out API keys, in the same spirit as Google API keys. The basic specs are simple: 1) enforce the one key per customer rule; 2) be robot-proof; 3) be reasonably difficult to circumvent even for humans. Define customer. You probably cannot do better than defining it as an e-mail address, which makes requirements 2) and 3) pretty much impossible unless you add invite codes or something. -- Aahz (a...@pythoncraft.com) * http://www.pythoncraft.com/ It is easier to optimize correct code than to correct optimized code. --Bill Harlan -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
In article 87zl0klvki@castleamber.com, John Bokma j...@castleamber.com wrote: Why I am asking this is that most Oh, like Perl statements I've seen the past months were made by people who either haven't used Perl themselves or have very little skill in the language. What evidence do you have for your assertion? -- Aahz (a...@pythoncraft.com) * http://www.pythoncraft.com/ It is easier to optimize correct code than to correct optimized code. --Bill Harlan -- http://mail.python.org/mailman/listinfo/python-list
Dynamically change __del__
Hi, I'm trying to be very clever: class tst(object): def destroy(self): print 'Cleaning up.' self.__del__ = lambda: None def __del__(self): raise RuntimeError('Instance destroyed without running destroy! Hell may break loose!') However, it doesn't work: In [2]: t = tst() In [3]: t = None Exception RuntimeError: RuntimeError('Instance destroyed without running destroy! Hell may break loose!',) in bound method tst.__del__ of __main__.tst object at 0x978566c ignored In [4]: t = tst() In [5]: t.destroy() Cleaning up. In [6]: t = None Exception RuntimeError: RuntimeError('Instance destroyed without running destroy! Hell may break loose!',) in bound method tst.__del__ of __main__.tst object at 0x978566c ignored $ python -V Python 2.6.4 Apparently Python calls the class attribute __del__ rather than the instance's __del__ attribute. Is that a bug or a feature? Is there any way to implement the desired functionality without introducing an additional destroy_has_been_called attribute? (I know that invocation of __del__ is unreliable, this is just an additional safeguard to increase the likelihood of bugs to get noticed). Best, -Nikolaus -- »Time flies like an arrow, fruit flies like a Banana.« PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamically change __del__
On Fri, Apr 30, 2010 at 3:16 PM, Nikolaus Rath nikol...@rath.org wrote: Apparently Python calls the class attribute __del__ rather than the instance's __del__ attribute. Is that a bug or a feature? Is there any way to implement the desired functionality without introducing an additional destroy_has_been_called attribute? It's a documented feature: http://docs.python.org/reference/datamodel.html#new-style-special-lookup I'm not aware of a way to get around it, so I think you'll need to fall back to checking a flag in the class's __del__ method. -- Jerry -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamically change __del__
On 05/01/10 05:16, Nikolaus Rath wrote: Hi, I'm trying to be very clever: snip Apparently Python calls the class attribute __del__ rather than the instance's __del__ attribute. Is that a bug or a feature? Is there any way to implement the desired functionality without introducing an additional destroy_has_been_called attribute? (I know that invocation of __del__ is unreliable, this is just an additional safeguard to increase the likelihood of bugs to get noticed). All Exception in __del__ is ignored for various reasons. It's safer if you call destroy from inside __del__() so you cannot forget to remember to call it manually. def __del__(self): self.destroy() -- http://mail.python.org/mailman/listinfo/python-list
Re: assigning multi-line strings to variables
On 05/01/10 04:08, Neil Cerutti wrote: On 2010-04-30, Lie Ryan lie.1...@gmail.com wrote: Use triple-quoted, let them flow, done. I've never heard of any text editor in current use without text wrapping capability, even Notepad has it. And if I've got 5k of text in source code without line breaks I wouldn't want that silly string to disturb my view of the code. You argument aren't even convincing. Perhaps you like to do it the hard way, I don't. Arguing about how to write 5k of text into your code is about as sensible as arguing about how to stuff a potato into the tailpipe of your Chevrolet. The point is, what you're suggesting doesn't save work at all as you've shown it. There are other ways to do the same thing, for virtually no work at all. Don't put big text dumps in your program. Problem solved! Alf suggested it, not me. -- http://mail.python.org/mailman/listinfo/python-list
Re: External Hashing [was Re: matching strings in a large set of strings]
On 04/30/2010 12:51 PM, Helmut Jarausch wrote: I think one could apply an external hashing technique which would require only very few disk accesses per lookup. Unfortunately, I'm now aware of an implementation in Python. Does anybody know about a Python implementation of external hashing? While you don't detail what you're hashing, Stephan Behnel already suggested (in the parent thread) using one of Python's native dbm modules (I just use anydbm and let it choose). The underlying implementations should be fairly efficient assuming you don't use the dumbdbm last-resort fallback). With the anydbm interface, you can implement dict/set semantics as long as you take care that everything is marshalled into and out of strings for keys/values. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Access class from staticmethod
Is that possible? class A(object): @staticmethod def set_b(x): # A.b = x, without knowing A is A pass Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamically change __del__
Nikolaus Rath wrote: Apparently Python calls the class attribute __del__ rather than the instance's __del__ attribute. Is that a bug or a feature? Is there any way to implement the desired functionality without introducing an additional destroy_has_been_called attribute? For newstyle classes __special__() methods are always looked up in the class, never in the instance. Your best bet is probably to manage the lifetime of the instance explicitly with try...finally or a context manager: import contextlib class T(object): ... def close(self): ... print Cleaning up. ... with contextlib.closing(T()) as t: ... print t ... __main__.T object at 0x7fc50c1e61d0 Cleaning up. Alternatively you can try and cook up something with weakref.ref and a callback. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: assigning multi-line strings to variables
On 05/01/10 03:56, Alf P. Steinbach wrote: Use triple-quoted, let them flow, done. I've never heard of any text editor in current use without text wrapping capability, even Notepad has it. And if I've got 5k of text in source code without line breaks I wouldn't want that silly string to disturb my view of the code. You argument aren't even convincing. You'd put a 5K line in your source code, + you're working with text wrapping in your editor. In the other hand, you'd put a 5K line in your source code, + you're writing, debugging, and running a script to wrap and put various escapes for quotes and newlines, + you need to figure out how to force that script to accept your 5k string. -- http://mail.python.org/mailman/listinfo/python-list
Re: assigning multi-line strings to variables
On 05/01/10 05:43, Lie Ryan wrote: On 05/01/10 03:56, Alf P. Steinbach wrote: Use triple-quoted, let them flow, done. I've never heard of any text editor in current use without text wrapping capability, even Notepad has it. And if I've got 5k of text in source code without line breaks I wouldn't want that silly string to disturb my view of the code. You argument aren't even convincing. You'd put a 5K line in your source code, + you're working with text wrapping in your editor. In the other hand, you'd put a 5K line in your source code, + you're writing, debugging, and running a script to wrap and put various escapes for quotes and newlines, + you need to figure out how to force that script to accept your 5k string. + now your chunk is in obfuscated form with various quote noise, unnecessary escape characters and the like. -- http://mail.python.org/mailman/listinfo/python-list
Re: Access class from staticmethod
Ah ha, @classmethod. On Apr 30, 3:47 pm, Thomas Allen thomasmal...@gmail.com wrote: Is that possible? class A(object): @staticmethod def set_b(x): # A.b = x, without knowing A is A pass Thomas -- http://mail.python.org/mailman/listinfo/python-list
Need direction on mass find/replacement in HTML files
Hello Folks: I want to globally change the following: a href=http:// www.mysite.org/?page=contactsfont color=#269BD5 into: a href=pages/contacts.htmfont color=#269BD5 You'll notice that the match would be http://www.mysite.org/?page= but I also need to add a .htm to the end of contacts so it becomes contacts.htm This part of the URL is variable, so how can I use a combination of Python and/or a regular expression to replace the match the above and also add a .htm to the end of that variable part? Here are a few dummy URLs for example so you can see the pattern and the variable too. a href=http://www.mysite.org/?page=newsletter;font color=#269BD5 change to: a href=pages/newsletter.htmfont color=#269BD5 a href=http://www.mysite.org/?page=faq; change to: a href=pages/faq.htm So, again the script needs to replace all the full absolute URL links with nothing and replace the PHP ?page= with just the variable page name (i.e. contacts) plus the .htm Is there a combination of Python code and/or regex that can do this? Any help would be greatly appreciated! Kevin -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamically change __del__
On 05/01/10 05:39, Lie Ryan wrote: On 05/01/10 05:16, Nikolaus Rath wrote: Hi, I'm trying to be very clever: snip Apparently Python calls the class attribute __del__ rather than the instance's __del__ attribute. Is that a bug or a feature? Is there any way to implement the desired functionality without introducing an additional destroy_has_been_called attribute? (I know that invocation of __del__ is unreliable, this is just an additional safeguard to increase the likelihood of bugs to get noticed). All Exception in __del__ is ignored for various reasons. It's safer if you call destroy from inside __del__() so you cannot forget to remember to call it manually. def __del__(self): self.destroy() Never mind that, just realized you're asking for a different thing. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
On Apr 30, 9:04 am, Jabapyth jabap...@gmail.com wrote: At least a few times a day I wish python had the following shortcut syntax: vbl.=func(args) this would be equivalent to vbl = vbl.func(args) example: foo = Hello world foo.=split( ) print foo # ['Hello', 'world'] and I guess you could generalize this to vbl.=[some text] # vbl = vbl.[some text] e.g. temp.=children[0] # temp = temp.children[0] thoughts? I tend to use this design pattern occasionally in my code as well: val = val.func() PROPOSED: val .= func() OR val = func(val) PROPOSED: val .= func(?) (not really sure how this should look) However, these are the only two use cases I can think of for this language syntax modification proposal. The first use case could lead to namespace issues (if func() exists as a function as well as a method), and even if the interpreter can choose correctly, reading it may lead to confusion. There will be at least some ambiguity in the second use case (what if func() takes more than one argument, what if it requires keyword arguments?). The current implementation is much more readable (THE guiding principle with python), and doesn't require any lengthier code than the proposed changes, especially when you factor in any other syntax changes that would be necessary to handle the aforementioned ambiguities. Just my 2 cents. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for registration package
In hrf8s3$6s...@panix5.panix.com a...@pythoncraft.com (Aahz) writes: In article hqnrgs$sm...@reader1.panix.com, kj no.em...@please.post wrote: I'm looking for a Python-based, small, self-contained package to hand out API keys, in the same spirit as Google API keys. The basic specs are simple: 1) enforce the one key per customer rule; 2) be robot-proof; 3) be reasonably difficult to circumvent even for humans. Define customer. You probably cannot do better than defining it as an e-mail address, which makes requirements 2) and 3) pretty much impossible unless you add invite codes or something. Sorry to ask, but are you being cute here? As I wrote, what I'm looking for is something in the spirit of Google API keys. Therefore, if you understand Google's one key per customer rule, then you understand what I want. I realize that with enough determination, any scheme for limiting keys to one per customer can be circumvented, but as long as the enough determination threshold is high enough the requirement is met for practical purposes. (This paragraph applies to **any** security measure, of course.) -- http://mail.python.org/mailman/listinfo/python-list
Re: [SOLVED] Calling multiple programs with subprocess
On Sat, Apr 24, 2010 at 08:22:14AM +0530, Kushal Kumaran wrote: [snip] Run a shell (cmd.exe, I think) using subprocess and send it the commands you want to run using the communicate() method. Actually, I ended up using stdin.write('...\n'), and it works as expected: # # Set WindRiver environment for VxWorks 6.4 # wrenv_path = C:\WindRiver\wrenv.EXE args = [wrenv_path, '-p', 'vxworks-6.4'] proc = subprocess.Popen(args, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE ) proc.stdin.write(str.encode('wrws_update -data %s -l %s -b clean\n' % (workspace, self.target))) output, error = proc.communicate() print(output) print(error) You can keep using stdin.write until you call communicate() this allows you to write certain commands to the stdin for the process you launched. Note: wrenv.exe is actually a shell like cmd.exe. The above code is written for Python 3.1. Thanks for the help. -- http://mail.python.org/mailman/listinfo/python-list
Re: Need direction on mass find/replacement in HTML files
KevinUT wrote: Hello Folks: I want to globally change the following: a href=http:// www.mysite.org/?page=contactsfont color=#269BD5 into: a href=pages/contacts.htmfont color=#269BD5 You'll notice that the match would be http://www.mysite.org/?page= but I also need to add a .htm to the end of contacts so it becomes contacts.htm This part of the URL is variable, so how can I use a combination of Python and/or a regular expression to replace the match the above and also add a .htm to the end of that variable part? Here are a few dummy URLs for example so you can see the pattern and the variable too. a href=http://www.mysite.org/?page=newsletter;font color=#269BD5 change to: a href=pages/newsletter.htmfont color=#269BD5 a href=http://www.mysite.org/?page=faq; change to: a href=pages/faq.htm So, again the script needs to replace all the full absolute URL links with nothing and replace the PHP ?page= with just the variable page name (i.e. contacts) plus the .htm Is there a combination of Python code and/or regex that can do this? Any help would be greatly appreciated! Don't know if the following will in practice be more reliable than a simple regex, but here goes: import sys import urlparse from BeautifulSoup import BeautifulSoup as BS if __name__ == __main__: html = open(sys.argv[1]).read() bs = BS(html) for a in bs(a): href = a[href] url = urlparse.urlparse(href) if url.netloc == www.mysite.org: qs = urlparse.parse_qs(url.query) a[href] = pages/ + qs[upage][0] + .htm print print bs Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: External Hashing [was Re: matching strings in a large set of strings]
Helmut Jarausch wrote: I think one could apply an external hashing technique which would require only very few disk accesses per lookup. Unfortunately, I'm now aware of an implementation in Python. Does anybody know about a Python implementation of external hashing? Thanks, Helmut. That's what databases are for. But if you're trying to streamline it further than a database, you need to give some constraints. In an earlier thread, you mentioned strings with a constant size. I don't think you ever actually said that the match string was also of the same size, but I'll assume it is. Final question is how often you'll be updating the list, versus searching it. If you do both frequently, stick to a database. On the other hand, if the list on disk is fixed, you could thoroughly optimize its makeup. Perhaps the easiest efficient form would be to have a file in two parts. The first part is the hash table, where each entry has a seek-offset and a size. And the rest of the file is just a list of items, sorted by hash value. If you make a hash large enough that the maximum collision list is a couple of k, then two seeks would get any record. First seek to the hash entry, then use that to decide where to seek for the data, and how much to read. Then you simply search that block in memory. You could also play games with a two-level hash, where each block of records has its own mini hash table. Doesn't seem worth it for a mere 83million records. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Inheritable computed class attributes?
I want to define a class attribute that is computed from other class attributes. Furthermore, this attribute should be inheritable, and its value in the subclasses should reflect the subclasses values of the attributes used to compute the computed attribute. I tried the following: class Spam(object): X = 3 @property @classmethod def Y(cls): return cls.X * 3 ...but Spam.Y returns property object at 0x.., rather than 9. How can I define a class property? Is it possible at all? Ultimately, I'd like to be able to define multiple subclasses of Spam, e.g. class Ham(Spam): X = 7 class Eggs(Spam): X = '.' and have Ham.Y and Eggs.Y evaluate to 21 and '...', respectively. Thanks! ~K -- http://mail.python.org/mailman/listinfo/python-list
Re: Need direction on mass find/replacement in HTML files
On 04/30/2010 02:54 PM, KevinUT wrote: I want to globally change the following:a href=http:// www.mysite.org/?page=contactsfont color=#269BD5 into:a href=pages/contacts.htmfont color=#269BD5 Normally I'd just do this with sed on a *nix-like OS: find . -iname '*.html' -exec sed -i.BAK 's...@href=http://www.mysite.org/?page=\([^]*\)@href=pages/\1@g' {} \; This finds all the HTML files (*.html) under the current directory ('.') calling sed on each one. Sed then does the substitution you describe, changing href=http://www.mysite.org/?page=whatever into href=pages/whatever.htm moving the original file to a .BAK file (you can omit the -i.BAK parameter if you don't want this backup behavior; alternatively assuming you don't have any pre-existing .BAK files, after you've vetted the results, you can then use find . -name '*.BAK' -exec rm {} \; to delete them all) and then overwrites the original with the modified results. Yes, one could hack up something in Python, perhaps adding some real HTML-parsing brains to it, but for the most part, that one-liner should do what you need. Unless you're stuck on Win32 with no Cygwin-like toolkit -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Difference between Popen and open() for reading a file
In article mailman.2142.1271960906.23598.python-l...@python.org, J dreadpiratej...@gmail.com wrote: Say I had a file, foo.txt that I wanted to read from, only one time and only read. So what's the difference between this: mylist = Popen([cat,foo.txt], stdout=PIPE).communicate()[0].splitlines() Is there a reason why you would not use subprocess.Popen for this, other than just not relying on external programs to perfrom the task? http://uuoc.com/ -- Aahz (a...@pythoncraft.com) * http://www.pythoncraft.com/ It is easier to optimize correct code than to correct optimized code. --Bill Harlan -- http://mail.python.org/mailman/listinfo/python-list
Re: Inheritable computed class attributes?
On 05/01/10 06:42, kj wrote: I want to define a class attribute that is computed from other class attributes. Furthermore, this attribute should be inheritable, and its value in the subclasses should reflect the subclasses values of the attributes used to compute the computed attribute. I tried the following: I believe you will need to deal with metaclass and the descriptor protocol (__get__/__set__) to have a computed class attribute. If anyone knows of simpler ways, please shout out. Anyway, 'property' only works for instances. The property decorator returns the property descriptor object, which has a __get__ and __set__ methods which defines how the attribute gets accessed from its instances. Now, remember that just like instance is an instance of class, a class is an instance of metaclass. Therefore if we want to make class property, we need to define the property at the metaclass. Don't worry about these details. There are several ways to achieve what you wanted, the easiest probably would be: class MetaSpam(type): @property def Y(cls): return cls.X * 3 class Spam(object): __metaclass__ = MetaSpam and there we go: class Ham(Spam): ... X = 7 ... class Eggs(Spam): ... X = '.' ... Ham.Y; Eggs.Y 21 '...' class Spam(object): X = 3 @property @classmethod def Y(cls): return cls.X * 3 but Spam.Y returns property object at 0x.., rather than 9. How can I define a class property? Is it possible at all? Ultimately, I'd like to be able to define multiple subclasses of Spam, e.g. class Ham(Spam): X = 7 class Eggs(Spam): X = '.' and have Ham.Y and Eggs.Y evaluate to 21 and '...', respectively. Thanks! ~K -- http://mail.python.org/mailman/listinfo/python-list
Re: assigning multi-line strings to variables
On 30.04.2010 21:46, * Lie Ryan: On 05/01/10 05:43, Lie Ryan wrote: On 05/01/10 03:56, Alf P. Steinbach wrote: Use triple-quoted, let them flow, done. I've never heard of any text editor in current use without text wrapping capability, even Notepad has it. And if I've got 5k of text in source code without line breaks I wouldn't want that silly string to disturb my view of the code. You argument aren't even convincing. You'd put a 5K line in your source code, + you're working with text wrapping in your editor. In the other hand, you'd put a 5K line in your source code, + you're writing, debugging, and running a script to wrap and put various escapes for quotes and newlines, + you need to figure out how to force that script to accept your 5k string. + now your chunk is in obfuscated form with various quote noise, unnecessary escape characters and the like. Personally, for Python I'd put such text in a separate text file, as I recommended first of all in the posting you've reacted so negatively to. Cheers hth., - Alf -- http://mail.python.org/mailman/listinfo/python-list
Re: Ignoring XML Namespaces with cElementTree
I think that's your main mistake: don't remove them. Instead, use the fully qualified names when comparing. Stefan Yes. That's what I'm forced to do. Pre-calculating tags like tagChild = {%s}child % uri and using them instead of child. As a result the code looks ugly and there is extra overhead concatenating/comparing these repeating and redundant prefixes. I don't understand why cElementTree forces users to do that. So far I couldn't find any way around that without rebuilding cElementTree from source. Apparently somebody hard-coded the namespace_separator parameter in the cElementTree.c (what a dumb thing to do!!!, it should have been a parameter in the cElementTree.XMLParser() arguments): === self-parser = EXPAT(ParserCreate_MM)(encoding, memory_handler, }); === Simply replacing } with NULL gives me desired tags without stinking URIs. -- http://mail.python.org/mailman/listinfo/python-list
Re: assigning multi-line strings to variables
On 30.04.2010 21:40, * Lie Ryan: On 05/01/10 04:08, Neil Cerutti wrote: On 2010-04-30, Lie Ryanlie.1...@gmail.com wrote: Use triple-quoted, let them flow, done. I've never heard of any text editor in current use without text wrapping capability, even Notepad has it. And if I've got 5k of text in source code without line breaks I wouldn't want that silly string to disturb my view of the code. You argument aren't even convincing. Perhaps you like to do it the hard way, I don't. Arguing about how to write 5k of text into your code is about as sensible as arguing about how to stuff a potato into the tailpipe of your Chevrolet. The point is, what you're suggesting doesn't save work at all as you've shown it. There are other ways to do the same thing, for virtually no work at all. Don't put big text dumps in your program. Problem solved! Alf suggested it, not me. On the contrary, I responded to you on the 30th, using a concrete example of your triple-quote literals rationale posted on the 29th, that ... quote author=Lie Ryan Yes, apparently my statement that implicit concatenation is an artifact is erroneous but it doesn't make the important points less true, that implicit concatenation is not suitable for integrating large chunk of text into source code. /quote ... but I may have misunderstood what you meant by large. Anyway, if you read my first posting in this thread you'll see that the first thing I suggested is to put the text in a separate text file. I didn't discuss rationales for choosing this or that method because where it isn't personal preference it's rather obvious. ;-) Cheers hth, - Alf -- http://mail.python.org/mailman/listinfo/python-list
Re: assigning multi-line strings to variables
On 05/01/10 07:54, Alf P. Steinbach wrote: You'd put a 5K line in your source code, + you're working with text wrapping in your editor. In the other hand, you'd put a 5K line in your source code, + you're writing, debugging, and running a script to wrap and put various escapes for quotes and newlines, + you need to figure out how to force that script to accept your 5k string. + now your chunk is in obfuscated form with various quote noise, unnecessary escape characters and the like. Personally, for Python I'd put such text in a separate text file, as I recommended first of all in the posting you've reacted so negatively to. Didn't we all already agree that we wouldn't put large chunk of text directly in source code unless we're too lazy to create a separate file for a one-off scripts. But your suggested alternative[i.e. quoting everything, uglily] to the proper way[i.e. creating separate file] is so distasteful to make it looks like the only clean way of inserting chunk of text is to create separate file. -- http://mail.python.org/mailman/listinfo/python-list
Re: Inheritable computed class attributes?
In 4bdb4e4...@dnews.tpgi.com.au Lie Ryan lie.1...@gmail.com writes: class MetaSpam(type): @property def Y(cls): return cls.X * 3 class Spam(object): __metaclass__ = MetaSpam and there we go: class Ham(Spam): ... X = 7 ... class Eggs(Spam): ... X = '.' ... Ham.Y; Eggs.Y 21 '...' !!!hmmm!!! That's very interesting! I did not know about metaclasses; I need to learn more about them. Thanks for the pointer! ~K -- http://mail.python.org/mailman/listinfo/python-list
Re: Ignoring XML Namespaces with cElementTree
Here's a link to the patch exposing this parameter: http://bugs.python.org/issue8583 -- http://mail.python.org/mailman/listinfo/python-list
Bug Search Browser Plugin (Firefox)
The Bug Search Browser Plugin (Firefox) http://python.org/dev/searchplugin does not seem to install. http://www.python.org/dev/searchplugin/ *Vincent Davis 720-301-3003 * vinc...@vincentdavis.net my blog http://vincentdavis.net | LinkedInhttp://www.linkedin.com/in/vincentdavis -- http://mail.python.org/mailman/listinfo/python-list
Re: Download Proprietary Microsoft Products Now
In message 530938a5-d865-4228-8d4a-402a39941...@k36g2000yqn.googlegroups.com, Andrej Mitrovic wrote: Sure beats having to recompile a kernel to support 3rd party audio drivers. Which is a less fraught process than {B7C0D3A0-F949-44AD-ACE5-FB845B8C1EB7} ing Registry edits, don’t you thin? You never know what {46E68550-70E4-4CAE-9C69-5C73CE2893CA} it might be having on your machine, to the point where you are no longer able to {362CAB02-8A1F-4337- A441-8149848BD319} it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
On Fri, 30 Apr 2010 18:50:46 +0200, Jean-Michel Pichavant wrote: Jabapyth wrote: At least a few times a day I wish python had the following shortcut syntax: vbl.=func(args) this would be equivalent to vbl = vbl.func(args) [...] Useless if you use meaningful names for your variables attributes. It may happen that one object attribute refer to an object of the same type, but it is quite rare that both can share the same name anyway. How about these common operations? my_string = my_string.replace('quite rare', 'very common') my_string = my_string.upper() my_string = my_string.strip() my_dict = my_dict.copy() Or from decimal, a whole lot of methods that return new decimals, including: to_integral, next_plus, canonical, ln, sqrt, and many others. But regardless of whether the pattern x = x.method is common or rare, I don't like the suggestion, I don't think it's necessary, and because of the moratorium it isn't going to happen any time soon even if Guido himself suggests it. -1 for the suggested .= syntactic sugar. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
On Fri, 30 Apr 2010 12:34:34 -0400, D'Arcy J.M. Cain wrote: I assume you mean the former to be analagous to += and friends but I am not sure since . isn't an operator. It's a de facto operator. If you google on python dot operator you will find many people who refer to it as such, and attribute lookup can be over-ridden at runtime (using __getattribute__, __getattr__, etc.) just like operators +, -, * etc. Also you can do this: s = something s . upper() 'SOMETHING' (s+ else) . upper() 'SOMETHING ELSE' And even apply the dot operator to floats and ints, although because of the ambiguity with floats you need to be clever: 1.2.is_integer() False 4 .numerator 4 However, dot is not a real operator, whatever that means: internally, CPython treats it as a delimiter: http://docs.python.org/reference/lexical_analysis.html#operators In practice though, I think that's a difference that makes no difference. It walks like an operator, it swims like an operator, and it quacks like an operator. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
ANN: expy 0.6.5 released!
EXPY is an express way to extend Python! EXPY provides a way to extend python in an elegant way. For more information and a tutorial, see: http://expy.sourceforge.net/ What's new: 1. Correct treatment of __init__ method. 2. Give warnings of missing Py_INCREF on appropriate special type methods. 3. Documentation update. Cheers, Yingjie -- http://mail.python.org/mailman/listinfo/python-list
Fast Efficient way to transfer an object to another list
Hello I have a relatively simple thing to do; move an object from one to list into another. But I think my solution maybe inefficient slow. Is there a faster better way to move my stock object from one list to another? (IE, without having to use a dictionary instead of a list or is that my only solution?) [code] class stock: code = NULL price = 0 stock_list1 = [] stock_list2 = [] def transfer_stock(stock_code, old_list, new_list): Transfer a stock from one list to another # is there a more efficient faster way to index = 0 for stock in old_list: temp_stock = stock if temp_stock.code == stock_code: new_list.append(temp_stock) del old_list[index] index += 1 return new_list[/code] -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast Efficient way to transfer an object to another list
On Sat, May 1, 2010 at 7:46 AM, Jimbo nill...@yahoo.com wrote: Hello I have a relatively simple thing to do; move an object from one to list into another. But I think my solution maybe inefficient slow. Is there a faster better way to move my stock object from one list to another? (IE, without having to use a dictionary instead of a list or is that my only solution?) [code] class stock: code = NULL price = 0 stock_list1 = [] stock_list2 = [] def transfer_stock(stock_code, old_list, new_list): Transfer a stock from one list to another # is there a more efficient faster way to index = 0 for stock in old_list: temp_stock = stock if temp_stock.code == stock_code: new_list.append(temp_stock) del old_list[index] index += 1 return new_list[/code] Does your code work correctly? Modifying old_list while iterating over it will not work the way you expect, unless you're careful. And what is the point of temp_stock? -- regards, kushal -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast Efficient way to transfer an object to another list
On Fri, 30 Apr 2010 19:16:04 -0700, Jimbo wrote: Hello I have a relatively simple thing to do; move an object from one to list into another. But I think my solution maybe inefficient slow. Is there a faster better way to move my stock object from one list to another? (IE, without having to use a dictionary instead of a list or is that my only solution?) If you already know which object needs to be moved, it is easy: fruits = [apple, banana, carrot, orange, pear] vegetables = [potato, lettuce, onion] # move carrot from fruits to vegetables ... vegetables.append(carrot) fruits.remove(carrot) print fruits ['apple', 'banana', 'orange', 'pear'] print vegetables ['potato', 'lettuce', 'onion', 'carrot'] The only tricky thing is if you don't know which object needs to be moved. The way to solve that depends on what you need to do -- is there only one object that might need to be moved, or could there be more than one? How do you recognise it? There are many solutions. Here's one: even_numbers = [] odd_numbers = odd_numbers = [1, 9, 8, 6, 2, 7, 3, 0, 4, 5] for i in range(len(odd_numbers)-1, -1, -1): ... n = odd_numbers[i] ... if n % 2 == 0: # even? ... even_numbers.append(n) ... del odd_numbers[i] ... even_numbers [4, 0, 2, 6, 8] odd_numbers [1, 9, 7, 3, 5] Can you see why I iterated over the list backwards? Why didn't I just do this? for i, n in enumerate(odd_numbers): if n % 2 == 0: # even? even_numbers.append(n) del odd_numbers[i] You can re-write this more efficiently by using Python built-ins. First you need to recognise what it is doing: first it searches for the item with the stock code, then it appends it to the new list, then it deletes it from the old list. def transfer_stock(stock_code, old_list, new_list): Transfer a stock from one list to another # is there a more efficient faster way to index = 0 for stock in old_list: temp_stock = stock if temp_stock.code == stock_code: new_list.append(temp_stock) del old_list[index] index += 1 return new_list If you know there is one, and only one, item with that stock code: def transfer_stock(stock_code, old_list, new_list): Transfer a stock from one list to another i = old_list.index(stock_code) # search new_list.append(old_list[i]) # copy del old_list[i] # delete return new_list However, this will fail to work correctly if you have more than one identical stock codes that all need to be moved, or if there are none. So here's a version that handles both cases: def transfer_stock(stock_code, old_list, new_list): Transfer a stock from one list to another while True: # loop forever try: i = old_list.index(stock_code) except ValueError: # not found, so we're done break new_list.append(old_list[i]) del old_list[i] return new_list -- Steven -- http://mail.python.org/mailman/listinfo/python-list
[no subject]
Dear all, I am trying my problem in this way: import re expr = re.compile(Helix Helix| Sheet Sheet| Turn Turn| Coil Coil) f = open(CalcSecondary4.txt) for line in f: if expr.search(line): print line but with this it is printing only those line in which helix, sheet, turn and coil are coming twice. Kindly suggest how should I modify it so that whatever secondary structure is coming more than or equal to two times it should write that as final secondary structure and if two seconday structure are coming two-two times in one line itself like: 4 ALA Helix Sheet Helix Sheet then it should write that as doubtful and rest it should write as error. Thanks, Dear all, I have a file like: 1 ALA Helix Sheet Helix Coil 2 ALA Coil Coil Coil Sheet 3 ALA Helix Sheet Coil Turn now what I want is that write a python program in which I will put the condition that in each line whatever secondary structure is coming more than or equal to two times it should write that as final secondary structure and if two seconday structure are coming two-two times in one line itself like: 4 ALA Helix Sheet Helix Sheet then it should write that as doubtful and rest it should write as error. Thanks,-- http://mail.python.org/mailman/listinfo/python-list
Re: winreg - access mask
Richard Lamboj richard.lam...@bilcom.at wrote: if i want to read, write a key and set a value, does i only need to set KEY_WRITE, or does i need to set KEY_READ, KEY_WRITE and KEY_SET_VALUE? This questions is related to the OpenKey Function. You need KEY_READ|KEY_WRITE|KEY_SET_VALUE. Those constants are straight from the Windows API. -- Tim Roberts, t...@probo.com Providenza Boekelheide, Inc. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dot-equals (syntax proposal)
On Apr 30, 11:04 am, Jabapyth jabap...@gmail.com wrote: At least a few times a day I wish python had the following shortcut syntax: vbl.=func(args) this would be equivalent to vbl = vbl.func(args) example: foo = Hello world foo.=split( ) print foo # ['Hello', 'world'] and I guess you could generalize this to vbl.=[some text] # vbl = vbl.[some text] e.g. temp.=children[0] # temp = temp.children[0] thoughts? First thought: good luck getting something like this through. Probably not going to happen, although I do find the idea very intriguing. Second thought: I don't like the proposed syntax at all. +=, -=, /=, *=, etc. conceptually (and, if lhs object supports in- place operator methods, actually) *modify* the lhs object. Your proposed .= syntax conceptually *replaces* the lhs object (actually, rebinds the lhs symbol to the new object). If this were to be deemed worthy of the language, I would think a better syntax would be something like: mystring = .upper() mystring = .replace('a', 'b') etc. The '=' shows clearly that mystring is being rebound to a new object. As Steven has shown, '.' functions as an operator, so if this change were accepted, in reality you would probably be able to write: mystring = . upper() mystring=.upper() or whatever. But the canonical form would probably be with a space before the period but not after. Regards, Pat -- http://mail.python.org/mailman/listinfo/python-list
[issue7946] Convoy effect with I/O bound threads and New GIL
Nir Aides n...@winpdb.org added the comment: Dave, The behavior of your patch on Windows XP/2003 (and earlier) might be related to the way Windows boosts thread priority when it is signaled. Try to increase priority of monitor thread and slice size. Another thing to look at is how to prevent Python CPU bound threads from (starving) messing up scheduling of threads of other processes. Maybe increasing slice significantly can help in this too (50ms++ ?). XP/NT/CE scheduling and thread boosting affect all patches and the current GIL undesirably (in different ways). Maybe it is possible to make your patch work nicely on these systems: http://www.sriramkrishnan.com/blog/2006/08/tale-of-two-schedulers-win_115489794858863433.html Vista and Windows 7 involve CPU cycle counting which results in more sensible scheduling: http://technet.microsoft.com/en-us/magazine/2007.02.vistakernel.aspx -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7946 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8576] test_support.find_unused_port can cause socket conflicts on Windows
New submission from Paul Moore p.f.mo...@gmail.com: test_support.find_unused_port attempts to find an unused port to use. The approach is fragile (as noted in the docstring) in certain cases. In particular, it appears that Windows takes a short time to free the socket after it is closed, meaning that when the caller tries to open a socket on the returned port number, it gets permission issues. A sleep(0.1) just before returning the port number appears to address the issue, but this is not a proper solution. http://msdn.microsoft.com/en-us/library/ms737582%28v=VS.85%29.aspx describes the SO_LINGER and SO_DONTLINGER socket options, which may be related, but in my initial tests I haven't been able to get these to work. -- components: Tests, Windows keywords: buildbot messages: 104614 nosy: pmoore priority: normal severity: normal status: open title: test_support.find_unused_port can cause socket conflicts on Windows ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8576 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8576] test_support.find_unused_port can cause socket conflicts on Windows
Changes by Paul Moore p.f.mo...@gmail.com: -- nosy: +pitrou ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8576 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8576] test_support.find_unused_port can cause socket conflicts on Windows
Changes by Antoine Pitrou pit...@free.fr: -- nosy: +brian.curtin, exarkun, tim.golden type: - behavior versions: +Python 2.6, Python 2.7, Python 3.1, Python 3.2 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8576 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7584] datetime.rfcformat() for Date and Time on the Internet
anatoly techtonik techto...@gmail.com added the comment: The point is that your implementation doesn't allow people to generate 'Z'-ending timestamp if they need a subset of rfc3339. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7584 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8577] test_distutils fails if srcdir != builddir
New submission from Ronald Oussoren ronaldousso...@mac.com: When I build the trunk with srcdir != builddir test_distutils fails when running tests. To reproduce: * Create a checkout of the trunk and chdir into this * mkdir build * cd build * ../configure * make test This results in a failure for test_distutils -- assignee: tarek components: Distutils, Library (Lib) messages: 104616 nosy: ronaldoussoren, tarek priority: normal severity: normal stage: needs patch status: open title: test_distutils fails if srcdir != builddir type: behavior versions: Python 2.7 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8577 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue4180] warnings.simplefilter(always) does not make warnings always show up
Tres Seaver tsea...@agendaless.com added the comment: This patch tidies up the FilterWarnings tests to nomalize use of 'self.assertEquals' (matching the rest of the module) and make the 'test_always' assertions meaningful. -- Added file: http://bugs.python.org/file17144/issue4180-test_janitorial.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue4180 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue4180] warnings.simplefilter(always) does not make warnings always show up
Tres Seaver tsea...@agendaless.com added the comment: This patch adds tests for the 'error', 'ignore', and 'always' filters being applied *after* the default warning has been issued, and therefore the registry populated. It causes failures for the 'error' and 'always' on both the Python and C sides. -- Added file: http://bugs.python.org/file17145/issue4180-test_filter_after_default.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue4180 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue4180] warnings.simplefilter(always) does not make warnings always show up
Tres Seaver tsea...@agendaless.com added the comment: This patch replaces my earlier 'py_warnings' patch. It revamps the Python side to check filters before deciding not to emit the warning based on the registry. The new filter_after_default tests pass on the Python side with this patch. -- Added file: http://bugs.python.org/file17146/issue4180-py_warnings2.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue4180 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8577] test_distutils fails if srcdir != builddir
Tarek Ziadé ziade.ta...@gmail.com added the comment: distutils.sysconfig.get_python_inc() uses os.path.dirname(os.path.abspath(sys.executable)) to find the srcdir. I'll change it to sysconfig.get_config_var('srcdir') -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8577 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue3646] MacOS X framework install to non-standard directory fails
Ronald Oussoren ronaldousso...@mac.com added the comment: In r80647 (trunk) you can use configure --enable-framework=$HOME/Library/Frameworks to get the right situation: - Framework in ~/Library/Frameworks - Apps in ~/Applications/Python 2.7 - Command-line tools in ~/bin I will port this to 3.2 later on, but won't backport to 2.6 and 3.1. -- resolution: - fixed versions: +Python 2.7, Python 3.2 -Python 2.4, Python 2.5, Python 2.6 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue3646 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8577] test_distutils fails if srcdir != builddir
Tarek Ziadé ziade.ta...@gmail.com added the comment: Mmm, the problem is that srcdir is given by the Makefile. So I need to find the Makefile. I don't know how to get these info from a python built in another directory. Investigating... -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8577 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com