Re: When to derive from object?
On Oct 13, 8:02 am, Matimus wrote: > On Oct 13, 7:45 am, Igor Mikushkin wrote: > > > Hello all! > > > I'm a newbie to Python. > > Could you please say me when it is better to derive from "object" and > > when not? > > > Thanks, > > Igor > > The only reason to derive from 'object' is ... erm... that should say "the only reason _not_ to derive from object" oops. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: When to derive from object?
On Oct 13, 7:45 am, Igor Mikushkin wrote: > Hello all! > > I'm a newbie to Python. > Could you please say me when it is better to derive from "object" and > when not? > > Thanks, > Igor The only reason to derive from 'object' is if there is some sort of weird side effect of using new style classes. I _have_ run into some interesting scenarios when creating com objects in python using the win32com module. Other than that, I always use new style classes (derived from object). Here are some articles that point out the distinctions between old and new style classes: http://python.org/doc/newstyle/ Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Modules/packages by GvR?
On Aug 28, 7:58 am, gb345 wrote: > Are there any Python-only modules or packages in the latest releases > of Python 2.x or Python 3.x that were largely written by Guido van > Rossum? What's the best way to find this out? I know that some > modules mention the author(s) in the source code, but this does > not seem to be true most of the time, as far as I can tell. > > I'm interested in reading this code as prime examplars of "Pythonicity". > (I'm sure that many other programmers could serve as models of the > Pythonic ideal, but I doubt that there would be a *less debatable* > choice in this category than GvR.) > > Many thanks in advance, > > Gabe I'm sure there are. You might be able to figure that out by browsing the source repository: http://hg.python.org. But, I wouldn't necessarily say that any code written by Guido would make a good example of 'Pythonic' code. Not that he doesn't create good code, but the language and standards have evolved over time. There may be code that he wrote from the 2.0 days that may have been perfectly 'Pythonic' then but is just out-of-date now. In general though, browsing the standard modules is a good way to find examples, no matter who wrote it. Just keep in mind when it was written more than who wrote it. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: 2.6 windows install
On Aug 20, 10:21 am, "Tim Arnold" wrote: > Hi, > I installed python2.6 to a netapp device. I can use it from my local windows > machine (XP). But others cannot use it from their pcs. > > They get this response > "The system cannot execute the specified program.". > > If they double click on python.exe, they get a window > > with: This application has failed to start because the application > > configuration is incorrect. Reinstalling the application may fix this > > problem. > > When I installed it I didn't see any mention of an 'administrators' install, > it just installed. The permissions on the directories where it installed are > set wide-open for everyone. > > Any ideas on what I'm missing here? > > thanks, > --Tim Arnold The default windows install puts Python26.dll in \windows\system32. I haven't tried this, but you could probably fix your install by moving Python26.dll into the Python26 directory. That being said, the usual thing to do would be to install it on each machine. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: What is the purpose of "struct" and "array" modules
On May 28, 11:17 am, Igor Katson wrote: > I pretty much understand what they do, but what's the case of using > these modules by example? Is it something like pickle, to store the data > efficiently in files? For one it provides a mechanism for reading and writing arbitrary file formats. For example, suppose you wanted to parse a PNG image using python code. It is likely that that format is described in its specification using C data structures (I've never looked at PNG format specifically). In python you can't just read out some data and copy it directly into a data structure. You could use the struct module though. It really has nothing to do with serializing python data structures. It is (mostly) about data formats and objects defined in other languages. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Inheritance and Design Question
On May 27, 12:58 pm, imageguy wrote: > I have an object the I would like to use as a base class. Some of the > methods I would like to override completely, but others I would simply > like to call the base class method and use the return value in the > child method. The purpose here is to eliminate the duplication of > valuable code in the parent, when I really just need the child to > operate of a results of the parent. > > Consider the following two classes; > > class Parent(object): > def process(self, value): > retval = "Parent.result('%s')" % value > return retval > > class Child(Parent): > def __init__(self): > Parent.__init__(self) > > def process(self, value): > retval = "Child.result('%s')" % super(Child, self).process > (value) > return retval > > So > > foo = Child() > print foo.process('the value') > > >> Child.result('Parent.result('the value')') > > IS there another pattern or idiom that would accomplish this? > This seems a bit 'smelly' to me. Also seems almost the inverse of > decorators, but I am not sure decorators would be appropriate in this > case. > > Any help suggestions would be appreciated. > > g. >From what you have shown, there really is no _good_ reason to use inheritance at all. Just delegate (which is the decorator pattern, different from function decorators). class Inner(object): def process(self, value): retval = "Parent.result('%s')" % value return retval class Outer(object): def __init__(self, inner): self._inner = inner def process(self, value): retval = "Outer.result('%s')" % self._inner.process(value) return retval This is a silly example, but if you can get the same thing done without creating a direct dependence between classes then don't. You will have to construct outer and pass it inner somewhere. If that is a problem, then just make a factory. In python I implement factories as just functions most of the time. The above also encourages reuse. Now you have a decorator that could be used anywhere. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: SQL and CSV
On May 5, 9:25 am, Nick wrote: > On May 5, 5:19 pm, Tim Golden wrote: > > > > > Nick wrote: > > > I have a requirement to read a CSV file. Normally, no problem, just > > > import CSV and slurp the file up. > > > > However, in this case I want to filter out lines that have fields set > > > to particular values. > > > > It would be neat to be able to do something like this. > > > > select * from test.csv where status <> "Canceled" > > > > Using adodb I can do this, so long as I don't have the where clause. :- > > > ( > > > > Is there a reasonable lightweight way of doing this in Python? > > > > I could write some python code that is used to filter rows, and inport > > > that from config, but it's not quite as elegant as an SQL route. > > > Not entirely clear what you are and aren't prepared to try here, but... > > the most obvious Python-based way to do this is treating the csv reader > > as an iterator and filtering there. Your last line suggests that's not > > what you want but just in case I've misunderstood: > > > > > id,code,status > > 1,"ONE","Active" > > 2,"TWO","Cancelled" > > 3,"THREE","Active" > > > > > > > import csv > > > for row in csv.DictReader (open ("c:/temp/test.csv", "rb")): > > if row['status'] != 'Cancelled': > > print row > > > > > > Doesn't seem too onerous, and could obviously be wrapped in > > some useful class/module. > > > But if you really want to go the SQL route, I believe there are > > ODBC adapters for CSV which, combined with PyODBC or CeODBC, > > would probably take you where you want to go. > > > TJG > > Part of the problem is that the 'selection' needs to be in a config > file. I can put the if row['status'] != 'Cancelled': return True into > a config, read it and eval it, but its not quite as clean as an sql > route. > > Nick Well, if you are using 2.5.x you could always stuff it into a sqlite in-memory database, and then execute a SQL query. Heck, you don't even _need_ 2.5, but in 2.5 sqlite is part of the distribution. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: nested looping
On Apr 8, 2:15 pm, PK wrote: > So I'm trying to see whats the cleanest way to do this: > > I have a > > checklist = [ax, bx, by, cy ..] (a combination of a,b,c with x and y, > either both on one) > > allist = [a,b,c,] > xlist = [x, y, ..] > > now I wanna loop through alist and xlist and see if the combination > exists in checklist > > so something like, > > for alpha in alist: > for xy in xlist: > if alpha+xy not in checklist: > missing.append(alpha) > > now the problem is I want to include alpha in missing list only if > none of the combinations from xlist with alpha are in checklist. > > The above will exclude the possibility where ax doesn't exist but ay > or az does exist. > > Hope There is a cleaner way to accomplish this. > > Thanks in advance, > PK That code doesn't look too bad. It is fairly readable. There are several ways to accomplish this though. If you aren't interested in duplicate values or order then sets would be a good option. something like this: missing = set(alpha + xy for alpha in alist for xy in xlist).difference (checklist) if you are using 2.6 you could also make use of the product generator in itertools: from itertools import product missing = set(''.join(p) for p in product(alist, xlist)).difference (checklist) If you could adapt your code to use tuples instead of strings you could do it like this: missing = set(product(alist, xlist)).difference(checklist) Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with lists.
On Feb 20, 8:12 am, "ssd" wrote: > Hi, > > In the following code, (in Python 2.5) > I was expecting to get in "b" variable the values b: [[0, 0], [0, 1],[0, > 2], [0, 3],[0, 4], [1, 0],[1, 1], [1, 2], .] > But I get only the last value [4,4], b: b: [[4, 4], [4, 4], [4, 4], ... ] > > My code: > > a = ["",""] > b = [] > > for i in range (0,5): > for j in range (0,5): > a[0] = i > a[1] = j > print "a: " + str(a) > b.append(a) > > print "b: " + str(b) > > what is worng in the code? > > Thanks, > Bye, The problem is that `a' is the name of a list object. When you change the contents of that list object you are simply mutating that object. You then append that list object to list `b'. But, you aren't creating a new list, you are simply mutating the same list object over and over and appending it to list `b'. So list `b' contains 5 references to the same object. A couple of things I would do differently. There is no reason to declare `a' outside of the loop. This isn't C you don't have to declare your variables before they are needed. `range(0, 5)' is usually just written `range(5)'. Also, check out `xrange'. Here are a couple of examples of alternatives: construct new lists and append them in the inner loop: b = [] for i in range(5): for j in range(5): b.append([i, j]) print b check out list comprehension: b = [[i, j] for i in range(5) for j in range(5)] print b Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: "import" not working?
On Feb 20, 3:56 pm, Lionel wrote: > On Feb 20, 3:52 pm, Chris Rebert wrote: > > > > > On Fri, Feb 20, 2009 at 3:33 PM, Lionel wrote: > > > Hello all: > > > > I've crafted several classes and exceptions which I've stored in a > > > file called "DataFileType.py". I then invoke them from within other > > > files like this: > > > > # Top of file > > > > import sys > > > sys.path.append("c:\DataFileTypes") > > > Recall that the backslash is the escape character in Python and that > > therefore you need to put \\ to get a backslash in the resulting path > > string. Thus, the path you think you're adding isn't the path that's > > getting added. > > Alternatively, you can just use forward slashes instead (yes, that > > works on Windows from Python). > > > Cheers, > > Chris > > > -- > > Follow the path of the Iguana...http://rebertia.com > > But I'm only using a single backslash in the first example I gave, and > it works just fine there. How can this be? You must be running the python script from a directory where the file you are trying to import is already in the path. It never tries to look in the (bad) path because it found a file with the same name locally. My guess is that you are running the wx example from another location, and that is when you run into problems. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Keeping the Console Open with IDLE
On Feb 19, 8:06 pm, "W. eWatson" wrote: > I'm using IDLE for editing, but execute programs directly. If there are > execution or "compile" errors, the console closes before I can see what it > contains. How do I prevent that? > -- > W. eWatson > > (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) > Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet > > Web Page: Open a console window and type in the name of the script rather than just double clicking on it. Or, you can terminate your script with a 'raw_input("press enter to quit")'. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Python knapsack problem
On Feb 13, 8:06 am, "Kurioz" wrote: > Hi, > > I got the assignment to solve the knapsack problem in Python. I have to find > the solution to put items in a sack (I have only one item A, B and C) which > maxWeight can't be larger than 6 kilograms. Solution of this problem should > be A and C but the only solution I'm getting is B and C which aren't true > because weight of the B and C is 7 kilograms which is heavier than the > maxWeight. > > If anyone could point me what am I doing wrong I'd be very grateful! Thanks > in advance! > > Code: > > #1st array element is weight of an item, and 2nd array element is value > A=[2.0,1.0] > B=[3.0,7.0] > C=[4.0,8.0] > #Checking the values per one kilo > vA=A[1]/A[0] > vB=B[1]/B[0] > vC=C[1]/C[0] > maxWeight=0 > print vA,vB,vC > #Checking the values > while maxWeight<6: > if int(vA)>int(vB) and int(vA)>int(vC): > maxWeight=maxWeight+A[0] > vA=0 > print maxWeight > > elif int(vB)>int(vA) and int(vB)>int(vC): > maxWeight=maxWeight+B[0] > print maxWeight > > else: > vC=0 > maxWeight=maxWeight+C[0] > print maxWeight You will need to check whether each item can fit before adding it. Currently you are doing: while there is room in the sac: add the next most valuable item You should be doing: while there is room in the sac: if the next most valuable item fits add it But... once you fix that you will run into another issue. You are using ints to compare. Casting floating point values to ints will always round down. vA = 0.5 vB = 2.... vC = 2.0 But.. >>> int(vA) 0 >>> int(vB) 2 >>> int(vC) 2 Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Added-value of frameworks?
On Feb 4, 8:08 am, Gilles Ganault wrote: > Hello > > If I wanted to build some social web site such as Facebook, what do > frameworks like Django or TurboGears provide over writing a site from > scratch using Python? > > Thank you for your feedback. Why not just look at the frameworks themselves and see what they have to offer. Django and Turbogears both have pretty good tutorials. You can be up and running in 20 minutes with each one. You be the judge. The frameworks provide a lot of boilerplate code that I would rather not write. They are probably more secure and scalable than something I would come up with. You also get many extras for free. I think in both of the frameworks you mention you get an administrative back end for free. Other people have created apps/plugins that you can use with those frameworks. So, for example, you might be able to borrow the code to help you add a forum to your site. I'm not sure I know the advantage of not using a framework. Unless "I get to write more code" is an advantage. Creating your own framework might be fun, but if you really just want a website don't do more work than you need to. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Window (tkinter) with no decoration
On Feb 3, 8:58 am, Djames Suhanko wrote: > Hello, programmers! > I would like to do a menu bar like kicker or windows menu. is possible? > > -- > Djames Suhanko > LinuxUser 158.760 Maybe you are looking for this? import Tkinter rt = Tkinter.Tk() rt.overrideredirect(True) # do stuff Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: optparse question
> I did all the requisite reading and found that I should use optparse > instead of getopt. I read the documentation and since the words > "simple" and "easy" often appeared in the examples and documentation, I > just knew that it would be a snap to implement. I don't know where you got that. 'getopt' works just fine. 'optparse' works fine too. I don't think anybody is going to get too worked up over which you decide to use for such a simple case. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: what's the point of rpython?
The goals are listed here: http://codespeak.net/pypy/dist/pypy/doc/architecture.html Speed is mentioned, but as a secondary concern. The main goal seems to be to create a vehicle into exploring the concept of dynamic languages themselves. If that seems amorphous then it is because it is a research project. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: optimizing large dictionaries
On Jan 15, 1:39 pm, Per Freem wrote: > hello > > i have an optimization questions about python. i am iterating through > a file and counting the number of repeated elements. the file has on > the order > of tens of millions elements... > > i create a dictionary that maps elements of the file that i want to > count > to their number of occurs. so i iterate through the file and for each > line > extract the elements (simple text operation) and see if it has an > entry in the dict: > > for line in file: > try: > elt = MyClass(line)# extract elt from line... > my_dict[elt] += 1 > except KeyError: > my_dict[elt] = 1 > > i am using try/except since it is supposedly faster (though i am not > sure > about this? is this really true in Python 2.5?). > > the only 'twist' is that my elt is an instance of a class (MyClass) > with 3 fields, all numeric. the class is hashable, and so my_dict[elt] > works well. > the __repr__ and __hash__ methods of my class simply return str() > representation > of self, while __str__ just makes everything numeric field into a > concatenated string: > > class MyClass > > def __str__(self): > return "%s-%s-%s" %(self.field1, self.field2, self.field3) > > def __repr__(self): > return str(self) > > def __hash__(self): > return hash(str(self)) > > is there anything that can be done to speed up this simply code? right > now it is taking well over 15 minutes to process, on a 3 Ghz machine > with lots of RAM (though this is all taking CPU power, not RAM at this > point.) > > any general advice on how to optimize large dicts would be great too > > thanks for your help. You can use a tuple instead of a string, which should be a little quicker: def __hash__(self): return self.field1, self.field2, self.field3 You could speed it up even more if you just saved a single attribute "fields" as a tuple to begin with. Also, you can use defauldict and get rid of the try/except. I don't think try/except is slow, but avoiding it will give you a speed up. from collections import defaultdict my_dict = defaultdict(int) for line in file: elt = MyClass(line)# extract elt from line... my_dict[elt] += 1 You might even consider turning "MyClass" into just a function that extracts the values from the line and returns a tuple, which should give you even more of a boost since a tuple is completely implemented in C. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: the name of a method
On Jan 15, 8:23 am, thomas.steffe...@googlemail.com wrote: > Hello, > > I have a Class: > > class myClass: > def __init__(self): > # do something > print "name of class = " + self.__class__.__name__ > > def myMethod(self): > # do something > print "name of method = " + "myMethod" > return > > ... > > I print the name of the class with self.__class__.__name__ in > __init__. > I want to print also in every method of myClass the name of the > method. > How can I get the name? I would not like to write e.g. "myMethod". Is > there a variable like self.__class__.__name__ for this? > Thanks for your hints, Thomas I would just use a decorator: >>> def print_method_name(meth): ... def new_meth(*args, **kwargs): ... print meth.func_name ... return meth(*args, **kwargs) ... return new_meth ... >>> class MyClass(object): ... @print_method_name ... def my_method(self): ... pass ... >>> x = MyClass() >>> x.my_method() my_method Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: point class help
On Jan 14, 8:50 am, r wrote: > On Jan 14, 10:44 am, Steve Holden wrote: > > > Thous it does seem particularly perverse to have the add method not > > itself return a Point. > > Thanks Steve, > i was going implement exactly this but thought there "might" be a > better way i did not know about. So i feel better about myself > already. And your right, i should be returning a Point2d() > Many Thanks I just inherited from tuple and did it like this. Note that this served my needs, but it definitely needs some work to become general purpose. Also note that I used '__new__' instead of __init__, making this type immutable. class Point(tuple): "Simple immutable point class (vector) supports addition and subtraction" def __new__(cls, x, y=None): if y is None: x, y = x return super(Point, cls).__new__(cls,(x, y)) def __add__(self, p): return Point(self[0]+p[0], self[1]+p[1]) def __sub__(self, p): return Point(self[0]-p[0], self[1]-p[1]) def __neg__(self): return Point(-self[0], -self[1]) def __pos__(self): return self def __str__(self): return "Point(%d, %d)"%(self[0], self[1]) __repr__ = __str__ Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 3.0 nonlocal statement
On Jan 6, 5:31 am, Casey wrote: > In PEP 3104 the nonlocal statement was proposed and accepted for > implementation in Python 3.0 for access to names in outer scopes. The > proposed syntax included an optional assignment or augmented > assignment to the outer name, such as: > > nonlocal x += 1 > > This syntax doesn't appear to be supported in the 3.0 implementation. > My question is: was this intentional or was it missed in the initial > release? If it was intentional, is there any plan to support it in a > later 3.x release? I realize it is a very small convenience feature > but I have already come across a couple of cases where I use nested > functions where it does make the code seem a little cleaner. > > Regards, Casey `nonlocal` should behave just like `global` does. It doesn't support that syntax either. So, yes it was intentional. No, there probably is no plan to support it in a later release. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: How to initialize a class variable once
On Dec 8, 8:08 pm, Roy Smith <[EMAIL PROTECTED]> wrote: > I've got a class with a class variable: > > class Foo: > _map = {} > > How do I make sure this only gets initialized the *first* time the > module containing the class is imported? What appears to be happening > as it stands is each time the module gets imported, Foo._map get re- > initialized. Unless you are calling reload() on the module, it will only ever get _loaded_ once. Each additional import will just yield the existing module. Perhaps if you post an example of the behavior that leads you to believe that the class variables are getting reinitialized I can provide more useful help. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: "as" keyword woes
On Dec 4, 6:08 am, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Wed, 03 Dec 2008 17:15:21 -0800, Matimus wrote: > >> Couldn't we have continued along just fine using a smarter parser > >> without elevating "as" to reserved status (and thus potentially > >> breaking a 10+ years of existing code)? > > > Nothing broke your code. It works just fine under the version it was > > developed for. Who forced you to upgrade to python2.6? > > Be reasonable. Python 2.5 is not very far away from being put into > "security updates only" mode, and in a year or so it won't even get > security updates. I dare say there are already platforms that use Python > 2.6 as standard. Tying your software to an obsolete version of a language > is a good way to force your software into obsolescence. > > Not that 2.5 is obsolete *now*. But it will be soon (for some definition > of soon): in no more than a year or three, software that only runs on > Python 2.5 would be like software that only runs on 2.3 now. > > -- > Steven Here is the list of downloads from python.org: # Python 3.0 (December 3, 2008) # Python 2.6 (October 1, 2008) # Python 2.5.2 (February 22, 2008) # Python 2.4.5 (March 11, 2008) # Python 2.3.7 (March 11, 2008) Notice that Python 2.3 was given the update treatment in March of this year. I don't think I was being unreasonable. The point was that there is that new releases don't _break_ anything. You should always expect to have to test and update your code when going from Python2,x to Python2.(x+1). Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: "as" keyword woes
> What I want to understand is why this parser change was necessary in > order to enable new 2.6/3.0 features. Was this change potentially > avoidable? Does it really matter? The change occurred and it isn't going to go back. What you should be asking yourself is whether the affect it had on your code could have been avoided. What could you have done to prevent your current situation? > Couldn't we have continued along just fine using a smarter parser > without elevating "as" to reserved status (and thus potentially breaking > a 10+ years of existing code)? Nothing broke your code. It works just fine under the version it was developed for. Who forced you to upgrade to python2.6? 'as' was already coming close to being a keyword with its use in import statements. It be came a full fledged keyword to support context managers: with open('filename.x', 'w') as fout: # do stuff > (Unfortunately, our project may now have to maintain a branch at 2.5.x > in order to preserve compatibility with existing third-party scripts & > infrastructure which routinely rely upon "as" as an object method. > Sigh.) Not necessarily. You can do something like this: import sys import warnings class MyClass(object): def new_as(self): # call this something appropriate, it is your new 'as' method pass # do what 'as' does if sys.version[:3] <= '2.5': def _old_as(self): warnings.warn( DeprecationWarning( 'This method is deprecated, use `new_as` instead')) return self.new_as() exec 'as = _old_as' You could even write a metaclass that does this (a little more elegantly). Your users will get a nice warning telling them not to use the 'as' method anymore. It will work in both code bases, and you can schedule to remove it at some later version after customers have had the opportunity to remove 'as' from their code. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: performance question: dictionary or list, float or string?
On Dec 2, 3:51 am, [EMAIL PROTECTED] wrote: > I forgot to mention that I did a simple timeit test which doesn't > show > significant runtime difference 3.5 sec for dictionary case and 3.48 > for > list case. > > def read_as_dictionary(): > fil = open('myDataFile', 'r') > forces = {} > for region in range(25): > forces[region] = {} > > for step in range(2): > for region in range(25): > line = fil.next(); spl = line.split() > forces[region] [step] = spl > > def read_as_list(): > fil = open('myDataFile.txt', 'r') > forces = [] > for region in range(25): > forces.append([]) > > for step in range(2): > for region in range(25): > line = fil.next(); spl = line.split() > forces[region].append(spl) > > Cheers, > /Ben There really isn't enough information to recommend a particular direction. A dictionary doesn't seem appropriate for this information though. Also, you are hard coding the step range to 2. Is that the number of lines in the file? That isn't really a safe way to do it. # this is just bad style in python: line = fil.next(); spl = line.split() # better written spl = fil.next().split() I would just do it this way: def read_as_list(data, regions=25, maxlines=2): # If data is a filename, open the file. If it is a file # object or any sequence of 'lines' it should just work. file_opened = False if isinstance(data, basestring): data = open(data, 'r') file_opened = True forces = [[] for _ in xrange(regions)] try: for i, line in data: if i == maxlines: break forces[i % 25].append(line.split()) finally: if file_opened: f.close() return forces Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: how to dynamically instantiate an object inheriting from several classes?
On Nov 21, 2:11 pm, Joe Strout <[EMAIL PROTECTED]> wrote: > I have a function that takes a reference to a class, and then > instantiates that class (and then does several other things with the > new instance). This is easy enough: > > item = cls(self, **itemArgs) > > where "cls" is the class reference, and itemArgs is obviously a set of > keyword arguments for its __init__ method. > > But now I want to generalize this to handle a set of mix-in classes. > Normally you use mixins by creating a class that derives from two or > more other classes, and then instantiate that custom class. But in my > situation, I don't know ahead of time which mixins might be used and > in what combination. So I'd like to take a list of class references, > and instantiate an object that derives from all of them, dynamically. > > Is this possible? If so, how? > > Thanks, > - Joe I wrote this a while ago. I sort of regret it though. Mixins could (and I will argue should) be avoided most of the time by delegating to other objects with less functionality. Utilizing many mixin classes tends to just make gigantic classes. This is a huge violation of the "Single Responsibility Principle". That isn't to say that I don't think there is a place for multiple inheritance. Multiple inheritance to the point where it is easier to write a metaclass to automatically generate your __init__ method than it is to write it yourself is a good indicator that you have gone too far. Which is what I did. code: import inspect class AutoGenInitMetaError(Exception): """ Exception is raised if AutoGenInitMeta cannot auto-generate a constructor for a class because of conflicting parameters in base classes. """ pass class AutoGenInitMeta(type): """ Meta-Class for automatically generating __init__ method for a class with multiple mixin base classes. """ def __new__(cls, name, bases, assoc): if "__init__" in assoc: return super(AutoGenInitMeta, cls).__new__(cls, name, bases, assoc) args = ['self'] dargs = [] defaults = [] tmpl = [] varg = None vkwarg = None tmpl = ["def __init__%s:\n"] for base in bases[::-1]: a, va, vkw, d = argspec = inspect.getargspec (base.__init__) argspecfmt = inspect.formatargspec(*argspec[:3]) if d: num_d = len(d) args += a[1:-num_d] defaults += d dargs += a[-num_d:] else: # remember to stip off self args += a[1:] if va is not None: if varg is not None: raise AutoGenInitMetaError( "There must be only one `*` arg in base constructors" ) varg = va if vkw is not None: if vkwarg is not None: raise AutoGenInitMetaError( "There must be only one `**` arg in base constructors" ) vkwarg = vkw tmpl.append("%s.__init__%s\n"%(base.__name__, argspecfmt)) tmpl = "".join(tmpl) argspec = (args + dargs, varg, vkwarg, defaults) exec tmpl%inspect.formatargspec(*argspec) in globals(), assoc return super(AutoGenInitMeta, cls).__new__(cls, name, bases, assoc) How do you use it? >>> class C(object): ... def __init__(self, a, b): ... self.a = a ... self.b = b ... >>> class D(object): ... def __init__(self, c, d): ... self.c = c ... self.d = d ... >>> class CD(C, D): ... __metaclass__ = AutoGenInitMeta ... >>> >>> x = CD(1,2,3,4) >>> >>> x.a 3 >>> x.b 4 >>> x.c 1 >>> x.d 2 >>> Notice that the arguments to D came before C. So you have to list the classes in reverse order of how you want the arguments listed. I post it as an example of a "neat python trick". Even the "neat" might be self indulgence. I encourage anybody tempted to use this to refactor/redesign instead. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Searching for Regular Expressions in a string WITH overlap
On Nov 20, 4:31 pm, Ben <[EMAIL PROTECTED]> wrote: > I apologize in advance for the newbie question. I'm trying to figure > out a way to find all of the occurrences of a regular expression in a > string including the overlapping ones. > > For example, given the string 123456789 > > I'd like to use the RE ((2)|(4))[0-9]{3} to get the following matches: > > 2345 > 4567 > > Here's what I'm trying so far: > > #!/usr/bin/env python > > import re, repr, sys > > string = "123456789" > > pattern = '(((2)|(4))[0-9]{3})' > > r1 = re.compile(pattern) > > stringList = r1.findall(string) > > for string in stringList: > print "string type is:", type(string) > print "string is:", string > > > Which produces: > > string type is: > string is: ('2345', '2', '2', '') > > > I understand that the findall method only returns the non-overlapping > matches. I just haven't figured out a function that gives me the > matches including the overlap. Can anyone point me in the right > direction? I'd also really like to understand why it returns a tuple > and what the '2', '2' refers to. > > Thanks for your help! > -Ben 'findall' returns a list of matched groups. A group is anything surrounded by parens. The groups are ordered based on the position of the opening paren. so, the first result is matching the parens you have around the whole expression, the second one is matching the parens that are around '(2)|(4)', the third is matching '(2)', and the last one is matching '(4)', which is empty. I don't know of a way to find all overlapping strings automatically. I would just do something like this: >>> import re >>> text = "0123456789" >>> p = re.compile(r"(?:2|4)[0-9]{3}") # The (?:...) is a way of isolating the >>> values without grouping them. >>> start = 0 >>> found = [] >>> while True: ... m = p.search(text, start) ... if m is None: ... break ... start = m.start() + 1 ... found.append(m.group(0)) ... >>> found ['2345', '4567'] Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: special editor support for indentation needed.
On Nov 14, 11:41 am, "Eric S. Johansson" <[EMAIL PROTECTED]> wrote: > in trying to make programming in Python more accessible to disabled > programmers > (specifically mobility impaired speech recognition users), and hitting a bit > of > a wall. The wall (for today) is indentation. I need a method of getting the > "right indentation" without having to speak a bunch of unnecessary commands. > For example, depth specified by the previous line. But, frequently you need > to > go to a more arbitrary indentation for example the right level of indentation > for a method definition or class definition. This indentation should be > something you could get by typing/speaking the right command with your eyes > closed. > > For example if I was to give the command "new method", I should be able to > spit > out a template (contained within the speech recognition environment) and > through > a command embedded in the template, force indentation to the right level for > "def" and then the editor would control indentation for the rest of the text > injection. > > I prefer are working in Emacs because that's where I have a lot of my speech > grammars set up but I'm not averse to trying another editor. The only > condition > is that the editor must run a Windows and have remote editing mode (like > tramp) > so I can edit on remote machines. > > ideas? You could make use of something like 'pindent.py' (comes with python in windows under the \Python2x\Tools\Scripts folder). Which allows you to put in block delimiters as comments, and then fixes the code to align itself correctly to those blocks: def foobar(a, b): if a == b: a = a+1 elif a < b: b = b-1 if b > a: a = a-1 # end if else: print 'oops!' # end if # end def foobar Funny though. I always saw this tool as a sort of joke response to people who missed their block delimiters. "You want block delimiters? Use this." And see how long before they decide it is silly. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: using "private" parameters as static storage?
On Nov 13, 9:16 am, Joe Strout <[EMAIL PROTECTED]> wrote: > One thing I miss as I move from REALbasic to Python is the ability to > have static storage within a method -- i.e. storage that is persistent > between calls, but not visible outside the method. I frequently use > this for such things as caching, or for keeping track of how many > objects a factory function has created, and so on. > > Today it occurred to me to use a mutable object as the default value > of a parameter. A simple example: > > def spam(_count=[0]): > _count[0] += 1 > return "spam " * _count[0] > > >>> spam() > 'spam ' > >>> spam() > 'spam spam ' > Don't Do this, it is confusing and there are definitely (many) better facilities in python for handling saved state. > Ooh, for a change I had another thought BEFORE hitting Send rather > than after. Here's another trick: > > def spam2(): > if not hasattr(spam2,'count'):spam2.count=0 > spam2.count += 1 > return "spam2 " * spam2.count This is definitely preferred over the first. However the preferred method is just to use a class. Preserving state is what classes are for. >>> class Spam(object): ... def __init__(self): ... self._count = 0 ... def spam(self): ... self._count += 1 ... return " ".join("spam" for _ in xrange(self._count)) ... >>> x = Spam() >>> print x.spam() spam >>> print x.spam() spam spam >>> print x.spam() spam spam spam It also gives you the ability to have two compleately separate instances of the same state machine. >>> y = Spam() >>> print y.spam() spam >>> print y.spam() spam spam >>> print y.spam() spam spam spam >>> You can use it like a function if you need to for convenience or backwards compatibility.: >>> spam = Spam().spam >>> print spam() spam >>> print spam() spam spam >>> print spam() spam spam spam Or: >>> class Spam(object): ... def __init__(self): ... self._count = 0 ... ... def spam(self): ... self._count += 1 ... return " ".join("spam" for _ in xrange(self._count)) ... ... __call__ = spam ... >>> spam = Spam() >>> print spam() spam >>> print spam() spam spam >>> print spam() spam spam spam Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: duck-type-checking?
On Nov 12, 7:06 am, Joe Strout <[EMAIL PROTECTED]> wrote: > Let me preface this by saying that I think I "get" the concept of duck- > typing. > > However, I still want to sprinkle my code with assertions that, for > example, my parameters are what they're supposed to be -- too often I > mistakenly pass in something I didn't intend, and when that happens, I > want the code to fail as early as possible, so I have the shortest > possible path to track down the real bug. Also, a sufficiently clever > IDE could use my assertions to know the type of my identifiers, and so > support me better with autocompletion and method tips. > > So I need functions to assert that a given identifier quacks like a > string, or a number, or a sequence, or a mutable sequence, or a > certain class, or so on. (On the class check: I know about > isinstance, but that's contrary to duck-typing -- what I would want > that check to do instead is verify that whatever object I have, it has > the same public (non-underscore) methods as the class I'm claiming.) > > Are there any standard methods or idioms for doing that? > > Thanks, > - Joe The only thing that comes to mind is to use explicit checking (isinstance). The new 'abc' module in 2.6 is worth a look. It seems like this sort of thing might become a _little_ more popular. I think duck-typing is great, but there are instances where you really want the code to be better at documenting your intention about what interface you expect an object to have, and also to catch the problems that it might cause early and in a place where you might actually be able to catch a meaningful exception. I've been looking and haven't found any clear rules here. It is a trade off. The more complex the interface, the more you will likely want to do an explicit check. On the other hand, the more complex the interface the more likely it is that you are violating the 'Interface Segregation Principle'. That is, you probably want to consider breaking the functionality down into smaller interfaces, or even separate classes. IMO explicit checking, like global variables or using a 'goto' in C, are evil. That isn't to say that you should _never_ use them. Heck, there are plenty of gotos in CPython. You want to minimize, and clearly document the places where your code is evil. There are times where it is necessary. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Extending Logger
> Yes but in the other hand > :http://docs.python.org/library/logging.html#logger-objects > "Note that Loggers are never instantiated directly, but always through > the module-level function logging.getLogger(name)." That is part of the power of the logging module. If you ask for a logger of the same name in two different place you will get the same object. That way you don't have to pass the logger instances around, and it always has the same properties (same handlers attached, same level, etc.). If you want to add functionality you getLoggerClass, inherit from that, then setLoggerClass, and use the regular logging facilities to instantiate them. The only limitation here is that you can't change the constructor. Well, it isn't useful to change the constructor. You can add a method that does the same thing. I can't help but feel that if you have a problem with this, you simply aren't using the Logging module the way it was meant to be used. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Building loop with some exceptions?
On Nov 4, 11:20 am, Gilles Ganault <[EMAIL PROTECTED]> wrote: > Hello > > I need to call a URL through a loop that starts at 01 and ends at 99, > but some of the steps must be ignored: > > = > url = "http://www.acme.com/list?code="; > p = re.compile("^(\d+)\t(.+)$") > > for i=01 to 99 except 04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89: > f = urllib.urlopen(url + i) > page = f.read() > f.close() > > for line in page: > m = p.search(line) > if m: > code = m.group(1) > label = m.group(2) > > print "Code=" + code + " # Label=" + label > = > > I'm clueless at what the Python way is to do this :-/ > > Thank you for any help. I would just do something like this (not tested): url_tmpl = "http://www.acme.com/list?code=%02d"; p = re.compile("^(\d+)\t(.+)$") EXCLUDED_VALUES = 4, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89 for i in xrange(1,100): if i in EXCLUDED_VALUES: continue f = urllib.urlopen(url_tmpl % i) try: for line in f: m = p.search(line) if m: code = m.group(1) label = m.group(2) print "Code=", code, "# Label=", label finally: f.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: brackets content regular expression
On Oct 31, 11:57 am, netimen <[EMAIL PROTECTED]> wrote: > Thank's but if i have several top-level groups and want them match one > by one: > > text = "a < b < Ó > d > here starts a new group: < e < f > g >" > > I want to match first " b < Ó > d " and then " e < f > g " but not " > b < Ó > d > here starts a new group: < e < f > g " > On 31 ÏËÔ, 20:53, Matimus <[EMAIL PROTECTED]> wrote: > > > On Oct 31, 10:25šam, netimen <[EMAIL PROTECTED]> wrote: > > > > I have a text containing brackets (or what is the correct term for > > > '>'?). I'd like to match text in the uppermost level of brackets. > > > > So, I have sth like: ' 123 < 1 aaa < t bbb < a ff > > 2 > > > > b'. How to match text between the uppermost brackets ( 1 aaa < t > > > bbb < a ff > > 2 )? > > > > P.S. sorry for my english. > > > I think most people call them "angle brackets". Anyway it should be > > easy to just match the outer most brackets: > > > >>> import re > > >>> text = " 123 < 1 aaa < t bbb < a ff > > 2 >" > > >>> r = re.compile("<(.+)>") > > >>> m = r.search(text) > > >>> m.group(1) > > > ' 1 aaa < t bbb < a ff > > 2 ' > > > In this case the regular expression is automatically greedy, matching > > the largest area possible. Note however that it won't work if you have > > something like this: " ". > > > Matt > > As far as I know, you can't do that with a regular expressions (by definition regular expressions aren't recursive). You can use a regular expression to aid you, but there is no magic expression that will give it to you for free. In this case it is actually pretty easy to do it without regular expressions at all: >>> text = "a < b < O > d > here starts a new group: < e < f > g >" >>> def get_nested_strings(text, depth=0): ... stack = [] ... for i, c in enumerate(text): ... if c == '<': ... stack.append(i) ... elif c == '>': ... start = stack.pop() + 1 ... if len(stack) == depth: ... yield text[start:i] ... >>> for seg in get_nested_strings(text): ... print seg ... b < O > d e < f > g Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: brackets content regular expression
On Oct 31, 10:25 am, netimen <[EMAIL PROTECTED]> wrote: > I have a text containing brackets (or what is the correct term for > '>'?). I'd like to match text in the uppermost level of brackets. > > So, I have sth like: ' 123 < 1 aaa < t bbb < a ff > > 2 > > b'. How to match text between the uppermost brackets ( 1 aaa < t > bbb < a ff > > 2 )? > > P.S. sorry for my english. I think most people call them "angle brackets". Anyway it should be easy to just match the outer most brackets: >>> import re >>> text = " 123 < 1 aaa < t bbb < a ff > > 2 >" >>> r = re.compile("<(.+)>") >>> m = r.search(text) >>> m.group(1) ' 1 aaa < t bbb < a ff > > 2 ' In this case the regular expression is automatically greedy, matching the largest area possible. Note however that it won't work if you have something like this: " ". Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing with interspersed element
On Oct 30, 2:10 pm, "Paulo J. Matos" <[EMAIL PROTECTED]> wrote: > On Thu, Oct 30, 2008 at 8:42 PM, Arnaud Delobelle > > > > <[EMAIL PROTECTED]> wrote: > > On Oct 30, 8:07 pm, "Paulo J. Matos" <[EMAIL PROTECTED]> wrote: > >> Hi all, > > >> I guess this is a recurring issue for someone who doesn't really know > >> the python lib inside out. There must be a simple way to do this. > >> I have a list of objects [x1, x2, x3, ..., xn] and I have defined a > >> print method for them print_obj(). Now I want to print them > >> intersepersed by an element. > >> If I print [x1, x2, x3] interspersed by the element 10: > >> x1.print_obj() 10 x2.print_obj() 10 x3.print_obj() > > >> Now, the question is, what's the best way to do this? > > > Defining a print_obj() method is probably a bad idea. What if you > > want to print to a file for example? Instead you can define a > > __str__() method for your objects and then use the join() method of > > strings like this: > > > print ' 10 '.join(str(x) for x in lst) > > Thanks for the tip but that has an issue when dealing with potentially > millions of objects. You are creating a string in memory to then dump > to a file [or screen] while you could dump to the file [or screen] as > you go through the original string. Right? Then I hope you are using stackless, because you are going to stack overflow _way_ before you recurse 1 million times. def print_list(seq, sep=','): seq = iter(seq) print seq.next(), for item in seq: print sep, print item, print Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Limit between 0 and 100
On Oct 25, 1:42 pm, [EMAIL PROTECTED] wrote: > Hi. I'm very new to Python, and so this is probably a pretty basic > question, but I'm lost. I am looking to limit a float value to a > number between 0 and 100 (the input is a percentage). > > I currently have: > > integer = int() > running = True > > while running: > try: > per_period_interest_rate = float(raw_input("Enter per-period > interest rate, in percent: ")) > break > except ValueError: > print "Please re-enter the per-period interest rate as a number > between 0 and 100." > > I also have to make sure it is a number and not letters or anything. > > Thanks for the help. > > James > > P.S. I don't understand a lot of what I have there, I got most of it > from the beginning tutorials and help sections. I have never > programmed before, but this is for a school assignment. You aren't very far off. You are going to need to use 'if' and '<' or '>' to check for range though. As in: if x > 10: print "x is greater than 10" OR: if 10 < x < 20: print "x is between 10 and 20" If you describe exactly what it is that you don't understand, people here will be willing to help you to understand it. However, you will find that people here are very unwilling to do your homework for you. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Global dictionary or class variables
On Oct 24, 1:44 pm, Mr.SpOOn <[EMAIL PROTECTED]> wrote: > Hi, > in an application I have to use some variables with fixed valuse. > > For example, I'm working with musical notes, so I have a global > dictionary like this: > > natural_notes = {'C': 0, 'D': 2, 'E': 4 } > > This actually works fine. I was just thinking if it wasn't better to > use class variables. > > Since I have a class Note, I could write: > > class Note: > C = 0 > D = 2 > ... > > Which style maybe better? Are both bad practices? It really depends on how you plan to use them. I might use a dictionary if I'm likely be handling the notes as characters. If they are just constants that I plan to use in my program, I would probably just define a set of global names. The best practice I have found is a combination. NOTES = C,D,E,F,G,A,B = "CDEFGAB" note_2_step = dict(C=0, D=2, E=4, F=5, G=7, A=9, B=11) This allows you to do both. There are schemes where you might want to use a class, but without more information it doesn't really seem necessary. Globals are frowned upon, but constant tend to be just fine. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: logging module
On Oct 23, 7:58 am, "Werner F. Bruhin" <[EMAIL PROTECTED]> wrote: > I am starting to use the logging module. > > Simple log to file and/or console work very nicely. > > Even managed to get TimedRotatingFileHandler to work. > > The problem I am trying to solve. > > 1. I would like to have a "log viewer" a wxPython based app to be able > to look at a log generated by another script. Running in a separate process? That isn't possible. Okay, well, it isn't possible without doing a fair amount of work on your own. You would probably need to create a handler that writes everything to a pipe or a socket, and then have your second application read from the pipe/socket and display it. I don't think that feature exists right now though. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: ImportError in python 2.5 in C API DLL
On Oct 16, 10:58 am, Henrik <[EMAIL PROTECTED]> wrote: > Hi, > > We are upgrading from Python 2.3 to verion 2.5 and when we recompile > we get ImportError. > > To test we took the spam example from the web documentation and > compiled it with Py23 and it imports without a problem. Changing the > libs in visual studio 2008 to point to Py25 and we get: > > >>> import spam > > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named spam > > > > Would really appreciate any assistance. > > H > > -- > #include "stdafx.h" > #ifdef _MANAGED > #pragma managed(push, off) > #endif > BOOL APIENTRY DllMain( HMODULE hModule, > DWORD ul_reason_for_call, > LPVOID lpReserved > ) > { > return TRUE;} > > #ifdef _MANAGED > #pragma managed(pop) > #endif > static PyObject * > spam_system(PyObject *self, PyObject *args) > { > const char *command; > int sts; > if (!PyArg_ParseTuple(args, "s", &command)) > return NULL; > sts = system(command); > return Py_BuildValue("i", sts);} > > static PyMethodDef PyBSMethods[] = { > {"spam", (PyCFunction) spam_system, METH_VARARGS|METH_KEYWORDS, > "Hi"}, > {NULL, NULL, 0, NULL} /* Sentinel */}; > > extern "C" { > __declspec(dllexport) void initspam(void) > { > PyObject* module = Py_InitModule("spam", PyBSMethods); > PyObject* d = PyModule_GetDict(module); > }} > > -- What about the simple stuff like: did you put your dll/pyd file where python 2.5 can find it? Are you including the correct Python.h, are you linking against the correct library (python25.lib or python25_d.lib)? Have you tried debugging with the "python -v" option (shows import trace messages)? Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Making class attributes non-case-sensitive?
> So is iterating through dir() to force both the members of dir(), and > the requested attribute name, to lower case for a comparison, really > the easiest way? > > Thanks again for sticking with me. I hope I didn't add to the > confusion. What I learn I will of course pass on. > > - Rafe It still isn't clear to me _why_ you are wrapping this COM object. You aren't adding any functionality. If you are using win32com and the TLB object you are using has a tlb, then you can generate wrapper classes for them automatically using makepy. You can extend those. If you want to do it by hand you should be able to just create a class and inherit win32com.client.DispatchBaseClass (and object if you want to make it new-style). Unless your users are screaming for this feature, or there is some technical reason that it is required, then implementing it is a waste of time. Anybody who is used to developing at all is going to accept that the software is case sensitive. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Upgrading from 2.5 to 2.6
> Python 2.5 and 2.6 can coexist, so there isn't any need for some > kind of upgrade procedure. Installing 2.6 will not affect your > 2.5 installation. That isn't entirely true. In Windows, python files bound to a particular version of python in the registry. So, for example, if you double click on "some_prog.py" to run it, then it will by default start up using python2.6 once it is installed. Also right-clicking on a file and selecting "edit with Idle" will open the version of Idle associated with 2.6 (under 2.6). This is a minor difference, but I wouldn't say that there are no side effects. They can be easily worked around. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Making class attributes non-case-sensitive?
On Oct 13, 4:08 am, Rafe <[EMAIL PROTECTED]> wrote: > Just so I don't hijack my own thread, the issue is 'how to wrap an > object which is not case sensitive'. > > The reason I am stuck dealing with this?... The application's API is > accessed through COM, so I don't know if I can do anything but react > to what I get. The API was written while the app (Softimage|XSI - one > of 3 leading 3D applications for high-end visual effects) was owned by > Microsoft. I'm not sure if it is standard for Microsoft or just the > way this app was implemented (perhaps because under-users were > scripting in VBscript which is not case sensitive). > > XSI allows many languages to be used via COM, even from within the > software (there are built-in code editors). In the early days, > VBScript was the most common scripting language used while anything > more hard-core was done in C++ (of course the C implementation is case > sensitive - well as far as I know). Then JScript became the most > common, now Python is considered standard. > > Anyway, the standard practice is to use mixed-case, so I need to > adhere to it as the resulting framework I am creating needs to be > intuitive to use (my end-user is still writing code. It's an API for > an API I guess...) > > I don't *think* I need to worry too much about performance because I'm > not doing any serious processing, this is more about convention > enforcement and quality control rather than number crunching. I might > try to write something generic which gets executed by the wrappers > __getattr__ and __setattr__, but I was hoping for some nifty > workaround, maybe in the form of a decorator or something? Again... > any ideas? > > Cheers, > > - Rafe > > On Oct 13, 4:15 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > > > Rafe wrote: > > > Hi, > > > > I'm working within an application (making a lot of wrappers), but the > > > application is not case sensitive. For example, Typing obj.name, > > > obj.Name, or even object.naMe is all fine (as far as the app is > > > concerned). The problem is, If someone makes a typo, they may get an > > > unexpected error due accidentally calling the original attribute > > > instead of the wrapped version. Does anyone have a simple solution for > > > this? > > > > I can protect against some cases just by making an 'alias': > > > class AClass(object): > > > def name(self): > > > print "hello" > > > > Name = name > > > > ...but this doesn't protect against typos, it gets more complicated > > > with multi-word attribute names, and it makes my epydocs confusing to > > > read since all spelling versions are shown (I AM concerned about my > > > docs being clear, but not as much as stopping typo related errors). > > > > I thought about using my wrapper's __getattr__ and __setattr__, but I > > > I am concerned about the overhead of every delegated attribute call > > > running a search and compare (.lower() based compare?). > > > > Any ideas or precedence? > > > Ideas? Don't do that... > > > Seriously: where does that code come from, who's typing it? If it is python, > > then make people follow python's rules. If it is some sort of homebrewn > > language you map to python, adapt the mapper to enforce lower-case and make > > all your properties lower case. > > > Diez > > So, this application you are writing for allows you to script/write callbacks in python? They are then somehow accessable through a COM interface exposed by the application? You are worried that someone using the application will run into case-sensitivity if they access the code written in python? There isn't much overhead with __getattr__ since it is _only_ called if the initial look-up didn't find the name. You can do something like this: class C(object): def __init__(self, ...): ... self._lookingup = False ... # If this is re-entered while already looking up a value, # then we know that there is a problem. Not thread safe. def __getattr__(self, attr): try: if self._lookingup: raise AttributeError("'C' object has no attribute %r"%attr) self._lookingup = True return getattr(self, attr.lower()) finally: self._lookingup = False def __setattr__(self, attr, value): super(C, self).__setattr__(attr.lower(), value) Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Get "code object" of class
On Oct 10, 5:50 am, Okko Willeboordse <[EMAIL PROTECTED]> wrote: > To get the "code object" c of my_class I can do; > > c = compile(inspect.getsource(my_class), "
Re: Quality control in open source development
On Oct 8, 8:43 am, Dave <[EMAIL PROTECTED]> wrote: > With the open source licenses that allow redistribution of modified > code, how do you keep someone unaffiliated with the Python community > from creating his or her own version of python, and declaring it to be > Python 2.6, or maybe Python 2.7 without any approval of anyone at the > PSF? How are they going to "declare" that their version is Python 2.x? What forum would they use. Current users of python most likely look to comp.lang.python or python.org for their python update news. New users of python are likely to use google or another search engine, and probably land at python.org. Is it possible for me to take Python's source code, make some changes, and post it somewhere as Python 2.7? Yes. Will anybody notice? Not likely. Others have made some pretty sound arguments around trademarks and such, but I'm going to simply argue that Python as a community has its own inertia, and it simply isn't a practical to be concerned about a dubious fork. It simply wouldn't take off. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Inheritance but only partly?
On Oct 2, 1:16 pm, process <[EMAIL PROTECTED]> wrote: > Let's say I have a class X which has 10 methods. > > I want class Y to inherit 5 of them. > > Can I do that? Can I do something along the lines of super(Y, exclude > method 3 4 7 9 10) ? I think the noral way of doing that is to split the origional class into two classes. What you have: class BaseClass(object): def method0: pass def method1: pass def method2: pass ... def method9: pass What you need: class BaseClassWant(object): def method0: pass def method1: pass ... def method4: pass class BaseClassDontWant(object): def method5: pass def method6: pass ... def method9: pass class BaseClass(BaseClassWant, BaseClassDontWant): # same as BaseClass above pass class YourClass(BaseClassWant): pass Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Wait or not?
On Sep 30, 4:20 pm, Eric <[EMAIL PROTECTED]> wrote: > I've been wanting to learn Python for a while now but I can't decide > on whether to wait for Python 3's final release and learn it or just > go ahead and learn 2.x. Would it be hard to make the transition being > a noob? It shouldn't be a hard transition. I would recommend learning 2.x anyway. You are likely to want to use 3rd party modules (or you will anyway). You won't want to wait for module support to be added in 3.0. The 2.x series isn't going to go away for quite a while. I think they are planning to continue to do releases all the way up to 2.9 or so. Even then, the biggest thing that a new user is going to run into is `print("stuff")` vs. `print "stuff"`. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: One class per file?
> The book "Code Complete" recommends that you put only one class in a > source file, which seems a bit extreme for me. It seems that many > classes are small, so that putting several of them in a file seems > reasonable. I noticed that the decimal.py module in the standard > library has several classes, all of which of course revolve around the > "decimal" topic. Perhaps a better rule of thumb is "one idea per > file." I checked the Python style guide and there seems to be no > mention of this topic. I know this is an elementary question, but what > is the Python way of doing this? I'm in a similar situation. I've been reading Robert C. Martins book "Agile Software Development" and he suggests something similar. I would highly recommend that book by the way. He also has a couple of chapters on packaging where he makes some very good points about the reasoning behind it. Basically that you want to organize your code in such a way that package dependencies move in the direction of increasing stability. In this case stability is a metric which is defined as how likely the code is going to change in the future as determined by how many packages depend on it as opposed to how many packages it depends on. He paints a couple of scenarios in which he groups packages together that _seem_ to be related, only to see that it results in all of his packages needing to be re-built every time a change is required. Obviously we don't have to re-build python code, but it is still useful to organize your code in such a way that you don't have to constantly re-visit collections of code. I have actually started using his suggestion and have been putting one class per file. When one class depends on another I include it with a "from x import X". This makes it very easy to determine the dependencies (well, the non-abstract dependencies anyway, interfaces are a different story*). Then I put all of my classes together in a package, and make the "public" classes visible on a package level by importing them into the package __init__ module. With that being said, If I made a class that was _only_ used by one other single class, and it was clear that it would never be made visible outside of that file, I would certainly put it in the same file as that class. Heck, you might even refactor your code and determine at that time that some piece of code is only used by one other piece. It is much easier to put code together after the fact than it is to separate it, especially later in the game. My advice: don't knock it until you try it. I think my code has improved quite a bit since taking this advice. It can be much more difficult to determine which classes to package together until much later in the development cycle. One thing that can help is to find an IDE that helps you to easily switch between files. I use WingIDE, but I have even used vim with a tags file and it wasn't terrible. I wouldn't call it a hard rule, but at the same time I wouldn't just ignore it. Give it a shot and adapt your development technique to see what works best for you. Example: [mypackage/__init__.py] __all__ = ["X"] from .x import X [end mypackage/__init__.py] [mypackage/x.py] from .y import Y __all__ = ["X"] class X(object): # code - using Y hopefully [end mypackage/x.py] [mypackage/y.py] __all__ = ["Y"] class Y(object): # code [end mypackage/y.py] Matt * Interfaces in python represent an implicit dependency. The Zen of Python states: "Explicit is better than implicit". I plan to experiment with the ABC module in python 2.6/3.0. I want to make my interfaces explicitly defined, but on the other hand I still want it to be easy for people who use my code to duck-type. This _seems_ to be possible since you can .register a class with an interface after it has been created, but I'm not sure that it is very pythonic to explicitly check for an interface every time it is used. It would seem silly however to import an interface into a file where it isn't explicitly used just to document the dependency. If anybody has pointers let me know. -- http://mail.python.org/mailman/listinfo/python-list
Re: ConfigParser subclass problem
On Sep 26, 12:56 pm, Strato <[EMAIL PROTECTED]> wrote: > Hi folks, > > I think I do something wrong, but I don't see why it doesn't work, so I > will explain: > > I've searched in the list archive and found this thread, that explain > exactly what I want to have: the options strings returned by > ConfigParser without being lower cased. > > I tryed to reproduce this, by subclassing the SafeConfigParser class, > like this: > > * in my main script: > > from MyConfigParser import * > > * in the MyConfigParser.py file: > from ConfigParser import SafeConfigParser > > class MyConfigParser(SafeConfigParser): > def optionxform(self, optionstr): > print "Called the modified version of the class" > return optionstr > > then, when I run the script, every time a calls to my own class is done, > the print statment is shown in the console, so this may work, but when I > use the .items() method of the class, I got a lower cased result ! > > (In my test, using the .items() method is my only try to test the new class) > > Does anybody has an idea of what I'm doing wrong ? > > Thanks, > Strato I don't know what you are doing wrong. It works just fine for me: [code] from ConfigParser import SafeConfigParser from StringIO import StringIO class MyConfigParser(SafeConfigParser): def optionxform(self, optionstr): print "Called the modified version of the class" return optionstr def main(): contents = StringIO(""" [Section1] Foo=Bar Bar=Baz Spam=Eggs [Section2] Hello=World """) raw_parser = SafeConfigParser() raw_parser.readfp(contents) print "Using SafeConfigParser Section1" for item in raw_parser.items("Section1"): print item print "Using SafeConfigParser Section2" for item in raw_parser.items("Section2"): print item contents.seek(0) my_parser = MyConfigParser() my_parser.readfp(contents) print print "Using MyConfigParser Section1" for item in my_parser.items("Section1"): print item print "Using MyConfigParser Section2" for item in my_parser.items("Section2"): print item if __name__ == "__main__": main() [/code] Produces the output: Using SafeConfigParser Section1 ('foo', 'Bar') ('bar', 'Baz') ('spam', 'Eggs') Using SafeConfigParser Section2 ('hello', 'World') Called the modified version of the class Called the modified version of the class Called the modified version of the class Called the modified version of the class Using MyConfigParser Section1 ('Foo', 'Bar') ('Bar', 'Baz') ('Spam', 'Eggs') Using MyConfigParser Section2 ('Hello', 'World') Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: a short-cut command for globals().clear() ??
On Sep 22, 2:31 pm, [EMAIL PROTECTED] wrote: > hi all, > > forgive me , but the RTFM and Google search approaches are not > yielding an answer on this question. I need to know if there's a top > level python interpreter command that clears all user variables (not > built-ins) from the global namespace. In other words a statement, or > some_command_or_function(), that does this: > > >>> x=3 > >>> y=4 > >>> z=[] > >>> dir() > > ['__builtins__', '__doc__', '__name__', 'x', 'y', 'z'] > > >>> some_command_or_function() > >>> dir() > > ['__builtins__', '__doc__', '__name__'] > > thanks, > 1 desperate snake oil programmer I don't think you will find anything. The interpreter is essentially the same whether you are in interactive mode or not. That is, there is very little use for a method that clears globals in general, so why would we add it just so that it could be used by the interpreter. There is almost* nothing available to the interactive interpreter which isn't part of the core language. * The only difference I can think of is the "_" variable, which is added to __builtins__ and contains the last value returned in interactive mode. If you have ever tried to run code that uses the locale module from the interpreter you will see why having any differences between the interactive and non-interactive interpreter can be a pain. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: What do you call a class not intended to be instantiated
On Sep 21, 3:39 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > I have a class which is not intended to be instantiated. Instead of using > the class to creating an instance and then operate on it, I use the class > directly, with classmethods. Essentially, the class is used as a function > that keeps state from one call to the next. > > The problem is that I don't know what to call such a thing! "Abstract > class" isn't right, because that implies that you should subclass the > class and then instantiate the subclasses. > > What do you call such a class? > > -- > Steven It actually sounds like you are doing something similar to the monostate pattern. In Java the monostate pattern is usually implemented by creating a class that only has static functions and variables. You _can_ instantiate it, but every instance will share the same state. In that way it is very similar to the Singleton pattern (http://en.wikipedia.org/wiki/Singleton_pattern). In Python I haven't found a need for monostate, since you can override __new__ and return exactly the same instance. However, you you wanted to be able to inherit a monostate object and share some of the state between the class and subclass it might still be useful. Perhaps if you post some example code? Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: optparse
I'm assuming you read at least some of the docs. This page makes it pretty clear: http://docs.python.org/lib/optparse-default-values.html Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Persuading ConfigParser to give me the section elements in the same order as the file
On Sep 10, 1:52 pm, geoffbache <[EMAIL PROTECTED]> wrote: > Hi all, > > I recently needed to parse a file that was perfect for ConfigParser > apart from one thing: the elements in the sections, although > definitions, could in some cases clash with each other and therefore > it was important to be able to retrieve them in the same order as they > appeared in the file. > > Unfortunately ConfigParser uses ordinary dictionaries for the section > elements and they are therefore returned in an arbitrary order. > > The only solution I found was to copy ConfigParser.py and replace all > the dictionaries with "sequential dictionaries" > which are exactly like dictionaries except that elements are returned > in the order they were inserted. > (seehttp://home.arcor.de/wolfgang.grafen/Python/Modules/seqdict/Seqdict.html) > > I wonder if there was a better way? For example, is there any hook > that could modify what is created by the statement > > x = {} > > I tried setting > > __builtins__.dict = ndict.seqdict > > But that didn't seem to have any effect on the above statement. > > As a secondary question, I find sequential dictionaries to be an > essential part of programming in Python and I use them all the time. I > wondered a bit if there were any plans or proposals to include them as > part of the Python library? > > Regards, > Geoff Bache Have a look at this: http://www.python.org/dev/peps/pep-0372/ Looking at the config parser module, it looks like there are only a couple of places where {} is used. I would create a mixin class to replace the offending methods. That should work because it looks like you only have to replace "__init__" and "add_section". So... class OrderedConfigParserMixin: def __init__(self, defaults=None): self._sections = ndict.seqdict() self._defaults = ndict.seqdict() if defaults: for key, value in defaults.items(): self._defaults[self.optionxform(key)] = value def add_section(self, section): """Create a new section in the configuration. Raise DuplicateSectionError if a section by the specified name already exists. """ if section in self._sections: raise DuplicateSectionError(section) self._sections[section] = ndict.seqdict() # Then you can use this to create your own ordered config parsers. Note that # multiple inheritance in python uses a breadth first search. If you want # the methods on your mixin to get called instead of the methods on the # original class you must include the mixin first. from ConfigParser import RawConfigParser, ConfigParser, SafeConfigParser class OrderedRawConfigParser(OrderedConfigParserMixin, RawConfigParser): pass class OrderedConfigParser(OrderedConfigParserMixin, ConfigParser): pass class OrderedSafeConfigParser(OrderedConfigParserMixin, SafeConfigParser): pass I don't know if this is the _best_ approach, but it is certainly much preferred over monkey patching the built-ins module. Note that I haven't tested any of the above code. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Test if list contains another list
On Sep 8, 12:32 am, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > mathieu a écrit : > > > Hi there, > > > I am trying to write something very simple to test if a list > > contains another one: > > > a = [1,2,3] > > > b = [3,2,1,4] > > > but 'a in b' returns False. > > Indeed. Lists are not sets, and the fact that all elements of list a > happens to also be part of list b doesn't make the list a itself an > element of list b. > > >>> a = [1, 2, 3] > >>> b = [3,2,1,4] > >>> c = [b, a] > >>> a in c > True > >>> b in c > True > >>> c > [[3, 2, 1, 4], [1, 2, 3]] > >>> > > > How do I check that a is indeed contained > > in b ? > > But that's what you did - you *did* checked if a was contained in b, and > this is not the case. What you want is to check if *all elements* of a > are contained in b, which is quite another problem. Now the answer to > your question : use sets. > > >>> set(a).issubset(set(b)) > True > >>> > > HTH Just to clarify, doing it using sets is not going to preserve order OR number of elements that are the same. That is: >>> a = [1,1,2,3,4] >>> b = [4,5,3,7,2,6,1] >>> set(a).issubset(set(b)) True This will return True if b contains at least on of each element found in a. If the OP wanted to check that list `a` appeared in order somewhere in list `b` then sets won't work. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: creating a similar object from an derived class
On Sep 3, 12:09 pm, Scott <[EMAIL PROTECTED]> wrote: > Let's say I have an object: > > class foo(): > def create_another() > return foo() > > def blah(): > x = self.create_another() > ... do something with X > > Now I create a inherited class of this object: > > class bar(foo): > ... > > If I call bar.create_another(), it will return a foo() instead of a > bar(). This isn't what I want. I would like bar.create_another() to > create an instance for bar(). Obviously I can do this by overriding > create_another, i.e. > > class bar(foo): > def create_another() > return bar() > > However, is there a way for me to modify foo() so that it > automatically creates objects of the derived class, so that I don't > have to continue to redefine create_another() ? > > For example, I tried the following: > > def create_another() > return self.type()() > > but it did not work. > > Thanks, > Scott This works: >>> class C(object): ... @classmethod ... def create_another(cls): ... return cls() ... >>> class D(C): ... pass ... >>> d = D() >>> e = d.create_another() >>> isinstance(e, D) True Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: eval() == evil? --- How to use it safely?
On Aug 28, 3:09 pm, "Guilherme Polo" <[EMAIL PROTECTED]> wrote: > On Thu, Aug 28, 2008 at 6:51 PM, Fett <[EMAIL PROTECTED]> wrote: > > I am creating a program that requires some data that must be kept up > > to date. What I plan is to put this data up on a web-site then have > > the program periodically pull the data off the web-site. > > > My problem is that when I pull the data (currently stored as a > > dictionary on the site) off the site, it is a string, I can use eval() > > to make that string into a dictionary, and everything is great. > > However, this means that I am using eval() on some string on a web- > > site, which seems pretty un-safe. > > > I read that by using eval(code,{"__builtins__":None},{}) I can prevent > > them from using pretty much anything, and my nested dictionary of > > strings is still allowable. What I want to know is: > > > What are the dangers of eval? > > - I originally was using exec() but switched to eval() because I > > didn't want some hacker to be able to delete/steal files off my > > clients computers. I assume this is not an issue with eval(), since > > eval wont execute commands. > > - What exactly can someone do by modifying my code string in a command > > like: thing = eval(code{"__builtins__":None},{}), anything other than > > assign their own values to the object thing? > > By "disabling" __builtins__ you indeed cut some obvious tricks, but > someone still could send you a string like "10 ** 10 ** 10". > > > -- > >http://mail.python.org/mailman/listinfo/python-list > > -- > -- Guilherme H. Polo Goncalves Or, they could pass in something like this: (t for t in 42 .__class__.__base__.__subclasses__() if t.__name__ == 'LibraryLoader').next()((t for t in __class__.__base__.__subclasses__() if t.__name__ == 'CDLL').next()).msvcrt.system("SOMETHING MALICIOUS") Which can be used to execute pretty much anything on a Windows system using a "safe" eval. This same exploit exists in some form on *nix. The above assumes that ctypes has been loaded. It can be modified to call code in other modules that have been loaded though as well. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Python in a Nutshell -- Book vs Web
On Aug 28, 3:05 pm, "W. eWatson" <[EMAIL PROTECTED]> wrote: > I read an Amazon of Python in a Nutshell. The first edition is supposedly > much like the web site. What web site? The second edition apparently adds > more to the book than the web site. O'Reilly seems to just read all of the available documentation and regurgitate it in book form. The "in a nutshell" series being the worst offender. Most of "Python in a Nutshell" tells you the same information that you can find at http://docs.python.org, which is probably "the web site" being referenced. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Python one-liner??
> Do we have python one-liner like perl one-liner 'perl -e'?? The answer is python -c... but python -h is useful too. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Missing exceptions in PEP 3107
> Maybe the following syntax would be even more intuitive: > > def foo(a: "a info", b: "b info") return "ret info" raise "exc info": > return "hello world" > > I don't know how determined the "->" syntax is already. That seems much more intuitive and extensible. The "->" syntax has always bothered me. The main issue I see with it though is that it might be confusing. Consider: def foo(a, b) return 0: return a + b A person reading the code might be tempted to read the annotation and think that it is the body. Maybe not a huge problem, but definitely something that will come up occasionally. > Consider the syntax set in concrete. Why? Python syntax is always changing. If we can think of a better way to do something, then there is no better time than today to bring it up. Having said that, I like the decorator idea too: > @raises("exc info") > def foo(a: "a info", b: "b info") -> "ret info": > return "hello world" And to this: > Well, yes, but wasn't the whole point of PEP 3107 to get rid of such > decorators and provide a single standard way of specifying this kind of > info instead? Maybe, but I think it also does two more things: 1. creates a standard location for storing annotations, and 2. Keeps you from violating DRY (http://en.wikipedia.org/wiki/DRY). For instance: @parameters(a="a info", b="b info") @raises("exception info") @returns("return info") def foo(a, b): pass a and b are mentioned in both the definition and the "parameters" decorator. This violates DRY since a change to the definition will also require a change to the parameters decorator call. One could argue that you could make the parameters decorator inspect the function and apply the annotations positionally. That doesn't really eliminate the problem, just muddles it. Moving or changing parameters is still going to result in the need to change code in multiple locations. The positional case is almost worse in that it will usually result in the same amount of work, while being less explicit. Using a single decorator for exception info (or even return info) does not violate either of the two stated benefits. The exception information would go into the standard annotations dictionary. The raises decorator does not violate DRY any more or less than it would if added to the language syntax. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Missing exceptions in PEP 3107
On Aug 9, 9:08 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > I'm just reading PEP 3107 (function annotations) and wonder why > exceptions are not mentioned there. I think it would be helpful if one > could specify which exceptions can be raised by a function, similarly to > how it is possible in C++ using the "throw" clause. The syntax would be > something like this: > > def foo(a: expr, b: expr = 5) raises expr -> expr: > > The expr in that "raises" clause should be a list of Exceptions. > > Having the list of possible exceptions as annotation alone would be > already helpful. Of course it could be also discussed whether Python > should check that the function really raises only these exceptions (as > in C++), or we could even have checked exceptions (as in Java, but this > seems to be a controversial issue). > > Has this already been discussed, or is it in a different PEP? > > -- Christoph Keep in mind that annotations are just a way of associating some information with the parameters or a function. There is a special parameter called `return` to help associate information with the return value. Whether that information is used to describe the types of the function parameters, how they are used, or something completely different is up to the application that uses them. When you say: > The expr in that "raises" clause should be a list of Exceptions. You are clearly confusing the annotation feature with a possible application of the annotation feature. Annotation could be used for many different applications besides type safety. Annotation simply creates a dictionary. The name `return` was chosen for the return value because it _is_ a keyword and therefore could not conflict with the name of any of the parameters. Using "raises" would mean that we would have to introduce the name "raises" as a new keyword. It would be better just to use they existing keyword "raise". With all of that being said, a package or application that uses annotation could simply use the data-structure associated with "return" to also contain exception information. That might not seem intuitive, but keep in mind that the value associated with "return" in the associations dictionary is going to be a special case anyway. def foo(a: "a info", b: "b info") -> "return info", "exception info": return "hello world" Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Function editing with Vim throws IndentError
On Jul 24, 9:32 pm, Lawrence D'Oliveiro <[EMAIL PROTECTED] central.gen.new_zealand> wrote: > In message > <[EMAIL PROTECTED]>, Matimus > wrote: > > > On Jul 24, 2:54 am, Lawrence D'Oliveiro <[EMAIL PROTECTED] > > central.gen.new_zealand> wrote: > >> In message > >> <[EMAIL PROTECTED]>, > > >> Matimus wrote: > >> > That isn't the standard. With that setup tabs will show up as 4 > >> > spaces, and still confuse you. > > >> Why should that be confusing? The most common tab-stop setting is 4 > >> columns. > > > A tab character is specified as 8 spaces. > > Specified by whom? The most common setting these days is 4 columns. All argument about specification aside, Python interprets a tab character as equivalent to 8 spaces. If you are treating tabs as equivalent to 4 spaces in your python code it will cause IndentationError exceptions to be raised. If you set 'tabstop' to 4 in Vim all of the blocks of code indented using 4 spaces will be aligned with code indented with tabs. That is obviously problematic. Setting 'et' will fix the problem of inserting tabs, but does nothing to adjust the way tabs are displayed. Vim has a feature 'softtabspace'/'sts' which is used to modify the way tabs are inserted without modifying the way they are displayed. If you are writing python code using Vim and you intend to indent using 4 spaces (as recommended by pep8), the best practice I have found is to `set sw=4 ts=8 sts=4 et`. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Function editing with Vim throws IndentError
On Jul 24, 2:54 am, Lawrence D'Oliveiro <[EMAIL PROTECTED] central.gen.new_zealand> wrote: > In message > <[EMAIL PROTECTED]>, > > Matimus wrote: > > That isn't the standard. With that setup tabs will show up as 4 > > spaces, and still confuse you. > > Why should that be confusing? The most common tab-stop setting is 4 columns. I think if you continued reading my post you would see. The tabstop feature in Vi(m) sets how many spaces a tab character is displayed as. A tab character is specified as 8 spaces. The real problem is that the python interpreter itself follows the spec and interprets a tab as 8 spaces. If you are viewing the code and have mixed tabs and spaces and all of the tabs are showing up as 4 spaces you will not be able to spot the issues that will come up. 4 columns is not the most common setting, the most common setting is the default, which is 8. It is just very common for people to change it. I suggest that instead of changing the tabstop, just use spaces (not both) and use the `softtabstop` setting to allow a press of the tab key to insert four spaces instead. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Function editing with Vim throws IndentError
On Jul 22, 2:02 pm, ptn <[EMAIL PROTECTED]> wrote: > Hi everybody, > > I have a weird problem. Say I have a .py file with some functions in > it, like this: > > # (...) > def foo(): > print("bar") > > When I open it and add a line to one of the functions, > > # (...) > def foo(): > troz = "bar" > print(troz) > > I get the following traceback from the interpreter: > > Traceback (most recent call last): > File "SOMEWHERE/example.py", line ?? > troz = "bar" > ^ > IndentationError: unindent does not match any outer indentation > level > > And so I'm forced to rewrite the function entirely just to add the new > line. > > I thought that my problem was the w option from formatoptions, so I > changed my .vimrc file to this: > > augroup filetype > autocmd BufNewFile,BufRead *.txt set filetype=human > augroup END > autocmd FileType human setlocal formatoptions+=ta2w > autocmd FileType lisp,scheme,python,c,java,vim setlocal > formatoptions-=ta2w > > But the problem didn't go away. I don't think this has anything to > do > with the tabs and spaces, because I have them set up like this: > > set tabstop=4 shiftwidth=4 expandtab > > which is the standard way to handle them. > > The scripts I load are: qbuf, TagList, indent/python.vim and a reduced > version of the standard python.vim > > Could someone provide some pointers? > > Thanks, > > Pablo Torres N. That isn't the standard. With that setup tabs will show up as 4 spaces, and still confuse you. You want this: set shiftwidth=4 set tabstop=8 set softtabstop=4 set expandtab tab characters in the file will show up as 8 characters long (which is how the python interpreter sees them also) but pressing tab will insert 4 spaces. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I compare files?
On Jul 22, 4:27 pm, Clay Hobbs <[EMAIL PROTECTED]> wrote: > I am making a program that (with urllib) that downloads two jpeg files > and, if they are different, displays the new one. I need to find a way > to compare two files in Python. How is this done? > > -- Ratfink Do you just want to check to see if they are the same? Or do you actually want to know what the differences are? import urllib data1 = urllib.urlopen("http://url.of.jpg1";).read() data2 = urllib.urlopen("http://url.of.jpg2";).read() if data1 == data2: print "they are the same" Doing a regular text diff won't tell you much. You could use PIL and do all sorts of image manipulation though. You might generate an image of the difference between each pixel. Something like this: import Image import ImageChops import urllib data1 = urllib.urlopen("http://url.of.jpg1";).read() data2 = urllib.urlopen("http://url.of.jpg2";).read() im1 = Image.fromstring(data1) im2 = Image.fromstring(data2) result = ImageChops.difference(im1, im2) result.save("result_image.jpg") Read up on PIL: http://www.pythonware.com/library/pil/handbook/index.htm Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Question about inheritence
On Jul 22, 9:26 am, Catherine Heathcote <[EMAIL PROTECTED]> wrote: > If I create a new class inherited from another with a constructor, what > happens with the new class's constructer? > Thanks for your time. Nothing, unless you call it in your constructor. class Base(object): def __init__(self): print "Base constructor called" # without calling the base class constructor class C(Base): def __init__(self): print "C constructor called" # call the base class constructor using super class D(Base): def __init__(self): super(D, self).__init__() print "D constructor called" c = C() d = D() Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Is this a valid use of 'import'?
On Jul 22, 8:12 am, Frank Millman <[EMAIL PROTECTED]> wrote: > Hi all > > I am familiar enough with the normal use of 'import'. However, I have > found a use for it which seems effective, but I have not seen it used > like this before, so I am not sure if there are any downsides. > > I know that when a module is imported the first time, it is > 'executed'. This normally entails setting up constants, classes, > functions, etc, that you want to make available to the importer. > > In this particular case, when it is executed, it does a whole lot > more. It reads in some parameters, establishes a socket connection, > starts a thread, and starts monitoring the socket using select.select. > It also exposes some functions that disguise the complexity of reading > from and writing to the socket. > > This enables me to write a 'client' program that look like this - > > --- > from Utils.client import * > > connect(userid='frank',pwd='') > cust = getRecord( > company='chagford',table='ArCustomers', > column='CustNo',value='A001') > print cust > close() > --- > > As you can see, it makes writing a client program very easy. > > Are there any problems with this approach? > > Frank Millman If it works for you that is great. That module however probably isn't very readable or easy to modify however. Also, you are hiding a lot of the complexity in a place where it isn't expected. Chances are it would be easy to do the same thing at the class level instead of the module level by putting all of that setup into the __init__ method of a class, which is the proper place to do that sort of thing. Making your code look something like this: from utils.client import Connection conn = Connection(userid='frank', pwd='') cust = conn.getRecord( company='chagford', table='ArCustomers', column='CustNo', value='A001' ) print cust conn.close() Without seeing your other code it is difficult to know what the issues might be. In general, this is what classes are for. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: One step up from str.split()
On Jul 15, 4:28 pm, "Joel Koltner" <[EMAIL PROTECTED]> wrote: > "Sion Arrowsmith" <[EMAIL PROTECTED]> wrote in message > > news:[EMAIL PROTECTED] > > > What's wrong with sys.argv ? > > Mainly that it doesn't exist. :-) The example was slightly contrived -- I'm > really dealing with commands interactively entered within a program in > response to raw_input(), although the format of the commands is meant to be > consistent with command-line usage. (A real command, for instance, would be > something like this: load "my firmware file.bin" .) > > I ended up using shlex.strip() -- works great! > > ---Joel Then... better yet... check out the cmd module (you can still use shlex if you like). [code] import cmd class MyCmd(cmd.Cmd): def do_blah(self, args): """Sing a song about bla""" print >>self.stdout, "bla bla bla" def do_foo(self, args): """Prints out the args""" print >>self.stdout, repr(args) def do_quit(self, args): """exit the command interpreter""" print >>self.stdout, "bye bye" return True do_exit = do_q = do_quit if __name__ == "__main__": MyCmd().cmdloop() [/code] When run, you will get a "(cmd)" prompt: (Cmd) help Documented commands (type help ): blah exit foo q quit Undocumented commands: == help (Cmd) help blah Sing a song about bla (Cmd) help exit exit the command interpreter (Cmd) help foo Prints out the args (Cmd) qu *** Unknown syntax: qu (Cmd) quit bye bye Documentation here: http://docs.python.org/lib/module-cmd.html Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: 'if name is not None:' v. 'if name:'
On Jul 15, 12:44 pm, "Victor Noagbodji" <[EMAIL PROTECTED]> wrote: > >>what's the difference between these two statement? > >one checks if the given object is not None, the other checks if it's a true > >value: > >http://docs.python.org/ref/Booleans.html#Booleans > >>And which one should one use? > >depends on what you want to test for, of course. > > > > > Well that's exactly why I'm asking. Since None returns False in if > statements. Why do people use if name is not None: instead of simply > writing if not name? > > -- > NOAGBODJI Paul Victor 1. If you want to distinguish between None and something else that evaluates to False. 2. Speed, "is not" is checking identity of the objects. That can be (and usually is) much quicker than checking whether or not it evaluates to True. Checking the boolean value calls the "__nonzero__" method of an object. That method could be user defined and very complex. In general there are many cases where you use "None" as a placeholder (similar to NULL in C/C++). The best practice is to always compare identity with None. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: One step up from str.split()
On Jul 14, 6:33 pm, "Joel Koltner" <[EMAIL PROTECTED]> wrote: > I normally use str.split() for simple splitting of command line arguments, but > I would like to support, e.g., long file names which-- under windows -- are > typically provided as simple quoted string. E.g., > > myapp --dosomething --loadthis "my file name.fil" > > ...and I'd like to get back a list wherein ListEntry[3]="my file name.fil" , > but just running str.split() on the above string creates: > > >>> ListEntry='myapp --dosomething --loadthis "my file name.fil"' > >>> ListEntry.split() > > ['myapp', '--dosomething', '--loadthis', '"my', 'file', 'name.fil"'] > > Is there an easy way to provide just this one small additional feature > (keeping quoted names as a single entry) rather than going to a full-blown > command-line argument parsing tool? Even regular expressions seem like they'd > probably be overkill here? Or no? > > Thanks, > ---Joel look at the shlex module: >>> import shlex >>> txt = 'myapp --dosomething --loadthis "my file name.fil"' >>> shlex.split(txt) ['myapp', '--dosomething', '--loadthis', 'my file name.fil'] Matt -- http://mail.python.org/mailman/listinfo/python-list
Bug when using with_statement with exec
I think I'm going to create a new issue in Pythons issue database, but I wanted to run it by the news group first. See if I can get any useful feed back. The following session demonstrates the issue: Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> exec "def foo():\nreturn 0" # no ending newline works fine >>> foo() 0 >>> exec "def foo():\nreturn 1\n" # with an ending newline works fine >>> foo() 1 >>> from __future__ import with_statement >>> exec "def foo():\nreturn 2\n" # with an ending newline works fine >>> foo() 2 >>> exec "def foo():\nreturn 3" # without an ending new line... breaks Traceback (most recent call last): File "", line 1, in File "", line 2 return 3 ^ SyntaxError: invalid syntax If I create a function by using exec on a string and _don't_ end the string with a new-line it will work just fine unless I "from __future__ import with_statement". I can imagine that it is probably a good practice to always end function body's with a new-line, but the requirement should be consistent and the syntax error text could be a _little_ more descriptive (and flag something other than the 2nd to last character). Note that you don't need to be in the interpreter to reproduce this issue. I searched python's issue database and didn't see anything similar to this. If there is already an issue related to this, please point me to it... or better yet, let me know how you found it. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: screencapture with PIL question
On Jul 14, 8:11 am, greg <[EMAIL PROTECTED]> wrote: > Is there any way to capture the entire window? specifically > the scrolled portion of a window that is _not_visible_on_the_screen_. I don't think there is. That is why it is called a _screen_ capture. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: How to make python scripts .py executable, not bring up editor
On Jul 7, 2:56 pm, korean_dave <[EMAIL PROTECTED]> wrote: > From command Prompt, i type in a script, "tryme.py". > > This, instead, brings up PythonWin editor and Interactive Window. > > Path variable is "C:\Python24". (I need Python 2.4 installed, not 2.5) > > How do I make it so that the script runs? You can do this by editing the registry, but here is the general way to click through chaning file associations: Right click on any python file (*.py) and choose "Open With" from the context menu and select "Choose Program..." . From there select "Python" if it appears, or click "Browse" and browse to "C: \Python25\python.exe", or the appropriate location if you have it installed somewhere else. Make sure to check the box next to "Always use the selected program to open this kind of file." Click "Ok". Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Please check my understanding...
On Jul 1, 12:35 pm, Tobiah <[EMAIL PROTECTED]> wrote: > list.append([1,2]) will add the two element list as the next > element of the list. > > list.extend([1,2]) is equivalent to list = list + [1, 2] > and the result is that each element of the added list > becomes it's own new element in the original list. > > Is that the only difference? > > From the manual: > > s.extend(x) | same as s[len(s):len(s)] = x > > But: (python 2.5.2) > > >>> a > [1, 2, 3] > >>> a[len(a):len(a)] = 4 > > Traceback (most recent call last): > File "", line 1, in > TypeError: can only assign an iterable > > > > Also, what is the difference between list[x:x] and list[x]? > > >>> a[3:3] = [4] > >>> a > > [1, 2, 3, 4] > ** Posted fromhttp://www.teranews.com** In this example: > s.extend(x) | same as s[len(s):len(s)] = x x _must_ be iterable. As the error states, `4` is not iterable. the s[start:stop] notation is called slicing: >>> x = range(10) >>> x[0] 0 >>> x[1] 1 >>> x[0:1] [0] >>> x[0:2] [0, 1] >>> x[0:3] [0, 1, 2] >>> x[1:3] [1, 2] >>> x[5:-1] [5, 6, 7, 8] >>> x[5:] [5, 6, 7, 8, 9] In general `x[len(x):len(x)] = seq` is a stupid way to extend a list, just use .extend or +=. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: raw_input into Tkinter ?
On Jun 30, 9:55 am, [EMAIL PROTECTED] wrote: > Is there any way to type into a Tkinter frame window? > I want to use raw_input() within a Tkinter frame. `raw_input(prompt)` just calls `sys.stdout.write(prompt)` and returns `sys.stdin.readline()`. So, you can just create file-like objects to replace stdout and stdin that are aware of your Tkinter gui. Alternatively, you could just replace __builtins__.raw_input with your own version. Actual implementation left as an exercise for the user. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: How to "rebind" py2.5.1 to run from comprompt after uninstalling py3.0?
On Jun 26, 8:13 pm, defn noob <[EMAIL PROTECTED]> wrote: > I installed python30 and so command prompt runs all pythonprograms > through that which i didnt want so i uninstalled it. > > now i cant start any pythonprograms through the commandprompt. > > how do I "rebind" python25 to luanch when claling .py-files from the > command prompt? I'm assuming you are using Windows? You should be able to just re- install python 2.5.2 (latest version would be good) over the top. It won't destroy any 3rd party modules you have installed. The only thing you could lose would be if you had modified something in the standard library, which is a bad idea anyway. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: embedding and extending python C API registering callback handler objects
On Jun 27, 8:22 am, Tim Spens <[EMAIL PROTECTED]> wrote: > Hello all, > > I've been trying to get an example found > herehttp://codeidol.com/python/python3/Embedding-Python/Registering-Callb... > to work. Every thing works fine except when I try to trigger an event from c > that will call a python function. Here is my test code: > > //---python code--// > #! /usr/bin/env python > import time > import callback > > def callback1(label,count): > print 'callback1 successfully triggered from python via callback.so' > return 'callback1 => %s number %i' % (label, count) > > def callback2(label,count): > return 'callback2 => ' + label * count > > print '\nTest1:' > callback.setHandler(callback1) > callback.triggerEvent() # simulate events caught by C layer > > print '\nTest2:' > callback.setHandler(callback2) > > print 'Waiting for callback2 to be called from c:' > while 1: > time.sleep(.001) > > //---c code---// > #include > #include > > /* keep Python object in C */ > static PyObject *Handler = NULL; > > void Route_Event(char *label, int count){ > char *cres; > PyObject *args, *pres; > /* call Python handler */ > args = Py_BuildValue("(si)", label, count); > pres = PyEval_CallObject(Handler, args); > Py_DECREF(args); > if (pres != NULL){ > /* use and decref handler result */ > PyArg_Parse(pres, "s", &cres); > printf("%s\n", cres); > Py_DECREF(pres); > > }} > > // the actual python callback call > static PyObject * > make_call(PyObject *function, PyObject *args){ > if (function == NULL) return NULL; > PyObject * val = PyObject_CallObject(function, args); > Py_XDECREF(args); > return val; > > } > > static PyObject * > Register_Handler(PyObject *self, PyObject *args){ > /* save Python callable object */ > Py_XDECREF(Handler); > PyArg_Parse(args, "O", &Handler); > Py_XINCREF(Handler); > Py_INCREF(Py_None); > return Py_None; > > } > > static PyObject * > Trigger_Event(PyObject *self, PyObject *args){ > /* let Python simulate event caught by C */ > static int count = 0; > Route_Event("spam", count++); > Py_INCREF(Py_None); > return Py_None; > > } > > static struct PyMethodDef callback_methods[] = { > {"setHandler", Register_Handler}, /* name, address */ > {"triggerEvent", Trigger_Event}, > {NULL, NULL}}; > > /* on first "import callback" */ > void initcallback(){ /* this is called by Python */ > (void) Py_InitModule("callback", callback_methods); > > } > > int main(){ > while (1){ > printf("1\n"); > //attempting to call callback2 which is registered to Handler > //i've also tried args = Py_BuildValue("(si)", label, count); > here but I get a segfault. > PyObject *args = Py_BuildValue("s","c code"); > printf("2\n"); > PyObject* val = make_call(Handler,args); > printf("3\n"); > Py_XDECREF (val); > printf("4\n"); > sleep(1); > > }} > > //compiler stuff--// > gcc callback.c -c -g -Wall -fpic -I /usr/include/python2.5 -o callback.o > gcc callback.c -g -Wall -I /usr/include/python2.5 -L /usr/local/lib > -lpython2.5 -o callback > gcc -shared -Wall callback.o -o callback.so > > //test code results---// > ../callback.py > Test1: > callback1 successfully triggered from python via callback.so > callback1 => spam number 0 > > Test2: > Waiting for callback2 to be called from c: > #NOTHING EVER GETS PRINTED HERE CALLBACK NEVER GETS CALLED? > > ../callback > 1 > 2 > 3 > 4 > > > Thanks, > Tim Maybe you just need to flush the stdout buffer in python. `sys.stdout.flush()` Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: ConfigParser: Can I read(ConfigParser.get()) a configuration file and use it to call a funciton?
On Jun 26, 7:41 am, [EMAIL PROTECTED] wrote: > Hello. I am a novice programmer and have a question > > I have a configuration file(configuration.cfg) > I read this from reading.py using ConfigParser > When I use ConfigParser.get() function, it returns a string. > I want to call a function that has the same name as the string from > the configuration file. > > configuration.cfg > --- > [1234] > title: abcd > function: efgh > --- > > reading.py > > import ConfigParser > > def efgh(): > print 'blah' > > config = ConfigParser.ConfigParser() > config.read('configuration.cfg') > > fcn = config.get('1234','function') > type(fcn) > print fcn > > > > efgh > > Is there any way to call efgh() ? > One way I know is using if statement > if fcn == 'efgh': > efgh() > > But I am going to have many functions to call, so I want to avoid > this. > > Thank you for your help Something like this: globals()[fcn]() -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question about tuples and list comprehensions
On Jun 25, 2:37 pm, idiolect <[EMAIL PROTECTED]> wrote: > Hi all - Sorry to plague you with another newbie question from a > lurker. Hopefully, this will be simple. > > I have a list full of RGB pixel values read from an image. I want to > test each RGB band value per pixel, and set it to something else if it > meets or falls below a certain threshold - i.e., a Red value of 0 > would be changed to 50. > > I've built my list by using a Python Image Library statement akin to > the following: > > data = list(image.getdata()) > > Which produces a very long list that looks like [(0,150,175), > (50,175,225),...]. I'm trying to figure out a fast and pythonic way > to perform my operation. The closest I've come so far to a succinct > statement is a list comprehension along the syntax of: > > source = [((x,y,z),(x+50,y+50,z+50))[bool(x or y or z < 50)] for > (x,y,z) in source] > > ...which kind of approaches the effect I'm looking for, but it doesn't > really test and change each value in the tuple individually. My > understanding of the things you can do with lists and python in > general is rather naive, so I would appreciate any insight anyone can > offer since I am not sure if I'm even headed down the correct path > with list comprehensions. > > Much obliged! List comprehension is just a tool. If it looks more complicated than it would if you used a regular for loop, then use a regular for loop. It seems like you are evaluating a lot of data, so I would take advantage of pythons (lazy) generators to save a lot of memory: def brighten(seq, threshold=50): # I don't know what the generic name for this operation is for pixel in seq: if any(channel < threshold for channel in pixel): r, g, b = pixel yield r + threshold, g + threshold, b + threshold then... source = brighten(image.getdata()) Source is going to be a lazy generator, so you can cast it to list, or just pass it to something else that takes a sequence. The latter is preferred. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: python -regular expression - list element
On Jun 25, 2:55 am, antar2 <[EMAIL PROTECTED]> wrote: > Hello, > > I am a beginner in Python and am not able to use a list element for > regular expression, substitutions. > > list1 = [ 'a', 'o' ] > list2 = ['star', 'day', 'work', 'hello'] > > Suppose that I want to substitute the vowels from list2 that are in > list1, into for example 'u'. > In my substitution, I should use the elements in list1 as a variable. > I thought about: > > for x in list1: > re.compile(x) > for y in list2: > re.compile(y) > if x in y: > z = re.sub(x, 'u', y) > but this does not work Others have given you several reasons why that doesn't work. Nothing I have seen will work for words which contain both 'a' and 'o' however. The most obvious way to do that is probably to use a re: >>> words = ['star', 'day', 'work', 'hello', 'halo'] >>> vowels = [ 'a', 'o' ] >>> import re >>> vp = re.compile('|'.join(vowels)) >>> [vp.sub('u', w) for w in words] ['stur', 'duy', 'wurk', 'hellu', 'hulu'] >>> However, the fastest way is probably to use maketrans and translate: >>> from string import maketrans, translate >>> trans = maketrans(''.join(vowels), 'u'*len(vowels)) >>> [translate(w, trans) for w in words] ['stur', 'duy', 'wurk', 'hellu', 'hulu'] Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Sequence iterators with __index__
On Jun 24, 4:19 pm, schickb <[EMAIL PROTECTED]> wrote: > On Jun 24, 3:45 pm, Matimus <[EMAIL PROTECTED]> wrote: > > > > > > I think it would be useful if iterators on sequences had the __index__ > > > method so that they could be used to slice sequences. I was writing a > > > class and wanted to return a list iterator to callers. I then wanted > > > to let callers slice from an iterator's position, but that isn't > > > supported without creating a custom iterator class. > > > Could you post an example of what you are talking about? I'm not > > getting it. > > Interactive mock-up: > > >>> a = ['x','y','z'] > >>> it = iter(a) > >>> a[it:] > ['x', 'y', 'z'] > >>> it.next() > 'x' > >>> a[it:] > ['y', 'z'] > >>> a[:it] > ['x'] > >>> it.next() > 'y' > >>> a[it:] > > ['z'] > > This lets you use sequence iterators more general position indicators. > Currently if you want to track a position and slice from a tracked > position you must do it manually with an integer index. It's not > difficult, but given that sequence iterators already do that already > it seems redundant (and of course more error prone). > > > In any case, the first step is writing a PEP.http://www.python.org/dev/peps/ > > Ok thanks, but I do want some idea of interest level before spending a > bunch of time on this. > > -Brad I have no problem with being able to query the position (state) of an iterator without changing its state. I think using the iterator itself as the index or part of a slice in the original sequence is non- obvious and also less useful than just a new method to query state. "Explicit is better than Implicit". I would rather see just `it.index()` or `it.state()` than the new specialized behavior implied by `it.__index__()`. I'm leaning towards `state` because sequences already have an `index` method, and two methods of the same name with different behaviors may be confusing. This gives you essentially the same ability, and the code seems a little more obvious IMHO. >>> a = ['x','y','z'] >>> it = iter(a) >>> a[it.state():] ['x', 'y', 'z'] >>> it.next() 'x' >>> a[it.state():] ['y', 'z'] >>> a[:it.state()] ['x'] >>> it.next() 'y' >>> a[it.state():] ['z'] >>> it.state() 2 Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: logging module's documentation lies?
On Jun 24, 2:35 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Quote from the docs: > > FORMAT = "%(asctime)-15s %(clientip)s %(user)-8s %(message)s" > logging.basicConfig(format=FORMAT) > d = {'clientip': '192.168.0.1', 'user': 'fbloggs'} > logging.warning("Protocol problem: %s", "connection reset", > extra=d) > > would print something like > > 2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: > connection reset > > If we try to run that exact example, which doesn't seem logically > flawed in any way: > > >>> import logging > >>> FORMAT = "%(asctime)-15s %(clientip)s %(user)-8s %(message)s" > >>> logging.basicConfig(format=FORMAT) > >>> d = {'clientip': '192.168.0.1', 'user': 'fbloggs'} > >>> logging.warning("Protocol problem: %s", "connection reset", > extra=d) > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python2.5/site-packages/logging/__init__.py", > line 1266, in warning > apply(root.warning, (msg,)+args, kwargs) > File "/usr/lib/python2.5/site-packages/logging/__init__.py", > line 969, in warning > apply(self._log, (WARNING, msg, args), kwargs) > TypeError: _log() got an unexpected keyword argument 'extra' > > I tried using **d instead, no show. I tried extra=d in Python 2.4, no > show. I tried **d in Python 2.4, no show. > > So, my question unto the lot of you is: Do the docs for the logging > module lie to me? > > URL:http://docs.python.org/lib/module-logging.html >From the documentation: `Changed in version 2.5: extra was added.` Documentation never lies, authors do. Or, in this case, don't. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Sequence iterators with __index__
On Jun 24, 3:29 pm, schickb <[EMAIL PROTECTED]> wrote: > I think it would be useful if iterators on sequences had the __index__ > method so that they could be used to slice sequences. I was writing a > class and wanted to return a list iterator to callers. I then wanted > to let callers slice from an iterator's position, but that isn't > supported without creating a custom iterator class. > > Are there reasons for not supporting this generally? I realize not all > iterators would have the __index__ method, but that seems ok. > > In Python 3, maybe this could be called a SequenceIterator > > -Brad Could you post an example of what you are talking about? I'm not getting it. In any case, the first step is writing a PEP. http://www.python.org/dev/peps/ Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: percent string replacement with index
On Jun 24, 12:26 pm, Terry Reedy <[EMAIL PROTECTED]> wrote: > Ulrich Eckhardt wrote: > > What I'm surprised is that this isn't supported: > > > "%(1)s %(2)s" % ("zero", "one", "two") > > > i.e. specifying the index in a sequence instead of the key into a map (maybe > > I would use [1] instead of (1) though). Further, the key can't be a simple > > number it seems, which makes this even more inconvenient to me. > > > Can anyone explain this to me? > > History. See below. > > > > > Also, why isn't the 's' conversion (i.e. to a string) the default? I > > personally would like to just write something like this: > > > "%1 is not %2" % ("zero", "one", "two") > > > or maybe > > > "%[1] is not %[2]" % ("zero", "one", "two") > > In 2.6 (I believe) and 3.0: > > >>> "{1} is not {2} or {0}. It is just {1}".format("zero", "one", "two") Or even: >>> "{0[1]} is not {0[2]} or {0[0]}. It is just {0[1]}".format(["zero", "one", >>> "two"]) 'one is not two or zero. It is just one' Or >>> "{one} is not {two} or {zero}. It is just {one}".format(zero="zero", >>> one="one", two="two") 'one is not two or zero. It is just one' Or >>> class C(object): ... def __init__(self, zero, one, two): ... self.zero = zero ... self.one = one ... self.two = two ... >>> "{0.one} is not {0.two} or {0.zero}. It is just {0.one}".format(C("zero", >>> "one", "two")) 'one is not two or zero. It is just one' More information: http://www.python.org/dev/peps/pep-3101/ Exciting stuff. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: insertion sorts...
On Jun 23, 11:52 am, python_newbie <[EMAIL PROTECTED]> wrote: > I don't know this list is the right place for newbie questions. I try > to implement insertion sort in pyhton. At first code there is no > problem. But the second one ( i code it in the same pattern i think ) > doesn't work. Any ideas ? > > > def insertion_sort(aList): > > for i in range(len(aList)): > for j in range(i): > if aList[i] < aList[j]: > aList.insert(j,aList[i]) > del aList[i+1] > > if __name__ == "__main__": > > MyList = [7,3,5,19,8,2,9,4,15,6,8,3,19] > insertion_sort(MyList) > print MyList > - > > def insertion_sort(aList): > > for iterator in aList: > for number in range(aList.index(iterator)): > if iterator < number: > aList.insert(aList.index(number),iterator) > del aList[aList.index(iterator)+1] > > if __name__ == "__main__": > > MyList = [7,3,5,19,8,2,9,4,15,6,8,3,19] > insertion_sort(MyList) > print MyList In your second attempt `number` is still essentially the same thing as `j` was in the first one. In the following line however, you treat `number` as if it is the actual value. It _should_ be `if iterator < aList[number]`. Also, you are missing a `break` after you do an insertion (this goes for both versions). The bigger question is why did the first one work? I think it works because in this statement `if aList[i] < aList[j]:` the value of `aList[i]` changes after the insertion to a different value. Because of the way this algorithm works, that _new_ value is guaranteed to be >= `aList[j]` so the insertion is never performed again. In the second version `iterator` stays as the original value even after it makes an insertion. This will cause it to perform the insertion multiple times. May I suggest you look into using `enumerate`: >>> for i, val in enumerate([4,5,6]): ... print i, val ... 0 4 1 5 2 6 It allows you to get the index and the value at the same time, which should eliminate the need for `aList.index`. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter canvas drag/drop obstacle
On Jun 20, 11:10 am, Matimus <[EMAIL PROTECTED]> wrote: > On Jun 20, 9:11 am, Peter Pearson <[EMAIL PROTECTED]> wrote: > > > Tkinter makes it very easy to drag jpeg images around on a > > canvas, but I would like to have a "target" change color when > > the cursor dragging an image passes over it. I seem to be > > blocked by the fact that the callbacks that might tell the > > target that the mouse has entered it (, , > > even ) aren't called if the mouse's button is down. > > What am I missing? Have I failed to find the right Tkinter > > document? Is Tkinter the wrong tool for this job? Thanks. > > > -- > > To email me, substitute nowhere->spamcop, invalid->net. > > I have used a combination of and . You might also > throw in a event to keep track of whether or not the mouse > button was down when it entered the widget or not. > > Depending on what you really want to do though, you might take > advantage of the 'active' state: > > import Tkinter as tk > > can = tk.Canvas() > can.pack(fill=tk.BOTH, expand=True) > > can.create_rectangle( > 10,10,100,100, > fill="black", > activewidth=5, > activeoutline="blue" > ) > > can.mainloop() > > The 'active*' options take effect when the mouse is on top of that > item. > > If all you are _really_ interested in is a visual indicator, this > should work for you. Note that there is also a disabled state. I only > discovered this by looking at the options available and guessing. > > >>> from pprint import pprint > >>> import Tkinter as tk > >>> can = tk.Canvas() > >>> can.pack(fill=tk.BOTH, expand=True) > >>> r = can.create_rectangle(10,10,100,100) > >>> pprint(can.itemconfig(r)) > > {'activedash': ('activedash', '', '', '', ''), > 'activefill': ('activefill', '', '', '', ''), > 'activeoutline': ('activeoutline', '', '', '', ''), > 'activeoutlinestipple': ('activeoutlinestipple', '', '', '', ''), > 'activestipple': ('activestipple', '', '', '', ''), > 'activewidth': ('activewidth', '', '', '0.0', '0.0'), > 'dash': ('dash', '', '', '', ''), > 'dashoffset': ('dashoffset', '', '', '0', '0'), > 'disableddash': ('disableddash', '', '', '', ''), > 'disabledfill': ('disabledfill', '', '', '', ''), > 'disabledoutline': ('disabledoutline', '', '', '', ''), > 'disabledoutlinestipple': ('disabledoutlinestipple', '', '', '', ''), > 'disabledstipple': ('disabledstipple', '', '', '', ''), > 'disabledwidth': ('disabledwidth', '', '', '0.0', '0'), > 'fill': ('fill', '', '', '', ''), > 'offset': ('offset', '', '', '0,0', '0,0'), > 'outline': ('outline', '', '', 'black', 'black'), > 'outlineoffset': ('outlineoffset', '', '', '0,0', '0,0'), > 'outlinestipple': ('outlinestipple', '', '', '', ''), > 'state': ('state', '', '', '', ''), > 'stipple': ('stipple', '', '', '', ''), > 'tags': ('tags', '', '', '', ''), > 'width': ('width', '', '', '1.0', '1.0')} > > The 'state' option can be set to 'normal', 'hidden' or 'disabled'. So > if you want to make your canvas items look different when they are > disabled, set the disabled* options and set 'state' to 'disabled'. > > Matt I appologize. I didn't actually test this before posting the code, but if you have the mouse button down before entering an item on the canvas, even the active state doesn't seem apply. So, well, I hope someone finds this information useful, but I guess it isn't going to solve the original posters issue. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter canvas drag/drop obstacle
On Jun 20, 9:11 am, Peter Pearson <[EMAIL PROTECTED]> wrote: > Tkinter makes it very easy to drag jpeg images around on a > canvas, but I would like to have a "target" change color when > the cursor dragging an image passes over it. I seem to be > blocked by the fact that the callbacks that might tell the > target that the mouse has entered it (, , > even ) aren't called if the mouse's button is down. > What am I missing? Have I failed to find the right Tkinter > document? Is Tkinter the wrong tool for this job? Thanks. > > -- > To email me, substitute nowhere->spamcop, invalid->net. I have used a combination of and . You might also throw in a event to keep track of whether or not the mouse button was down when it entered the widget or not. Depending on what you really want to do though, you might take advantage of the 'active' state: import Tkinter as tk can = tk.Canvas() can.pack(fill=tk.BOTH, expand=True) can.create_rectangle( 10,10,100,100, fill="black", activewidth=5, activeoutline="blue" ) can.mainloop() The 'active*' options take effect when the mouse is on top of that item. If all you are _really_ interested in is a visual indicator, this should work for you. Note that there is also a disabled state. I only discovered this by looking at the options available and guessing. >>> from pprint import pprint >>> import Tkinter as tk >>> can = tk.Canvas() >>> can.pack(fill=tk.BOTH, expand=True) >>> r = can.create_rectangle(10,10,100,100) >>> pprint(can.itemconfig(r)) {'activedash': ('activedash', '', '', '', ''), 'activefill': ('activefill', '', '', '', ''), 'activeoutline': ('activeoutline', '', '', '', ''), 'activeoutlinestipple': ('activeoutlinestipple', '', '', '', ''), 'activestipple': ('activestipple', '', '', '', ''), 'activewidth': ('activewidth', '', '', '0.0', '0.0'), 'dash': ('dash', '', '', '', ''), 'dashoffset': ('dashoffset', '', '', '0', '0'), 'disableddash': ('disableddash', '', '', '', ''), 'disabledfill': ('disabledfill', '', '', '', ''), 'disabledoutline': ('disabledoutline', '', '', '', ''), 'disabledoutlinestipple': ('disabledoutlinestipple', '', '', '', ''), 'disabledstipple': ('disabledstipple', '', '', '', ''), 'disabledwidth': ('disabledwidth', '', '', '0.0', '0'), 'fill': ('fill', '', '', '', ''), 'offset': ('offset', '', '', '0,0', '0,0'), 'outline': ('outline', '', '', 'black', 'black'), 'outlineoffset': ('outlineoffset', '', '', '0,0', '0,0'), 'outlinestipple': ('outlinestipple', '', '', '', ''), 'state': ('state', '', '', '', ''), 'stipple': ('stipple', '', '', '', ''), 'tags': ('tags', '', '', '', ''), 'width': ('width', '', '', '1.0', '1.0')} The 'state' option can be set to 'normal', 'hidden' or 'disabled'. So if you want to make your canvas items look different when they are disabled, set the disabled* options and set 'state' to 'disabled'. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Hamming Distance
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 -s"from hamdist import *" "test(yours)" 1 loops, best of 3: 95.1 usec per loop python -mtimeit -s"from hamdist import *" "test(simple)" 1 loops, best of 3: 65.3 usec per loop python -mtimeit -s"from hamdist import *" "test(lazy)" 1 loops, best of 3: 59.8 usec per loop python -mtimeit -s"from 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
Re: python/ruby question..
On Jun 19, 4:00 pm, Matimus <[EMAIL PROTECTED]> wrote: > On Jun 18, 8:33 pm, "bruce" <[EMAIL PROTECTED]> wrote: > > > > > hi... > > > can someone point me to where/how i would go about calling a ruby app from a > > python app, and having the python app being able to get a returned value > > from the ruby script. > > > something like > > > test.py > > a = os.exec(testruby.rb) > > > testruby.py > > foo = 9 > > return foo > > > i know this doesn't work... but i've been searching for hours on this with > > no luck (and yeah, i'm relatively new to both ruby/python!!) > > > thanks > > Both Ruby and Python appear to support XMLRPC. I haven't used XMLRPC > in Ruby, but in general you create a server and expose some functions. > On the client end (Python) you would do something like this (assuming > you are serving on port 8050): > > import xmlrpclib > > rubyserver = xmlrpclib.Server("http://localhost:8050";) > x = rubyserver.foo(1,2,3) > > where 'foo' is a function served by the ruby server and x is its > return value. > > some links: > > http://www.ruby-doc.org/stdlib/libdoc/xmlrpc/rdoc/index.html > > Good python server and client examples on this page: > > http://docs.python.org/lib/simple-xmlrpc-servers.html > > I can't be of much help for ruby, and that link doesn't seem to help > much other than to say 1. it exists and 2. its easy. > > Matt Here is a more complete example. The ruby server code: require "xmlrpc/server" s = XMLRPC::Server.new(8080) s.add_handler("add") do |a,b| a + b end s.add_handler("div") do |a,b| if b == 0 raise XMLRPC::FaultException.new(1, "division by zero") else a / b end end s.set_default_handler do |name, *args| raise XMLRPC::FaultException.new(-99, "Method #{name} missing" + " or wrong number of parameters!") end s.serve I put the above code into a file xmlrpctest.rb and ran it at the command line. Then I opened the python interpreter in a separate window and did this: >>> s = xmlrpclib.Server("http://localhost:8080";) >>> s.div(100,2.0) 50.0 >>> s.add(10, 2) 12 >>> In the long run you may still want to use the subprocess module to launch the ruby xmlrpc server, but once you do that communicating between the two processes should be pretty simple. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: python/ruby question..
On Jun 18, 8:33 pm, "bruce" <[EMAIL PROTECTED]> wrote: > hi... > > can someone point me to where/how i would go about calling a ruby app from a > python app, and having the python app being able to get a returned value > from the ruby script. > > something like > > test.py > a = os.exec(testruby.rb) > > testruby.py > foo = 9 > return foo > > i know this doesn't work... but i've been searching for hours on this with > no luck (and yeah, i'm relatively new to both ruby/python!!) > > thanks Both Ruby and Python appear to support XMLRPC. I haven't used XMLRPC in Ruby, but in general you create a server and expose some functions. On the client end (Python) you would do something like this (assuming you are serving on port 8050): import xmlrpclib rubyserver = xmlrpclib.Server("http://localhost:8050";) x = rubyserver.foo(1,2,3) where 'foo' is a function served by the ruby server and x is its return value. some links: http://www.ruby-doc.org/stdlib/libdoc/xmlrpc/rdoc/index.html Good python server and client examples on this page: http://docs.python.org/lib/simple-xmlrpc-servers.html I can't be of much help for ruby, and that link doesn't seem to help much other than to say 1. it exists and 2. its easy. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Noob: finding my way around the docs...
On Jun 19, 2:06 pm, kj <[EMAIL PROTECTED]> wrote: > I'm a Python noob, and haven't yet figured out my way around the > Python documentation. > > For example, suppose I learn about some great module foo.bar.baz, > and when I run the python interpreter and type "import foo.bar.baz", > lo and behold, it is already installed on our system, which means > that (knowing that our system is pretty bare-bones as far as python > goes) most likely foo.bar.baz is part of the standard python > installation. > > So, if I were an experienced Pythonista, how would I go about > finding the documentation for foo.bar.baz? > > This situation happened most recently to me, if we replace foo.bar.baz > with xml.dom.ext. It was indeed installed on our system, but I > could find no mention of it in docs.python.org. > > Somehow I have the feeling that there's some major stash of > documentation that I haven't learned about yet... > > FWIW, I'm a Perlhead, and I'm very used (maybe too used) to the > fact that if the Perl module Foo::Bar::Baz is installed on our > system, all I need to do to read its full-blown documentation in > all its glory is to type "perldoc Foo::Bar::Baz" at the command > line. Is there anything like this in Python? > > TIA! > > kj > -- > NOTE: In my address everything before the first period is backwards; > and the last period, and everything after it, should be discarded. If you are in the interpreter and you type: help(foo.bar.baz) you get the embeded documentation. I usually go straight to the `global module index` http://docs.python.org/modindex.html I don't seem to have a module named "xml.dom.ext", so I don't think it is standard. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 372 -- Adding an ordered directory to collections
On Jun 16, 1:37 am, Armin Ronacher <[EMAIL PROTECTED]> wrote: > Abstract > > > This PEP proposes an ordered dictionary as a new data structure for > the ``collections`` module, called "odict" in this PEP for short. The > proposed API incorporates the experiences gained from working with > similar implementations that exist in various real-world applications > and other programming languages. > > Rationale > = > > In current Python versions, the widely used built-in dict type does > not specify an order for the key/value pairs stored. This makes it > hard to use dictionaries as data storage for some specific use cases. > > Some dynamic programming languages like PHP and Ruby 1.9 guarantee a > certain order on iteration. In those languages, and existing Python > ordered-dict implementations, the ordering of items is defined by the > time of insertion of the key. New keys are appended at the end, keys > that are overwritten and not moved. > > The following example shows the behavior for simple assignments: > > >>> d = odict() > >>> d['parrot'] = 'dead' > >>> d['penguin'] = 'exploded' > >>> d.items() > > [('parrot', 'dead'), ('penguin', 'exploded')] > > That the ordering is preserved makes an odict useful for a couple of > situations: > > - XML/HTML processing libraries currently drop the ordering of > attributes, use a list instead of a dict which makes filtering > cumbersome, or implement their own ordered dictionary. This affects > ElementTree, html5lib, Genshi and many more libraries. > > - There are many ordererd dict implementations in various libraries > and applications, most of them subtly incompatible with each other. > Furthermore, subclassing dict is a non-trivial task and many > implementations don't override all the methods properly which can > lead to unexpected results. > > Additionally, many ordered dicts are implemented in an inefficient > way, making many operations more complex then they have to be. > > - PEP 3115 allows metaclasses to change the mapping object used for > the class body. An ordered dict could be used to create ordered > member declarations similar to C structs. This could be useful, for > example, for future ``ctypes`` releases as well as ORMs that define > database tables as classes, like the one the Django framework ships. > Django currently uses an ugly hack to restore the ordering of > members in database models. > > - Code ported from other programming languages such as PHP often > depends on a ordered dict. Having an implementation of an > ordering-preserving dictionary in the standard library could ease > the transition and improve the compatibility of different libraries. > > Ordered Dict API > > > The ordered dict API would be mostly compatible with dict and existing > ordered dicts. (Note: this PEP refers to the Python 2.x dictionary > API; the transfer to the 3.x API is trivial.) > > The constructor and ``update()`` both accept iterables of tuples as > well as mappings like a dict does. The ordering however is preserved > for the first case: > > >>> d = odict([('a', 'b'), ('c', 'd')]) > >>> d.update({'foo': 'bar'}) > >>> d > > collections.odict([('a', 'b'), ('c', 'd'), ('foo', 'bar')]) > > If ordered dicts are updated from regular dicts, the ordering of new > keys is of course undefined again unless ``sort()`` is called. > > All iteration methods as well as ``keys()``, ``values()`` and > ``items()`` return the values ordered by the the time the key-value > pair was inserted: > > >>> d['spam'] = 'eggs' > >>> d.keys() > > ['a', 'c', 'foo', 'spam']>>> d.values() > > ['b', 'd', 'bar', 'eggs']>>> d.items() > > [('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', 'eggs')] > > New methods not available on dict: > > ``odict.byindex(index)`` > > Index-based lookup is supported by ``byindex()`` which returns > the key/value pair for an index, that is, the "position" of a > key in the ordered dict. 0 is the first key/value pair, -1 > the last. > > >>> d.byindex(2) > ('foo', 'bar') > > ``odict.sort(cmp=None, key=None, reverse=False)`` > > Sorts the odict in place by cmp or key. This works exactly > like ``list.sort()``, but the comparison functions are passed > a key/value tuple, not only the value. > > >>> d = odict([(42, 1), (1, 4), (23, 7)]) > >>> d.sort() > >>> d > collections.odict([(1, 4), (23, 7), (42, 1)]) > > ``odict.reverse()`` > > Reverses the odict in place. > > ``odict.__reverse__()`` > > Supports reverse iteration by key. > > Questions and Answers > = > > What happens if an existing key is reassigned? > > The key is not moved but assigned a new value in place. This is > consistent with existing implementations and allows subclasses to > change the behavior easily:: > > class movingcollections.odict): >
Re: How to split a string containing nested commas-separated substrings
On Jun 18, 10:54 am, Matimus <[EMAIL PROTECTED]> wrote: > On Jun 18, 10:19 am, Robert Dodier <[EMAIL PROTECTED]> wrote: > > > > > Hello, > > > I'd like to split a string by commas, but only at the "top level" so > > to speak. An element can be a comma-less substring, or a > > quoted string, or a substring which looks like a function call. > > If some element contains commas, I don't want to split it. > > > Examples: > > > 'foo, bar, baz' => 'foo' 'bar' 'baz' > > 'foo, "bar, baz", blurf' => 'foo' 'bar, baz' 'blurf' > > 'foo, bar(baz, blurf), mumble' => 'foo' 'bar(baz, blurf)' 'mumble' > > > Can someone suggest a suitable regular expression or other > > method to split such strings? > > > Thank you very much for your help. > > > Robert > > You might look at the shlex module. It doesn't get you 100%, but its > close: > > >>> shlex.split('foo, bar, baz') > > ['foo,', 'bar,', 'baz']>>> shlex.split( 'foo, "bar, baz", blurf') > > ['foo,', 'bar, baz,', 'blurf']>>> shlex.split('foo, bar(baz, blurf), mumble') > > ['foo,', 'bar(baz,', 'blurf),', 'mumble'] > > Using a RE will be tricky, especially if it is possible to have > recursive nesting (which by definition REs can't handle). For a real > general purpose solution you will need to create a custom parser. > There are a couple modules out there that can help you with that. > > pyparsing is one:http://pyparsing.wikispaces.com/ > > Matt Following up to my own post, Here is a working example that uses the built-in _ast module. I posted something similar the other day. This uses pythons own internal parser to do it for you. It works in this case because, at least from what you have posted, your syntax doesn't violate python syntax. [code] import _ast def eval_tuple(text): """ Evaluate a string representing a tuple of strings, names and calls, returns a tuple of strings. """ ast = compile(text, "", 'eval', _ast.PyCF_ONLY_AST) return _traverse(ast.body) def _traverse(ast): """ Traverse the AST returning string representations of tuples strings names and calls. """ if isinstance(ast, _ast.Tuple): return tuple(_traverse(el) for el in ast.elts) elif isinstance(ast, _ast.Str): return ast.s elif isinstance(ast, _ast.Name): return ast.id elif isinstance(ast, _ast.Call): name = ast.func.id args = [_traverse(x) for x in ast.args] return "%s(%s)"%(name, ", ".join(args)) raise SyntaxError() examples = [ ('foo, bar, baz', ('foo', 'bar', 'baz')), ('foo, "bar, baz", blurf', ('foo', 'bar, baz', 'blurf')), ('foo, bar(baz, blurf), mumble', ('foo', 'bar(baz, blurf)', 'mumble')), ] def test(): for text, expected in examples: print "trying %r => %r"%(text, expected) result = eval_tuple(text) if result == expected: print "PASS" else: print "FAIL, GOT: %r"%result if __name__ == "__main__": test() [/code] Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: How to split a string containing nested commas-separated substrings
On Jun 18, 10:19 am, Robert Dodier <[EMAIL PROTECTED]> wrote: > Hello, > > I'd like to split a string by commas, but only at the "top level" so > to speak. An element can be a comma-less substring, or a > quoted string, or a substring which looks like a function call. > If some element contains commas, I don't want to split it. > > Examples: > > 'foo, bar, baz' => 'foo' 'bar' 'baz' > 'foo, "bar, baz", blurf' => 'foo' 'bar, baz' 'blurf' > 'foo, bar(baz, blurf), mumble' => 'foo' 'bar(baz, blurf)' 'mumble' > > Can someone suggest a suitable regular expression or other > method to split such strings? > > Thank you very much for your help. > > Robert You might look at the shlex module. It doesn't get you 100%, but its close: >>> shlex.split('foo, bar, baz') ['foo,', 'bar,', 'baz'] >>> shlex.split( 'foo, "bar, baz", blurf') ['foo,', 'bar, baz,', 'blurf'] >>> shlex.split('foo, bar(baz, blurf), mumble') ['foo,', 'bar(baz,', 'blurf),', 'mumble'] Using a RE will be tricky, especially if it is possible to have recursive nesting (which by definition REs can't handle). For a real general purpose solution you will need to create a custom parser. There are a couple modules out there that can help you with that. pyparsing is one: http://pyparsing.wikispaces.com/ Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: 2d graphics - drawing a vescica piscis in Python
On Jun 17, 12:45 pm, Terrence Brannon <[EMAIL PROTECTED]> wrote: > Hello, I have written a program to draw a vescica piscis en.wikipedia.org/wiki/Vesica_piscis> > > from turtle import * > > def main(): > setup(width=400, height=400) > > r = 50 > color("black") > circle(r) > color("white") > forward(r) > color("black") > circle(r) > x = raw_input('please enter a string:') > > if __name__ == '__main__': > main() > > ... but I would like the following: > > 1 - I dont like how the bottom of the first circle is not complete > 2 - I would like for the left circle to be filled with verticle lines > and the right circle to be filled with horizontal lines, so that the > vescica piscis is cross-hatched. > > And finally, is turtle the "best" option for what I'm doing? pyCairo > looked a bit hard to get going with, but very powerful. sping looked a > bit alpha/beta. I would just draw on the tk canvas: >>> import Tkinter as tk >>> can = tk.Canvas() >>> can.pack(fill=tk.BOTH, expand=True) >>> c1 = can.create_oval(10,10,110,110) >>> c2 = can.create_oval(60,10,170,110) You can draw cross hatching using can.create_line(...). Have fun, Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Please explain Python "__whatever__" construct.
When and why would I ever use > "__main__" or the many other "__whatever__" constructs? You don't generally use those names directly, they are 'magic'. The __add__ example is a good one. When you do `"hello " + "world"` behind the scenes python is actually calling "hello ".__add__("world"). There are a couple of places though that you do use them. "__main__" is a good example. That is the name of the `main` module. The module attribute `__name__` is the name of that module. If the code is being executed as a script the value of `__name__` is set to "__main__". Hence, if you create a module and you want to execute some code only if that module is run as a script you can use this construct: if __name__ == "__main__": # do stuff Here is an example of a the `__name__` attribute when it isn't "__main__": >>> import sys >>> sys.__name__ 'sys' Also, these names are frequently used when creating a class where you want special behavior. >>> class myint(object): ... def __init__(self, a): # The constructor ... self.a = a ... ... def __add__(self, x): ... print "I'm adding" ... return self.a + x ... >>> x = myint(10) >>> x + 12 I'm adding 22 As an added note, `"hello " "world"` is not concatenating two strings, The parser just sees it as one string. Otherwise, this would also work: >>> x = "hello " >>> x "world" File "", line 1 x "world" ^ SyntaxError: invalid syntax Where: >>> x = "hello " >>> x + "world" 'hello world' Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Subclassing list, what special methods do this?
On Jun 13, 11:38 am, Mike Kent <[EMAIL PROTECTED]> wrote: > For Python 2.5 and new-style classes, what special method is called > for mylist[2:4] = seq and for del mylist[2:4] (given that mylist is a > list, and seq is some sequence)? > > I'm trying to subclass list, and I'm having trouble determining what > special methods I have to override in my class for the above two > operations. From my testing, it seems to be __setslice__ for both, > but the docs say __setslice__ and brethren are deprecated. I would > have thought that __setitem__ and __delitem__ would be what was > called, but again, my testing says otherwise. It is a combination. http://docs.python.org/ref/sequence-methods.html For setting a slice in the form `x[from:to] = seq` the __setslice__ method will be called if it exists: x.__setslice__(from, to, seq) if __setslice__ doesn't exists __setitem__ is called with a slice object: x.__setitem__(slice(from,to), seq) if setting a slice in the form `x[from:to:step] = seq` (extended slicing) __setitem__ will be called with a slice object: x.__setitem__(slice(from, to, step), seq) For non slices (single index values) __setitem__ is always called. The real problem, and someone correct me if I'm wrong (this is a newsgroup... of course they will :p), is that you can't make your class hide or get rid of __setslice__ on the base class. I've tried making __getattribute__ raise an AttributeError and making __hasattr__ return False, neither works. I think this is because list is implemented in C and the __setslice__ slot is filled. Because the list object is implemented in C and is optimized, it doesn't use __getattribute__ or __hasattr__ to find out if the slice method exists, Python just grabs the pointer from the c structure and uses it. In my examples I only mentioned __setslice__/__setitem__, but all the same should apply to __delslice__/__delitem__ as well. So, it looks like as long as you want to subclass list, you are stuck implementing both __*slice__ and __*item__ methods. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: weird iteration/assignment problem
On Jun 13, 8:07 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > cirfu schrieb: > > > for i in xrange(0, len(texts)): > > texts[i] = "yes" > > > for i in texts: > > i = "no" > > > why is the first one working but not the second. i mean i see why the > > firts one works but i dont udnerstand why the second doesnt. > > Because in the second you only bind the contents of texts to a name i. > > But that doesn't mean that i magically became an "alias" for > texts[index] - it just happens to point at the same object. > > To accomplish what you want, the pythonic idiom is to use enumerate: > > for i, text in enumerate(texts): > text[i] = "yes" > > Diez That should be: for i, text in enumerate(texts): texts[i] = "yes" -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple and safe evaluator
On Jun 11, 9:16 pm, George Sakkis <[EMAIL PROTECTED]> wrote: > On Jun 11, 8:15 pm, bvdp <[EMAIL PROTECTED]> wrote: > > > > > Matimus wrote: > > > > The solution I posted should work and is safe. It may not seem very > > > readable, but it is using Pythons internal parser to parse the passed > > > in string into an abstract symbol tree (rather than code). Normally > > > Python would just use the ast internally to create code. Instead I've > > > written the code to do that. By avoiding anything but simple operators > > > and literals it is guaranteed safe. > > > Just wondering ... how safe would: > > > eval(s, {"__builtins__":None}, {} ) > > > be? From my testing it seems that it parses out numbers properly (int > > and float) and does simple math like +, -, **, etc. It doesn't do > > functions like int(), sin(), etc ... but that is fine for my puposes. > > > Just playing a bit, it seems to give the same results as your code using > > ast does. I may be missing something! > > Probably you do; within a couple of minutes I came up with this: > > >>> s = """ > > ... (t for t in 42 .__class__.__base__.__subclasses__() > ... if t.__name__ == 'file').next()('/etc/passwd') > ... """>>> eval(s, {"__builtins__":None}, {} ) > > Traceback (most recent call last): > File "", line 1, in > File "", line 3, in > IOError: file() constructor not accessible in restricted mode > > Not an exploit yet but I wouldn't be surprised if there is one. Unless > you fully trust your users, an ast-based approach is your best bet. > > George You can get access to any new-style class that has been loaded. This exploit works on my machine (Windows XP). [code] # This assumes that ctypes was loaded, but keep in mind any classes # that have been loaded are potentially accessible. import ctypes s = """ ( t for t in 42 .__class__.__base__.__subclasses__() if t.__name__ == 'LibraryLoader' ).next()( ( t for t in 42 .__class__.__base__.__subclasses__() if t.__name__ == 'CDLL' ).next() ).msvcrt.system('dir') # replace 'dir' with something nasty """ eval(s, {"__builtins__":None}, {}) [/code] Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple and safe evaluator
On Jun 11, 4:38 pm, bvdp <[EMAIL PROTECTED]> wrote: > I'm finding my quest for a safe eval() quite frustrating :) > > Any comments on this: Just forget about getting python to do this and, > instead, grab my set of values (from a user supplied text file) and call > an external program like 'bc' to do the dirty work. I think that this > would avoid someone from embedding os.system("rm ...") in what I thought > would be a math expression and having it maybe do damage? Perhaps I'm > getting too paranoid in my old age. > > I guess this would slow things down a bit, but that is not a big > concern. Bigger concern would be that I'm not sure if 'bc' or whatever > is guaranteed to be on other platforms than *nix. And if I want to be > really paranoid, I could worry that someone had planted a bad 'bc' on > the target. The solution I posted should work and is safe. It may not seem very readable, but it is using Pythons internal parser to parse the passed in string into an abstract symbol tree (rather than code). Normally Python would just use the ast internally to create code. Instead I've written the code to do that. By avoiding anything but simple operators and literals it is guaranteed safe. If you only want numbers you can even remove a bunch of code: [code] import _ast class SafeEvalError(Exception): pass class UnsafeCode(SafeEvalError): pass def num_eval(text): "similar to eval, but only works on numerical values." ast = compile(text, "", 'eval', _ast.PyCF_ONLY_AST) return _traverse(ast.body) def _traverse(ast): if isinstance(ast, _ast.Num): return ast.n elif isinstance(ast, _ast.Expr): return _traverse(ast.value) elif isinstance(ast, _ast.BinOp): if isinstance(ast.op, _ast.Add): return _traverse(ast.left) + _traverse(ast.right) elif isinstance(ast.op, _ast.Sub): return _traverse(ast.left) - _traverse(ast.right) elif isinstance(ast.op, _ast.Div): return _traverse(ast.left) / _traverse(ast.right) elif isinstance(ast.op, _ast.FloorDiv): return _traverse(ast.left) // _traverse(ast.right) elif isinstance(ast.op, _ast.Mod): return _traverse(ast.left) % _traverse(ast.right) elif isinstance(ast.op, _ast.Mult): return _traverse(ast.left) * _traverse(ast.right) elif isinstance(ast.op, _ast.Pow): return _traverse(ast.left) ** _traverse(ast.right) elif isinstance(ast.op, _ast.BitAnd): return _traverse(ast.left) & _traverse(ast.right) elif isinstance(ast.op, _ast.BitOr): return _traverse(ast.left) | _traverse(ast.right) elif isinstance(ast.op, _ast.BitXor): return _traverse(ast.left) ^ _traverse(ast.right) elif isinstance(ast.op, _ast.LShift): return _traverse(ast.left) << _traverse(ast.right) elif isinstance(ast.op, _ast.RShift): return _traverse(ast.left) >> _traverse(ast.right) elif isinstance(ast, _ast.UnaryOp): if isinstance(ast.op, _ast.Invert): return ~_traverse(ast.operand) elif isinstance(ast.op, _ast.USub): return -_traverse(ast.operand) elif isinstance(ast.op, _ast.UAdd): return +_traverse(ast.operand) raise UnsafeCode() [/code] To use: print num_eval("1 + 44 / 3") -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple and safe evaluator
On Jun 11, 1:25 pm, bvdp <[EMAIL PROTECTED]> wrote: > Is there a simple/safe expression evaluator I can use in a python > program. I just want to pass along a string in the form "1 + 44 / 3" or > perhaps "1 + (-4.3*5)" and get a numeric result. > > I can do this with eval() but I really don't want to subject my users to > the problems with that method. > > In this use I don't need python to worry about complex numbers, > variables or anything else. Just do the math on a set of values. Would > eval() with some restricted list of permitted operators do the trick? > > I'm feeling too lazy to write/debug my own parser for this :) > > Thanks, Bob. Here is something that I wrote using the _ast module. It works pretty well, and might be a good example for others wanting to experiment with the _ast module. On a related note... if anybody wants to provide feedback on this code it would be much appreciated. It involves a lot of if/elif branches, and feels ugly. Matt [code] import _ast class SafeEvalError(Exception): pass class UnsafeCode(SafeEvalError): pass # safe types: # Sequences: # list, tuple, dict, set, frozen_set* # Literals: str, unicode, int, long, complex, float def safe_eval(text): "similar to eval, but only works on literals" ast = compile(text, "", 'exec', _ast.PyCF_ONLY_AST) return _traverse(ast.body[0].value) def _traverse(ast): if isinstance(ast, _ast.List): return [_traverse(el) for el in ast.elts] elif isinstance(ast, _ast.Tuple): return tuple(_traverse(el) for el in ast.elts) elif isinstance(ast, _ast.Dict): return dict( zip( (_traverse(k) for k in ast.keys), (_traverse(v) for v in ast.values) ) ) elif isinstance(ast, _ast.Str): return ast.s elif isinstance(ast, _ast.Num): return ast.n elif isinstance(ast, _ast.Expr): return _traverse(ast.value) elif isinstance(ast, _ast.BinOp): if isinstance(ast.op, _ast.Add): return _traverse(ast.left) + _traverse(ast.right) elif isinstance(ast.op, _ast.Sub): return _traverse(ast.left) - _traverse(ast.right) elif isinstance(ast.op, _ast.Div): return _traverse(ast.left) / _traverse(ast.right) elif isinstance(ast.op, _ast.FloorDiv): return _traverse(ast.left) // _traverse(ast.right) elif isinstance(ast.op, _ast.Mod): return _traverse(ast.left) % _traverse(ast.right) elif isinstance(ast.op, _ast.Mult): return _traverse(ast.left) * _traverse(ast.right) elif isinstance(ast.op, _ast.Pow): return _traverse(ast.left) ** _traverse(ast.right) elif isinstance(ast.op, _ast.BitAnd): return _traverse(ast.left) & _traverse(ast.right) elif isinstance(ast.op, _ast.BitOr): return _traverse(ast.left) | _traverse(ast.right) elif isinstance(ast.op, _ast.BitXor): return _traverse(ast.left) ^ _traverse(ast.right) elif isinstance(ast.op, _ast.LShift): return _traverse(ast.left) << _traverse(ast.right) elif isinstance(ast.op, _ast.RShift): return _traverse(ast.left) >> _traverse(ast.right) elif isinstance(ast, _ast.BoolOp): if isinstance(ast.op, _ast.And): return all(_traverse(v) for v in ast.values) if isinstance(ast.op, _ast.Or): return any(_traverse(v) for v in ast.values) elif isinstance(ast, _ast.UnaryOp): if isinstance(ast.op, _ast.Invert): return _traverse(ast.operand) if isinstance(ast.op, _ast.USub): return -_traverse(ast.operand) if isinstance(ast.op, _ast.UAdd): return +_traverse(ast.operand) if isinstance(ast.op, _ast.Not): return not _traverse(ast.operand) raise UnsafeCode() if __name__ == "__main__": print safe_eval("[1,2,3,{'hello':1}, (1,-2,3)], 4j, 1+5j, ~1+2*3") [/code] -- http://mail.python.org/mailman/listinfo/python-list
Re: can't assign to literal
On Jun 10, 12:53 pm, maehhheeyy <[EMAIL PROTECTED]> wrote: > this is stopping my program from running properly. is there something > wrong in my code when that happens? yes Post your code, or at least the full error message if you want more details. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Question by someone coming from C...
On Jun 9, 2:00 pm, Skye <[EMAIL PROTECTED]> wrote: > Writing this app in Python, not sure what the "best practice" would > be. > > I want a bitfield global logging level that allows me to turn specific > debugging modules on and off. If I was doing this in C, I'd just use > some globals like: > > unsigned int debug_level = 0; > #define DEBUG_GENERAL 0x0001 > #define DEBUG_CONFIG 0x0002 > #define DEBUG_OPTIONS 0x0004 > etc etc > > So I guess my questions are: > > 1. there doesn't seem to be a way to define global constants like in > other languages? > 2. any special voodoo to use bitfields in Python? > > Thanks! > Skye There is no conventional way to define a constant. There may be some tricks if you look around, but I've been coding Python for a long time and never actually needed one. The side effect is that you could redefine one of the values at run-time. By naming convention though, the user will know not to do that, just like they know not to mess with attributes of an object that begin with "_". If they do mess with it, well, they better know what they are doing. There is no special bitfield voodoo that I know of. Generally, naming conventions are the same as they would be in C (all caps): FOO = 0x01 BAR = 0x02 BAZ = 0x04 val = FOO | BAR | BAZ if val & BAR: #do bar stuff The only time to do that sort of thing (in python) is when interacting with something else that isn't written in Python though. In general, for logging, just use the standard logging module: http://docs.python.org/lib/module-logging.html Matt -- http://mail.python.org/mailman/listinfo/python-list