[issue16990] re: match of nongreedy regex not grouping right
New submission from Jared Grubb: re.match matches, but the capture groups are empty. That's not possible. Python 2.7.2 (default, Oct 11 2012, 20:14:37) [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin Type help, copyright, credits or license for more information. import re re.match('.*', 'stuff').group() # greedy matches and captures; cool. 'stuff' re.match('.*?', 'stuff').group() # nongreedy matches (cool) but doesnt capture (huh?) '' -- components: Library (Lib) messages: 180162 nosy: jaredgrubb priority: normal severity: normal status: open title: re: match of nongreedy regex not grouping right versions: Python 2.7 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16990 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16990] re: match of nongreedy regex not grouping right
Jared Grubb added the comment: Yes: re.match('.*', '') _sre.SRE_Match object at 0x107c6d308 re.match('.*?', '') _sre.SRE_Match object at 0x107c6d370 -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16990 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16990] re: match of nongreedy regex not grouping right
Jared Grubb added the comment: You're right. My mistake. I thought match meant the full string must match, but in Python it means the beginning must match. Sorry for the noise. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16990 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7432] Py3k doc: from __future__ import division not necessary
New submission from Jared Grubb pyt...@jaredgrubb.com: In the Python 3.1 docs for the 'dis' module, the following appears: ( http://docs.python.org/3.1/library/dis.html ) BINARY_TRUE_DIVIDE()¶ Implements TOS = TOS1 / TOS when from __future__ import division is in effect. There is always true in 3k, correct? The when clause should be removed. -- assignee: georg.brandl components: Documentation messages: 95950 nosy: georg.brandl, jaredgrubb severity: normal status: open title: Py3k doc: from __future__ import division not necessary versions: Python 3.1 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7432 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7432] Py3k doc: from __future__ import division not necessary
Jared Grubb pyt...@jaredgrubb.com added the comment: Ditto on a few dozen lines later: INPLACE_TRUE_DIVIDE()¶ Implements in-place TOS = TOS1 / TOS when from __future__ import division is in effect. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7432 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7343] What's new in 3.0 says % operator will be deprecated in 3.1
New submission from Jared Grubb pyt...@jaredgrubb.com: The existing text: http://www.python.org/doc/3.0/whatsnew/3.0.html A new system for built-in string formatting operations replaces the % string formatting operator. (However, the % operator is still supported; it will be deprecated in Python 3.1 and removed from the language at some later time.) Read PEP 3101 for the full scoop. Python 3.1 did not deprecate the % operator, right? Is it appropriate to edit the 3.0 docs to reflect that? -- assignee: georg.brandl components: Documentation messages: 95411 nosy: georg.brandl, jaredgrubb severity: normal status: open title: What's new in 3.0 says % operator will be deprecated in 3.1 versions: Python 3.0 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7343 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1580] Use shorter float repr when possible
Jared Grubb pyt...@jaredgrubb.com added the comment: I think ANY attempt to rely on eval(repr(x))==x is asking for trouble, and it should probably be removed from the docs. Example: The following C code can vary *even* on a IEEE 754 platform, even in two places in the same source file (so same compile options), even in the same 5 lines of code recompiled after changing code that does even not touch/read 'x' or 'y': double x, y; x = 3.0/7.0; y = x; /* ... code that doesnt touch/read x or y ... */ printf( x==y: %s, (x==y) ? true : false); So, how can we hope that eval(repr(x))==x is EVER stable? Equality and floating point should raise the hairs on the back of everyone's neck... (Code above based on http://docs.sun.com/source/806-3568/ncg_goldberg.html in section Current IEEE 754 Implementations, along with a great explanation on why this is true. The code example is a little different, but the same concept applies.) -- nosy: +jaredgrubb ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1580 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1580] Use shorter float repr when possible
Jared Grubb pyt...@jaredgrubb.com added the comment: The process that you describe in msg85741 is a way of ensuring memcmp(x, y, sizeof(x))==0, and it's portable and safe and is the Right Thing that we all want and expect. But that's not x==y, as that Sun paper explains. It's close, but not technically accurate, as the implication arrow only goes one way (just as x=1/y implies xy=1 in algebra, but not the other way around) I'd be interested to see if you could say that the Python object model/bytecode interpretation enforces a certain quauntum of operations that actually does imply eval(repr(x))==x; but I suspect it's unprovable, and it's fragile as Python grows to have more support in CLI/LLVM/JVM backends. My pedantic mind would strip any and all references to floating-point equality out of the docs, as it's dangerous and insidiously misleading, even in obvious cases. But, I'll stop now :) (FYI: I've enjoyed the ~100 messages here.. Great stuff!) -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1580 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue5349] abstractmethod vs C++ pure virtual
New submission from Jared Grubb pyt...@jaredgrubb.com: On page library/abc.html documenting abc.abstractmethod, the following text about C++ is placed in a note: Note: Unlike C++’s pure virtual functions, or Java abstract methods, these abstract methods may have an implementation. This implementation can be called via the super() mechanism from the class that overrides it. This could be useful as an end-point for a super-call in a framework that uses cooperative multiple-inheritance. This is not an accurate description of C++'s pure virtual functions. Pure virtual functions CAN have an implementation, which can be called from derived classes; this makes them behave just like Python's abstractmethod. Example: struct Abstract { virtual void foo() = 0; }; void Abstract::foo() { std::cout pure virtual function called; } struct Derived : public Abstract { virtual void foo() { Abstract::foo(); // call the abstract impl } }; -- assignee: georg.brandl components: Documentation messages: 82618 nosy: georg.brandl, jaredgrubb severity: normal status: open title: abstractmethod vs C++ pure virtual versions: Python 2.6 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5349 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: extend getattr()
You could overload __getattr__ (might have to play around a bit to make sure any possible AttributeError's look right, but the basic idea is here) class A(object): # ... def __getattr__(self, name): try: return object.__getattribute__(self, name) except AttributeError: if '.' in name: attrs = name.split('.') first = object.__getattribute__(self, attrs[0]) return getattr(first, '.'.join(attrs[1:])) raise a = A() a.b = A() a.b.c = A() a.b.c.d = A() getattr(a, 'b.c.d') __main__.A object at 0x67f50 On 26 Jun 2008, at 04:06, Rotlaus wrote: Hello, lets assume i have some classes: class A(object): def __init__(self): b = B() class B(object): def __init__(self): c = C() class C(object): def __init__(self): pass and now i wanna do something like this: a=A() c=getattr(a, 'b.c') I know this doesn't work, but what can i do to get this or a similar functionality to get it work for this sample and for even more nested classes? Kind regards, Andre -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Hamming Distance
Matimus, I was surprised that lazy was the algorithm that won your time tests, and I saw a way to improve it even better (algorithm is O(# ones in number) rather than O(# bits in number)) def lazy2(a, b, bits=32): x = (a ^ b) ((1 bits) - 1) tot = 0 while x: tot += 1 x = x-1 return tot # times on my system (run a few times just to check for sure) python -mtimeit -sfrom ham import * test(lazy) 1 loops, best of 3: 121 usec per loop python -mtimeit -sfrom ham import * test(lazy2) 1 loops, best of 3: 62.4 usec per loop Check my math, but I think that's correct. Here's my derivation (but it's been a while since my Boolean algebra days, so I may've made a mistake!) It sounds right, though, since subtracting one in two's complement flips the rightmost one and inverts the zeros to the right of it. So, x (x-1) would remove the rightmost one. Right? Long Derivation: The trick to finding the rightmost one in a number: pos = x ^ (x-1) It has to do with how two's-complement works. In our algorithm above, we are trying to count them, so we want to flip off the bits one by one from the right. So in each loop: x = x ~pos But, then you notice you can simplify it even more (let y=x-1) and use mult/add syntax for and | and use X=~x and Y=~y x * ~(x ^ y) x * ~(xY+Xy)[ def of ^ ] x * (~(xY)*~(Xy)) [ DeMoires Law ] x * ( (X+y)*(x+Y) ) [ inversion] x * (X+y) * (x+Y) [ associative] (xX+xy)*(x+Y) [ distributive ] xy*(x+Y) [ xX = 0 ] xy+xyY [ distrib ] xy[yY = 0] So, x = x-1 On 19 Jun 2008, at 17:37, Matimus wrote: On Jun 19, 4:27 pm, godavemon [EMAIL PROTECTED] wrote: I need to calculate the Hamming Distance of two integers. The hamming distance is the number of bits in two integers that don't match. I thought there'd be a function in math or scipy but i haven't been able to find one. This is my function but it seems like there should be a faster way. I do this computation many times and speed up is important. def hamdist( a, b , bits = 32): def _hamdist( x, bits): if bits: return (x 1) + _hamdist(x 1, bits-1) return x 1 return _hamdist( a ^ b, bits) Another alternative would be to convert the XOR to a binary string and count the # of 1's. Which would be fastest? Are there better alternatives? Thanks! I see no good reason to use recursion for this type of thing. Here are some of my attempts: [code] from math import log def yours(a, b , bits = 32): def _hamdist( x, bits): if bits: return (x 1) + _hamdist(x 1, bits-1) return x 1 return _hamdist(a ^ b, bits) def simple(a, b, bits=32): x = a ^ b return sum((x i 1) for i in xrange(bits)) def lazy(a, b, bits=32): x = (a ^ b) ((1 bits) - 1) tot = 0 while x: tot += x 1 x = 1 return tot def fancy(a, b, bits=32): x = (a ^ b) ((1 bits) - 1) tot = 0 while x: tot += 1 x ^= 1 int(log(x, 2)) return tot test_vals = ( ((0x, 0), 32), ((0,0), 0), ((1,0), 1), ((0x8000, 0), 1), ((0x, 0), 16) ) def test(f): test_vals = ( ((0x, 0), 32), # ALL ((0,0), 0), # None ((1,0), 1), # First ((0x8000, 0), 1), # Last ((0x, 0), 16), # Every Other ((0x, 0), 16), # First Half ((0x, 0), 16), # Last Half ) for i, (args, exp) in enumerate(test_vals): if f(*args) != exp: return 0 return 1 if __name__ == __main__: for f in (yours, simple, lazy, fancy): if not test(f): print %s failed%f.__name__ [/code] The python module `timeit` is handy for testing speed: python -mtimeit -sfrom hamdist import * test(yours) 1 loops, best of 3: 95.1 usec per loop python -mtimeit -sfrom hamdist import * test(simple) 1 loops, best of 3: 65.3 usec per loop python -mtimeit -sfrom hamdist import * test(lazy) 1 loops, best of 3: 59.8 usec per loop python -mtimeit -sfrom hamdist import * test(fancy) 1 loops, best of 3: 77.2 usec per loop Even the ridiculous `fancy` version beat the recursive version. Matt -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Partition list with predicate
I want a function that removes values from a list if a predicate evaluates to True. The best I could come up with is: def extract(lst, pred): idx = 0 ret = [] for obj in lst[:]: if pred(obj): ret.append(obj) lst.pop(idx) else: idx += 1 return ret Anybody have a better, more Pythonic solution? One of my failed attempts was this code, which fails when the predicate itself has state: def extract(lst, pred): # BAD: Would not work in a case like pred = extract every other object ret = filter(lst, pred) for obj in ret: lst.remove(obj) return ret -- http://mail.python.org/mailman/listinfo/python-list
Re: Partition list with predicate
I guess I forgot one requirement: the removed elements need to be remembered. Basically, I have a list of objects in a buffer, one class operates on some of the objects, but other classes use others. So, a class must extract the ones it can handle, and leave the rest in the buffer for the other classes to handle. I haven't found a function that will both remove objects from a list, but save the ones that do get removed. Jared On 23 Apr 2008, at 10:15, Tim Golden wrote: Jared Grubb wrote: I want a function that removes values from a list if a predicate evaluates to True. The best I could come up with is: Have a look at the itertools module, and the ifilter function in particular. TJG -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Partition list with predicate
That almost works, except that a predicate can have memory. For example, the predicate could be extract every other object, which doesn't care about the value of the object (so you cant remove them later based on value!). Or your predicate might be remove the first 5 objects that are even, which means that the predicate must be run against the list in forward order with only one test per element (like an input/output iterator in C++; running the predicate changes the predicate itself, so you cant run one pass with pred and then another pass with not pred to get the rest). Example of a case where your proposed solution wouldn't quite work: class EveryOtherOne: def __init__(self): self.parity = False def __call__(self, obj): ret, self.parity = self.parity, not self.parity return ret pred = EveryOtherOne() lst = [1,2,2,1] extracted = [ obj for obj in lst if pred(obj) ] extracted [2, 1] lst = [ obj for obj in lst if obj not in extracted ] lst [] On Wed, Apr 23, 2008 at 6:14 PM, Brian [EMAIL PROTECTED] wrote: On Wed, Apr 23, 2008 at 9:08 PM, Jared Grubb [EMAIL PROTECTED] wrote: I guess I forgot one requirement: the removed elements need to be remembered. Basically, I have a list of objects in a buffer, one class operates on some of the objects, but other classes use others. So, a class must extract the ones it can handle, and leave the rest in the buffer for the other classes to handle. I haven't found a function that will both remove objects from a list, but save the ones that do get removed. Jared On 23 Apr 2008, at 10:15, Tim Golden wrote: Jared Grubb wrote: I want a function that removes values from a list if a predicate evaluates to True. The best I could come up with is: Have a look at the itertools module, and the ifilter function in particular. TJG -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list I would do it like this: # This takes out the values extracted = [ obj for obj in lst if pred(obj) ] # This filters out any item that was extracted lst = [ obj for obj in list if obj not in extracted ] Brian -- http://mail.python.org/mailman/listinfo/python-list
[issue2182] tokenize: does not allow CR for a newline
Jared Grubb [EMAIL PROTECTED] added the comment: Yes, but exec(string) also gives a syntax error for \r\n: exec('x=1\r\nprint x') The only explanation I could find for ONLY permitting \n as newlines in exec(string) comes from PEP278: There is no support for universal newlines in strings passed to eval() or exec. It is envisioned that such strings always have the standard \n line feed, if the strings come from a file that file can be read with universal newlines. (This is why my original example had to be exec(file) and not just a simple exec(string)) Of the 3 newline types, exec(*) allows 1 or all 3 as the case may be, and tokenize allows exactly 2 of them. I honestly am not sure what the right way is (or should be), but either way, the tokenize module is not consistent with exec. (By the way, if you're curious why I filed this issue and Issue#2180, I'm working on the PyPy project to help improve its current Python lexer/parser. In order to ensure that it is correct and robust, I was experimenting with corner cases in Python syntax and I found these cases where tokenize disagrees with exec.) __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue2182 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue2182] tokenize: does not allow CR for a newline
Jared Grubb [EMAIL PROTECTED] added the comment: I actually hadnt thought of that. PyPy should actually use universal newlines to its advantage; after all, it IS written in Python... Thanks for the suggestion! In any case, I wanted to get this bug about the standard library in your record, in case you wanted to handle it. It is fairly innocuous, so I'll let it go. Take care. __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue2182 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue2182] tokenize: does not allow CR for a newline
Jared Grubb [EMAIL PROTECTED] added the comment: This is not a report on a bug in exec(), but rather a bug in the tokenize module -- the behavior between the CPython tokenizer and the tokenize module is not consistent. If you look in the tokenize.py source, it contains code to recognize both \n and \r\n as newlines, but it ignores the possibility that \r could be the line ending character (as the Python reference says). __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue2182 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue2273] test_decimal: possible thread lockup in test case
New submission from Jared Grubb [EMAIL PROTECTED]: In Lib\test\test_decimal.py, attached is a bugfix for two bugs: 1) If the thfunc2 actually fails, then its thread will throw an exception and never set the Events that thfunc1 is waiting for; thus, thfunc1 never returns, causing the whole unittest to hang. 2) DecimalUseOfContextTest.test_threading should wait on both finish1 and finish2 (instead of waiting on finish1 twice). -- components: Library (Lib) files: test_decimal.patch keywords: patch messages: 63463 nosy: jaredgrubb severity: normal status: open title: test_decimal: possible thread lockup in test case type: crash versions: Python 2.5 Added file: http://bugs.python.org/file9656/test_decimal.patch __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue2273 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue2273] test_decimal: possible thread lockup in test case
Jared Grubb [EMAIL PROTECTED] added the comment: I ran into this bug because I created a context manager in one of my own projects, and the regression tests in test_decimal looked like a good start for my own regression tests... when some recent changes broke MY code, I found the test bug too and realized that the test_decimal file had the same problem. So, I figured I'd share the wealth :) __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue2273 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: for-else
I think bearophile makes an excellent point. I also have a hard time remembering what else does. I have always pictured that the normal behavior of a for loop is to get through all the items. In special circumstances, it is necessary to break out early. Therefore, it FEELS like the else loop should excute in the unexpected or abnormal case (break), whereas the else actually gets executed only when the entire loop DOES complete. Ben Finney's idea of using an except clause after the for loop is an excellent idea, as it makes it VERY clear when the case gets executed. (I would disagree with his suggestion to use else in the break case, however, because that would confuse the previous meaning.) However, you could then make break equivalent to raise BreakException, and provide the following: for foo in bar_sequence: # normal iteration spam(foo) if funky(foo): break except StopIteration, exc: # the iterator stopped normally eggs(exc) except BreakException, exc: # the iterator exited abnormally, i.e. 'break' sausage() Giving a loop a try-like behavior could open up the ability to jump out of multiple levels of loops: while 1: while 1: break except BreakException: break # or re-raise? Or maybe you can do raise BreakException(2) without any except clauses at all? Just my $0.02. Jared On 4 Mar 2008, at 12:27, [EMAIL PROTECTED] wrote: Raymond HettInger: FWIW, I'm very happy with for-else. Most of the time, you don't need it, but when you do, it beats the heck out of doing silly tricks with flags. I'd like it to be renamed to something more natural :-) Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
[issue2180] tokenize: mishandles line joining
Changes by Jared Grubb: -- components: Extension Modules nosy: jaredgrubb severity: minor status: open title: tokenize: mishandles line joining type: behavior versions: Python 2.5 __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue2180 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue2180] tokenize: mishandles line joining
New submission from Jared Grubb: tokenize does not handle line joining properly, as the following string fails the CPython tokenizer but passes the tokenize module. Example 1: s = if 1:\n \\\n #hey\n print 1 exec s Traceback (most recent call last): File stdin, line 1, in module File string, line 3 #hey ^ SyntaxError: invalid syntax tokenize.tokenize(StringIO(s).readline) 1,0-1,2:NAME'if' 1,3-1,4:NUMBER '1' 1,4-1,5:OP ':' 1,5-1,6:NEWLINE '\n' 2,0-2,2:INDENT ' ' 3,2-3,6:COMMENT '#hey' 3,6-3,7:NEWLINE '\n' 4,2-4,7:NAME'print' 4,8-4,9:NUMBER '1' 5,0-5,0:DEDENT '' 5,0-5,0:ENDMARKER '' __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue2180 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue2182] tokenize: does not allow CR for a newline
New submission from Jared Grubb: tokenize recognizes '\n' and '\r\n' as newlines, but does not tolerate '\r': s = print 1\nprint 2\r\nprint 3\r open('temp.py','w').write(s) exec(open('temp.py','r')) 1 2 3 tokenize.tokenize(open('temp.py','r').readline) 1,0-1,5:NAME'print' 1,6-1,7:NUMBER '1' 1,7-1,8:NEWLINE '\n' 2,0-2,5:NAME'print' 2,6-2,7:NUMBER '2' 2,7-2,9:NEWLINE '\r\n' 3,0-3,5:NAME'print' 3,6-3,7:NUMBER '3' 3,7-3,8:ERRORTOKEN '\r' 4,0-4,0:ENDMARKER '' -- components: Extension Modules messages: 62959 nosy: jaredgrubb severity: minor status: open title: tokenize: does not allow CR for a newline type: behavior versions: Python 2.5 __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue2182 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue2180] tokenize: mishandles line joining
Jared Grubb added the comment: CPython allows \ at EOF, but tokenize does not. s = 'print 1\\\n' exec s 1 tokenize.tokenize(StringIO(s).readline) 1,0-1,5:NAME'print' 1,6-1,7:NUMBER '1' Traceback (most recent call last): File stdin, line 1, in module File /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/tokenize.py, line 153, in tokenize tokenize_loop(readline, tokeneater) File /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/tokenize.py, line 159, in tokenize_loop for token_info in generate_tokens(readline): File /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/tokenize.py, line 283, in generate_tokens raise TokenError, (EOF in multi-line statement, (lnum, 0)) tokenize.TokenError: ('EOF in multi-line statement', (2, 0)) __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue2180 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: Adding properties to an instance
Here's one way of doing what you're asking... I would suggest using __getattribute__ and __setattr__ to dispatch the methods to the custom class you invent that holds all those properties. For example (I simplified your makeprops into __init__ just to keep the example short, but you can probably see the idea here..) class A(object): def __init__(self, **kw): class _PropHolder(object): pass for k,v in kw.items(): setattr(_PropHolder, k, property(fget=itemgetter(k))) self._custom_props = _PropHolder() def __getattribute__(self, attr): try: return getattr(self._custom_props, attr) except AttributeError: return getattr(self, attr) def __setattr__(self, attr, val): if hasattr(self._custom_props, attr): setattr(self._custom_props, attr, val) else: setattr(self, attr, val) def __delattr__(self, attr): if hasattr(self._custom_props, attr): delattr(self._custom_props, attr) else: delattr(self, attr) On 6 Feb 2008, at 12:06, [EMAIL PROTECTED] wrote: Hi all, So I understand that properties belong to a class not an instance, but nonetheless I want to add properties to an instance. I have a class which when an instance is created runs some fairly complicated code and produces a set of names which I'd like to be able to access via properties. At the moment, I'm using something like obj.getvar(name) but I'd like to be able to write obj.name. (Note that they can't be just standard attributes because they only get computed when they are accessed.) I could generate functions like obj.name() but I want it to be obj.name instead. The solution I've come up with is to create a new class for each object which is just the real class with some extra properties, and then dynamically change the class of the object to this new class. This seems to work, but I wonder if (a) there is a nicer solution than the one I'll post below, (b) if there are any dangers or pitfalls of this approach. The obvious difficulty is with derived classes. At the moment, I'm insisting that a derived class has to call a makeprops() method to create the properties. It's kind of similar to this recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/197965 but that recipe has a much simpler situation in which the properties and values are known at the time of the creation of the object (by contrast, I don't know what the properties are until the end of the __init__ method). Any thoughts? Code below to illustrate my approach. import warnings from operator import itemgetter class A(object): def __init__(self,**kwds): self._kwds = kwds self.makeprops() def __getitem__(self,i): return self._kwds[i] def makeprops(self): if not hasattr(self,'_madeprops'): self._madeprops = set() self._failedprops = set() class _A(self.__class__): pass for k,v in self._kwds.items(): if not k in self._madeprops and k in dir(self): if not k in self._failedprops: warnings.warn(Cannot create property +k+, already used in object +str(self),RuntimeWarning) self._failedprops.add(k) else: setattr(_A,k,property(fget=itemgetter(k))) self._madeprops.add(k) self.__class__ = _A class B(A): def __init__(self,**kwds): super(B,self).__init__(**kwds) self.makeprops() class C(A): def __init__(self,**kwds): self._kwds = kwds a = A(x=1) b = B(x=2,makeprops=3) c = C(x=3) print isinstance(a,A), isinstance(a,B), isinstance(a,C) # True False False print isinstance(b,A), isinstance(b,B), isinstance(b,C) # True True False print isinstance(c,A), isinstance(c,B), isinstance(c,C) # True False True print a.__class__ # class '__main__._A' print b.__class__ # class '__main__._A' print c.__class__ # class '__main__.C' print a.x # 1 print b.x # 2 print b.makeprops # bound method _A.makeprops of __main__._A object at 0x00A86810 try: print c.x # raises exception except AttributeError: print c has no element x c.makeprops() print c.x # 3 print a.__class__ # class '__main__._A' print b.__class__ # class '__main__._A' print c.__class__ # class '__main__._A' --- Dan Goodman http://thesamovar.net/contact -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding properties to an instance
Er, instead of getattr(self,...) you gotta do object.__getattr__(self,...) and same for setattr and delattr. Dumb error on my part. (Otherwise you get infinite recursion!) On Feb 6, 2008 12:43 PM, Jared Grubb [EMAIL PROTECTED] wrote: Here's one way of doing what you're asking... I would suggest using __getattribute__ and __setattr__ to dispatch the methods to the custom class you invent that holds all those properties. For example (I simplified your makeprops into __init__ just to keep the example short, but you can probably see the idea here..) class A(object): def __init__(self, **kw): class _PropHolder(object): pass for k,v in kw.items(): setattr(_PropHolder, k, property(fget=itemgetter(k))) self._custom_props = _PropHolder() def __getattribute__(self, attr): try: return getattr(self._custom_props, attr) except AttributeError: return getattr(self, attr) def __setattr__(self, attr, val): if hasattr(self._custom_props, attr): setattr(self._custom_props, attr, val) else: setattr(self, attr, val) def __delattr__(self, attr): if hasattr(self._custom_props, attr): delattr(self._custom_props, attr) else: delattr(self, attr) On 6 Feb 2008, at 12:06, [EMAIL PROTECTED] wrote: Hi all, So I understand that properties belong to a class not an instance, but nonetheless I want to add properties to an instance. I have a class which when an instance is created runs some fairly complicated code and produces a set of names which I'd like to be able to access via properties. At the moment, I'm using something like obj.getvar(name) but I'd like to be able to write obj.name. (Note that they can't be just standard attributes because they only get computed when they are accessed.) I could generate functions like obj.name() but I want it to be obj.name instead. The solution I've come up with is to create a new class for each object which is just the real class with some extra properties, and then dynamically change the class of the object to this new class. This seems to work, but I wonder if (a) there is a nicer solution than the one I'll post below, (b) if there are any dangers or pitfalls of this approach. The obvious difficulty is with derived classes. At the moment, I'm insisting that a derived class has to call a makeprops() method to create the properties. It's kind of similar to this recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/197965 but that recipe has a much simpler situation in which the properties and values are known at the time of the creation of the object (by contrast, I don't know what the properties are until the end of the __init__ method). Any thoughts? Code below to illustrate my approach. import warnings from operator import itemgetter class A(object): def __init__(self,**kwds): self._kwds = kwds self.makeprops() def __getitem__(self,i): return self._kwds[i] def makeprops(self): if not hasattr(self,'_madeprops'): self._madeprops = set() self._failedprops = set() class _A(self.__class__): pass for k,v in self._kwds.items(): if not k in self._madeprops and k in dir(self): if not k in self._failedprops: warnings.warn(Cannot create property +k+, already used in object +str(self),RuntimeWarning) self._failedprops.add(k) else: setattr(_A,k,property(fget=itemgetter(k))) self._madeprops.add(k) self.__class__ = _A class B(A): def __init__(self,**kwds): super(B,self).__init__(**kwds) self.makeprops() class C(A): def __init__(self,**kwds): self._kwds = kwds a = A(x=1) b = B(x=2,makeprops=3) c = C(x=3) print isinstance(a,A), isinstance(a,B), isinstance(a,C) # True False False print isinstance(b,A), isinstance(b,B), isinstance(b,C) # True True False print isinstance(c,A), isinstance(c,B), isinstance(c,C) # True False True print a.__class__ # class '__main__._A' print b.__class__ # class '__main__._A' print c.__class__ # class '__main__.C' print a.x # 1 print b.x # 2 print b.makeprops # bound method _A.makeprops of __main__._A object at 0x00A86810 try: print c.x # raises exception except AttributeError: print c has no element x c.makeprops() print c.x # 3 print a.__class__ # class '__main__._A' print b.__class__ # class '__main__._A' print c.__class__ # class '__main__._A' --- Dan Goodman http://thesamovar.net/contact -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: boolean decisions
Quine-McCluskey isn't too bad to do once or twice by hand, but if you change even one row in your dataset, you'll have to repeat the ENTIRE Q-M algorithm. It gets very tedious. For your application, I'd just use a hash table. You dont need the reduced form of your data, you just need a look-up table. It's a bit tedious to do by hand, but it will be much easier to change later for example, if (a,b,c,d,e,f) represents the 6 parameters as a tuple, then: # my_decision_tree.py - a module that gives a decision based on these data # Decision table encoded datarow = {} datarow[(0,0,0,0,0,0)] = 0 datarow[(0,0,0,0,0,1)] = 1 datarow[(0,0,0,0,1,0)] = 0 datarow[(0,0,0,0,1,1)] = 1 datarow[(0,0,0,1,0,0)] = 1 # . def get_decision(a,b,c,d,e,f): # Helper-function return datarow[(a,b,c,d,e,f)] Or you could assign functions: datarow[(0,0,0,0,0,0)] = function1 datarow[(0,0,0,0,0,0)] = function7 or lists datarow[(0,0,0,0,0,1)] = [ True, False, Andrew Jackson ] or whatever value you really want to return based on the input. You could create a Python script that would generate the 64 lines so all you have to do is manually set what each line evaluates to Again, this seems easier than trying to do Q-M by hand! Jared On 5 Feb 2008, at 06:31, Robin Becker wrote: Diez B. Roggisch wrote: Robin Becker wrote: ... terms or something, but perhaps I am daft. Triggered this in some deep-rootet parts of my brain stem: http://en.wikipedia.org/wiki/Quine-McCluskey_algorithm . seems like the sort of thing I can deal with though at least for this small case. -- Robin Becker -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Binary file Pt 1 - Only reading some
You should look into the struct module. For example, you could do the same thing via (using the variable names you used before): header_str = info.read(13) a,b,c,d,e = struct.unpack(6s4sBBB, header_str) After that, you will probably be able to get the integers by (doing it one at a time... read'ing more than 2 bytes at a time and processing it all at once is going to be more efficient, but this is the basic idea): read_str = info.read(2) val = struct.unpack(H, read_str) Read up on the struct module and I think you'll see what you need to do. (note that if you arent getting the right numbers for val, you will need to specify whether the data is big-endian or little-endian if you dont know what that means, you will find it on Wikipedia) Jared On 4 Feb 2008, at 19:51, Mastastealth wrote: I'm trying to create a program to read a certain binary format. I have the format's spec which goes something like: First 6 bytes: String Next 4 bytes: 3 digit number and a blank byte --- Next byte: Height (Number up to 255) Next byte: Width (Number up to 255) Next byte: Number 0 - 5 Every 2 bytes after that: Supposedly a number - 0899? Anyway, I'm able to do the first 2 objects fine: a = info.read(6) b = info.read(4) Printing both gives me what I mentioned above, a string and a 3 digit number with a space. However, as I continue, things get trickier. c = info.read(1) d = info.read(1) Printing c and d in this case gives me a block in the SPE output, or if I run in a DOS prompt, 2 funny symbols. How do I get an integer out of this? I'll probably need help once I get to the every 2 byte section, but that'll be a separate post. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: char string 2 hex string
You could also do: .join(['%02x' % ord(c) for c in 'AAA']) On 31 Jan 2008, at 14:09, Paul Rubin wrote: Antonio Chay [EMAIL PROTECTED] writes: AAA should be 414141 'AAA'.encode('hex') -- http://mail.python.org/mailman/listinfo/python-list On 31 Jan 2008, at 14:05, Antonio Chay wrote: Hello! I need to transform a string from a file into a hexadecimal representation, for example: AAA should be 414141 With perl I do this with: unpack(H*,AAA) And with python I got this: .join([str(hex(ord(x)))[2:] for x in AAA]) But seems a little weird for me. Is there another way? Thanks in advance! -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Iterate through slots
How can I iterate through the slots of a class, including those it inherits from parent classes? class C(object): __slots__ = ['a'] class D(C): __slots__ = ['b'] d = D() d.__slots__ ['b'] d.a = 1 # this works, so slots inherit properly d.b = 2 d.c = 3 # this doesnt work, like we expect Traceback (most recent call last): File stdin, line 1, in ? AttributeError: 'D' object has no attribute 'c' The reason I want to do this is that I want to have the following, and let all the children inherit the parent's __init__: class Parent(object): __slots__ = ['whatever'] def __init__(self, context=None, **kw): if context is None: context=getContext() for slot in SLOTITERATORHERE: # Resolve the slot's value, even for those things not specifically set in kw class Child1(Parent): __slots__ = ['for_me_only'] -- http://mail.python.org/mailman/listinfo/python-list