Re: Creating Import Hooks
On Feb 17, 10:48 pm, Sreejith K wrote: > Hi everyone, > > I need to implement custom import hooks for an application > (http://www.python.org/dev/peps/pep-0302/). I want to restrict an application > to import certain modules (say socket module). Google app engine is > using a module hook to do this (HardenedModulesHook in google/ > appengine/tools/dev_appserver.py). But I want to allow that > application to use an sdk module (custom) which imports and uses > socket module. But the module hook restricts the access by sdk. > Finding out, which file is importing a module give a solution?? ie. If > the application is importing socket module, I want to restrict it. But > if the sdk module is importing socket I want to allow it. Is there any > way I can do this ? > > Application > > import sdk > import socket # I dont want to allow this (need to raise > ImportError) > > SDK > > import socket # need to allow this SDK === import socket App === import SDK import sys socket = sys.modules['socket'] -- http://mail.python.org/mailman/listinfo/python-list
Re: MODULE FOR I, P FRAME
DANNY wrote: > >Hm, well I see that and now I am thinking of using reference software >for MPEG4/10 which is written in c++ http://iphome.hhi.de/suehring/tml/ >just to use it as a decoder on my client side, save the file in that >format and then play it in my player using pyffmpeg >http://code.google.com/p/pyffmpeg/ and just manipulate frames in that >clip-I think that could be possibleam I right? After you have passed the video through a decoder, it's no longer in MPEG format at all. It's just a series of bitmaps, so pyffmpeg wouldn't apply. If you want to save the raw MPEG data, you can certainly do that, but such data is often not divided into "frames". -- Tim Roberts, t...@probo.com Providenza & Boekelheide, Inc. -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating Import Hooks
On Thu, 18 Feb 2010 00:03:51 -0800, Jonathan Gardner wrote: > On Feb 17, 10:48 pm, Sreejith K wrote: >> Hi everyone, >> >> I need to implement custom import hooks for an application >> (http://www.python.org/dev/peps/pep-0302/). I want to restrict an >> application to import certain modules (say socket module). Google app >> engine is using a module hook to do this (HardenedModulesHook in >> google/ appengine/tools/dev_appserver.py). But I want to allow that >> application to use an sdk module (custom) which imports and uses socket >> module. But the module hook restricts the access by sdk. Finding out, >> which file is importing a module give a solution?? ie. If the >> application is importing socket module, I want to restrict it. But if >> the sdk module is importing socket I want to allow it. Is there any way >> I can do this ? >> >> Application >> >> import sdk >> import socket # I dont want to allow this (need to raise >> ImportError) >> >> SDK >> >> import socket # need to allow this > > > SDK > === > import socket > > App > === > import SDK > import sys > socket = sys.modules['socket'] I'm not sure, but I think Sreejith wants to prohibit imports from the App layer while allowing them from the SDK layer, not work around a prohibition in the SDK layer. In other words, he wants the import hook to do something like this: if module is socket and the caller is not SKD: prohibit else allow I could be wrong of course. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Draft paper submission deadline is extended: SETP-10, Orlando, USA
It would be highly appreciated if you could share this announcement with your colleagues, students and individuals whose research is in software engineering, software testing, software quality assurance, software design and related areas. Draft paper submission deadline is extended: SETP-10, Orlando, USA The 2010 International Conference on Software Engineering Theory and Practice (SETP-10) (website: http://www.PromoteResearch.org) will be held during 12-14 of July 2010 in Orlando, FL, USA. SETP is an important event in the areas of Software development, maintenance, and other areas of software engineering and related topics. The conference will be held at the same time and location where several other major international conferences will be taking place. The conference will be held as part of 2010 multi-conference (MULTICONF-10). MULTICONF-10 will be held during July 12-14, 2010 in Orlando, Florida, USA. The primary goal of MULTICONF is to promote research and developmental activities in computer science, information technology, control engineering, and related fields. Another goal is to promote the dissemination of research to a multidisciplinary audience and to facilitate communication among researchers, developers, practitioners in different fields. The following conferences are planned to be organized as part of MULTICONF-10. • International Conference on Artificial Intelligence and Pattern Recognition (AIPR-10) •International Conference on Automation, Robotics and Control Systems (ARCS-10) • International Conference on Bioinformatics, Computational Biology, Genomics and Chemoinformatics (BCBGC-10) • International Conference on Computer Communications and Networks (CCN-10) • International Conference on Enterprise Information Systems and Web Technologies (EISWT-10) • International Conference on High Performance Computing Systems (HPCS-10) • International Conference on Information Security and Privacy (ISP-10) • International Conference on Image and Video Processing and Computer Vision (IVPCV-10) • International Conference on Software Engineering Theory and Practice (SETP-10) • International Conference on Theoretical and Mathematical Foundations of Computer Science (TMFCS-10) MULTICONF-10 will be held at Imperial Swan Hotel and Suites. It is a full-service resort that puts you in the middle of the fun! Located 1/2 block south of the famed International Drive, the hotel is just minutes from great entertainment like Walt Disney World® Resort, Universal Studios and Sea World Orlando. Guests can enjoy free scheduled transportation to these theme parks, as well as spacious accommodations, outdoor pools and on-site dining — all situated on 10 tropically landscaped acres. Here, guests can experience a full- service resort with discount hotel pricing in Orlando. We invite draft paper submissions. Please see the website http://www.PromoteResearch.org for more details. -- http://mail.python.org/mailman/listinfo/python-list
Python library for working with simple equations
Hello Is there is any Python library that allow such things: Given a string expression as: x + 5 + x * (y + 2), any library that can develop the equation for example. Or if we say factor with "x" then it renders the expression with x * ( rest of expression ). There could be a functionality where when x,y are given then the expression can be evaluated. If there are two expressions, they can be added and the symbols preserved. Does such a thing exist? Thanks, Elias -- http://mail.python.org/mailman/listinfo/python-list
Re: Python library for working with simple equations
On Thu, Feb 18, 2010 at 1:09 AM, lallous wrote: > Hello > > Is there is any Python library that allow such things: > > Given a string expression as: x + 5 + x * (y + 2), any library that > can develop the equation for example. > Or if we say factor with "x" then it renders the expression with x * > ( rest of expression ). > There could be a functionality where when x,y are given then the > expression can be evaluated. > If there are two expressions, they can be added and the symbols > preserved. > > Does such a thing exist? They're called computer algebra systems: http://en.wikipedia.org/wiki/Computer_algebra_system SymPy is one for Python: http://code.google.com/p/sympy/ Don't know if it supports factoring specifically; I've never used it, I just have Google-Fu. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python library for working with simple equations
> Given a string expression as: x + 5 + x * (y + 2), any library that > can develop the equation for example. > Or if we say factor with "x" then it renders the expression with x * > ( rest of expression ). > There could be a functionality where when x,y are given then the > expression can be evaluated. > If there are two expressions, they can be added and the symbols > preserved. Take a look at sage: http://www.sagemath.org/ I wouldn't say it's simple, in fact it's huge, but it'll do the job. Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list
Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.
Jonathan Gardner wrote: > On Feb 17, 12:02 am, Lawrence D'Oliveiro central.gen.new_zealand> wrote: >> In message >> <8ca440b2-6094-4b35-80c5-81d000517...@v20g2000prb.googlegroups.com>, >> >> Jonathan Gardner wrote: >> > I used to think anonymous functions (AKA blocks, etc...) would be a >> > nice feature for Python. >> >> > Then I looked at a stack trace from a different programming >> > language with lots of anonymous functions. (I believe it was perl.) >> >> Didnt it have source line numbers in it? >> >> What more do you need? > > I don't know, but I tend to find the name of the function I called to > be useful. It's much more memorable than line numbers, particularly > when line numbers keep changing. > > I doubt it's just me, though. Some problems with using just line numbers to track errors: In any language it isn't much use if you get a bug report from a shipped program that says there was an error on line 793 but no report of exactly which version of the shipped code was being run. Microsoft love telling you the line number: if IE gets a Javascript error it reports line number but not filename, so you have to guess which of the HTML page or one of many included files actually had the error. Plus the line number that is reported is often slightly off. Javascript in particular is often sent to the browser compressed then uncompressed and eval'd. That makes line numbers completely useless for tracking down bugs as you'll always get the line number of the eval. Also the way functions are defined in Javascript means you'll often have almost every function listed in a backtrace as 'Anonymous'. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating Import Hooks
On Feb 18, 1:57 pm, Steven D'Aprano wrote: > On Thu, 18 Feb 2010 00:03:51 -0800, Jonathan Gardner wrote: > > On Feb 17, 10:48 pm, Sreejith K wrote: > >> Hi everyone, > > >> I need to implement custom import hooks for an application > >> (http://www.python.org/dev/peps/pep-0302/). I want to restrict an > >> application to import certain modules (say socket module). Google app > >> engine is using a module hook to do this (HardenedModulesHook in > >> google/ appengine/tools/dev_appserver.py). But I want to allow that > >> application to use an sdk module (custom) which imports and uses socket > >> module. But the module hook restricts the access by sdk. Finding out, > >> which file is importing a module give a solution?? ie. If the > >> application is importing socket module, I want to restrict it. But if > >> the sdk module is importing socket I want to allow it. Is there any way > >> I can do this ? > > >> Application > >> > >> import sdk > >> import socket # I dont want to allow this (need to raise > >> ImportError) > > >> SDK > >> > >> import socket # need to allow this > > > SDK > > === > > import socket > > > App > > === > > import SDK > > import sys > > socket = sys.modules['socket'] > > I'm not sure, but I think Sreejith wants to prohibit imports from the App > layer while allowing them from the SDK layer, not work around a > prohibition in the SDK layer. > > In other words, he wants the import hook to do something like this: > > if module is socket and the caller is not SKD: > prohibit > else > allow > > I could be wrong of course. > > -- > Steven @Steven, Thats exactly what I want.. Anyway to do that ?? -- http://mail.python.org/mailman/listinfo/python-list
Re: Referring to class methods in class attributes
Mark Lawrence a écrit : Ben Finney wrote: Bruno Desthuilliers writes: Mmmm... Let's try to explain the whole damn thing. It's really (and IMHO beautifully) simple once you get it, but I agree it's a bit peculiar when compared to most mainstream OO languages. […] Bruno, that's the first time I've understood the descriptor protocol, or even understood *why* the descriptor protocol is important. Could you please post (a version of) this as a separate article somewhere? A weblog post, or a new thread in this forum, or in the documentation? Somewhere that web searches will find it prominently when searching for the Python descriptor protocol. Thank you. I'll second, third and fourth this request. More please Bruno, or from anybody similarly qualified. Thanks very much. First, thanks Ben and Mark for the kind words... I never tried to google for them, but there must be at least 4 previous posts here where I tried to explain the topic. Perhaps this latest attempt was better ?-) But anyway : if it's about making this text easier to find, it seems that posting it here is not the most efficient solution !-) Anyway : I don't have a blog and don't really have enough time / motivation for blogging. Now if anyone is willing and able to host such a text on his own website / blog / whatever, I'd be happy to rework it a bit. /wrt to have it in the documentation and the "from anybody similarly qualified", there's already a pretty comprehensive article in the official docs (well : linked from the official docs), from someone who's _way_ more qualified that I'll ever be - FWIW, everything I explained here and all my knowledge of the descriptor protocol, methods etc come from this article: http://users.rcn.com/python/download/Descriptor.htm But I understand it can be a bit intimidating for newcomers, so yes, perhaps a lighter introductory text could be helpful. So guys, if you think a revised version of my post would be of interest, I'll take you on words: provide the hosting, I'll provide the content !-) B. -- http://mail.python.org/mailman/listinfo/python-list
Re: Referring to class methods in class attributes
Stephen Hansen wrote: Or just leave it as a top level function where it was perfectly happy to live :) Yes. This is probably the sanest solution anyway, because probably having many such functions to use, packing them into smth like package.utils anyway is a good idea. I'm trying mainly to learn and test-drive such solutions, not that I would be keen to use them excessively in production code. This obviously means no other method can call it like self.print_internal_date(), because self would get passed as first argument, yes? It doesn't have to be. It could be a class method-- @classmethod does that, makes it receive 'cls' the actual class as the first argument. Or a @staticmethod, in which case it has no first-argument at all. Ahh now I get it. Thanks! So that's the use for @staticmethod (I was wondering if there was any). -- http://mail.python.org/mailman/listinfo/python-list
How to use AWS/PAA nowadays? PyAWS / pyamazon outdated?
Hi, is anybody aware of any updated and / or maintained library for accessing AWS/PAA with Python? I found the dusty pyamazon and a derivate of it, pyaws, but both of them do not seem to support request signatures which must be used by August 15, 2009 to use the Amazon Services, and generally seem to be a little bit outdated... Is there a better way doing signed requests to AWS/PAA nowadays? How are you doing it? I found a good PHP Library here: http://getcloudfusion.com/docs - but I would like to program stuff with python - is there anything pythonic that is close to that? Thank you very much for your attention, have a nice day, Snaky -- http://mail.python.org/mailman/listinfo/python-list
Re: Referring to class methods in class attributes
Bruno Desthuilliers writes: > perhaps a lighter introductory text could be helpful. So guys, if you > think a revised version of my post would be of interest, I'll take you > on words: provide the hosting, I'll provide the content !-) Here, let me work my hosting magic: http://wiki.python.org/>. -- \“Choose mnemonic identifiers. If you can't remember what | `\mnemonic means, you've got a problem.” —Larry Wall | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating Import Hooks
Sreejith K wrote: On Feb 18, 1:57 pm, Steven D'Aprano wrote: On Thu, 18 Feb 2010 00:03:51 -0800, Jonathan Gardner wrote: On Feb 17, 10:48 pm, Sreejith K wrote: Hi everyone, I need to implement custom import hooks for an application (http://www.python.org/dev/peps/pep-0302/). I want to restrict an application to import certain modules (say socket module). Google app engine is using a module hook to do this (HardenedModulesHook in google/ appengine/tools/dev_appserver.py). But I want to allow that application to use an sdk module (custom) which imports and uses socket module. But the module hook restricts the access by sdk. Finding out, which file is importing a module give a solution?? ie. If the application is importing socket module, I want to restrict it. But if the sdk module is importing socket I want to allow it. Is there any way I can do this ? Application import sdk import socket # I dont want to allow this (need to raise ImportError) SDK import socket # need to allow this SDK === import socket App === import SDK import sys socket = sys.modules['socket'] I'm not sure, but I think Sreejith wants to prohibit imports from the App layer while allowing them from the SDK layer, not work around a prohibition in the SDK layer. In other words, he wants the import hook to do something like this: if module is socket and the caller is not SKD: prohibit else allow I could be wrong of course. -- Steven @Steven, Thats exactly what I want.. Anyway to do that ?? import sys sys.modules['socket'] = None import socket --- ImportError Traceback (most recent call last) ImportError: No module named socket JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Using class attributes
Arnaud Delobelle writes: > Descriptors to the rescue :) > > def read_body_from(filename): > print "** loading content **" > return "" % filename > > # This is a kind of class property > class TemplateFilename(object): > def __get__(self, obj, cls): > return "%s.tmpl" % cls.__name__ > > # And this is a kind of cached class property > class TemplateBody(object): > def __get__(self, obj, cls): > try: > return cls._body > except AttributeError: > cls._body = read_body_from(cls.template_filename) > return cls._body > > class Foo(object): > template_filename = TemplateFilename() > template_body = TemplateBody() > > class FooA(Foo): >pass > > class FooB(Foo): >pass Very enlightening, thanks! By the way, I completely agree with the other posters in this thread that intricate solutions such as this are likely to be overkill, especially since at this point I have no idea if the inefficiency of reading those templates multiple times would at all matter (frankly, I'd doubt it). But it's certainly been educational to learn about these techniques. One observation: if I implement the descriptor solution as given above, the code works perfectly, but running the code through pychecker now causes an error, because that again causes an attempt to read from the non-existant base class template file "Foo.tmpl"... -- Leo Breebaart -- http://mail.python.org/mailman/listinfo/python-list
Re: Python library for working with simple equations
>> Given a string expression as: x + 5 + x * (y + 2), any library that >> can develop the equation for example. >> Or if we say factor with "x" then it renders the expression with x * >> ( rest of expression ). >> There could be a functionality where when x,y are given then the >> expression can be evaluated. >> If there are two expressions, they can be added and the symbols >> preserved. > > Take a look at sage: http://www.sagemath.org/ > I wouldn't say it's simple, in fact it's huge, but it'll do the job. Probably you can isolate the part of sage that you actually need and can throw away 95% of it. HTH, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list
Re: working with laptop battery
On Sat, Feb 13, 2010 at 09:19:59PM -0500, Chris Colbert wrote: >You'll need acpi installed: >In [6]: import subprocess Thanks for that code, I'll try putting something together this weekend. Dan -- http://mail.python.org/mailman/listinfo/python-list
Static method
Hello everyone, Disclaimer: I'm doing this mainly for learning purposes, to feel what it's good for. I'm trying to get print_internal_date become a static method AND to refer to it in a class attribute 'tagdata' dict. class PYFileInfo(FileInfo): 'python file properties' @staticmethod def print_internal_date(filename): f = open(filename + 'c', "rb") data = f.read(8) mtime = struct.unpack(" insts = list_dir(r'c:\mp3i',['.mp3', '.py']) File "C:/mp3i/finfo2.py", line 93, in list_dir insts = [c(f) for c,f in class_files] File "C:/mp3i/finfo2.py", line 69, in __init__ FileInfo.__init__(self,fname) File "C:/mp3i/finfo2.py", line 12, in __init__ self['name'] = filename File "C:/mp3i/finfo2.py", line 74, in __setitem__ self.__get_props(value) File "C:/mp3i/finfo2.py", line 79, in __get_props self[tag] = fun(value) TypeError: 'staticmethod' object is not callable I think I know where the problem is: what resides in tagdata is a static method 'wrapper', not the function itself, according to: http://docs.python.org/reference/datamodel.html "Static method objects Static method objects provide a way of defeating the transformation of function objects to method objects described above. A static method object is a wrapper around any other object, usually a user-defined method object. When a static method object is retrieved from a class or a class instance, the object actually returned is the wrapped object, which is not subject to any further transformation. Static method objects are not themselves callable, although the objects they wrap usually are." So, how do I get out the wrapped function out of static method without class call or instance call? (to be called in self[tag] = fun(value)) Yes, I do know that if I just get rid of @staticmethod, this works without a hitch. -- http://mail.python.org/mailman/listinfo/python-list
Re: working with laptop battery
I'm not sure I have those files, but I'll look a little harder this weekend when I put together the script. Thanks for your help, Dan On Sat, Feb 13, 2010 at 08:23:28PM -0600, Tim Chase wrote: > Daniel Dalton wrote: > >On Sat, Feb 13, 2010 at 05:26:02PM -0800, Chris Rebert wrote: > >>It's probably gonna depend on which OS you're running. Which would be...? > > > >Sorry, forgot to mention this. I'm running debian linux. > > You should be able to read/poll the various files in > > /proc/acpi/battery/BAT*/* > > for whatever battery information you need. Each BAT* directory > contains information about one of the batteries in the system (it's > possible, albeit rare, to have more than one). So you might have > some script that runs every $INTERVAL that looks something like > > from glob import glob > for fname in glob('/proc/acpi/battery/BAT*/*'): > f = file(fname) > for line in f: > do_something(line) > f.close() > > On my Debian laptop (Gateway Solo 1200, OEM battery circa 2001), the > contents look something like > > t...@rubbish:/proc/acpi/battery/BAT0$ cat alarm > alarm: unsupported > t...@rubbish:/proc/acpi/battery/BAT0$ cat info > present: yes > design capacity: 4016 mAh > last full capacity: 4011 mAh > battery technology: rechargeable > design voltage: 9600 mV > design capacity warning: 602 mAh > design capacity low: 401 mAh > capacity granularity 1: 201 mAh > capacity granularity 2: 3409 mAh > model number:QT08 > serial number: SANYOQT08 > battery type:NiMH > OEM info:SANYO > t...@rubbish:/proc/acpi/battery/BAT0$ cat state > present: yes > capacity state: ok > charging state: charged > present rate:unknown > remaining capacity: 4011 mAh > present voltage: 9600 mV > > > > -tkc > -- http://mail.python.org/mailman/listinfo/python-list
Re: working with laptop battery
On Sun, Feb 14, 2010 at 03:22:11AM +0100, Daniel Fetchinson wrote: > /proc/acpi/battery/BAT0/info > /proc/acpi/battery/BAT0/state Had a quick look, but that path doesn't seem to exist, I'll look harder on the weekend when I put the script together, because it has to be somewhere. Thanks, Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: Referring to class methods in class attributes
Ben Finney a écrit : Bruno Desthuilliers writes: perhaps a lighter introductory text could be helpful. So guys, if you think a revised version of my post would be of interest, I'll take you on words: provide the hosting, I'll provide the content !-) Here, let me work my hosting magic: http://wiki.python.org/>. Duh ! (me banging my head on the desktop) Ok, I'll do my part of the job then !-) -- http://mail.python.org/mailman/listinfo/python-list
Re: MediaWiki to RTF/Word/PDF
Hi, - I checked some ways doing this, and starting over with a new thing will give you a lot of headaches - all XSLT processors have one or another problem - success depends very much on how you where using wikipedia (plugins?) and you will have to expect a lot of poking around with details and still not beeing happy with the solutions available - there are many really crazy approaches out there to generate pdf of mediawiki "markup" - many tried, not many succeeded, most of them stop at "good enough for me"-level. So it might be a good idea, not running too far away from what they are doing at http://code.pediapress.com - you will spend much less time with installing ubuntu in a virtualbox. However there is one quite impressive tool, that does pdf conversion via css and is good for getting the job done quick and not too dirty: http://www.princexml.com/samples/ - scroll down to the mediawiki examples - they offer a free license for non-commercial projects. Good luck! Have a nice day, Snaky -- http://mail.python.org/mailman/listinfo/python-list
Re: Static method
mk, 18.02.2010 12:12: > I'm trying to get print_internal_date become a static method AND to > refer to it in a class attribute 'tagdata' dict. > > class PYFileInfo(FileInfo): > 'python file properties' > > @staticmethod > def print_internal_date(filename): > f = open(filename + 'c', "rb") > data = f.read(8) > mtime = struct.unpack(" return time.asctime(time.gmtime(mtime[0])) > > tagdata = {'compiled_fname': lambda x: x + 'c', > 'size': os.path.getsize, > 'internal_date': print_internal_date > } You can 'unroll' the decorator like this: class PYFileInfo(FileInfo): def print_internal_date(filename): f = open(filename + 'c', "rb") data = f.read(8) mtime = struct.unpack("http://mail.python.org/mailman/listinfo/python-list
Re: Static method
mk a écrit : I'm trying to get print_internal_date become a static method AND to refer to it in a class attribute 'tagdata' dict. class PYFileInfo(FileInfo): 'python file properties' @staticmethod def print_internal_date(filename): f = open(filename + 'c', "rb") data = f.read(8) mtime = struct.unpack(" (snip) def __get_props(self, value): py_compile.compile(value) for tag, fun in PYFileInfo.tagdata.items(): self[tag] = fun(value) But: c:/Python26/pythonw.exe -u "C:/mp3i/finfo2.py" Traceback (most recent call last): (snip) File "C:/mp3i/finfo2.py", line 79, in __get_props self[tag] = fun(value) TypeError: 'staticmethod' object is not callable I think I know where the problem is: what resides in tagdata is a static method 'wrapper', not the function itself, according to: Indeed. Sorry, I'm afraid I gave you bad advice wrt/ using a staticmethod here - I should know better :( (well, OTHO staticmethods are not something I use that often). Anyway: here are a simplified version of your problem, a possible solution that _won't_ statisfy your other constraints, 2 ugly hacks that could work but that I don't really like, and what's possibly the "less worse" solution if you really need a staticmethod here. ### class Foo1(object): """ simplified version of mk's code - test() fails """ @staticmethod def bar(baaz): print baaz tagada = {'bar': bar} def test(self, baaz): self.tagada['bar'](baaz) class Foo2(object): """ naive solution : kinda work, BUT will fail with the real code that has plain functions in 'tagada' """ @staticmethod def bar(baaz): print baaz tagada = {'bar': bar} def test(self, baaz): self.tagada['bar'].__get__(self)(baaz) class Foo3(object): """ working solution 1 : defer the wrapping of 'bar' as a staticmethod """ def bar(baaz): print baaz tagada = {'bar': bar} bar = staticmethod(bar) def test(self, baaz): self.tagada['bar'](baaz) class Foo4(object): """ working solution 2 : use a lambda """ @staticmethod def bar(baaz): print baaz tagada = {'bar': lambda x : Foo4.bar(x)} def test(self, baaz): self.tagada['bar'](baaz) """ and as a "less worse" solution """ def foo5bar(baaz): print baaz class Foo5(object): tagada = {'bar': foo5bar} bar = staticmethod(foo5bar) def test(self, baaz): self.tagada['bar'](baaz) ### Another "solution" might be to write an alternate callable implementation of 'staticmethod' - which I'll leave as an exercise to the reader (...) - but that's possibly a bit overkill !-) http://docs.python.org/reference/datamodel.html So, how do I get out the wrapped function out of static method without class call or instance call? (to be called in self[tag] = fun(value)) cf above. None of the solutions I could came with really statisfy me, but well, at least 3 of them might be "good enough" depending on the context. As far as I'm concerned, I'd first try the last one, but YMMV Yes, I do know that if I just get rid of @staticmethod, this works without a hitch. But then you can't use print_internal_date as a method !-) HTH -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating Import Hooks
On Feb 18, 3:49 pm, Jean-Michel Pichavant wrote: > Sreejith K wrote: > > On Feb 18, 1:57 pm, Steven D'Aprano > > wrote: > > >> On Thu, 18 Feb 2010 00:03:51 -0800, Jonathan Gardner wrote: > > >>> On Feb 17, 10:48 pm, Sreejith K wrote: > > Hi everyone, > > I need to implement custom import hooks for an application > (http://www.python.org/dev/peps/pep-0302/). I want to restrict an > application to import certain modules (say socket module). Google app > engine is using a module hook to do this (HardenedModulesHook in > google/ appengine/tools/dev_appserver.py). But I want to allow that > application to use an sdk module (custom) which imports and uses socket > module. But the module hook restricts the access by sdk. Finding out, > which file is importing a module give a solution?? ie. If the > application is importing socket module, I want to restrict it. But if > the sdk module is importing socket I want to allow it. Is there any way > I can do this ? > > Application > > import sdk > import socket # I dont want to allow this (need to raise > ImportError) > > SDK > > import socket # need to allow this > > >>> SDK > >>> === > >>> import socket > > >>> App > >>> === > >>> import SDK > >>> import sys > >>> socket = sys.modules['socket'] > > >> I'm not sure, but I think Sreejith wants to prohibit imports from the App > >> layer while allowing them from the SDK layer, not work around a > >> prohibition in the SDK layer. > > >> In other words, he wants the import hook to do something like this: > > >> if module is socket and the caller is not SKD: > >> prohibit > >> else > >> allow > > >> I could be wrong of course. > > >> -- > >> Steven > > > @Steven, Thats exactly what I want.. Anyway to do that ?? > > import sys > sys.modules['socket'] = None > > import socket > --- > ImportError Traceback (most recent call last) > > ImportError: No module named socket > > JM @Jean. Thanks for the reply. But this is not what I wanted. The import hook already restricts socket imports in applications. But I want them in sdk package (alone) which is being imported in the application. I don't want applications to directly use the socket module. That means I want to make some exceptions for sdk in import hooks. -- http://mail.python.org/mailman/listinfo/python-list
Help with lambda
Hello, I am still fairly new to Python. Can someone explain to me why there is a difference in f and g: def make_power(n): return lambda x: x ** n # Create a set of exponential functions f = [lambda x: x ** n for n in xrange(2, 5)] g = [make_power(n) for n in xrange(2, 5)] print f[0](3), f[1](3) print g[0](3), g[1](3) I expect to have "f" act same like "g". Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating Import Hooks
Sreejith K wrote: On Feb 18, 3:49 pm, Jean-Michel Pichavant wrote: Sreejith K wrote: On Feb 18, 1:57 pm, Steven D'Aprano wrote: On Thu, 18 Feb 2010 00:03:51 -0800, Jonathan Gardner wrote: On Feb 17, 10:48 pm, Sreejith K wrote: Hi everyone, I need to implement custom import hooks for an application (http://www.python.org/dev/peps/pep-0302/). I want to restrict an application to import certain modules (say socket module). Google app engine is using a module hook to do this (HardenedModulesHook in google/ appengine/tools/dev_appserver.py). But I want to allow that application to use an sdk module (custom) which imports and uses socket module. But the module hook restricts the access by sdk. Finding out, which file is importing a module give a solution?? ie. If the application is importing socket module, I want to restrict it. But if the sdk module is importing socket I want to allow it. Is there any way I can do this ? Application import sdk import socket # I dont want to allow this (need to raise ImportError) SDK import socket # need to allow this SDK === import socket App === import SDK import sys socket = sys.modules['socket'] I'm not sure, but I think Sreejith wants to prohibit imports from the App layer while allowing them from the SDK layer, not work around a prohibition in the SDK layer. In other words, he wants the import hook to do something like this: if module is socket and the caller is not SKD: prohibit else allow I could be wrong of course. -- Steven @Steven, Thats exactly what I want.. Anyway to do that ?? import sys sys.modules['socket'] = None import socket --- ImportError Traceback (most recent call last) ImportError: No module named socket JM @Jean. Thanks for the reply. But this is not what I wanted. The import hook already restricts socket imports in applications. But I want them in sdk package (alone) which is being imported in the application. I don't want applications to directly use the socket module. That means I want to make some exceptions for sdk in import hooks. give us your code (the hook import) in your entry file: import socket import sys sys.modules['sdkSocket'] = sys.modules['socket'] # allow to import socket ad sdkSocket sys.modules['socket'] = None # forbid to import socket del socket within your SDK: import sdkSocket # actually the socket module print sdkSocket.__file__ '/usr/lib/python2.5/socket.pyc' JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Using class attributes
Leo Breebaart writes: > Arnaud Delobelle writes: > >> Descriptors to the rescue :) >> >> def read_body_from(filename): >> print "** loading content **" >> return "" % filename >> >> # This is a kind of class property >> class TemplateFilename(object): >> def __get__(self, obj, cls): >> return "%s.tmpl" % cls.__name__ >> >> # And this is a kind of cached class property >> class TemplateBody(object): >> def __get__(self, obj, cls): >> try: >> return cls._body >> except AttributeError: >> cls._body = read_body_from(cls.template_filename) >> return cls._body >> >> class Foo(object): >> template_filename = TemplateFilename() >> template_body = TemplateBody() >> >> class FooA(Foo): >>pass >> >> class FooB(Foo): >>pass > > Very enlightening, thanks! > > By the way, I completely agree with the other posters in this > thread that intricate solutions such as this are likely to be > overkill, especially since at this point I have no idea if the > inefficiency of reading those templates multiple times would at > all matter (frankly, I'd doubt it). But it's certainly been > educational to learn about these techniques. Writing programs is a great way to keep learning :) > One observation: if I implement the descriptor solution as given > above, the code works perfectly, but running the code through > pychecker now causes an error, because that again causes an > attempt to read from the non-existant base class template file > "Foo.tmpl"... As someone said before, you could just provide a dummy Foo.tmpl file. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Static method
Bruno Desthuilliers wrote: I think I know where the problem is: what resides in tagdata is a static method 'wrapper', not the function itself, according to: Indeed. Sorry, I'm afraid I gave you bad advice wrt/ using a staticmethod here - I should know better :( (well, OTHO staticmethods are not something I use that often). Do not worry the least bit! I'm testdriving on a closed circuit here for sake of becoming better 'driver', I'm not going to drive like this on the streets. class Foo2(object): """ naive solution : kinda work, BUT will fail with the real code that has plain functions in 'tagada' """ @staticmethod def bar(baaz): print baaz tagada = {'bar': bar} def test(self, baaz): self.tagada['bar'].__get__(self)(baaz) Well I could always do: if isinstance(self.tagada['bar'], staticmethod): self.tagada['bar'].__get__(self)(baaz) else: self.tagada['bar'](baaz) But 1. this apparently defeats the purpose of using print_internal_date on instance/class in 'normal' way, and 2. I probably shouldn't be doing that since using isinstance is apparently like playing with yourself: while technically legal, people look at you weirdly. :-) class Foo3(object): """ working solution 1 : defer the wrapping of 'bar' as a staticmethod """ def bar(baaz): print baaz tagada = {'bar': bar} bar = staticmethod(bar) def test(self, baaz): self.tagada['bar'](baaz) Neat! I like this one. class Foo4(object): """ working solution 2 : use a lambda """ @staticmethod def bar(baaz): print baaz tagada = {'bar': lambda x : Foo4.bar(x)} def test(self, baaz): self.tagada['bar'](baaz) Huh? How does this one work? After all, while in Foo4 body, the Foo4 does not exist yet? Does lambda defer evaluation to runtime (when it's executed) or smth? """ and as a "less worse" solution """ def foo5bar(baaz): print baaz class Foo5(object): tagada = {'bar': foo5bar} bar = staticmethod(foo5bar) def test(self, baaz): self.tagada['bar'](baaz) Yes. I probably should have stayed with this one in the first place. I feel bad for using up bandwidth and people's attention with such stuff... -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with lambda
I'm looking at your code and was thinking ... why writing code that is difficult to understand? To answer your question though, they're different because in case "f", your lambda experssion is only evaluated once. That means the variable 'n' is ever only created once, and replaced repeatedly. In the second case "g", the function is only ever created once, and in effect, the lambda expression is evaluated as a new expression every time the function is called. That's why it may have been what you wanted. Seriously though, you should use a generater instead. And if that doesn't work for you, just make a function mate. Way easier to read, and you don't have to have ugly calls like f[0](3). >>> def powers(n): ... for x in xrange(2,5): ... yield x ** n ... >>> for result in powers(3): ... print result ... 8 27 64 Cheers, Xav -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with lambda
lallous writes: > Hello, > > I am still fairly new to Python. Can someone explain to me why there > is a difference in f and g: > > def make_power(n): > return lambda x: x ** n > > # Create a set of exponential functions > f = [lambda x: x ** n for n in xrange(2, 5)] > g = [make_power(n) for n in xrange(2, 5)] > > print f[0](3), f[1](3) > print g[0](3), g[1](3) > > > I expect to have "f" act same like "g". > > Thanks It's a FAQ! Except I don't think it's in the official Python FAQ, but it ought to be. The reason (very quickly) is that each time you call make_power() you create a new name 'n' which is bound to a different value each time, whereas in f: [lambda x: x ** n for n in xrange(2, 5)] The 'n' is the same name for each of the lambda functions and so is bound to the same value, which by the end of the loop is 4. So each of the lambdas in the list f is the same as: lambdsa x; x**4 after the end of the list comprehension. The usual fix for this is to change f to: f = [lambda x, n=n: x ** n for n in xrange(2, 5)] I'll let you think why this works. HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Referring to class methods in class attributes
Bruno Desthuilliers wrote: Thanks, that worked. But in order to make it work I had to get rid of 'self' in print_internal_date signature Indeed. Using it that way, the print_internal_date will not be wrapped in a method object. Hold on! How does Python know what to wrap and what not to wrap, assuming of course programmer doesn't use @classmethod or @staticmethod? Bc self has no special significance, it's just a (strong) convention, Python can't know what's the first argument of a function supposed to be, self or regular argument, and therefore it has no way of differentiating between functions (defined in class body) designed to become methods and those that are not? Where can I read on Python internals like this (aside from post of yours, that is)? Bc frankly skimming http://docs.python.org/reference/ didn't give me impression that a lot on the subject is there (well there's some, I found smth akin to your explanation below, although yours is way more readable)? Thanks for explanation below -- I'm going to ask some related questions. Mmmm... Let's try to explain the whole damn thing. It's really (and IMHO beautifully) simple once you get it, but I agree it's a bit peculiar when compared to most mainstream OO languages. The first thing is that the def statement *always* yield a function object. Always. If you don't believe it, try the following snippet: class Foo(object): def bar(self): return "baaz" print Foo.__dict__.keys() print type(Foo.__dict__['bar']) Just one thing here: >>> Foo.bar Huh?! Why does it say 'unbound' method? Shouldn't that be bound method (bound to Foo, that is)? So, why is it that type(Foo.bar) != type(Foo.__dict__['bar']) ? >>> type(Foo.__dict__['bar']) >>> type(Foo.bar) instancemethod - now that's something new. The answer is : attribute lookup rules and the descriptor protocol. To make a long story short, the descriptor protocol specify that, when, during an attribute lookup, a name resolves to a class attribute AND this attribute has a __get__ method, then this __get__ method is called (with either the instance or None and the class itself as arguments) Depending, I assume, on whether this is instance call | class method call, respectively? Hmm why does the __get__ receive class as argument on top of instance | None? After all, when having an instance, the class can always be found by instance.__class__ ? Is this for sake of class methods? Python is science, I gather: an answer to one question bears another 10 questions. and whatever it returns becomes the result of the attribute lookup. This mechanism is what provides support for computed attributes. Now the trick is that the function type do implement the descriptor protocol. So when a function is an attribute of a class object and you try to access it as an attribute of either the class itself or an instance of the class, it's __get__ method is called with the instance (or None) and the class. Having access to itself (of course), Quick question: how does a function access itself? Aside from rejected PEP (http://www.python.org/dev/peps/pep-3130/) I don't see the way of accessing itself outside globals() (and even then how would a function know its name -- well it shouldn't care about it really, as function object doesn't care how it's labelled, right?). Or does in "real Python" func's __get__ receive its own function (func) as an argument, like in your example implementation below? the instance (if there's one) and the class, it's easy for it to wrap all this into a method object. Which is itself a callable object, that when called mostly inject the instance as first object in the argument's list and returns the result of calling the wrapped function object. Aha! So that's the mechanism that makes self magically appear in an argument list! I always wondered how it worked. !!THANKS!! My 2 cents... Well, Bruno -- that was more like $200! Regards, mk -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with lambda
Yes it should be listed somewhere, now I get it. Thanks Arnaud. -- Elias On Feb 18, 1:47 pm, Arnaud Delobelle wrote: > lallous writes: > > Hello, > > > I am still fairly new to Python. Can someone explain to me why there > > is a difference in f and g: > > > def make_power(n): > > return lambda x: x ** n > > > # Create a set of exponential functions > > f = [lambda x: x ** n for n in xrange(2, 5)] > > g = [make_power(n) for n in xrange(2, 5)] > > > print f[0](3), f[1](3) > > print g[0](3), g[1](3) > > > I expect to have "f" act same like "g". > > > Thanks > > It's a FAQ! Except I don't think it's in the official Python FAQ, but > it ought to be. > > The reason (very quickly) is that each time you call make_power() you > create a new name 'n' which is bound to a different value each time, > whereas in f: > > [lambda x: x ** n for n in xrange(2, 5)] > > The 'n' is the same name for each of the lambda functions and so is > bound to the same value, which by the end of the loop is 4. So each of > the lambdas in the list f is the same as: > > lambdsa x; x**4 > > after the end of the list comprehension. > > The usual fix for this is to change f to: > > f = [lambda x, n=n: x ** n for n in xrange(2, 5)] > > I'll let you think why this works. > > HTH > > -- > Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with lambda
On Thu, 18 Feb 2010 04:28:00 -0800 (PST) lallous wrote: > def make_power(n): > return lambda x: x ** n Hint: type(make_power(2)) Did you expect that to return "int"? > # Create a set of exponential functions > f = [lambda x: x ** n for n in xrange(2, 5)] > g = [make_power(n) for n in xrange(2, 5)] The result of make_power(n) is a function that raises it's argument to the power of n. I don't know what you are trying to do. Maybe this? g = [make_power(n)(2) for n in xrange(2, 5)] or g = [make_power(2)(n) for n in xrange(2, 5)] -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. -- http://mail.python.org/mailman/listinfo/python-list
Re: Static method
mk a écrit : Bruno Desthuilliers wrote: (snip) class Foo2(object): """ naive solution : kinda work, BUT will fail with the real code that has plain functions in 'tagada' """ @staticmethod def bar(baaz): print baaz tagada = {'bar': bar} def test(self, baaz): self.tagada['bar'].__get__(self)(baaz) Well I could always do: if isinstance(self.tagada['bar'], staticmethod): self.tagada['bar'].__get__(self)(baaz) else: self.tagada['bar'](baaz) But 1. this apparently defeats the purpose of using print_internal_date on instance/class in 'normal' way, and 2. I probably shouldn't be doing that since using isinstance is apparently like playing with yourself: while technically legal, people look at you weirdly. :-) As far as I'm concerned, this would be a valid use case for isinstance. But it breaks uniformity and requires quite a lot of mostly useless code. class Foo3(object): """ working solution 1 : defer the wrapping of 'bar' as a staticmethod """ def bar(baaz): print baaz tagada = {'bar': bar} bar = staticmethod(bar) def test(self, baaz): self.tagada['bar'](baaz) Neat! I like this one. Not me - with the wrapping so far away from the definition, it's too easy to miss part of the "process" when you come back to this code 6 month later. class Foo4(object): """ working solution 2 : use a lambda """ @staticmethod def bar(baaz): print baaz tagada = {'bar': lambda x : Foo4.bar(x)} def test(self, baaz): self.tagada['bar'](baaz) Huh? How does this one work? After all, while in Foo4 body, the Foo4 does not exist yet? Does lambda defer evaluation to runtime (when it's executed) or smth? or smth, yes !-) A lambda expression evals to an ordinary function - just like a def statement - so Foo4 is not resolved until Foo4.tagada['bar'] is actually called. """ and as a "less worse" solution """ def foo5bar(baaz): print baaz class Foo5(object): tagada = {'bar': foo5bar} bar = staticmethod(foo5bar) def test(self, baaz): self.tagada['bar'](baaz) Yes. I probably should have stayed with this one in the first place. I feel bad for using up bandwidth and people's attention with such stuff... Why so ? You learned something, I learned something, and quite a few other people will now have a chance to learn something. Sensible use of bandwith as far as I'm concerned. wrt/ people's attention, don't worry, it's up to the reader to pay attention or skip the whole topic !-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with lambda
On Feb 18, 1:56 pm, "D'Arcy J.M. Cain" wrote: > On Thu, 18 Feb 2010 04:28:00 -0800 (PST) > > lallous wrote: > > def make_power(n): > > return lambda x: x ** n > > Hint: type(make_power(2)) > > Did you expect that to return "int"? > No, I expect to see a specialized function. > > # Create a set of exponential functions > > f = [lambda x: x ** n for n in xrange(2, 5)] > > g = [make_power(n) for n in xrange(2, 5)] > > The result of make_power(n) is a function that raises it's argument to > the power of n. I don't know what you are trying to do. Maybe this? > > g = [make_power(n)(2) for n in xrange(2, 5)] > or > g = [make_power(2)(n) for n in xrange(2, 5)] > What I am trying to do is generate different functions. If you're curious, I was playing with ctypes and wanted to see how it handles C<->Python callbacks: CB_T = WINFUNCTYPE(c_int, c_int) many_cb_t = CFUNCTYPE(c_int, c_int, CB_T) many_cb = many_cb_t(addressof(c_void_p.in_dll(dll, "many_cb"))) #ANY_SIMPLER def make_power(n): return lambda x: x ** n cbs = [CB_T(make_power(n)) for n in xrange(0, 1000)] cbs.append(None) many_cb(3, *cbs) And the C code in a shared lib: int many_cb(int value, ...) { va_list va; va = va_start(va, value); cb_t cb; int s = 0; while ( (cb = va_arg(va, cb_t)) != NULL) { printf("calling %p", cb); int r = cb(value); s += r; printf(", r=%d\n", r); } va_end(va); return s; } Speaking of that, anyone has an idea how to make simpler the line with #ANY_SIMPLER? I try: many_cb = CB_T.in_dll(dll, "many_cb") <- but that seems to work with data items only. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using class attributes
Arnaud Delobelle writes: > > One observation: if I implement the descriptor solution as > > given above, the code works perfectly, but running the code > > through pychecker now causes an error, because that again > > causes an attempt to read from the non-existant base class > > template file "Foo.tmpl"... It's not just pychecker that doesn't like the descriptor solution: pydoc now also crashes with the same IOError. :-) > As someone said before, you could just provide a dummy Foo.tmpl > file. A pragmatic solution, but one that smells bad to me. All this started because I didn't want my program to read files more than once, but I also don't want it to read files it doesn't have to read (and that don't even need to exist) in the first place! I'll just go back to the original instance-based lazy evaluation and caching solution now -- I never really had a big problem with that. My thanks again to all of you for helping me out with this. -- Leo Breebaart -- http://mail.python.org/mailman/listinfo/python-list
Re: Referring to class methods in class attributes
mk wrote: > Bruno Desthuilliers wrote: >>> Thanks, that worked. But in order to make it work I had to get rid of >>> 'self' in print_internal_date signature >> >> Indeed. Using it that way, the print_internal_date will not be wrapped >> in a method object. > > Hold on! How does Python know what to wrap and what not to wrap, > assuming of course programmer doesn't use @classmethod or @staticmethod? > Bc self has no special significance, it's just a (strong) convention, > Python can't know what's the first argument of a function supposed to > be, self or regular argument, and therefore it has no way of > differentiating between functions (defined in class body) designed to > become methods and those that are not? > > Where can I read on Python internals like this (aside from post of > yours, that is)? Bc frankly skimming http://docs.python.org/reference/ > didn't give me impression that a lot on the subject is there (well > there's some, I found smth akin to your explanation below, although > yours is way more readable)? > > Thanks for explanation below -- I'm going to ask some related questions. > >> Mmmm... Let's try to explain the whole damn thing. It's really (and IMHO >> beautifully) simple once you get it, but I agree it's a bit peculiar >> when compared to most mainstream OO languages. >> >> The first thing is that the def statement *always* yield a function >> object. Always. If you don't believe it, try the following snippet: >> >> class Foo(object): >> def bar(self): >> return "baaz" >> >> print Foo.__dict__.keys() >> print type(Foo.__dict__['bar']) > > Just one thing here: > Foo.bar > > > Huh?! Why does it say 'unbound' method? Shouldn't that be bound method > (bound to Foo, that is)? > No. The "unbound method" means it's a callable class attribute. The significance of "unbound" is that no specific instance is attached. >> So, why is it that type(Foo.bar) != type(Foo.__dict__['bar']) ? > type(Foo.__dict__['bar']) > type(Foo.bar) > > > instancemethod - now that's something new. > Well "instancemethod" is just the type of the attribute. If you create a Foo instance, its bar method also has type instancemethod, even though it's a *bound* method: >>> foo = Foo() >>> foo <__main__.Foo object at 0x7ff2a16c> >>> foo.bar > >>> type(foo.bar) >>> Note that it's only when the method is looked up *from an instance of the class* does the interpreter create the bound method. And remember that this is behavior that's specific to Python 2. > >> The >> answer is : attribute lookup rules and the descriptor protocol. >> >> To make a long story short, the descriptor protocol specify that, when, >> during an attribute lookup, a name resolves to a class attribute AND >> this attribute has a __get__ method, then this __get__ method is called >> (with either the instance or None and the class itself as arguments) > > Depending, I assume, on whether this is instance call | class method > call, respectively? > Yes. > Hmm why does the __get__ receive class as argument on top of instance | > None? After all, when having an instance, the class can always be found > by instance.__class__ ? Is this for sake of class methods? > When Bruno wrote "... AND this attribute has a __get__ method ...", the __get__method has to be defined on the attribute's class - the interpreter won't even look at the instance when trying to resolve the reference. But inheritance, of course, means that the same __get__ method may be used by several classes, and when there is no instance the specific (sub)class in question must be identifiable. So you got that right. > Python is science, I gather: an answer to one question bears another 10 > questions. > Yes, but it isn't quite "turtles all the way down". Ultimately the behavior we are discussing is hard-wired into the interpreter at the __getattribute__ level. >> and whatever it returns becomes the result of the attribute lookup. This >> mechanism is what provides support for computed attributes. >> >> Now the trick is that the function type do implement the descriptor >> protocol. So when a function is an attribute of a class object and you >> try to access it as an attribute of either the class itself or an >> instance of the class, it's __get__ method is called with the instance >> (or None) and the class. > >> Having access to itself (of course), > > Quick question: how does a function access itself? Aside from rejected > PEP (http://www.python.org/dev/peps/pep-3130/) I don't see the way of > accessing itself outside globals() (and even then how would a function > know its name -- well it shouldn't care about it really, as function > object doesn't care how it's labelled, right?). Or does in "real Python" > func's __get__ receive its own function (func) as an argument, like in > your example implementation below? > The function is an object of type function, so the lookup triggers a call to the __get__() method of the function's class, prov
Re: Referring to class methods in class attributes
mk a écrit : Bruno Desthuilliers wrote: Thanks, that worked. But in order to make it work I had to get rid of 'self' in print_internal_date signature Indeed. Using it that way, the print_internal_date will not be wrapped in a method object. Hold on! How does Python know what to wrap and what not to wrap, assuming of course programmer doesn't use @classmethod or @staticmethod? answered below - read on, young padawan Bc self has no special significance, it's just a (strong) convention, Python can't know what's the first argument of a function supposed to be, self or regular argument, and therefore it has no way of differentiating between functions (defined in class body) designed to become methods and those that are not? Indeed. Where can I read on Python internals like this (aside from post of yours, that is)? Bc frankly skimming http://docs.python.org/reference/ didn't give me impression that a lot on the subject is there (well there's some, I found smth akin to your explanation below, although yours is way more readable)? Thanks Thanks for explanation below -- I'm going to ask some related questions. Mmmm... Let's try to explain the whole damn thing. It's really (and IMHO beautifully) simple once you get it, but I agree it's a bit peculiar when compared to most mainstream OO languages. The first thing is that the def statement *always* yield a function object. Always. If you don't believe it, try the following snippet: class Foo(object): def bar(self): return "baaz" print Foo.__dict__.keys() print type(Foo.__dict__['bar']) Just one thing here: >>> Foo.bar Huh?! Why does it say 'unbound' method? Shouldn't that be bound method (bound to Foo, that is)? Yes, but it's not bound to a Foo instance. So, why is it that type(Foo.bar) != type(Foo.__dict__['bar']) ? >>> type(Foo.__dict__['bar']) Yeps. That's the function object created by the def statement. Just a plain old function - expect it's an attribute of class object "Foo". >>> type(Foo.bar) instancemethod - now that's something new. Don't let the "" fools you - it's just instancemethod.__repr__ that issues different wording according to whether the instancemethod instance is bound or not. The answer is : attribute lookup rules and the descriptor protocol. To make a long story short, the descriptor protocol specify that, when, during an attribute lookup, a name resolves to a class attribute AND this attribute has a __get__ method, then this __get__ method is called (with either the instance or None and the class itself as arguments) Depending, I assume, on whether this is instance call | class method call, respectively? s/call/lookup/ If it's looked up on the class, there's no instance to pass to __get__. Hmm why does the __get__ receive class as argument on top of instance | None? After all, when having an instance, the class can always be found by instance.__class__ ? Is this for sake of class methods? Having access to the class is handy when you don't have the instance. The point is mostly to let the descriptor know how it has been looked up and take appropriate decisions based on this - for a definition of "appropriote" that depends on what the descriptor is intended for . Remember that this mechanism provides the generic support for all kind of computed attributes - methods, properties, and whatever you can write yourself. Python is science, I gather: an answer to one question bears another 10 questions. That's the case with most technical domains - until you solved enough of the puzzle to start and see the big picture. and whatever it returns becomes the result of the attribute lookup. This mechanism is what provides support for computed attributes. Now the trick is that the function type do implement the descriptor protocol. So when a function is an attribute of a class object and you try to access it as an attribute of either the class itself or an instance of the class, it's __get__ method is called with the instance (or None) and the class. Having access to itself (of course), Quick question: how does a function access itself? In that case, it's quite simple: function.__get__ is a method of the function type, so it's called with 'self' as first argument !-) Aside from rejected PEP (http://www.python.org/dev/peps/pep-3130/) I don't see the way of accessing itself outside globals() You're confusing the function instance itself with the content of the def statement's block. The code within the def statement's block has no access to the function instance that will be built from it, but other methods of the function instance are, well, ordinary methods. (and even then how would a function know its name -- well it shouldn't care about it really, as function object doesn't care how it's labelled, right?). Or does in "real Python" func's __get__ receive its own function (func) it receives itself, yes. Ordinary method call,
Re: Python 3.0 usage?
On Feb 18, 2010, at 12:20 AM, alex23 wrote: MRAB wrote: Python 3.0 had a relatively short run before it was superseded by Python 3.1 due to certain issues, so, IMHO, I wouldn't worry about it unless someone especially requests/requires it. And even then, I'd just tell them I accept patches :) Excellent idea. =) Thanks to all who replied. Cheers Philip -- http://mail.python.org/mailman/listinfo/python-list
Re: Static method
Bruno Desthuilliers wrote: class Foo4(object): """ working solution 2 : use a lambda """ @staticmethod def bar(baaz): print baaz tagada = {'bar': lambda x : Foo4.bar(x)} def test(self, baaz): self.tagada['bar'](baaz) Huh? How does this one work? After all, while in Foo4 body, the Foo4 does not exist yet? Does lambda defer evaluation to runtime (when it's executed) or smth? or smth, yes !-) A lambda expression evals to an ordinary function - just like a def statement - so Foo4 is not resolved until Foo4.tagada['bar'] is actually called. This works, but... Foo4.bar in tagada is a staticmethod. So what's needed is Foo4.bar.__get__(x) (not that I'm that smart, I just got 'staticmethod is not callable' exception). It appears I have to use __get__ anyway while referring to bar in Foo4 methods: def testrefer(self, val): self.bar.__get__(val) Foo4.bar.__get__(val) At least I have to do this in my own code: def testit(self, fname): self.print_internal_date.__get__(fname + 'c') PYFileInfo.print_internal_date.__get__(fname + 'c') Or else I get "TypeError: 'staticmethod' object is not callable". -- http://mail.python.org/mailman/listinfo/python-list
Re: How to efficiently extract information from structured text file
On Feb 17, 7:38 pm, Steven D'Aprano wrote: > On Wed, 17 Feb 2010 17:13:23 -0800, Jonathan Gardner wrote: > > And once you realize that every program is really a compiler, then you > > have truly mastered the Zen of Programming in Any Programming Language > > That Will Ever Exist. > > In the same way that every tool is really a screwdriver. > > -- > Steven The way I learned this was: - Use the right tool for the right job. - Every tool is a hammer. -- Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Timer
On Wed, Feb 17, 2010 at 2:37 PM, Stephen Hansen wrote: > On Wed, Feb 17, 2010 at 10:14 AM, Victor Subervi > wrote: > >> Obviously, the removeCSS isn't going to work in that last line. What can I >> put there to remove the splash page after 5 seconds? >> > > Even though you're generating this with python, it doesn't have anything to > do with Python. You'll have to use javascript and DHTML and enable/disable > classes. How to do that is beyond the scope of this group, and might depend > on the browser. I'd go look into jQuery or some such which will encapsulate > such dynamic things better. > OK. Thanks. beno -- http://mail.python.org/mailman/listinfo/python-list
Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.
On Feb 18, 1:23 am, Duncan Booth wrote: > Jonathan Gardner wrote: > > On Feb 17, 12:02 am, Lawrence D'Oliveiro > central.gen.new_zealand> wrote: > >> In message > >> <8ca440b2-6094-4b35-80c5-81d000517...@v20g2000prb.googlegroups.com>, > > >> Jonathan Gardner wrote: > >> > I used to think anonymous functions (AKA blocks, etc...) would be a > >> > nice feature for Python. > > >> > Then I looked at a stack trace from a different programming > >> > language with lots of anonymous functions. (I believe it was perl.) > > >> Didn’t it have source line numbers in it? > > >> What more do you need? > > > I don't know, but I tend to find the name of the function I called to > > be useful. It's much more memorable than line numbers, particularly > > when line numbers keep changing. > > > I doubt it's just me, though. > > Some problems with using just line numbers to track errors: > > In any language it isn't much use if you get a bug report from a shipped > program that says there was an error on line 793 but no report of > exactly which version of the shipped code was being run. > > Microsoft love telling you the line number: if IE gets a Javascript > error it reports line number but not filename, so you have to guess > which of the HTML page or one of many included files actually had the > error. Plus the line number that is reported is often slightly off. > > Javascript in particular is often sent to the browser compressed then > uncompressed and eval'd. That makes line numbers completely useless for > tracking down bugs as you'll always get the line number of the eval. > Also the way functions are defined in Javascript means you'll often have > almost every function listed in a backtrace as 'Anonymous'. If this is an argument against using anonymous functions, then it is a quadruple strawman. Shipping buggy code is a bad idea, even with named functions. Obscuring line numbers is a bad idea, even with named functions. Having your customers stay on older versions of your software is a bad idea, even with named functions. Not being able to know which version of software you're customer is running is a bad idea, even with named functions. Of course, using anonymous functions in no way prevents you from capturing a version number in a traceback. And in most modern source control systems, it is fairly easy to revert to an old version of that code. def factory(): return lambda: 15 / 0 def bar(method): method() def foo(method): bar(method) def baz(method): foo(method) try: baz(factory()) except: print 'problem with version 1.234a' raise problem with version 1.234a Traceback (most recent call last): File "foo.py", line 14, in baz(factory()) File "foo.py", line 11, in baz foo(method) File "foo.py", line 8, in foo bar(method) File "foo.py", line 5, in bar method() File "foo.py", line 2, in return lambda: 15 / 0 ZeroDivisionError: integer division or modulo by zero -- http://mail.python.org/mailman/listinfo/python-list
Re: Static method
mk a écrit : Bruno Desthuilliers wrote: class Foo4(object): """ working solution 2 : use a lambda """ @staticmethod def bar(baaz): print baaz tagada = {'bar': lambda x : Foo4.bar(x)} def test(self, baaz): self.tagada['bar'](baaz) Huh? How does this one work? After all, while in Foo4 body, the Foo4 does not exist yet? Does lambda defer evaluation to runtime (when it's executed) or smth? or smth, yes !-) A lambda expression evals to an ordinary function - just like a def statement - so Foo4 is not resolved until Foo4.tagada['bar'] is actually called. This works, but... Foo4.bar in tagada is a staticmethod. So what's needed is Foo4.bar.__get__(x) (not that I'm that smart, I just got 'staticmethod is not callable' exception). Huh ??? class Foo4(object): @staticmethod def bar(baaz): print baaz tagada = {'bar': lambda x : Foo4.bar(x)} def test(self, baaz): self.tagada['bar'](baaz) >>> f = Foo4() >>> f.test(42) 42 >>> Foo4.bar(42) 42 >>> WorksForMe(tm). It appears I have to use __get__ anyway while referring to bar in Foo4 methods: > def testrefer(self, val): self.bar.__get__(val) Foo4.bar.__get__(val) At least I have to do this in my own code: def testit(self, fname): self.print_internal_date.__get__(fname + 'c') PYFileInfo.print_internal_date.__get__(fname + 'c') Or else I get "TypeError: 'staticmethod' object is not callable". I think you broke something somewhere. Assuming you're using Python 2.x (>= 2.3 IIRC), my above code works. -- http://mail.python.org/mailman/listinfo/python-list
unit testing a routine that sends mail
Hello, I have a routine that sends an email (this is how a Django view notifies me that an event has happened). I want to unit test that routine. So I gave each mail a unique subject line and I want to use python's mailbox package to look for that subject. But sometimes the mail gets delivered and sometimes it does not (I use sendmail to send it but I believe that it is always sent since I can see an entry in the mail log). I thought that the mail delivery system was occasionally hitting my mailbox lock, and that I needed to first sleep for a while. So I wrote a test that sleeps, then grabs the mailbox and looks through it, and if the mail is not there then it sleeps again, etc., for up to ten tries. It is below. However, I find that if the mail is not delivered on the first try then it is never delivered, no matter how long I wait. So I think I am doing mailbox wrong but I don't see how. The real puzzler for me is that the test reliably fails every third time. For instance, if I try it six times then it succeeds the first, second, fourth, and fifth times. I have to say that I cannot understand this at all but it certainly makes the unit test useless. I'm using Python 2.6 on an Ubuntu system. If anyone could give me a pointer to why the mail is not delivered, I sure could use it. Thanks, Jim ... class sendEmail_test(unittest.TestCase): """Test sendEmail() """ config = ConfigParser.ConfigParser() config.read(CONFIG_FN) mailbox_fn=config.get('testing','mailbox') def look_in_mailbox(self,uniquifier='123456789'): """If the mailbox has a message whose subject line contains the given uniquifier then it returns that message and deletes it. Otherwise, returns None. """ sleep_time=10.0 # wait for message to be delivered? message_found=None i=0 while i<10 and not(message_found): time.sleep(sleep_time) m=mailbox.mbox(self.mailbox_fn) try: m.lock() except Exception, err: print "trouble locking the mailbox: "+str(err) try: for key,message in m.items(): subject=message['Subject'] or '' print "subject is ",subject if subject.find(uniquifier)>-1: print "+++found the message+++ i=",str(i) message_found=message m.remove(key) break m.flush() except Exception, err: print "trouble reading from the mailbox: "+str(err) m.unlock() try: m.unlock() except Exception, err: print "trouble unlocking the mailbox: "+str(err) try: m.close() except Exception, err: print "trouble closing the mailbox: "+str(err) del m i+=1 return message_found def test_mailbox(self): random.seed() uniquifier=str(int(random.getrandbits(20))) print "uniquifier is ",uniquifier # looks different every time to me rc=az_view.sendEmail(uniquifier=uniquifier) if rc: self.fail('rc is not None: '+str(rc)) found=self.look_in_mailbox(uniquifier) if not(found): self.fail('message not found') print "done" -- http://mail.python.org/mailman/listinfo/python-list
Re: string to list when the contents is a list
Wes James wrote: > I have been trying to create a list form a string. The string will be > a list (this is the contents will look like a list). i.e. "[]" or > "['a','b']" > > The "[]" is simple since I can just check if value == "[]" then return [] > > But with "['a','b']" I have tried and get: > > a="['a','b']" > > b=a[1:-1].split(',') > > returns > > [ " 'a' "," 'b' " ] > > when I want it to return ['a','b']. > > How can I do this? > > thx, > > -wes I am surprised nobody gave you the simple answer yet that may even work for your situation: b=a[2:-2].split("','") -- http://mail.python.org/mailman/listinfo/python-list
Re: unit testing a routine that sends mail
In article , commander_coder wrote: > The real puzzler for me is that the test reliably fails every third > time. For instance, if I try it six times then it succeeds the first, > second, fourth, and fifth times. I have to say that I cannot > understand this at all but it certainly makes the unit test useless. Just a wild guess here, but maybe there's some DNS server which round-robins three address records for some hostname you're using, one of which is bogus. I've seen that before, and this smells like the right symptoms. -- http://mail.python.org/mailman/listinfo/python-list
Re: string to list when the contents is a list
Wes James wrote: I have been trying to create a list form a string. The string will be a list (this is the contents will look like a list). i.e. "[]" or "['a','b']" The "[]" is simple since I can just check if value == "[]" then return [] But with "['a','b']" I have tried and get: a="['a','b']" b=a[1:-1].split(',') returns [ " 'a' "," 'b' " ] when I want it to return ['a','b']. Just to add to the list of solutions I've seen, letting the built-in csv module do the heavy lifting: >>> s = "['a','b']" >>> import csv >>> no_brackets = s[1:-1] # s.strip(' \t[]') >>> c = csv.reader([no_brackets], quotechar="'") >>> c.next() ['a', 'b'] This also gives you a bit of control regarding how escaping is done, and other knobs & dials to twiddle if you need. Additionally, if you have more than one string to process coming from an iterable source (such as a file), you can just pass that iterator to csv.reader() instead of concocting a one-element list. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: unit testing a routine that sends mail
On Feb 18, 9:55 am, Roy Smith wrote: > Just a wild guess here, but maybe there's some DNS server which > round-robins three address records for some hostname you're using, one of > which is bogus. > > I've seen that before, and this smells like the right symptoms. Everything happens on my laptop, localhost, where I'm developing. I'm sorry; I wasn't sure how much information was too much (or too little) but I should have said that. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python library for working with simple equations
On Thu, Feb 18, 2010 at 4:09 AM, lallous wrote: > Hello > > Is there is any Python library that allow such things: > > Given a string expression as: x + 5 + x * (y + 2), any library that > can develop the equation for example. > Or if we say factor with "x" then it renders the expression with x * > ( rest of expression ). > There could be a functionality where when x,y are given then the > expression can be evaluated. > If there are two expressions, they can be added and the symbols > preserved. > > Does such a thing exist? > > Thanks, > Elias > -- > http://mail.python.org/mailman/listinfo/python-list > >From sage: >>> x = var('x') >>> g = x**2 + x >>> g(x=5) 30 >>> g.factor ... (x + 1)*x Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list
Few questions on SOAP
Dear Group, I was reading on SOA or Service Oriented Architecture for last few days and got some questions. As this is a room for the expert computer scientists, if you can help me solve my queries. As per I read and felt SOA is an architecture, which relies on few basic principles as, the system must be modular, the modules must be distributive, module interfaces must be clearly defined and documented, module that implements a service can be swapped out for another module that offers the same service and interface, service provider modules must be shareable. SOA is an architecture which is now a days governed like XML by W3C. The latest version is SOAP 1.2. SOA is implemented mainly in a client/server environment, where applications have the use of service, thus we can say it is a service- oriented architecture. It is a step towards cloud computing with its sister WSDL and BPM. If both client/server are following the SOA then they can communicate and exchange information without worrying for platform. SOAP is a software implementation of Python,.NET,J2EE based on this principle. SOAPPy is a Python implementation. My questions are: (i) Am I understanding correctly? (ii)Does SOAP has any standard architecture like UML other than the W3C one? (iii) Can I write short programs to check the SOAP using my personal computer as client as well as Server? (iv)Is SOAPpy fine? (v) What is the difference among SOAP, WSDL and BPM. (vi)As SOAP is for communication with a server if I pick a URL and start to communicate with Google/Yahoo would they allow? (vii) Can I design a web based robot/crawler with SOAP? Wishing you a Happy Day Ahead, Best Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list
Re: Static method
Bruno Desthuilliers wrote: I think you broke something somewhere. Assuming you're using Python 2.x (>= 2.3 IIRC), my above code works. ARGH! Forgot the "delayed staticmethod" line -- in effect I called staticmethod twice: @staticmethod def print_internal_date(filename): f = open(filename + 'c', "rb") data = f.read(8) mtime = struct.unpack("'internal_date': lambda x: PYFileInfo.print_internal_date.__get__(x) } # HERE I BROKE IT print_internal_date = staticmethod(print_internal_date) def __init__(self, fname=None): FileInfo.__init__(self,fname) def __setitem__(self, key, value): FileInfo.__setitem__(self, key, value) if key == 'name' and value: self.__get_props(value) def testit(self, fname): self.print_internal_date(fname+'c') PYFileInfo.print_internal_date.__get__(fname+'c') You're right. It works. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to use AWS/PAA nowadays? PyAWS / pyamazon outdated?
This one works for the product API. http://pypi.python.org/pypi/python-amazon-product-api/0.2.1 On Thu, Feb 18, 2010 at 5:09 AM, Snaky Love wrote: > Hi, > > is anybody aware of any updated and / or maintained library for > accessing AWS/PAA with Python? I found the dusty pyamazon and a > derivate of it, pyaws, but both of them do not seem to support request > signatures which must be used by August 15, 2009 to use the Amazon > Services, and generally seem to be a little bit outdated... > > Is there a better way doing signed requests to AWS/PAA nowadays? How > are you doing it? > > I found a good PHP Library here: http://getcloudfusion.com/docs - but > I would like to program stuff with python - is there anything pythonic > that is close to that? > > Thank you very much for your attention, > have a nice day, > Snaky > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Upgrading Py2exe App
I have a Python app which I converted to an EXE (all files separate; single EXE didn't work properly) via py2exe - I plan on distributing this and would like the ability to remotely upgrade the program (for example, via HTTP/HTTPS). Looks like it's not just the EXE that I will need need to replace (DLLs, the library.zip, etc.). What would be the best way to go about doing this? -- http://mail.python.org/mailman/listinfo/python-list
Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.
Steve Howell wrote: > If this is an argument against using anonymous functions, then it is a > quadruple strawman. > > Shipping buggy code is a bad idea, even with named functions. I doubt very much whether I have ever shipped any bug-free code but even if it was fit for purpose when shipped it is quite possible that the software will interact badly with other software that did not exist at the time of shipping. > > Obscuring line numbers is a bad idea, even with named functions. In principle I agree, but where Javascript is concerned compressing the downloaded files is generally a pretty good idea and practicality beats purity. > > Having your customers stay on older versions of your software is a bad > idea, even with named functions. I think that's their decision, not mine. > > Not being able to know which version of software you're customer is > running is a bad idea, even with named functions. > I agree, but getting a complete coherent description out of a customer is not always an easy task. (I'm reading the word 'customer' here to include the case where there is no monetary relationship between the software author and the entity using it, but even when there is I think this still true.) -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: unit testing a routine that sends mail
On Feb 18, 10:27 am, Bruno Desthuilliers wrote: > you could just mock the send_mail > function to test that your app does send the appropriate mail - which is > what you really want to know. That's essentially what I think I am doing. I need to send a relatively complex email, multipart, with both a plain text and html versions of a message, and some attachments. I am worried that the email would fail to get out for some reason (say, attaching the html message fails), so I want to test it. I started out with a simple email, thinking to get unit tests working and then as I add stuff to the email I'd run the tests to see if it broke anything. I could mock the send_mail function by having it print to the screen "mail sent" or I could have a unit test that looked for the mail. I did the first, and it worked fine. I thought to do the second, starting with a unit test checking simply that the message got to the mailbox. But I am unable to check that, as described in the original message. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: unit testing a routine that sends mail
Bruno, I talked to someone who explained to me how what you said gives a way around my difficulty. Please ignore the other reply. I'll do what you said. Thank you; I appreciate your help. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Few questions on SOAP
joy99, 18.02.2010 16:36: > SOA is an architecture which is now a days governed like XML by W3C. > The latest version is SOAP 1.2. SOA has nothing to do with SOAP. SOAP is a (rather bloated) protocol for remote procedure calls. SOA is a system architecture, be it distributed or not. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Constraints on __sub__, __eq__, etc.
It seems intuitive to me that the magic methods for overriding the +, -, <, ==, >, etc. operators should have no sideffects on their operands. Also, that == should be commutative and transitive, that > and < should be transitive, and anti-commutative. Is this intuition written up in a PEP, or assumed to follow from the mathematical meanings? - Andrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.
On Feb 18, 7:50 am, Duncan Booth wrote: > Steve Howell wrote: > > If this is an argument against using anonymous functions, then it is a > > quadruple strawman. > > > Shipping buggy code is a bad idea, even with named functions. > > I doubt very much whether I have ever shipped any bug-free code but > even if it was fit for purpose when shipped it is quite possible that the > software will interact badly with other software that did not exist at the > time of shipping. > > > > > Obscuring line numbers is a bad idea, even with named functions. > > In principle I agree, but where Javascript is concerned compressing the > downloaded files is generally a pretty good idea and practicality beats > purity. > > > > > Having your customers stay on older versions of your software is a bad > > idea, even with named functions. > > I think that's their decision, not mine. > > > > > Not being able to know which version of software you're customer is > > running is a bad idea, even with named functions. > mpr > I agree, but getting a complete coherent description out of a customer is > not always an easy task. (I'm reading the word 'customer' here to include > the case where there is no monetary relationship between the software > author and the entity using it, but even when there is I think this still > true.) > Just to be clear, I'm not saying it's unforgivable to occasionally ship software with bugs. It happens. Compressing Javascript is sometimes necessary, but I believe that often mangles named functions too. To the the extent that your customer is running old software and cannot always coherently describe tracebacks over a telephone, that problem can be solved in the software itself, assuming an Internet connection. The software can capture the traceback and report back to a server with the version number. So, much of the argument against anonymous functions presented so far is really orthogonal to whether functions are named or not. Circling back to the original topic, Ruby blocks, I think there is a misconception about how blocks are often used in Ruby. Usually Ruby blocks are inlined into a function and execute within that function. Example: def print_numbers() [1, 2, 3, 4, 5, 6].map { |n| [n * n, n * n * n] }.reject { |square, cube| square == 25 || cube == 64 }.map { |square, cube| cube }.each { |n| puts n raise 'problem here' } end print_numbers() The bug that I inserted into the "each" block gets reported in the traceback: foo.rb:10:in `print_numbers': problem here (RuntimeError) from foo.rb:2:in `each' from foo.rb:2:in `print_numbers' from foo.rb:14 (I do prefer Python tracebacks BTW, but that is again orthogonal to blocks vs. named functions.) The blocks of code in the above Ruby code are somewhat analogous to blocks of code in Python that happen within certain control structures, such as "if," "while," "with," etc. The extra expressiveness of Ruby comes from the fact that you can act on those blocks with your own method. Of course, there is always a tradeoff between expressiveness and simplicity. I know the person who gave the talk about Ruby vs. Python, and trust me, nine times out of ten, he prefers Python's simplicity to Ruby's expressiveness. But he likes blocks. I'm in the same boat. I use Python a lot, Ruby less so, but when I'm in Ruby-land, I actually enjoy the expressiveness of blocks. They're not particularly dangerous, and they allow you to express certain sequential operations tersely and sequentially. The contrived code below maps numbers to squares and cubes, then rejects a couple tuples, then maps back to cubes, then prints each of the cubes. def print_numbers() [1, 2, 3, 4, 5, 6].map { |n| [n * n, n * n * n] }.reject { |square, cube| square == 25 || cube == 64 }.map { |square, cube| cube }.each { |n| puts n } end IMHO there is no reason that I should have to name the content of each of those four blocks of code, nor should I have to introduce the "lambda" keyword. I don't have a less contrived example handy, but the techniques above apply often when you are filtering and massaging data. -- http://mail.python.org/mailman/listinfo/python-list
Re: Constraints on __sub__, __eq__, etc.
On Thu, Feb 18, 2010 at 8:19 AM, Andrey Fedorov wrote: > It seems intuitive to me that the magic methods for overriding the +, -, <, > ==, >, etc. operators should have no sideffects on their operands. Also, > that == should be commutative and transitive, that > and < should be > transitive, and anti-commutative. > > Is this intuition written up in a PEP, or assumed to follow from the > mathematical meanings? > It may be intuitive to you, but its not true, written down anywhere, nor assumed by the language, and the mathematical meaning of the operators doesn't matter to Python. Python purposefully does not enforce anything for these methods. Consider: >>> class Test(object): ... def __init__(self, v): ... self.v = v ... def __add__(self, other): ... self.v = self.v + other ... return "Ow!" ... >>> t = Test(5) >>> t + 2 'Ow!' >>> t.v 7 It not only alters an operand, but its not even returning a meaningful result. This can be abused, but is also useful for certain uses. --S -- http://mail.python.org/mailman/listinfo/python-list
Re: Constraints on __sub__, __eq__, etc.
> > It may be intuitive to you, but its not true, written down anywhere, nor > assumed by the language, and the mathematical meaning of the operators > doesn't matter to Python. Python purposefully does not enforce anything for > these methods. Right, so neither is anything in PEP-8, but it's still considered "good practice". I'm running across examples like you gave (__sub__ having a side-effect on the left-hand operand) in some code, and am trying to find concrete justification for avoiding it. - Andrey On Thu, Feb 18, 2010 at 11:28 AM, Stephen Hansen wrote: > On Thu, Feb 18, 2010 at 8:19 AM, Andrey Fedorov wrote: > >> It seems intuitive to me that the magic methods for overriding the +, -, >> <, ==, >, etc. operators should have no sideffects on their operands. Also, >> that == should be commutative and transitive, that > and < should be >> transitive, and anti-commutative. >> >> Is this intuition written up in a PEP, or assumed to follow from the >> mathematical meanings? >> > > It may be intuitive to you, but its not true, written down anywhere, nor > assumed by the language, and the mathematical meaning of the operators > doesn't matter to Python. Python purposefully does not enforce anything for > these methods. Consider: > > >>> class Test(object): > ... def __init__(self, v): > ... self.v = v > ... def __add__(self, other): > ... self.v = self.v + other > ... return "Ow!" > ... > >>> t = Test(5) > >>> t + 2 > 'Ow!' > >>> t.v > 7 > > It not only alters an operand, but its not even returning a meaningful > result. This can be abused, but is also useful for certain uses. > > --S > -- http://mail.python.org/mailman/listinfo/python-list
Re: Constraints on __sub__, __eq__, etc.
On Thu, Feb 18, 2010 at 11:19 AM, Andrey Fedorov wrote: > It seems intuitive to me that the magic methods for overriding the +, -, <, > ==, >, etc. operators should have no sideffects on their operands. Also, > that == should be commutative and transitive, that > and < should be > transitive, and anti-commutative. > Is this intuition written up in a PEP, or assumed to follow from the > mathematical meanings? > - Andrey There are no assumptions about anything. You can do whatever you want when you implement the functions yourself. For the numeric types, they follow the intuitive meanings but that's not necessary for user defined types. Of course, your users will get very confused if a == b but b != a. Or if a == b and a != b at the same time. > -- > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Is automatic reload of a module available in Python?
Arnaud Delobelle wrote: Here is a very simple way to improve what you do, which won't require you to change the way you work or to learn a new paradigm: Instead of testing your functions interactively, put your testing code in a file, e.g. 'program_tests.py'. Your can then type python program_tests.py at the shell interactive prompt. To perform the tests again, just re-execute that file. If your tests are divided into different units, you can put these in functions: def test_frobz(): #testing code for frobzation of klops def test_frizz(): #testing code for frizzment of frobzied klops # etc.. So if you want to keep doing interactive tests, you can import program_tests and call whichever testing functions you want. You may even have arguments to those functions to test them with different parameters. I know some people will point at more 'pro' ways of testing but this has the merit of being very straightforward. Then when you move on to more sophisticated techniques, I think you will understand better the motivations behind them. It took me some time to cotton on to exactly what you were saying, but once I grasped it and tried it out, I found it very effective and time-saving. Thank you very much Arnaud. -- Chandra -- http://mail.python.org/mailman/listinfo/python-list
Re: Constraints on __sub__, __eq__, etc.
On 2010-02-18 10:19 AM, Andrey Fedorov wrote: It seems intuitive to me that the magic methods for overriding the +, -, <, ==, >, etc. operators should have no sideffects on their operands. Also, that == should be commutative and transitive, that > and < should be transitive, and anti-commutative. Is this intuition written up in a PEP, or assumed to follow from the mathematical meanings? Some of it is covered in the reference manual. E.g. http://docs.python.org/reference/datamodel.html#object.__lt__ -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Is automatic reload of a module available in Python?
2010/2/17 Arnaud Delobelle : > I know some people will point at more 'pro' ways of testing but this has > the merit of being very straightforward. Then when you move on to more > sophisticated techniques, I think you will understand better the > motivations behind them. Oh, I don't know. I like to think I'm fairly "pro" when it comes to TDD, and this is exactly what I do - a unit test module run from the shell. -- Cheers, Simon B. -- http://mail.python.org/mailman/listinfo/python-list
Re: Constraints on __sub__, __eq__, etc.
Andrey Fedorov wrote: It may be intuitive to you, but its not true, written down anywhere, nor assumed by the language, and the mathematical meaning of the operators doesn't matter to Python. Python purposefully does not enforce anything for these methods. Right, so neither is anything in PEP-8, but it's still considered "good practice". I'm running across examples like you gave (__sub__ having a side-effect on the left-hand operand) in some code, and am trying to find concrete justification for avoiding it. - Andrey This is all about communication. Python allows you to call a cat a dog. You'll confuse everyone if you do so, but you can do it. If your '-' operator is doing something else than returning the sub result, it's your call, but do not expect people to get it easily. You'd better be good at writting the documentation :-) JM -- http://mail.python.org/mailman/listinfo/python-list
Why this doesn't work?
Sorry to bother everyone again, but I have this problem bugging me: #!/usr/bin/python -i class Foo(object): def nostat(self,val): print val nostat.__orig_get__ = nostat.__get__ @staticmethod def nostatget(*args, **kwargs): print 'args:', args, 'kwargs:', kwargs nostat.__orig_get__(*args, **kwargs) nostat.__get__ = nostatget setattr(nostat,'__get__',nostatget) f = Foo() f.nostat('a') print f.nostat.__get__ is f.nostat.__orig_get__ This produces: a False I expected to see 'nostatget' output: nostat.__get__ = nostatget obviously failed to replace this function's __get__ method. The question is why? Isn't __get__ a normal attribute of a function nostat? This is made so much weirder that nostat.__get__ is no longer original __get__, so it looks like it should have been replaced, but if so, why nostatget doesn't get called? Regards, mk -- http://mail.python.org/mailman/listinfo/python-list
Re: Few questions on SOAP
On 18 February 2010 15:36, joy99 wrote: > (iv) Is SOAPpy fine? AFAIK, SOAPpy is unsupported, and a bit on the stale side. Those poor souls forced to make SOAP calls with Python seem to be using Suds mostly these days,. -- Cheers, Simon B. -- http://mail.python.org/mailman/listinfo/python-list
What happened to pyjamas?
Does anyone know what happened to pyjs.org ? Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list
Re: What happened to pyjamas?
Daniel Fetchinson wrote: Does anyone know what happened to pyjs.org ? Cheers, Daniel According to google cache it was fine 13/02/2010 and it's down according to this. http://downforeveryoneorjustme.com/pyjs.org HTH. Mark Lawrence -- http://mail.python.org/mailman/listinfo/python-list
Re: What happened to pyjamas?
it's working for me. On Thu, Feb 18, 2010 at 1:16 PM, Daniel Fetchinson < fetchin...@googlemail.com> wrote: > Does anyone know what happened to pyjs.org ? > > Cheers, > Daniel > > > -- > Psss, psss, put it down! - http://www.cafepress.com/putitdown > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: What happened to pyjamas?
>> Does anyone know what happened to pyjs.org ? > > it's working for me. That's odd, it's unpingable for me from both Europe and the US, ping says unknown host. Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list
How secure are temp files created via tempfile.TemporaryFile()?
I'm doing a code review of an application that occassionally writes blocks of secure data to temp files created with tempfile.TemporaryFile( delete=True ). How secure are temp files created via tempfile.TemporaryFile( delete=True )? Are there OS specific nuances I should be concerned about regarding use of this function on Windows (XP or higher) or Linux? Thank you, Malcolm -- http://mail.python.org/mailman/listinfo/python-list
Re: What happened to pyjamas?
Down from here (NH, US). S On Feb 18, 2010, at 1:44 PM, Chris Colbert wrote: > -- http://mail.python.org/mailman/listinfo/python-list
Re: string to list when the contents is a list
On Thu, Feb 18, 2010 at 8:18 AM, Tim Chase wrote: > Wes James wrote: > > Just to add to the list of solutions I've seen, letting the built-in csv > module do the heavy lifting: > > >>> s = "['a','b']" > >>> import csv > >>> no_brackets = s[1:-1] # s.strip(' \t[]') > >>> c = csv.reader([no_brackets], quotechar="'") > >>> c.next() > ['a', 'b'] > > This also gives you a bit of control regarding how escaping is done, and > other knobs & dials to twiddle if you need. Additionally, if you have more > than one string to process coming from an iterable source (such as a file), > you can just pass that iterator to csv.reader() instead of concocting a > one-element list. Thx, I think this will work for what I want. -wes -- http://mail.python.org/mailman/listinfo/python-list
Re: How secure are temp files created via tempfile.TemporaryFile()?
pyt...@bdurham.com wrote: I'm doing a code review of an application that occassionally writes blocks of secure data to temp files created with tempfile.TemporaryFile( delete=True ). How secure are temp files created via tempfile.TemporaryFile( delete=True )? Are there OS specific nuances I should be concerned about regarding use of this function on Windows (XP or higher) or Linux? Well, the contents of temp files aren't encrypted, if that's what you're asking, so if you're writing unencrypted data to a temp file then other applications could read it. -- http://mail.python.org/mailman/listinfo/python-list
Re: The future of "frozen" types as the number of CPU cores increases
John Nagle wrote: I look at this as Python's answer to multicore CPUs and "Go". On that note, I went to a talk at Stanford yesterday by one of the designers of Intel's Nelahem core. The four-core, eight thread version is out now. The six-core, twelve thread version is working; the speaker has one in his lab. The eight-core, sixteen thread version is some months away. This isn't an expensive CPU; this is Intel's "converged" mainstream product. (Although there will be a whole range of "economy" and "performance" versions, all with the same core but with some stuff turned off.) Python isn't ready for this. Not with the GIL. Multiple processes are not the answer. That means loading multiple copies of the same code into different areas of memory. The cache miss rate goes up accordingly. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: string to list when the contents is a list
On Thu, Feb 18, 2010 at 12:32 PM, Wes James wrote: > On Thu, Feb 18, 2010 at 8:18 AM, Tim Chase > wrote: >> Wes James wrote: > > >> >> Just to add to the list of solutions I've seen, letting the built-in csv >> module do the heavy lifting: >> >> >>> s = "['a','b']" >> >>> import csv >> >>> no_brackets = s[1:-1] # s.strip(' \t[]') >> >>> c = csv.reader([no_brackets], quotechar="'") >> >>> c.next() >> ['a', 'b'] Hmm. When I put csv.reader in a class: import csv class IS_LIST(): def __init__(self, format='', error_message='must be a list!'): self.format = format self.error_message = error_message def __call__(self, value): try: if value=='[]' or value=='': value=[] else: no_brackets = value[1:-1] # s.strip(' \t[]') c = csv.reader([no_brackets], quotechar="'") value=c.next() return (value, None) except: return (value, self.error_message) def formatter(self, value): return value I get an error (when I take the "try" out): AttributeError: 'function' object has no attribute 'reader' Why? -wes -- http://mail.python.org/mailman/listinfo/python-list
Re: Few questions on SOAP
Your question is borderline if not out of topic in this group. I will make a few comments though. On Feb 18, 3:36 pm, joy99 wrote: > Dear Group, > > I was reading on SOA or Service Oriented Architecture for last few > days and got some questions. As this is a room for the expert computer > scientists, if you can help me solve my queries. > > As per I read and felt SOA is an architecture, which relies on few > basic principles as, > the system must be modular, the modules must be distributive, module > interfaces must be clearly defined and documented, module that > implements a service can be swapped out for another module that offers > the same service and interface, service provider modules must be > shareable. > SOA is an architecture which is now a days governed like XML by W3C. SOA is an architecture which can be implemented using any of a number of different middleware choices. It is not governed or controlled by anyone. You are mixing it up with SOAP, which is a web services technology, currently governed by W3C. For example, I tend to use SOA implemented using REST (Representational State Transfer). All of these technologies are somewhat explained in wikipedia. You probably want to start there. > The latest version is SOAP 1.2. This is the latest of SOAP, not SOA. > SOA is implemented mainly in a client/server environment, where > applications have the use of service, thus we can say it is a service- > oriented architecture. > It is a step towards cloud computing with its sister WSDL and BPM. A SOAP web service is described using WSDL. BPM is not really connected with them, but is probably more connected with SOA as an architecture. BPM can be implemented using SOAP/WSDL web services, as can SOA. > > If both client/server are following the SOA then they can communicate > and exchange information without worrying for platform. You mean SOAP here. In real life, there are interoperability issues. So various implementations of SOAP will need a bit of work to actually work together. SOA is an architecture and does not help exchanging information in a direct way. > > SOAP is a software implementation of Python,.NET,J2EE based on this > principle. SOAP is a web services protocol that was created before the concept of SOA was developed enough for use and is largely independent of it. SOAP has been implemented in many different languages, like Java, .Net, C/C++, Python, etc. > SOAPPy is a Python implementation. SOAP implementations in Python are not of the best quality (in my opinion, there are better ways to achieve the ends needed). SOAPpy is currently a bit out of date. The better ones are ZSI (for client and server) and suds (for clients). I guess I have already answered some of your questions. > > My questions are: > (i) Am I understanding correctly? See above. > (ii) Does SOAP has any standard architecture like UML other than the > W3C one? Depends on what you mean by architecture. SOAP is a standard. UML is a way of modelling software and can be used to model web services, which can then be implemented in SOAP. > (iii) Can I write short programs to check the SOAP using my personal > computer as client as well as Server? Yes. Try ZSI examples (http://pywebsvcs.sourceforge.net/zsi.html). > (iv) Is SOAPpy fine? See above. > (v) What is the difference among SOAP, WSDL and BPM. SOAP standardise the communication between client and server. WSDL describes the methods provided by a server to be consumed by a client (interfaces in Java language). BPM is a management method to improve business and is not a software protocol but an approach. > (vi) As SOAP is for communication with a server if I pick a URL and > start to communicate with Google/Yahoo would they allow? To be able to talk SOAP to a server, it must understand SOAP. Some of those companies do provide SOAP web service access, but their standard search pages are accessible in pure HTTP (the way you browser uses them). To access a SOAP service you need to know the functions exported by it, which are usually defined in a WSDL document. > (vii) Can I design a web based robot/crawler with SOAP? No. Web pages are accessed using simple HTTP. SOAP is usually deployed on top of HTTP, but most web pages are not communicated using SOAP. You can search for Python web crawling; there are a lot of examples on the web. Regards, k -- http://mail.python.org/mailman/listinfo/python-list
Re: How secure are temp files created via tempfile.TemporaryFile()?
MRAB, > Well, the contents of temp files aren't encrypted, if that's what you're > asking I understand the contents of temp files aren't encrypted. > if you're writing unencrypted data to a temp file then other applications > could read it. That's my concern - can other applications really read my temp files created with tempfile.TemporaryFile( delete=True )? I don't think so because: 1. These files appear to be exclusively locked by my process, eg. no other processes can read or write to these temp files except the process that created these files. 2. As soon as my process terminates (voluntarily or involuntarily), the temp file gets deleted. But I want to make sure. Thanks, Mal -- http://mail.python.org/mailman/listinfo/python-list
Re: string to list when the contents is a list
import csv class IS_LIST(): def __init__(self, format='', error_message='must be a list!'): self.format = format self.error_message = error_message def __call__(self, value): try: if value=='[]' or value=='': value=[] else: no_brackets = value[1:-1] # s.strip(' \t[]') c = csv.reader([no_brackets], quotechar="'") value=c.next() return (value, None) except: return (value, self.error_message) def formatter(self, value): return value I get an error (when I take the "try" out): AttributeError: 'function' object has no attribute 'reader' A couple ideas occur to me: 1) you haven't copy/pasted the exact (or entirety of the) code, and something you're doing is shadowing the "csv" module 2) are you using Python 2.x or 3.x? I don't know if the csv module has changed in 3.x but it should work in 2.x The first thing to check would be to pull up a raw python prompt and see if your csv module has the expected reader: >>> import csv >>> csv.reader If not, something likely changed in 3.x and you'd have to inspect the docs to see what happened to the reader. If you get the above evidence of an existing reader, then you're likely shadowing it. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: unit testing a routine that sends mail
commander_coder wrote: > I have a routine that sends an email (this is how a Django view > notifies me that an event has happened). I want to unit test that > routine. Are you opening SMTP and POP3 sockets?? If you are not developing that layer itself, just use Django's built- in mock system. Here's my favorite assertion for it: def assert_mail(self, funk): from django.core import mail previous_mails = len(mail.outbox) funk() mails = mail.outbox[ previous_mails : ] assert [] != mails, 'the called block should produce emails' if len(mails) == 1: return mails[0] return mails You call it a little bit like this: missive = self.assert_mail( lambda: mark_order_as_shipped(order) ) Then you can use assert_contains on the missive.body, to see what mail got generated. That's what you are actually developing, so your tests should skip any irrelevant layers, and only test the layers where you yourself might add bugs. -- Phlip http://penbird.tumblr.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: What happened to pyjamas?
ah, yep, i was viewing the page chrome had cached. It's down for me too. I lose the trace in Washington: 3 G3-0-873.TAMPFL-LCR-08.verizon-gni.net (130.81.110.222) 14.399 ms 17.917 ms 18.040 ms 4 so-6-1-0-0.TPA01-BB-RTR2.verizon-gni.net (130.81.29.242) 18.666 ms 18.890 ms 19.232 ms 5 ge-1-0-0-0.ATL01-BB-RTR2.verizon-gni.net (130.81.17.48) 36.460 ms 38.546 ms 38.707 ms 6 0.so-2-2-0.XT2.ATL5.ALTER.NET (152.63.86.73) 41.357 ms 33.709 ms 35.851 ms 7 * 0.so-7-0-0.XT2.ATL4.ALTER.NET (152.63.86.109) 34.522 ms 37.320 ms 8 0.xe-10-1-0.BR3.ATL4.ALTER.NET (152.63.82.13) 37.558 ms 32.756 ms 35.533 ms 9 xe-11-2-0.edge4.Atlanta2.Level3.net (4.68.62.17) 37.715 ms 100.222 ms 102.317 ms 10 ae-71-52.ebr1.Atlanta2.Level3.net (4.68.103.60) 40.227 ms 34.315 ms 36.647 ms 11 ae-73-70.ebr3.Atlanta2.Level3.net (4.69.138.20) 42.695 ms 39.589 ms 41.666 ms 12 ae-2-2.ebr1.Washington1.Level3.net (4.69.132.86) 52.152 ms 54.256 ms 54.540 ms 13 ae-61-61.csw1.Washington1.Level3.net (4.69.134.130) 61.982 ms 62.316 ms 62.460 ms 14 ae-14-69.car4.Washington1.Level3.net (4.68.17.6) 55.660 ms 55.645 ms 56.943 ms 15 CO-LOCATION.car4.Washington1.Level3.net (4.79.170.254) 59.423 ms 58.914 ms 50.872 ms 16 * * * 17 * * * 18 * * * 19 * * * 20 * * * 21 * * * 22 * * * 23 * * * 24 * * * 25 * * * 26 * * * 27 * * * 28 * * * 29 * * * 30 * * * On Thu, Feb 18, 2010 at 1:58 PM, sstein...@gmail.com wrote: > Down from here (NH, US). > > S > > > On Feb 18, 2010, at 1:44 PM, Chris Colbert wrote: > > > > > -- http://mail.python.org/mailman/listinfo/python-list
Why do full path names to module show up multiple times in .PYC files?
I just happened to look at a compiled Python 2.6.4 .PYC file in an editor and noticed that the full path and file name of the compiled module showed up in my .PYC file at least 10 different times. Is there a reason for full vs. relative path names and why does the module name need to be duplicated so many times? It appears that .PYO files only use a filename (without a path). I assume the only way for us to suppress path names (which may have confidential client names) in our compiled distributions is to use .PYO vs. .PYC files? Is this correct? Thanks, Malcolm -- http://mail.python.org/mailman/listinfo/python-list
Re: string to list when the contents is a list
On Thu, Feb 18, 2010 at 2:56 PM, Wes James wrote: > > I get an error (when I take the "try" out): > > AttributeError: 'function' object has no attribute 'reader' > You have a function called "csv" that's defined after the import csv statement is executed. That function has no attribute 'reader", so you get the error. By the way, don't use a bare except- it's bad form because it hides any other problems you have. -- http://mail.python.org/mailman/listinfo/python-list
Re: Few questions on SOAP
Muhammad Alkarouri wrote: Your question is borderline if not out of topic in this group. I will make a few comments though. This might be a Python group, but threads often drift way off topic, which added to the language itself make this a great group to read. If you don't like the way a thread goes, you can always skip it. On Feb 18, 3:36 pm, joy99 wrote: Dear Group, I was reading on SOA or Service Oriented Architecture for last few days and got some questions. As this is a room for the expert computer scientists, if you can help me solve my queries. As per I read and felt SOA is an architecture, which relies on few basic principles as, the system must be modular, the modules must be distributive, module interfaces must be clearly defined and documented, module that implements a service can be swapped out for another module that offers the same service and interface, service provider modules must be shareable. SOA is an architecture which is now a days governed like XML by W3C. SOA is an architecture which can be implemented using any of a number of different middleware choices. It is not governed or controlled by anyone. You are mixing it up with SOAP, which is a web services technology, currently governed by W3C. For example, I tend to use SOA implemented using REST (Representational State Transfer). All of these technologies are somewhat explained in wikipedia. You probably want to start there. The latest version is SOAP 1.2. This is the latest of SOAP, not SOA. SOA is implemented mainly in a client/server environment, where applications have the use of service, thus we can say it is a service- oriented architecture. It is a step towards cloud computing with its sister WSDL and BPM. A SOAP web service is described using WSDL. BPM is not really connected with them, but is probably more connected with SOA as an architecture. BPM can be implemented using SOAP/WSDL web services, as can SOA. If both client/server are following the SOA then they can communicate and exchange information without worrying for platform. You mean SOAP here. In real life, there are interoperability issues. So various implementations of SOAP will need a bit of work to actually work together. SOA is an architecture and does not help exchanging information in a direct way. SOAP is a software implementation of Python,.NET,J2EE based on this principle. SOAP is a web services protocol that was created before the concept of SOA was developed enough for use and is largely independent of it. SOAP has been implemented in many different languages, like Java, .Net, C/C++, Python, etc. SOAPPy is a Python implementation. SOAP implementations in Python are not of the best quality (in my opinion, there are better ways to achieve the ends needed). SOAPpy is currently a bit out of date. The better ones are ZSI (for client and server) and suds (for clients). I guess I have already answered some of your questions. My questions are: (i) Am I understanding correctly? See above. (ii)Does SOAP has any standard architecture like UML other than the W3C one? Depends on what you mean by architecture. SOAP is a standard. UML is a way of modelling software and can be used to model web services, which can then be implemented in SOAP. (iii) Can I write short programs to check the SOAP using my personal computer as client as well as Server? Yes. Try ZSI examples (http://pywebsvcs.sourceforge.net/zsi.html). (iv)Is SOAPpy fine? See above. (v) What is the difference among SOAP, WSDL and BPM. SOAP standardise the communication between client and server. WSDL describes the methods provided by a server to be consumed by a client (interfaces in Java language). BPM is a management method to improve business and is not a software protocol but an approach. (vi)As SOAP is for communication with a server if I pick a URL and start to communicate with Google/Yahoo would they allow? To be able to talk SOAP to a server, it must understand SOAP. Some of those companies do provide SOAP web service access, but their standard search pages are accessible in pure HTTP (the way you browser uses them). To access a SOAP service you need to know the functions exported by it, which are usually defined in a WSDL document. (vii) Can I design a web based robot/crawler with SOAP? No. Web pages are accessed using simple HTTP. SOAP is usually deployed on top of HTTP, but most web pages are not communicated using SOAP. You can search for Python web crawling; there are a lot of examples on the web. Regards, k -- http://mail.python.org/mailman/listinfo/python-list
Re: The future of "frozen" types as the number of CPU cores increases
John Nagle wrote: John Nagle wrote: I look at this as Python's answer to multicore CPUs and "Go". On that note, I went to a talk at Stanford yesterday by one of the designers of Intel's Nelahem core. The four-core, eight thread version is out now. The six-core, twelve thread version is working; the speaker has one in his lab. The eight-core, sixteen thread version is some months away. This isn't an expensive CPU; this is Intel's "converged" mainstream product. (Although there will be a whole range of "economy" and "performance" versions, all with the same core but with some stuff turned off.) Python isn't ready for this. Not with the GIL. Multiple processes are not the answer. That means loading multiple copies of the same code into different areas of memory. The cache miss rate goes up accordingly. John Nagle Will the new GIL in 3.2 make this workable? It would still be one thread at a time, though, wouldn't it. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Python won't run
Quickly, I have a Mac Intel with Windows XP installed. Tried installing Python 2.6.4 from the binary and also ActivePython 2.6.4.10. Both installations acted the same. There seemed to be no problems during installation (used default options), but when I try to run Python I get an error message: "This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem." Of course I searched on that error and it seems to be related to a MS library. In a few different places it was recommended to install the MS Visual Studio redistributable package, which I did with no change in outcome. I really have no idea what to do. Any help is appreciated. Thanks, Cory -- http://mail.python.org/mailman/listinfo/python-list
Re: string to list when the contents is a list
In article , Wes James wrote: > >try: >if value=3D=3D'[]' or value=3D=3D'': > value=3D[] >else: > no_brackets =3D value[1:-1] # s.strip(' \t[]') > c =3D csv.reader([no_brackets], quotechar=3D"'") > value=3Dc.next() >return (value, None) >except: >return (value, self.error_message) Two important points: * Don't use bare except: clauses * Put less code in the try: clause to make it easier to track down problems -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" -- http://mail.python.org/mailman/listinfo/python-list
LISA 2010 CFP
Hello All, The USENIX Large Installation System Administration Conference is now accepting paper proposals. If you are interested in submitting a paper, please check out this blog post about submitting a paper (http://www.usenix.org/events/lisa10/cfp/), or feel free to contact me directly if you think you might have a topic to present. Thank you, Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing a command from within python using the subprocess module
In article <5yudnafyso8houtwnz2dnuvz_tidn...@westnet.com.au>, R (Chandra) Chandrasekhar wrote: > >--- >import subprocess > >width = 5 >height = 30 >colors = ['#abcdef]', '#456789'] >filename = "/tmp/image.png" > ># I want to get the equivalent of variable interpolation in Perl ># so that the command ># ># convert -size 5x30 gradient:#abcdef-#456789 /tmp/image.png Here's the equivalent of Peter's cute code in simpler form: cmd = [ 'convert', '-size', '%sx%s' % (width, height), 'gradient:%s-%s' % tuple(colors), # above could also be: 'gradient:%s-%s' % (colors[0], colors[1]), filename, ] subprocess.Popen(cmd) -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" -- http://mail.python.org/mailman/listinfo/python-list
Re: Why this doesn't work?
On Thu, 18 Feb 2010 18:28:44 +0100, mk wrote: > nostat.__orig_get__ = nostat.__get__ I should point out that leading-and-trailing-double-underscore names are reserved for use by the language. It's unlikely that Python will introduce a special method named __orig_get__, and in truth the prohibition against using such names is honoured more in the breach than in the observance (e.g. people often use metadata names like __version__, __author__, etc.). But just be aware that you are in technical breach of Python best practices, and should consider renaming it as _orig_get or __orig_get. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: The future of "frozen" types as the number of CPU cores increases
On Thu, Feb 18, 2010 at 11:58 AM, John Nagle wrote: > On that note, I went to a talk at Stanford yesterday by one of the > designers of Intel's Nelahem core. The four-core, eight thread > version is out now. The six-core, twelve thread version is working; > the speaker has one in his lab. The eight-core, sixteen thread version > is some months away. This isn't an expensive CPU; this is Intel's > "converged" mainstream product. (Although there will be a whole range > of "economy" and "performance" versions, all with the same core but > with some stuff turned off.) > > Python isn't ready for this. Not with the GIL. Is any language, save perhaps Erlang, really ready for it? Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: The future of "frozen" types as the number of CPU cores increases
On Thu, 18 Feb 2010 11:58:32 -0800, John Nagle wrote: > John Nagle wrote: > >>I look at this as Python's answer to multicore CPUs and "Go". > > On that note, I went to a talk at Stanford yesterday by one of the > designers of Intel's Nelahem core. The four-core, eight thread version > is out now. [...] > > Python isn't ready for this. Not with the GIL. Pardon me, but Python is perfectly ready for this. Why aren't you using Jython or IronPython, if the GIL is such a drag on your use-case? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.
On Thu, 18 Feb 2010 06:15:20 -0800, Steve Howell wrote: > On Feb 18, 1:23 am, Duncan Booth wrote: >> Jonathan Gardner wrote: >> > On Feb 17, 12:02 am, Lawrence D'Oliveiro > > central.gen.new_zealand> wrote: >> >> In message >> >> <8ca440b2-6094-4b35-80c5-81d000517...@v20g2000prb.googlegroups.com>, >> >> >> Jonathan Gardner wrote: >> >> > I used to think anonymous functions (AKA blocks, etc...) would be >> >> > a nice feature for Python. >> >> >> > Then I looked at a stack trace from a different programming >> >> > language with lots of anonymous functions. (I believe it was >> >> > perl.) >> >> >> Didn’t it have source line numbers in it? >> >> >> What more do you need? >> >> > I don't know, but I tend to find the name of the function I called to >> > be useful. It's much more memorable than line numbers, particularly >> > when line numbers keep changing. >> >> > I doubt it's just me, though. >> >> Some problems with using just line numbers to track errors: >> >> In any language it isn't much use if you get a bug report from a >> shipped program that says there was an error on line 793 but no report >> of exactly which version of the shipped code was being run. >> >> Microsoft love telling you the line number: if IE gets a Javascript >> error it reports line number but not filename, so you have to guess >> which of the HTML page or one of many included files actually had the >> error. Plus the line number that is reported is often slightly off. >> >> Javascript in particular is often sent to the browser compressed then >> uncompressed and eval'd. That makes line numbers completely useless for >> tracking down bugs as you'll always get the line number of the eval. >> Also the way functions are defined in Javascript means you'll often >> have almost every function listed in a backtrace as 'Anonymous'. > > If this is an argument against using anonymous functions, then it is a > quadruple strawman. There really ought to be a special level of Hell for people who misuse "strawman" to mean "a weak or invalid argument" instead of what it actually means, which is a weak or invalid argument NOT HELD by your opponent, which you (generic you) made up specifically for the sake of shooting down. If you actually read what Duncan says, he prefixes his response with: "Some problems with using just line numbers to track errors". Duncan's post is an argument against relying on line numbers as your main, or only, source of information about the location of bugs in Javascript. In fact, this post is remarkable for the sheer number of actual strawman arguments that you (Steve Howell) use: > Shipping buggy code is a bad idea, even with named functions. Strawman #1: nobody said that shipping buggy code was a good idea, with or without named functions. But shipping buggy code *happens*, no matter how careful you are, so you need to expect bug reports back from users. (And they will be *hard to find* bugs, because if they were easy to find you would have found them in your own testing before shipping.) > Obscuring line numbers is a bad idea, even with named functions. Strawman #2: nobody said that obscuring line numbers was a good idea. But apparently compressing Javascript is valuable for other reasons, and obscuring the line numbers is the side-effect of doing so. And even knowing the line numbers is not necessarily useful, because many bugs aren't due to the line that raises the stack trace. Just because you know the line which failed doesn't mean you know how to fix the bug. > Having your customers stay on older versions of your software is a bad > idea, even with named functions. Strawman #3: nobody said that staying on older versions is a good idea. But sometimes it happens whether you like it or not. (Although I'd like to point out that from the end user's perspective, sometimes we don't want your stinkin' new version with all the anti- features and pessimations and will stick to the old version for as long as possible. If you don't like it, then think a bit harder before adding anti-features like fragile, easily-corrupted databases which perform really, really badly when your home directory is mounted over the network. I'm talking to you, Firefox developers.) And it doesn't really matter: you either end-of-life the old version, in which case you don't need to do anything about the bug report except say "upgrade", or you decide to continue support, in which case it doesn't matter whether the bug is reported for an old version or the latest version, you still need to fix it. > Not being able to know which version of software you're customer is > running is a bad idea, even with named functions. Strawman #4. See the pattern? When you attack a position the other guy hasn't taken, that's a strawman. When you make a weak argument, it's just a weak argument. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
How to make an empty generator?
This has to be a stupid question, but :) I have some generators that do stuff, then start yielding results. On occasion, I don't want them to yield anything ever-- they're only really "generators" because I want to call them /as/ a generator as part of a generalized system. The only way I can figure out how to make an empty generator is: def gen(): # do my one-time processing here return yield Is there a better way? The return/yield just makes me flinch slightly. I tried just raising StopIteration at the end, but of course that didn't work. TIA, --S -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with lambda
On Feb 18, 4:28 am, lallous wrote: > > f = [lambda x: x ** n for n in xrange(2, 5)] This is (pretty much) what the above code does. >>> f = [] >>> n = 2 >>> f.append(lambda x: x**n) >>> n = 3 >>> f.append(lambda x: x**n) >>> n = 4 >>> f.append(lambda x: x**n) >>> n = 5 >>> f.append(lambda x: x**n) Now, when you call f[0], you are calling "lambda x: x**n". What is "n"? You need some way of creating a new namespace and a new variable pointing to what "n" was for that iteration. Python doesn't create a namespace for every iteration like some languages may. You have to be more explicit about that. After all, what would you expect the following code to do? >>> n = 5 >>> f = [lambda x: x**n for i in range(2,5)] >>> n = 2 >>> f[0][5] Or, just call a function that will have a new namespace *for every call* and generate a function within each execution of that function (like make_power does). -- http://mail.python.org/mailman/listinfo/python-list
Re: Few questions on SOAP
On 19 February 2010 08:07, Mark Lawrence wrote: > Muhammad Alkarouri wrote: >> >> Your question is borderline if not out of topic in this group. I will >> make a few comments though. > > This might be a Python group, but threads often drift way off topic, which > added to the language itself make this a great group to read. If you don't > like the way a thread goes, you can always skip it. Perhaps...but he answered the question very well and with great, IMO, patience. -- http://mail.python.org/mailman/listinfo/python-list