Fwd: Unit testing type comparison
Hello Mike, The reason of the problem is that the class Test was not pushed into the sys.modules. Use one more separate module for that stuff: *one.py* class Test(object): '''just define''' *three.py* from one import Test #push one.pyc to sys.modules if __name__ == '__main__': import two two.run( Test() ) *two.py* def run( a ): from one import Test #reuse existing Test print type(a), Test, type(a) == Test Regards, Alexey On Sat, Aug 2, 2008 at 5:16 PM, Mike Wyatt <[EMAIL PROTECTED]> wrote: > I have a problem where type comparisons don't work in a second module when > unit tests in the first module are run. In the example below, class Test is > declared in one.py. When one.py is executed, it calls a method in two.py > that imports Test from one.py. The problem is that the Test object passed > into the run method is in the "__main__" namespace, while the Test class > imported from one.py is in the "one" namespace. Comparing type(a) and Test > returns False, even though they are both Test objects. How can I tweak > these modules so that the type comparison returns True? > > I suppose one solution is to redesign my solution to not compare types, but > I would still appreciate a solution for my personal knowledge. > > one.py > class Test(object): > pass > > if __name__ == '__main__': > import two > two.run( Test() ) > > two.py *** > *def run( a ): > from one import Test > print type(a), Test, type(a) == Test > * > *** Output *** > * False > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: regex question
No, there is a bad way - because of the example doesn't solve arbitrary amount of ... blocks. But the python regexp engine supports for lookahead (?=pattern) and lookbehind (?<=pattern). In those cases patterns are not included into the replaced sequence of characters: >>> re.sub('(?<=\d)\.(?=\d)', '', '1234.324 abc.100.abc abc.abc') '1234324 abc.100.abc abc.abc' Alexey On Tue, Aug 5, 2008 at 2:10 PM, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]>wrote: > On Tue, 05 Aug 2008 11:39:36 +0100, Fred Mangusta wrote: > > > In other words I'd like to replace all the instances of a '.' character > > with something (say nothing at all) when the '.' is representing a > > decimal separator. E.g. > > > > 500.675 > 500675 > > > > but also > > > > 1.000.456.344 > 1000456344 > > > > I don't care about the fact the the resulting number is difficult to > > read: as long as it remains a series of digits it's ok: the important > > thing is to get rid of the period, because I want to keep it only where > > it marks the end of a sentence. > > > > I was trying to do like this > > > > s=re.sub("[(\d+)(\.)(\d+)]","... ",s) > > > > but I don't know much about regular expressions, and don't know how to > > get the two groups of numbers and join them in the sub. Moreover doing > > like this I only match things like "345.000" and not "1.000.000". > > > > What's the correct approach? > > In [13]: re.sub(r'(\d)\.(\d)', r'\1\2', '1.000.456.344') > Out[13]: '1000456344' > > > Ciao, > Marc 'BlackJack' Rintsch > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: regex question
=) Indeed. But it will replace all dots including ordinary strings instead of numbers only. On Tue, Aug 5, 2008 at 3:23 PM, Jeff <[EMAIL PROTECTED]> wrote: > On Aug 5, 7:10 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > > On Tue, 05 Aug 2008 11:39:36 +0100, Fred Mangusta wrote: > > > In other words I'd like to replace all the instances of a '.' character > > > with something (say nothing at all) when the '.' is representing a > > > decimal separator. E.g. > > > > > 500.675 > 500675 > > > > > but also > > > > > 1.000.456.344 > 1000456344 > > > > > I don't care about the fact the the resulting number is difficult to > > > read: as long as it remains a series of digits it's ok: the important > > > thing is to get rid of the period, because I want to keep it only where > > > it marks the end of a sentence. > > > > > I was trying to do like this > > > > > s=re.sub("[(\d+)(\.)(\d+)]","... ",s) > > > > > but I don't know much about regular expressions, and don't know how to > > > get the two groups of numbers and join them in the sub. Moreover doing > > > like this I only match things like "345.000" and not "1.000.000". > > > > > What's the correct approach? > > > > In [13]: re.sub(r'(\d)\.(\d)', r'\1\2', '1.000.456.344') > > Out[13]: '1000456344' > > > > Ciao, > > Marc 'BlackJack' Rintsch > > Even faster: > > '1.000.456.344'.replace('.', '') => '1000456344' > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: regular expressions.
Use the print statement: import re vowel = r'[aeiou]' print re.findall(vowel, r"vowel") Alexey On Fri, Aug 8, 2008 at 2:17 PM, Atul. <[EMAIL PROTECTED]> wrote: > > > Yes. You didn't paste the traceback into your message. > > > > >>> import re > > >>> vowel = r'[aeiou]' > > >>> re.findall(vowel, r"vowel") > > > > ['o', 'e'] > > > > It works as expected here. > > > > Peter > > When I key this input in IDLE it works but when I try to run the > module it wont work. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list