Re: simple question on dictionary usage
On Oct 27, 6:42 am, Karthik Gurusamy <[EMAIL PROTECTED]> wrote: > On Oct 26, 9:29 pm, Frank Stutzman <[EMAIL PROTECTED]> wrote: > > > > > My apologies in advance, I'm new to python > > > Say, I have a dictionary that looks like this: > > >record={'BAT': '14.4', 'USD': '24', 'DIF': '45', 'OAT': '16', > > 'FF': '3.9', 'C3': '343', 'E4': '1157', 'C1': '339', > > 'E6': '1182', 'RPM': '996', 'C6': '311', 'C5': '300', > > 'C4': '349', 'CLD': '0', 'E5': '1148', 'C2': '329', > > 'MAP': '15', 'OIL': '167', 'HP': '19', 'E1': '1137', > > 'MARK': '', 'E3': '1163', 'TIME': '15:43:54', > > 'E2': '1169'} > > > From this dictionary I would like to create another dictionary calld > > 'egt') that has all of the keys that start with the letter 'E'. In > > otherwords it should look like this: > > > egt = {'E6': '1182','E1': '1137','E4': '1157','E5': '1148', > >'E2': '1169','E3': '1163'} > > > This should be pretty easy, but somehow with all my googling I've > > not found a hint. > > One possible solution (read list-comprehension if you not familiar > with it): > > >>>record={'BAT': '14.4', 'USD': '24', 'DIF': '45', 'OAT': '16', > > ... 'FF': '3.9', 'C3': '343', 'E4': '1157', 'C1': '339', > ... 'E6': '1182', 'RPM': '996', 'C6': '311', 'C5': '300', > ... 'C4': '349', 'CLD': '0', 'E5': '1148', 'C2': '329', > ... 'MAP': '15', 'OIL': '167', 'HP': '19', 'E1': '1137', > ... 'MARK': '', 'E3': '1163', 'TIME': '15:43:54', > ... 'E2': '1169'}>>> egt =dict([(k,record[k]) for k inrecordif > k.startswith('E')]) > >>> egt > > {'E5': '1148', 'E4': '1157', 'E6': '1182', 'E1': '1137', 'E3': '1163', > 'E2': '1169'} > > Karthik > > > > > Thanks in advance > > > -- > > Frank Stutzman Hallo, a functional and concise way. egt= dict( filter( lambda item: item[0][0] == "E" , record.iteritems() )) Rainer -- http://mail.python.org/mailman/listinfo/python-list
operator overloading on built-ins
Hallo, could you explaint me the difference between the two following statements. Python 2.5 (r25:51908, Oct 7 2006, 23:45:05) [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> (1).__cmp__(10) -1 >>> 1.__cmp__(10) File "", line 1 1.__cmp__(10) ^ SyntaxError: invalid syntax The first works as expect, but the second. Regards Rainer -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding a HTTP header to a SOAPpy request
On Jan 3, 5:43 pm, Matias Surdi <[EMAIL PROTECTED]> wrote: > Hi, > > Could anybody tell me which is the easier way to do a SOAP call to a web > service wich requires an http header to be present? > > I can't figure it out. > > Thanks a lot > > Some code I'm using: > > import SOAPpy > s = > SOAPpy.SOAPProxy("http://10.3.5.128:10560/SERVICES",namespace="http://ws.mysite.com";) > > s.some_method() > > Thanks a lot. Hallo, look at http://pywebsvcs.sourceforge.net. There is a mailing list about SOAP and Python concerning SOAPpy. Greetings Rainer -- http://mail.python.org/mailman/listinfo/python-list
Re: Details about pythons set implementation
On Jan 4, 6:08 pm, Sion Arrowsmith <[EMAIL PROTECTED]> wrote: > Hrvoje Niksic <[EMAIL PROTECTED]> wrote: > > >BTW if you're using C++, why not simply use std::set? > > Because ... how to be polite about this? No, I can't. std::set is > crap. The implementation is a sorted sequence -- if you're lucky, > this is a heap or a C array, and you've got O(log n) performance. > But the real killer is that requirement for a std::set is that > T::operator< exists. Which means, for instance, that you can't > have a set of complex numbers > > -- Hallo and Sorry for being OT. As Arnaud pointed out, you must only overload the < Operator for the requested type. Something like bool operator < ( const Type& fir, const Type& sec ) similar to python with __lt__ . The rest of magic will be done by the compiler/interpreter. Assoziative Arrays (set,map,multi_set,multi_map) in the classical STL are implemented as binary trees. Therefore the keys must be comparable and the access time is O(log n ). To get a dictionary with O(1), the most STL implementation support a extension called hash_set. The new standard TR1 support unsorted_set ... . You can download it from www.boost.org. Newer gcc runtimes also including the new subnamespace tr1. There is no need to implement set in c++ to get O(1). Greetings Rainer -- http://mail.python.org/mailman/listinfo/python-list
Re: python interfaces
On Jan 4, 6:01 pm, Sion Arrowsmith <[EMAIL PROTECTED]> wrote: > hyperboreean <[EMAIL PROTECTED]> wrote: > >Why doesn't python provide interfaces trough its standard library? > > Because they're pointless. Java interfaces are a hack around the > complexities of multiple inheritence. Python does multiple > inheritence Just Fine (give or take the subtleties of super()) so > does not need them. > Hallo, Interfaces are a extremly smart Design Principle in static typed languages like Java and C++. C++ support Interfaces in a form of abstract base classes. They aren't pointless. They force the user of a framework to use it in a defined way. You prescribe in the base class the usage of a classsystem and allow only in derived classes to variate the behavior. To appreciate Interfaces look at the Template Methode Pattern (http:// en.wikipedia.org/wiki/Template_method_pattern ) or especially at the Non Virtual Interface ( http://www.gotw.ca/publications/mill18.htm ) Idiom from Herb Sutter. To be short C++ support Interfaces and multiple inheritace. Greetings Rainer -- http://mail.python.org/mailman/listinfo/python-list
Re: python interfaces
On Jan 6, 11:01 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > Interfaces are a extremly smart Design Principle in static typed > > languages like Java and C++. > > that's somewhat questionable in itself, and even more questionable as an > argument for interfaces in Python. > > I'd recommend anyone who thinks that they cannot program without formal > interfaces to try using Python as Python for a while, before they try > using it as something else. you might be surprised over how easy it is > to build robust stuff without having to add lots of extra constraints to > your code. > > Hallo, I argued, that Interface and multiple inheritance are different things and especially, that Interfaces are very useful in staticially typed languages. In such languages like Java and C++ you need a formalismen to guide the user. You may call it extension point, pure virtual function or abstract methode. Sorry for the misunderstanding, I argued for Interface in heavyweight static typed languages and nor for lightweight dynamic typed languages like python. They aren't pointless and a hack. Greetings Rainer -- http://mail.python.org/mailman/listinfo/python-list
Re: removing all instances of a certain value from a list
On Mar 19, 11:28 pm, Lee Sander <[EMAIL PROTECTED]> wrote: > Hi, > I have a float array ( eg [-1.3, 1.22, 9.2, None, 2.3] ) but there are > many missing vlaues which are represented as None. I would like to > remove all such instances in one go. > There is a remove function but it removes only the first instance, is > there a delete/remove all function? > thanks You can also do it with the filter function. >>> a= [-1.3, 1.22, 9.2, None, 2.3] >>> a=filter ( lambda b: b != None, a) >>> print a [-1.3, 1.22, 9.1993, 2.2998] Greetings Rainer -- http://mail.python.org/mailman/listinfo/python-list
singleton decorator
Hallo, playing with the decorators from PEP 318 I found the elegant singleton decorator. def singleton(cls): instances = {} def getinstance(): if cls not in instances: instances[cls] = cls() return instances[cls] return getinstance @singleton class A: pass class B: pass a1=A() a2=A() a3=A() b1=B() b2=B() b3=B() for i in ((a1,b1),(a2,b2),(a3,b3)): print id(i[0]),id(i[1]) But I always get a syntax error declaring class A as singleton. >>> reload ( decorator) Traceback (most recent call last): File "", line 1, in ? File "decorator.py", line 27 class A: pass ^ SyntaxError: invalid syntax What's the problem with this code because it's only copied for the PEP 318? It doesn't work with python 2.4 and python 2.5. Greetings Rainer -- http://mail.python.org/mailman/listinfo/python-list
Re: Pythonic design patterns
Hallo, > users in this forum has been kind enough to point out. Only my > implementations are often not that clean, and I may call things > something different than the normal convention, which is a source of > confusion for myself and others trying to communicate with me. I think, you should start with the classical books of Design Patterns to get a solid understanding and especially vocabulary to communicate with your coworkers. Its easier and better to say, that you will use a strategy pattern than to describe the architecture in many sentences to your partner in a ambigious way. Thats in my opinion the first and the key benefit of Design Patterns. Speaking in the same language. The next step should be to apply your knowledge to your programming language. So I will recommend the classical GOF Books and the serie Pattern Oriented Software Architecture. Greetings Rainer -- http://mail.python.org/mailman/listinfo/python-list
Re: folder extraction
On Dec 30 2008, 4:30 pm, ibpe...@gmail.com wrote: > how do i get along with this task of extracting multiples folder and > generating their names individually in a their respective files as > they were generated. Hallo, I hope, that I interpret your question in the right way. You can use the following function as a starting point to get all files ending with py or pyc from your working dir. Invoke getAllFilesOfPatterns(".","*.py *.pyc") import os import fnmatch def getAllFilesOfPatterns( dir ,patterns="*", recursive=True ): """ patterns must be space separeted string of patterns e.g: *.pdf *.ps *.html """ patterns= patterns.split() retValue=[] for path,dirs,files in os.walk(dir): for file in files: for pattern in patterns: if fnmatch.fnmatch( file , pattern ): retValue.append(os.path.join(path,file)) if not recursive: break return retValue Greetings -- http://mail.python.org/mailman/listinfo/python-list
None in comparison
Hello, I'm a little confused about None in comparison. >>> id ( None ) 3086100672L >>> id ( 1 ) 134541104 >>> None < 1 True >>> I thought, the id of the object is the last comparison criterion. Therefore, None must be greater then 1. Where is the behaviour of the comparison defined?. In the __cmp__ of int. Or in the global cmp Function? Thanks in advance Rainer -- http://mail.python.org/mailman/listinfo/python-list