Re: if the else short form
On Wed, Oct 06, 2010 at 01:45:51PM +1300, Lawrence D'Oliveiro wrote: In message mailman.1339.1286268545.29448.python-l...@python.org, Antoon Pardon wrote: On Tue, Oct 05, 2010 at 06:55:33PM +1300, Lawrence D'Oliveiro wrote: In message mailman.1232.1285927634.29448.python-l...@python.org, Antoon Pardon wrote: On Wed, Sep 29, 2010 at 01:38:48PM +0200, Hrvoje Niksic wrote: BTW adding ==True to a boolean value is redundant and can even break for logically true values that don't compare equal to True (such as the number 10 or the string foo). But leaving it out can also break things. Only in code which was incorrectly written to begin with. Well you can always define such code as incorrectly written of course. In this situation, I mean by ???incorrectly written??? code which uses non- Boolean values as conditional expressions. Please be more specific: A lot of times someone comes with code like the following: if len(lst) != 0: ... and than gets the advise to write it as follows: if lst: ... Do you mean that this second piece of code is incorrectly written, since it uses a non-boolean value as conditional expression? -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: subclass constructor problem
On 5 oct, 17:52, de...@web.de (Diez B. Roggisch) wrote: Btw, you are a bit on the overprotective side. The convention for marking attributes (methods or objects alike) private s/private/implementation/ I find that thinking in terms of interface / implementation instead of public / private really helps focusing on what's important here. is by prefixing them with a *single* underscore. And FWIW, the usual idiom is to avoid dummy accessor and use plain attributes until you have a need for a computed one - in which case you use a descriptor (either the builtin 'property' or custom descriptor). Python is not Java. -- http://mail.python.org/mailman/listinfo/python-list
Re: meta-class review
On Oct 5, 4:17 pm, Ethan Furman et...@stoneleaf.us wrote: On one the many mini-reports we use, we have a bunch of counts that are frequently zero; because the other counts can also be low, it becomes easy to miss the non-zero counts. For example: Code Description Conv Errors : 6 31,N DPV Failure : 4 10: Invalid Address : 0 11: Invalid C/S/Z : 0 12: Invalid State : 0 13: Invalid City : 0 17: Insufficient Information : 0 33: Non-Deliverable : 0 98: Non-USPS zip : 0 21: Address Not Found : 0 22: Multiple Responses : 3 23: Error in Primary : 0 24: Error in Secondary : 0 So I thought I would print '-' instead... Code Description Conv Errors : 6 31,N DPV Failure : 4 10: Invalid Address : - 11: Invalid C/S/Z : - 12: Invalid State : - 13: Invalid City : - 17: Insufficient Information : - 33: Non-Deliverable : - 98: Non-USPS zip : - 21: Address Not Found : - 22: Multiple Responses : 3 23: Error in Primary : - 24: Error in Secondary : - Much easier to pick out the numbers now. To support this, the code changed slightly -- it went from '%-25s: %7d' % ('DPV Failure', counts['D']) to '%-25s: %7s' % ('DPV Failure', counts['D'] if counts['D'] else '-')) This became a pain after a dozen lines, prompting my previous question about the difference between %s and %d when printing integers. With the excellent replies I received I coded a short class: class DashInt(int): def __str__(x): if x: return str(x) return '-' and my line printing code shrunk back to it's previous size. Well, it wasn't long before I realized that when a DashInt was added to an int, an int came back... and so did the '0's. So I added some more lines to the class. def __add__(x, other): result = super(DashInt, x).__add__(other) return result and then I tried to do a floating type operation, so added yet more lines... def __add__(x, other): result = super(DashInt, x).__add__(other) if result == NotImplemented: return NotImplemented return result and so on and so on for the basic math functions that I will be using... what a pain! And then I had a thought... metaclasses! If DashInt used a metaclass that would automatically check the result, and if it was base class wrap it up in the new subclass, my DashInt class could go back to being five simple lines, plus one more for the metaclass specifier. So DashInt currently looks like this: class TempInt(int): __metaclass__ = Perpetuate def __str__(x): if x == 0: return '-' return int.__str__(x) and Perpetuate looks like this: class Perpetuate(type): def __init__(yo, *args, **kwargs): super(type, yo).__init__(*args) def __new__(metacls, cls_name, cls_bases, cls_dict): if len(cls_bases) 1: raise TypeError(multiple bases not allowed) result_class = type.__new__( \ metacls, cls_name, cls_bases, cls_dict) base_class = cls_bases[0] known_methods = set() for method in cls_dict.keys(): if callable(getattr(result_class, method)): known_methods.add(method) base_methods = set() for method in base_class.__dict__.keys(): if callable(getattr(base_class, method, None)) and \ method not in ('__new__'): base_methods.add(method) for method in base_methods: if method not in known_methods: setattr(result_class, method, \ _wrap(base_class, getattr(base_class, method))) return result_class def _wrap(base, code): def wrapper(self, *args, **kwargs): result = code(self, *args, **kwargs) if type(result) == base: return self.__class__(result) return result wrapper.__name__ = code.__name__ wrapper.__doc__ = code.__doc__ return wrapper It seems to work fine for normal operations. I had to exclude __new__ because it was a classmethod, and I suspect I would have similar issues with staticmethods. Any comments appreciated, especially ideas on how to better handle class- and staticmethods Well, it's definitely overkill for printing a dash instead of a zero, but a lot of people have asked how to create a subtype of int (or other builtin) that coerces the other operand, and your idea is interesting in
Re: unable to mkvirtualenv
On 5 Okt., 10:17, Julian maili...@julianmoritz.de wrote: Hi, when creating a virtualenv withmkvirtualenv, I receive an error: http://pastebin.com/1N8yRZUv I've updated the relating packages (virtualenv, virtualenvwrapper, distutils, distribute, pip) and couldn't solve my problem via google. jannis leidel posted the solution via twitter: http://bitbucket.org/tarek/distribute/issue/168/ solution was commenting the following line: # export PYTHONDONTWRITEBYTECODE=1 in .bashrc. -- http://mail.python.org/mailman/listinfo/python-list
Re: if the else short form
In message mailman.1384.1286348190.29448.python-l...@python.org, Antoon Pardon wrote: A lot of times someone comes with code like the following: if len(lst) != 0: ... and than gets the advise to write it as follows: if lst: ... Do you mean that this second piece of code is incorrectly written ... Yes. -- http://mail.python.org/mailman/listinfo/python-list
Expression problem.
Hi, I am having trouble with an expression. I have the following line of code: self.failUnless(c.as == 65215) What happens when you compile this is that you get a syntax error. This is because as has been made a keyword. failUnless is from the module unittest. Now my problem is this. the .as is no where defined in the code. The code builds just fine if you removed it. But the origigal programmer must have put it in for a reason. I just cant understand what that is. Can anyone point me into the right direction? was .as before it became a keyword some kind of string manipulator? Let me know if you need more info. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Expression problem.
On Wed, Oct 6, 2010 at 1:32 AM, Nethirlon . nethir...@gmail.com wrote: Hi, I am having trouble with an expression. I have the following line of code: self.failUnless(c.as == 65215) What happens when you compile this is that you get a syntax error. This is because as has been made a keyword. failUnless is from the module unittest. Now my problem is this. the .as is no where defined in the code. The code builds just fine if you removed it. But the origigal programmer must have put it in for a reason. I just cant understand what that is. Can anyone point me into the right direction? was .as before it became a keyword some kind of string manipulator? Nope, it was just normal attribute syntax. I would assume the original author just named some object attribute as for whatever reason, but the fact that removing it doesn't cause a test failure is just bizarre. Perhaps you should examine the code for c's class (and/or its ancestors)? (Sounds like you might have already done that though, in which case: o_O *shrug*) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: if the else short form
On 5 Oct, 06:52, Lawrence D'Oliveiro l...@geek- central.gen.new_zealand wrote: In message e8b46ea8-8d1e-4db9-91ba-501fd1a44...@g18g2000yqk.googlegroups.com, James Harris wrote: On 29 Sep, 18:20, Seebs usenet-nos...@seebs.net wrote: On 2010-09-29, Tracubik affdfsdfds...@b.com wrote: button = gtk.Button((False,, True,)[fill==True]) Oh, what a nasty idiom. I'm surprised you don't like this construct. I hadn't seen it until I read the OP's question just now. However, it's meaning was immediately apparent. I’ve used it a lot, from habit because I only started heavily using Python with version 2.4. I’m still not sure I’m comfortable with “true-part if cond else false- part”, when just about every other language manages to standardize on “cond ? true-part : false-part”. For the bit you are not comfortable with do you mean (false-part, true-part)[cond] Of course, this is just an expression containing a selection. Arbitrarily complex tests can be dealt with in languages where if statements and case statements can be expressions. IIRC the great Algol 60 allowed if statements to return a value. I can't say I can see why a number of subsequent languages don't allow this. James -- http://mail.python.org/mailman/listinfo/python-list
Re: Expression problem.
Thank you Chris, I'll try and find the attribute in the code. That was my conclusion aswell... The original author must have defined it somewhere... Thanks. On Wed, Oct 6, 2010 at 10:59 AM, Chris Rebert c...@rebertia.com wrote: On Wed, Oct 6, 2010 at 1:32 AM, Nethirlon . nethir...@gmail.com wrote: Hi, I am having trouble with an expression. I have the following line of code: self.failUnless(c.as == 65215) What happens when you compile this is that you get a syntax error. This is because as has been made a keyword. failUnless is from the module unittest. Now my problem is this. the .as is no where defined in the code. The code builds just fine if you removed it. But the origigal programmer must have put it in for a reason. I just cant understand what that is. Can anyone point me into the right direction? was .as before it became a keyword some kind of string manipulator? Nope, it was just normal attribute syntax. I would assume the original author just named some object attribute as for whatever reason, but the fact that removing it doesn't cause a test failure is just bizarre. Perhaps you should examine the code for c's class (and/or its ancestors)? (Sounds like you might have already done that though, in which case: o_O *shrug*) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: help!!!
On 06/10/2010 05:28, Dennis Lee Bieber wrote: On Tue, 05 Oct 2010 23:54:00 -0400,fkr...@aboutrafi.net23.net declaimed the following in gmane.comp.python.general: plz can u convert this cpp file into python i need that badly as soon as possible... I am new to python. I just wanna learn it Step one... DON'T TRY TO PORT C++ to Python... the object models are quite different. Don't be rediculous... Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: if the else short form
On Wed, Oct 06, 2010 at 09:31:48PM +1300, Lawrence D'Oliveiro wrote: In message mailman.1384.1286348190.29448.python-l...@python.org, Antoon Pardon wrote: A lot of times someone comes with code like the following: if len(lst) != 0: ... and than gets the advise to write it as follows: if lst: ... Do you mean that this second piece of code is incorrectly written ... Yes. OK, but you do realise this is a minority opinion within the python community? -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: Expression problem.
Sebastiaan de Haan wrote: Thank you Chris, I'll try and find the attribute in the code. That was my conclusion aswell... The original author must have defined it somewhere... Don't forget to check whether the object's class (or any of its bases) has a __getattr__() or __getattribute__() method. class A(object): ... def __getattr__(self, name): ... return 42 ... a = A() a.as File stdin, line 1 a.as ^ SyntaxError: invalid syntax Note tha you can still access such an attribute using getattr() getattr(a, as) 42 Peter -- http://mail.python.org/mailman/listinfo/python-list
C++ vs. Python Was: Re: help!!!
On Wednesday 06 October 2010, 06:28:51 Dennis Lee Bieber wrote: On Tue, 05 Oct 2010 23:54:00 -0400, fkr...@aboutrafi.net23.net declaimed the following in gmane.comp.python.general: plz can u convert this cpp file into python i need that badly as soon as possible... I am new to python. I just wanna learn it Step one... DON'T TRY TO PORT C++ to Python... the object models are quite different. I do this all the time without any adverse effects (other than being glad to only rarely having the need of doing it the other way around ;-)). And the models aren't _that_ different, the syntax is. Check yourself: http://doc.qt.nokia.com/qtmobility-1.0/lightmaps.html vs. http://www.riverbankcomputing.com/pipermail/pyqt/2010-October/028040.html Pete -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python-ideas] [Python-Dev] Inclusive Range
On Tue, Oct 05, 2010 at 01:52:39PM -0700, Chris Rebert wrote: On Tue, Oct 5, 2010 at 1:31 PM, Wolfgang Rohdewald wolfg...@rohdewald.de wrote: On Dienstag 05 Oktober 2010, MRAB wrote: About notation, even if loved right-hand-half-open intervals, I would wonder about [a,b] noting it. I guess 99.9% of programmers and novices (even purely amateur) have learnt about intervals at school in math courses. Both notations I know of use [a,b] for closed intervals, while half-open ones are noted either [a,b[ or [a,b). Thus, for me, the present C/python/etc notation is at best misleading. So, what about a hypothetical language using directly math unambiguous notation, thus also letting programmers chose their preferred semantics (without fooling others)? End of war? Dijkstra came to his conclusion after seeing the results of students using the programming language Mesa, which does support all 4 forms of interval. what was his conclusion? That right-hand-half-open intervals (i.e. a = i b, equivalently [a, b) ), which are what Python uses, are to be preferred. (See aforelinked PDF: http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF) The problem is that the slice notation is sometimes handy in situations where an open interval doesn't allow easily to mark what you want. For instance I have at one time implemted a Tree. This is a dict like structure but it allows to visit the keys in order. Because there is an order, slice notation can make sense. e.g. if T is a tree with names as keys, T['bea':'mike'] is a subtree where we have for each key that 'bea' = key 'mike'. But what if I wanted a subtree where 'mike' was still included, but nothing further? Or what if the keys were floats or tuples and I wanted an inclusive upper boundary? And what if you needed the reverse sequence. If you start with inclusive limit, the reverse of a = item = b is b = item = a. If the second limit is to be exclusive the reverse of a = item b becomes (b - 1) = item (a - 1). So what do you do in python, if you are given a list, a lower inclusive and an upper exclusive limit and you have to return the reverse slice. Better don't use L[b-1:a-1:-1], because that may result in a nasty surprise, a surprise that could have been avoided by allowing the second limit to be inclusive. So I don't think the results in MESA are that relevant when you are working in an environment that is not limited to integers in upgoing sequences. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: meta-class review
On Wed, 2010-10-06, Ethan Furman wrote: MRAB wrote: On 06/10/2010 00:17, Ethan Furman wrote: [snip] Any comments appreciated, especially ideas on how to better handle class- and staticmethods I think that's a bit of overkill. The problem lies in the printing part, but you're spreading the solution into the rest of the application! (A case of the tail wagging the dog, perhaps? :-)) IMHO you should just use a simple function when printing: def dash_zero(x): return str(x) if x else '-' '%-25s: %7s' % ('DPV Failure', dash_zero(counts['D'])) Yes, simple is better than complex, isn't it? :) And certainly a *lot* less code! Thank you for pointing that out -- hopefully my blush of embarassment will fade by morning. IMHO wrapping it in a class made much sense -- I just didn't see why it exploded with more and more. There are a few classes like that which I frequently use: a. statistics counters which are like ints, but can only be incremented and printed (or placed into SNMP messages, or whatever the system uses) b. integers to be printed right-aligned in tables of a certain width, and as '-' or 'n/a' or '' when they are zero. If they are so int-like that you can't do (a), then just build them on-the-fly when you're printing: f.write('%s: %s\n' % (name, MyFormatted(value))) Class MyFormatted here is very much like dash_zero above; it has no methods except __init__ and __str__. I mostly do this in C++; perhaps it makes more sense in a language with static typing, overloading and templates. /Jorgen -- // Jorgen Grahn grahn@ Oo o. . . \X/ snipabacken.se O o . -- http://mail.python.org/mailman/listinfo/python-list
Re: Expression problem.
On 6 okt, 11:53, Peter Otten __pete...@web.de wrote: Sebastiaan de Haan wrote: Thank you Chris, I'll try and find the attribute in the code. That was my conclusion aswell... The original author must have defined it somewhere... Don't forget to check whether the object's class (or any of its bases) has a __getattr__() or __getattribute__() method. class A(object): ... def __getattr__(self, name): ... return 42 ... a = A() a.as File stdin, line 1 a.as ^ SyntaxError: invalid syntax Note tha you can still access such an attribute using getattr() getattr(a, as) 42 Peter Thank you Peter, While searching the document I found the following code: class Open(dpkt.Packet): __hdr__ = ( ('v', 'B', 4), ('as', 'H', 0), ('holdtime', 'H', 0), ('identifier', 'I', 0), ('param_len', 'B', 0) ) So, I am new at programming with Python, but doing my best to grasp the concept here. From what I am understanding is that the __hdr__ is something that the original programmer cameup with for him self. I am just curious as to weather the as in this piece of code is the one I am searching for. Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: [C-API] Weird sys.exc_info reference segfault
On Tue, 05 Oct 2010 16:17:57 +0200 Jonas H. jo...@lophus.org wrote: Right now I have this minimal struct: static PyTypeObject StartResponse_Type = { PyObject_HEAD_INIT(PyType_Type) 0, /* ob_size */ start_response, /* tp_name */ sizeof(StartResponse), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor)PyObject_FREE, /* tp_dealloc */ 0, 0, 0, 0, 0, 0, 0, 0, 0, /* tp_print, tp_{get,set}attr, stuff */ start_response /* tp_call */ }; I'm not sure about the `PyObject_HEAD_INIT` argument, but passing NULL to it made `dir(obj)` crash. It shouldn't. Are you sure you're calling PyType_Ready in the module initialization routine? By the way, it is recommended to use at least Py_TPFLAGS_DEFAULT for tp_flags. So does setting `GenericGetAttr` as `tp_getattr`. tp_getattr has the same signature as PyObject_GetAttrString. You're looking for tp_getattro, which takes the attribute name as a PyObject * rather than as a char *. However, if you want your type to have a __dict__, what you need to do is to have a PyObject *dict member (the name is not important, of course), and initialize tp_dictoffset to offsetof(StartResponse, dict). You don't have to set tp_getattr, PyType_Ready() should do the right thing. Regards Antoine. -- http://mail.python.org/mailman/listinfo/python-list
Re: sequence multiplied by -1
In article 4ca6bd15$0$2$c3e8...@news.astraweb.com, Steven D'Aprano st...@remove-this-cybersource.com.au wrote: On Fri, 01 Oct 2010 14:56:52 +0200, Antoon Pardon wrote: Think about the following possibility. Someone provides you with a library of functions that act on sequences. They rely on the fact that '+' concatenates. Someone else provides you with a library of functions that act on numbers. They rely on the fact that '+' provides addition. Now however you write your sequence like numbers or number like sequences one of those libraries will be useless for it. And? So what? Sequences aren't numbers. Numbers aren't sequences. You can't expect every library to correctly work with every data type regardless of the polymorphism of operators. If you have no idea what x is, you can't possibly expect to know what function(x) does, *for any function*. (Actually, there may be one or two exceptions, like id(x). But fewer than you think -- even str(x) is not guaranteed to work for arbitrary types.) If you don't know whether x is a number or a sequence, you can't know what x.sort() will do, or math.sqrt(x), or x+x. Why single out x+x as more disturbing than the other examples? -- which would it do with only one symbol? But why limit ourselves to one symbol for different kind of operations, to begin with? Because the more symbols you have, the higher the learning curve to become proficient in the language, and the more like line-noise the code becomes. If you want APL or Perl, you know where to find them -- they're perfectly reasonable languages, but they have made design choices that are not Python's design choices. We use operators, because for certain common operations it is more convenient and natural to use infix notation than function notation. And polymorphism is useful, because it reduces the size of namespaces and therefore the burden on the programmer. Putting these two factors together, instead of: There is one more important argument. Suppose + on a certain type of a objects generates the same type of object. Suppose (x+y)+z = x+(y+z) (always) This is called associativity. Then we can forget about the brackets. A simple expression like y = [1,2,3] + x + [4,5,6] + z becomes a mess without leaning on this associativity law. Note that concatenation is by nature associative. It is so natural that you almost must be a mathematician to realize that. concat_lists concat_tuples concat_strings concat_bytes add_floats add_ints add_rationals add_decimals plus mixed-operand versions of at least some of them, we have a single symbol, +, for all of these. Arguing about whether it should be a single operator + or two operators + is a comparatively trivial matter. I believe that the similarity between concatenation and addition is close enough that it is appropriate to use one symbol for both, and likewise between repetition and multiplication. I'm glad that Guido apparently agrees with me. The important thing is that manipulations are the same such that a += b a += c a += d can be replaced by a = b + c + d without much thought to what a b c and + represent. In other words reuse of the important brain resource of pattern recognition. -- Steven Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. alb...@spearc.xs4all.nl =n http://home.hccnet.nl/a.w.m.van.der.horst -- http://mail.python.org/mailman/listinfo/python-list
Re: resource module returns just zeros
On Tue, 2010-10-05 at 09:49 +1100, Cameron Simpson wrote: On 04Oct2010 09:02, Adam Tauno Williams awill...@whitemice.org wrote: | I'm using a call to the resource module's getrusage method. On openSUSE | this works, on CentOS [python26-2.6.5-3.el5] it 'works' but just returns | zeros for the memory utilization values. | resource.getrusage(resource.RUSAGE_SELF).ru_maxrss | openSUSE: returns 5512 | CentOS: returns 0 | Anyone know what condition causes this? Or is there a better / | more-reliable way to check memory utilization of the current process? Long standing Linux shortcoming. man getrusage on a handy Gentoo box says: The structure definition shown at the start of this page was taken from 4.3BSD Reno. Not all fields are meaningful under Linux. In Linux 2.4 only the fields ru_utime, ru_stime, ru_minflt, and ru_majflt are maintained. Since Linux 2.6, ru_nvcsw and ru_nivcsw are also maintained. Since Linux 2.6.22, ru_inblock and ru_oublock are also maintained. I ran across this deficiency a few years back hoping to get some memory stats for some software engineeers in a former life. Also look in /proc - the info may be available there - I expect that's where a Linux ps gets a lot of info. Yep, I went that route. def get_rss_size(): rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss * 1000 if (rss == 0): # NOTE: This means RSS via getrusage is not working on this system # So we try our fallback method or reading proc/{pid}/statm try: handle = open('/proc/{0}/statm'.format(os.getpid()), 'rb') data = handle.read(512) handle.close() rss = int(data.split(' ')[1]) * 4000 except Exception, e: rss = 0 return rss Both the /proc and getrusage produce the same value, and that value corresponds to what is seen in ps, top, or gnome-system-monitor. -- Adam Tauno Williams awill...@whitemice.org LPIC-1, Novell CLA http://www.whitemiceconsulting.com OpenGroupware, Cyrus IMAPd, Postfix, OpenLDAP, Samba -- http://mail.python.org/mailman/listinfo/python-list
Re: [C-API] Weird sys.exc_info reference segfault
On 10/06/2010 02:01 PM, Antoine Pitrou wrote: It shouldn't. Are you sure you're calling PyType_Ready in the module initialization routine? Yeah. The problem was that the type struct was declared 'static' in another module so the changes `PyType_Ready` made to the struct weren't applied correctly. By the way, it is recommended to use at least Py_TPFLAGS_DEFAULT for tp_flags. Thanks, but I chose not to use that flags. I don't need any. tp_getattr has the same signature as PyObject_GetAttrString. You're looking for tp_getattro, which takes the attribute name as a PyObject * rather than as a char *. Thanks again, my fault :-) I think my problems are solved and my questions answered -- thank you so much for you patience! Jonas -- http://mail.python.org/mailman/listinfo/python-list
Re: Expression problem.
Nethirlon . wrote: On 6 okt, 11:53, Peter Otten __pete...@web.de wrote: Sebastiaan de Haan wrote: Thank you Chris, I'll try and find the attribute in the code. That was my conclusion aswell... The original author must have defined it somewhere... Don't forget to check whether the object's class (or any of its bases) has a __getattr__() or __getattribute__() method. class A(object): ... def __getattr__(self, name): ... return 42 ... a = A() a.as File stdin, line 1 a.as ^ SyntaxError: invalid syntax Note tha you can still access such an attribute using getattr() getattr(a, as) 42 Peter Thank you Peter, While searching the document I found the following code: class Open(dpkt.Packet): __hdr__ = ( ('v', 'B', 4), ('as', 'H', 0), ('holdtime', 'H', 0), ('identifier', 'I', 0), ('param_len', 'B', 0) ) So, I am new at programming with Python, but doing my best to grasp the concept here. From what I am understanding is that the __hdr__ is something that the original programmer cameup with for him self. I am just curious as to weather the as in this piece of code is the one I am searching for. Side note: if the code you have questions about is publicly available it's always a good idea to give the url. I am assuming that you are referring to an older version to this beast: http://code.google.com/p/dpkt/source/browse/trunk/dpkt/bgp.py Here's where your problem was fixed/adjusted to newer Python versions: http://code.google.com/p/dpkt/source/detail?r=51 The __hdr__ is indeed an invention of the author of the package, and is feed to the metaclass* of dpkt.Packet. The metaclass uses it to create __slots__ that are filled dynamically in Packet.__init__(). I recommend that you read the docstring of the Packet class http://code.google.com/p/dpkt/source/browse/trunk/dpkt/dpkt.py but only bother about the implementation if you cannot avoid it. You can always have a second look after you have gained some Python experience. Peter (*) Every class in Python is an instance of its metaclass, i. e. the relation between metaclass and class is the same as between class and instance. Custom metaclasses are a powerful feature, but tend to make Python code harder to grasp. -- http://mail.python.org/mailman/listinfo/python-list
Re: Expression problem.
On 6 okt, 15:25, Peter Otten __pete...@web.de wrote: Nethirlon . wrote: On 6 okt, 11:53, Peter Otten __pete...@web.de wrote: Sebastiaan de Haan wrote: Thank you Chris, I'll try and find the attribute in the code. That was my conclusion aswell... The original author must have defined it somewhere... Don't forget to check whether the object's class (or any of its bases) has a __getattr__() or __getattribute__() method. class A(object): ... def __getattr__(self, name): ... return 42 ... a = A() a.as File stdin, line 1 a.as ^ SyntaxError: invalid syntax Note tha you can still access such an attribute using getattr() getattr(a, as) 42 Peter Thank you Peter, While searching the document I found the following code: class Open(dpkt.Packet): __hdr__ = ( ('v', 'B', 4), ('as', 'H', 0), ('holdtime', 'H', 0), ('identifier', 'I', 0), ('param_len', 'B', 0) ) So, I am new at programming with Python, but doing my best to grasp the concept here. From what I am understanding is that the __hdr__ is something that the original programmer cameup with for him self. I am just curious as to weather the as in this piece of code is the one I am searching for. Side note: if the code you have questions about is publicly available it's always a good idea to give the url. I am assuming that you are referring to an older version to this beast: http://code.google.com/p/dpkt/source/browse/trunk/dpkt/bgp.py Here's where your problem was fixed/adjusted to newer Python versions:http://code.google.com/p/dpkt/source/detail?r=51 The __hdr__ is indeed an invention of the author of the package, and is feed to the metaclass* of dpkt.Packet. The metaclass uses it to create __slots__ that are filled dynamically in Packet.__init__(). I recommend that you read the docstring of the Packet class http://code.google.com/p/dpkt/source/browse/trunk/dpkt/dpkt.py but only bother about the implementation if you cannot avoid it. You can always have a second look after you have gained some Python experience. Peter (*) Every class in Python is an instance of its metaclass, i. e. the relation between metaclass and class is the same as between class and instance. Custom metaclasses are a powerful feature, but tend to make Python code harder to grasp. Peter, Thank you very much, I did not know that the code was available online, and also did not know that the author updated it online. The package I was using came from the openSuse python repository, given to me by one of the maintainers of that repository. I am having a hard time understanding the concept of classes, so I think that I should focus on that first, before continueing to try and contribute. Thank you very much for your help! -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Re: How to save a binary file?
When you put the 'wb' extension you have to pass a Encode the string Python does not accept a string on a wb file, Python3 On Oct 6, 2010 1:01am, Chris Rebert c...@rebertia.com wrote: On Oct 5, 2010 8:03pm, MRAB pyt...@mrabarnett.plus.com wrote: On 05/10/2010 23:50, hid...@gmail.com wrote: I did but the mistake is: Error interpreting JPEG image file (Not a JPEG file: starts with 0x5c 0x6e) I think the problem is maybe in the binary code here is: On Oct 5, 2010 6:18pm, Jonas H. jo...@lophus.org wrote: On 10/05/2010 11:11 PM, hid...@gmail.com wrote: Hello, how i can save a binary file, i read in the manual in the IO area but doesn' t show how to save it. Here is the code what i am using: s = open('/home/hidura/test.jpeg', 'wb') s.write(str.encode(formFields[5])) s.close() Why are you encoding it? A JPEG file should contain the binary data, not a textual encoding of its bytes. The error message you got said that the contents of the file started with a backslash. If you print out, say, repr(str[ : 10]) you should get something like 'ÿØÿà\x00\x10JFIF'. Try this instead: s = open('/home/hidura/test.jpeg', 'wb') s.write(str) s.close() Incidentally, 'str' is a bad name for a variable because it's the name of the built-in string type. On Tue, Oct 5, 2010 at 9:53 PM, hid...@gmail.com wrote: I has to use repr to convert in this string: ÿØÿà\x00\x10JFIF? No, you don't/shouldn't convert it at all. Read/use the 3 lines of suggested code that MRAB gave: s = open('/home/hidura/test.jpeg', 'wb') s.write(str) s.close() Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Module loading trickery
On Wednesday 06 October 2010, it occurred to Dave Angel to exclaim: On 2:59 PM, Thomas Jollans wrote: snip % cat a.py foo = 'Meh.' import b % cat b.py from a import foo print(foo) % python a.py Meh. % But there are now two modules containing separate items foo, one is called __main__, and the other is called a. Good point. So let's change the example to match the intentions. % cat a.py foo = 'Meh.' import b % cat b.py from a import foo print(foo) % cat main.py import a % python3 main.py Meh. % The former is the script you ran, and the latter is the module imported by b. Several problems could occur, including if foo were a list, and b appended to it, the original script wouldn't see the change. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: if the else short form
James Harris james.harri...@googlemail.com wrote in message news:e8b46ea8-8d1e-4db9-91ba-501fd1a44...@g18g2000yqk.googlegroups.com... On 29 Sep, 18:20, Seebs usenet-nos...@seebs.net wrote: On 2010-09-29, Tracubik affdfsdfds...@b.com wrote: Hi all, I'm studying PyGTK tutorial and i've found this strange form: button = gtk.Button((False,, True,)[fill==True]) the label of button is True if fill==True, is False otherwise. i have googled for this form but i haven't found nothing, so can any of you pass me any reference/link to this particular if/then/else form? Oh, what a nasty idiom. Here's the gimmick. (False,, True,) is a tuple. That means you can index it. For instance: (False,, True,)[0] is the string False,. So, what is the numeric value of fill == True? Apparently, at least at the time this was written, it was 0 if fill was not equal to True, and 1 if fill was equal to True. Let me say, though, that I'm a C programmer, so I'm coming from a language where the result of 0-or-1 for test operators is guaranteed, and I still wouldn't use this in live code. It's insufficiently legible. I'm surprised you don't like this construct. I hadn't seen it until I read the OP's question just now. However, it's meaning was immediately apparent. I should say where I'm coming from. Contrast the following C and Python: text = fill == true ? True, : False,; (C) text = (False,, True,)[fill == true](Python) Surely the C should be: fill ? True, : False,; ? I never liked C's ?: construct partly because it doesn't scale well. To accept more than two options it requires the programmer to build a small hierarchy which can be awful to read and may be best expressed as a separate function. I'd rather have a language change a predicate to a small integer and use that to index a set of results - and this is exactly what the OP's tutorial does. I use this syntax where there are two possibilities chosen according to condition 'a': (a | b | c) similar to C's ?: operator. Where there are N possibilities chosen from a linear set, I use: (i | a, b, c, ... |z) # indexed from 1, default to z (I think from Algol68 originally.) The difference from what the Python is doing above, is that only one of the possibilities is ever evaluated. Extrapolating the syntax a little, Python I think will evaluate all N expressions (maybe even construct the tuple), before choosing one. And I'm not sure there is provision for a default value either, without having a far more complex expression: x = (One,Two,Three) [i-1] While this works for i = 1,2,3, it goes funny for i=0,-1,-2, and generates an error for the rest (I'm sure Python has myriad ways of achieving this succinctly, but this isn't it...) As another hypothetical example where sgn() returns -1, 0 or +1 position = (less, equal, greater)[sgn(a - b) + 1] Though where the list gets much longer it would be good to be able to label the cases for legibility. You can do, until you want to insert an item in the middle and have to re-label everything... For more complex cases I'd just use a conventional case or switch expression which (in my syntax at least), also evaluates just one expression, and returns that value. But then you can also start using if-else chains, so this is no longer a compact construction useful in an expression. -- Bartc -- http://mail.python.org/mailman/listinfo/python-list
SAML2 support in Python
Hi! For those that are interested in making there web applications work in a SAML2 federated environment I have two things to offer: 1) PySAML2 - a not complete but working implementation of SAML2 in Python. Started with implementing the SP part and are now slowly doing the IdP part too. https://code.launchpad.net/~roland-hedberg/pysaml2/main For those using Django you should take notice of what Lorenzo Gil Sanchez is doing using PySAML2. https://code.launchpad.net/~lgs/pysaml2/main 2) IdPproxy - a gateway between social media and the SAML2 world. To the SPs in a SAML2 federation it looks like a normal IdP, but it allows the user to authenticate using Twitter, Facebook, Google, OpenID and Windows Live ID. Work in progress, alpha quality. https://code.launchpad.net/~roland-hedberg/+junk/IdPproxy -- Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Re: Re: How to save a binary file?
How i get the code what MRAB says theres any manual or example?, because i was trying in Python2.x to do it and give me the same mistake, when i try to use repr that give me more backslash On Oct 6, 2010 10:25am, hid...@gmail.com wrote: When you put the 'wb' extension you have to pass a Encode the string Python does not accept a string on a wb file, Python3 On Oct 6, 2010 1:01am, Chris Rebert c...@rebertia.com wrote: On Oct 5, 2010 8:03pm, MRAB pyt...@mrabarnett.plus.com wrote: On 05/10/2010 23:50, hid...@gmail.com wrote: I did but the mistake is: Error interpreting JPEG image file (Not a JPEG file: starts with 0x5c 0x6e) I think the problem is maybe in the binary code here is: On Oct 5, 2010 6:18pm, Jonas H. jo...@lophus.org wrote: On 10/05/2010 11:11 PM, hid...@gmail.com wrote: Hello, how i can save a binary file, i read in the manual in the IO area but doesn' t show how to save it. Here is the code what i am using: s = open('/home/hidura/test.jpeg', 'wb') s.write(str.encode(formFields[5])) s.close() Why are you encoding it? A JPEG file should contain the binary data, not a textual encoding of its bytes. The error message you got said that the contents of the file started with a backslash. If you print out, say, repr(str[ : 10]) you should get something like 'ÿØÿà\x00\x10JFIF'. Try this instead: s = open('/home/hidura/test.jpeg', 'wb') s.write(str) s.close() Incidentally, 'str' is a bad name for a variable because it's the name of the built-in string type. On Tue, Oct 5, 2010 at 9:53 PM, hid...@gmail.com wrote: I has to use repr to convert in this string: ÿØÿà\x00\x10JFIF? No, you don't/shouldn't convert it at all. Read/use the 3 lines of suggested code that MRAB gave: s = open('/home/hidura/test.jpeg', 'wb') s.write(str) s.close() Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Python shelve with a file handle
shelve has open({filename}). Is there anyway to open a shelve 'database' using a file handle? Specifically I'd like to create a shelve database via a file handle acquired from a call to SpooledTemporaryFile. -- http://mail.python.org/mailman/listinfo/python-list
suggestions please what should i watch for/guard against' in a file upload situation?
in general, what are things i would want to 'watch for/guard against' in a file upload situation? i have my file upload working (in the self-made framework @ work without any concession for multipart form uploads), but was told to make sure it's cleansed and cannot do any harm inside the system. my checklist so far is basically to check the extension - ensure it has 3 places, ensure it's in the allowed list (like jpg gif etc...). not sure what else i could do to guard against anything bad happening. maybe the file name itself could cause greif? not sure but any suggestions or examples are most welcome :) -- http://mail.python.org/mailman/listinfo/python-list
Re: direct print to log file
Dave Angel da...@ieee.org wrote: If you want to be able to go back to the original, then first bind another symbol to it. Or restore from sys.__stdout__, as long as you're sure that nothing else has rebound sys.stdout first (or don't mind clobbering it). -- \S under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: meta-class review
Carl Banks wrote: On Oct 5, 4:17 pm, Ethan Furman et...@stoneleaf.us wrote: class DashInt(int): __metaclass__ = Perpetuate def __str__(x): if x == 0: return '-' return int.__str__(x) Well, it's definitely overkill for printing a dash instead of a zero, but a lot of people have asked how to create a subtype of int (or other builtin) that coerces the other operand, and your idea is interesting in that you don't have to write boilerplate to override all the operations. Main drawback is that it's incomplete. For example, it doesn't coerce properties. int.real returns the real part of the int (i.e., the int itself). A subclass's real attribute should return an instance of the subclass, but it won't. Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. a = int() a 0 a.real Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'int' object has no attribute 'real' int.real() Traceback (most recent call last): File stdin, line 1, in module AttributeError: type object 'int' has no attribute 'real' What am I missing here? Another example is float.__divmod__, which returns a tuple. Your coercive type would fail to convert the items of that tuple. Good point -- I'll get that included. A metaclass like this I think would be possible, with the understanding that it can never be foolproof, but it needs more work. Pointers: Defining __init__ isn't necessary for this metaclass. The len(cls_bases) 1 test can be thwarted if the base type multiply inherits from other types itself. The best thing to do is handle the case of arbitrary type hierarchies, but if you don't want to do that then the right way to catch it is to create the subtype then check that the __mro__ is (type, base_type, object). Thanks for the tips, Carl. What I had wanted was to be able to specify which type(s) to look for in cases of multiple inheritance, but I'm not sure how to pass parameters to the metaclass in python2... Can anybody shed some light on that? Thanks! ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: suggestions please what should i watch for/guard against' in a file upload situation?
On 2010-10-06, geekbuntu gmi...@gmail.com wrote: in general, what are things i would want to 'watch for/guard against' in a file upload situation? This question has virtually nothing to do with Python, which means you may not get very good answers. my checklist so far is basically to check the extension - ensure it has 3 places, ensure it's in the allowed list (like jpg gif etc...). This strikes me as 100% irrelevant. Who cares what the extension is? not sure what else i could do to guard against anything bad happening. maybe the file name itself could cause greif? Obvious things: * File name causes files to get created outside some particular upload directory (../foo) * File name has spaces * Crazy stuff like null bytes in file name * File names which might break things if a user carelessly interacts with them, such as foo.jpg /etc/passwd bar.jpg (all one file name including two spaces). Basically, the key question is, could a hostile user come up with input to your script which could break something? -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nos...@seebs.net http://www.seebs.net/log/ -- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) -- get educated! I am not speaking for my employer, although they do rent some of my opinions. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to save a binary file?
On 06/10/2010 15:25, hid...@gmail.com wrote: When you put the 'wb' extension you have to pass a Encode the string Python does not accept a string on a wb file, Python3 [snip] You are using Python 3 and type(str) returns class 'type'? Binary data in Python 3 should be an instance of the 'bytes' class, not an instance of the 'str' class. If you can't fix that, you could turn the string into bytes using: data = bytes(ord(c) for c in str) or by carefully choosing an encoding which would give the same result: data = str.encode('latin-1') Then you can save it: s = open('/home/hidura/test.jpeg', 'wb') s.write(data) s.close() I asked you to look at the result of repr so that you could see more clearly what the data actually looked like, an instance of str or an instance of bytes. -- http://mail.python.org/mailman/listinfo/python-list
Re: Strong typing vs. strong testing
On Sep 29, 9:01 pm, RG rnospa...@flownet.com wrote: That the problem is elsewhere in the program ought to be small comfort. But very well, try this instead: [...@mighty:~]$ cat foo.c #include stdio.h int maximum(int a, int b) { return a b ? a : b; } int main() { long x = 8589934592; printf(Max of %ld and 1 is %d\n, x, maximum(x,1)); return 0;} [...@mighty:~]$ gcc -Wall foo.c [...@mighty:~]$ ./a.out Max of 8589934592 and 1 is 1 $ gcc -Wconversion -Werror foo.c cc1: warnings being treated as errors foo.c: In function 'main': foo.c:5: warning: passing argument 1 of 'maximum' with different width due to prototype It's called learning to compile. And, yes, those warnings (and nearly every other one) should be enabled and treated as errors if a shop wants maximum protection. I only wish more (library) vendors did so. KHD -- http://mail.python.org/mailman/listinfo/python-list
Re: toy list processing problem: collect similar terms
On Sat, 25 Sep 2010 21:05:13 -0700 (PDT), Xah Lee xah...@gmail.com wrote: here's a interesting toy list processing problem. I have a list of lists, where each sublist is labelled by a number. I need to collect together the contents of all sublists sharing the same label. So if I have the list ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q r) (5 s t)) where the first element of each sublist is the label, I need to produce: output: ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t)) [snip] anyone care to give a solution in Python, Perl, javascript, or other lang? am guessing the scheme solution can be much improved... perhaps using some lib but that seems to show scheme is pretty weak if the lib is non-standard. Crossposting to Lisp, Python and Perl because the weird list of lists looks like Lisp or something else, and you mention other languages so I'm throwing this out for Perl. It appears this string you have there is actually list syntax in another language. If it is, its the job of the language to parse the data out. Why then do you want to put it into another language form? At runtime, once the data is in variables, dictated by the syntax, you can do whatever data manipulation you want (combining arrays, etc..). So, in the spirit of a preprocessor, given that the text is balanced, with proper closure, ie: ( (data) (data) )is ok. ( data (data) ) is not ok. the below does simple text manipulation, joining like labeled sublists, without going into the runtime guts of internalizing the data itself. Internally, this is too simple. -sln - Alternate input: ( ( (0 a b) (1 c d) (2 e f ) ) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q r) (5 s t) ) -- use strict; use warnings; my $input = EOI; ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q r) (5 s t)) EOI my $output = $input; my $regxout = qr/ ( (?: \( \s* [^()]+ \s* \) (\s*) )+ ) /x; $output =~ s{ $regxout } { my ( $list, $format ) = ( $1, $2 ); my ( %hseen, @order, $replace ); while ($list =~ /\(\s* (\S+) \s* (.+?) \s*\)/xsg) { if ( exists $hseen{$1} ) { $hseen{$1} .= $2; next; } push @order, $1; $hseen{$1} = $2; } for my $id (@order) { $replace .= ($hseen{$id}) ; } $replace =~ s/ $//; $replace . $format }xeg; print Input -\n$input\n; print Output -\n$output; __END__ Input - ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q r) (5 s t)) Output - ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t)) -- http://mail.python.org/mailman/listinfo/python-list
SysLogHandler message formatting
I have a daemon that uses the built-in SysLogHandler logging handler class to log messages to the host's syslog. Unfortunately, I am having trouble getting it to work with Metalog[1]. At first, I thought the problem was Metalog's fault because everything works as expected with syslog-ng. Upon further investigation, I have come to the conclusion that the SysLogHandler may be sending invalid syslog messages. When I disabled all filtering in Metalog, my messages started appearing in Metalog's output logs, but they were displayed inappropriately. Namely, the entire message was appearing where the program name should appear. Entries in the output file generally look like this: %b %d %H:%M:%S [%(ident)s] %(message)s For example, here is an entry from sshd: Oct 06 12:19:45 [sshd] Connection from 127.0.0.1 port 34142 In contrast, here is the entry generated by my program: Oct 06 11:41:05 [INFO Started an Idler for sysattend on mail.gosupertechs.com] 993 using SSL Here is the snippet of code I am using to set up the logger: root_logger = logging.getLogger() root_logger.setLevel(config.get_value(log_level)) syslog_hdlr = SysLogHandler(address='/dev/log', facility=SysLogHandler.LOG_DAEMON) syslog_hdlr.setLevel(logging.DEBUG) formatter = logging.Formatter('%(name)s: %(levelname)s % (message)s') syslog_hdlr.setFormatter(formatter) root_logger.addHandler(syslog_hdlr) logger = logging.getLogger(imapcd.daemon) logger.debug('test') I believe that the issue is LogRecords formatted using the Formatter class, but are not modified into a valid syslog message. I set up a fake syslog daemon that just listened on /dev/log and echoed everything it received to the console. When my program logs a message, it comes through like thist: 31imapcd.daemon: DEBUG test which is exactly how the Formatter formatted the message. Other programs that log to syslog, on the other hand, send messages that look like this: 149Oct 6 11:17:19 sudo: dhatch : TTY=pts/7 ; PWD=/home/dhatch ; USER=root ; COMMAND=/usr/bin/whoami If I adjust the Formatter definition to mimic that, like so: formatter = logging.Formatter('%(asctime)s %(name)s: %(levelname)s %(message)s', '%b %e %H:%M:%S') then all works well. My question, therefore, is where does this problem lie? Is it a bug in Metalog that it doesn't properly parse the message, or is it a bug in SysLogHandler that it doesn't properly format it? I guess it could also be that if one wants to use the SysLogHandler, then that particular format string must be used to ensure compatibility with all syslog daemons. I found a couple of examples[2][3] of people describing the usage of SysLogHandler, but none of them mention a problem with formatting. Any thoughts or suggestions would be appreciated. I will patch my program to use the latter format string for now, but I would still like to find the root cause for this problem. Regards, Dustin C. Hatch [1] http://metalog.sourceforge.net/ [2] http://scottbarnham.com/blog/2008/01/01/sysloghandler-not-writing-to-syslog-with-python-logging/ [3] http://www.mechanicalcat.net/richard/log/Python/Simple_usage_of_Python_s_logging_module -- http://mail.python.org/mailman/listinfo/python-list
hashkey/digest for a complex object
The short version of this question is: where can I find the algorithm used by the tuple class's __hash__ method? Now, for the long version of this question, I'm working with some complext Python objects that I want to be able to compare for equality easily. These objects are non-mutable once they are created, so I would like to use a two-step comparison for equality, based on the assumption that I can compute (either at creation time, or as needed and memoized) a hashkey/digest for each object. The test for equality of two of these objects would first compare their hashkeys. If they are different, the two objects are declared different; if they match, then a more stringent test for equality is performed. So the problem is to come up with a reasonable hashkey for each of these objects. The objects have two significant attributes, and two of these objects should be regarded as equal if these attributes are the same in both. The first attribute is a simple dictionary whose keys are integers and values are strings. The second attribute is more complicated. It is a tree structure, represented as a dictionary of dictionaries of dictionaries... until we get to the leaf elements, which are frozensets of strings. The keys at every level of this structure are strings. E.g. a simple example of such an attribute would look like: {'A': {'a': set(['1', '2', '3']), 'b': set(['4', '5'])}, 'B': set(['6', '7', '8'])} I'm looking for a good algorithm for computing a hash key for something like this? (Basically I'm looking for a good way to combine hashkeys.) Thanks! kj -- http://mail.python.org/mailman/listinfo/python-list
Re: C++ vs. Python Was: Re: help!!!
On Wed, Oct 6, 2010 at 12:16 PM, Hans-Peter Jansen h...@urpla.net wrote: On Wednesday 06 October 2010, 06:28:51 Dennis Lee Bieber wrote: On Tue, 05 Oct 2010 23:54:00 -0400, fkr...@aboutrafi.net23.net declaimed the following in gmane.comp.python.general: plz can u convert this cpp file into python i need that badly as soon as possible... I am new to python. I just wanna learn it Step one... DON'T TRY TO PORT C++ to Python... the object models are quite different. I do this all the time without any adverse effects (other than being glad to only rarely having the need of doing it the other way around ;-)). I have on occasion translated a few algorithms from python to c++ and only needed to spend minimal time pythonize the code. Generally I believe that porting from python to c++ and the other way around can be done pretty nicely but you always have to take care to restructure what has to be restructured. -- http://mail.python.org/mailman/listinfo/python-list
Re: suggestions please what should i watch for/guard against' in a file upload situation?
On 10/06/10 12:14, Seebs wrote: not sure what else i could do to guard against anything bad happening. maybe the file name itself could cause greif? Obvious things: * File name causes files to get created outside some particular upload directory (../foo) * File name has spaces * Crazy stuff like null bytes in file name * File names which might break things if a user carelessly interacts with them, such as foo.jpg /etc/passwd bar.jpg (all one file name including two spaces). And depending on the system, Win32 chokes on filenames like nul, con, com1...comN, lpt1...lptN, and a bunch of others. -tkc -- http://mail.python.org/mailman/listinfo/python-list
python-2.6.6 coredump running newspipe
Hi! I'm running newspipe-1.1.9, an RSS reader (http://newspipe.sourceforge.net/), on NetBSD-5.99.11/amd64 using Python-2.6.6. Sometimes, it core dumps with particular feeds in the configuration (I guess depending on the feed, because when I comment out the offending feed in the opml file, it runs through to completion). The backtrace looks like this: Core was generated by `python'. Program terminated with signal 10, Bus error. #0 0x7f7ffdc35a21 in PyOS_snprintf (str=0x7f7ff5dfe3d8 @, size=120, format=0x1 Address 0x1 out of bounds) at Python/mysnprintf.c:43 43 { (gdb) bt #0 0x7f7ffdc35a21 in PyOS_snprintf (str=0x7f7ff5dfe3d8 @, size=120, format=0x1 Address 0x1 out of bounds) at Python/mysnprintf.c:43 #1 0x7f7ffdc471a6 in PyOS_ascii_formatd (buffer=0x7f7ff5dfe3d8 @, buf_size=120, format=0x7f7ff5dfe388 %.2f, d=0.15256118774414062) at Python/pystrtod.c:455 #2 0x7f7ffdbaa7fa in formatfloat (buf=0x7f7ff5dfe3d8 @, buflen=120, flags=16, prec=2, type=102, v=0x7f7ffcc6d510) at Objects/stringobject.c:4378 #3 0x7f7ffdbabd32 in PyString_Format (format=0x7f7ffc8144e0, args=0x7f7ffcc6d510) at Objects/stringobject.c:4943 #4 0x7f7ffdbaa3b0 in string_mod (v=0x7f7ffc8144e0, w=0x7f7ffcc6d510) at Objects/stringobject.c:4116 #5 0x7f7ffdb459db in binary_op1 (v=0x7f7ffc8144e0, w=0x7f7ffcc6d510, op_slot=32) at Objects/abstract.c:917 #6 0x7f7ffdb45c81 in binary_op (v=0x7f7ffc8144e0, w=0x7f7ffcc6d510, op_slot=32, op_name=0x7f7ffdc6c089 %) at Objects/abstract.c:969 #7 0x7f7ffdb467ad in PyNumber_Remainder (v=0x7f7ffc8144e0, w=0x7f7ffcc6d510) at Objects/abstract.c:1221 #8 0x7f7ffdc08a03 in PyEval_EvalFrameEx (f=0x7f7fefa1dab0, throwflag=0) at Python/ceval.c:1180 #9 0x7f7ffdc1175f in fast_function (func=0x7f7ff8a9bed8, pp_stack=0x7f7ff5dfeae8, n=1, na=1, nk=0) at Python/ceval.c:3836 #10 0x7f7ffdc11565 in call_function (pp_stack=0x7f7ff5dfeae8, oparg=1) at Python/ceval.c:3771 #11 0x7f7ffdc0d81f in PyEval_EvalFrameEx (f=0x7f7fee920420, throwflag=0) at Python/ceval.c:2412 #12 0x7f7ffdc0f715 in PyEval_EvalCodeEx (co=0x7f7ffcc247b0, globals=0x7f7ffd1c5880, locals=0x0, args=0x7f7ff5b0aac8, argcount=8, kws=0x7f7ff5b0ab08, kwcount=0, defs=0x7f7ff8d3c4e8, defcount=5, closure=0x0) at Python/ceval.c:3000 #13 0x7f7ffdc1184a in fast_function (func=0x7f7ff8a9cc80, pp_stack=0x7f7ff5dfeff8, n=8, na=8, nk=0) at Python/ceval.c:3846 #14 0x7f7ffdc11565 in call_function (pp_stack=0x7f7ff5dfeff8, oparg=7) at Python/ceval.c:3771 #15 0x7f7ffdc0d81f in PyEval_EvalFrameEx (f=0x7f7ff5b0a820, throwflag=0) at Python/ceval.c:2412 #16 0x7f7ffdc1175f in fast_function (func=0x7f7ff8a9e140, pp_stack=0x7f7ff5dff358, n=1, na=1, nk=0) at Python/ceval.c:3836 #17 0x7f7ffdc11565 in call_function (pp_stack=0x7f7ff5dff358, oparg=0) at Python/ceval.c:3771 #18 0x7f7ffdc0d81f in PyEval_EvalFrameEx (f=0x7f7ff5b0a420, throwflag=0) at Python/ceval.c:2412 #19 0x7f7ffdc1175f in fast_function (func=0x7f7ffca1db90, pp_stack=0x7f7ff5dff6b8, n=1, na=1, nk=0) at Python/ceval.c:3836 #20 0x7f7ffdc11565 in call_function (pp_stack=0x7f7ff5dff6b8, oparg=0) at Python/ceval.c:3771 #21 0x7f7ffdc0d81f in PyEval_EvalFrameEx (f=0x7f7ff5b03190, throwflag=0) at Python/ceval.c:2412 #22 0x7f7ffdc0f715 in PyEval_EvalCodeEx (co=0x7f7ffca0d4e0, globals=0x7f7ffca473a0, locals=0x0, args=0x7f7ff04d3e68, argcount=1, kws=0x0, kwcount=0, defs=0x0, defcount=0, closure=0x0) at Python/ceval.c:3000 #23 0x7f7ffdb7a612 in function_call (func=0x7f7ffca1daa0, arg=0x7f7ff04d3e50, kw=0x0) at Objects/funcobject.c:524 #24 0x7f7ffdb495e8 in PyObject_Call (func=0x7f7ffca1daa0, arg=0x7f7ff04d3e50, kw=0x0) at Objects/abstract.c:2492 #25 0x7f7ffdb5eca0 in instancemethod_call (func=0x7f7ffca1daa0, arg=0x7f7ff04d3e50, kw=0x0) at Objects/classobject.c:2579 #26 0x7f7ffdb495e8 in PyObject_Call (func=0x7f7ff8ac2a00, arg=0x7f7ffd112050, kw=0x0) at Objects/abstract.c:2492 #27 0x7f7ffdc10cd3 in PyEval_CallObjectWithKeywords (func=0x7f7ff8ac2a00, arg=0x7f7ffd112050, kw=0x0) at Python/ceval.c:3619 #28 0x7f7ffdc4e69f in t_bootstrap (boot_raw=0x7f7ffd1b4590) at ./Modules/threadmodule.c:428 #29 0x7f7ffd90ba32 in pthread_setcancelstate () from /usr/lib/libpthread.so.1 #30 0x7f7ffd26e9b0 in ___lwp_park50 () from /usr/lib/libc.so.12 #31 0x in ?? () (gdb) fr 1 #1 0x7f7ffdc471a6 in PyOS_ascii_formatd (buffer=0x7f7ff5dfe3d8 @, buf_size=120, format=0x7f7ff5dfe388 %.2f, d=0.15256118774414062) at Python/pystrtod.c:455 455 PyOS_snprintf(buffer, buf_size, format, d); (gdb) l 450 format = tmp_format; 451 } 452 453 454 /* Have PyOS_snprintf do the hard work */ 455 PyOS_snprintf(buffer, buf_size, format, d); 456 457 /* Do various fixups on the return string */ 458 459 /* Get the current locale, and find the decimal point string. (gdb) p format $1 = 0x7f7ff5dfe388 %.2f (gdb) fr 0 #0
Re: help!!!
fkr...@aboutrafi.net23.net writes: plz can u convert this cpp file into python i need that badly as soon as possible... I am new to python. I just wanna learn it For such an aspiring student of the art of computer programming, I have the strange feeling of lack-of-effort-showing here. Do I have to lose my faith in youth? Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: How to save a binary file?
Ppl thanyou, for all your help finally i did it! thanks, another thing to i have to send a propouse code, i can fixed the litle problem of the wsig.input in Python 3 i will tested in the next months but i want to share the code with the comunnity, how i can do that? On Oct 6, 2010 1:45pm, MRAB pyt...@mrabarnett.plus.com wrote: On 06/10/2010 15:25, hid...@gmail.com wrote: When you put the 'wb' extension you have to pass a Encode the string Python does not accept a string on a wb file, Python3 [snip] You are using Python 3 and type(str) returns ? Binary data in Python 3 should be an instance of the 'bytes' class, not an instance of the 'str' class. If you can't fix that, you could turn the string into bytes using: data = bytes(ord(c) for c in str) or by carefully choosing an encoding which would give the same result: data = str.encode('latin-1') Then you can save it: s = open('/home/hidura/test.jpeg', 'wb') s.write(data) s.close() I asked you to look at the result of repr so that you could see more clearly what the data actually looked like, an instance of str or an instance of bytes. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: hashkey/digest for a complex object
kj no.em...@please.post wrote: The short version of this question is: where can I find the algorithm used by the tuple class's __hash__ method? http://svn.python.org/view/python/trunk/Objects/tupleobject.c?revision=81029view=markup static long tuplehash(PyTupleObject *v) { register long x, y; register Py_ssize_t len = Py_SIZE(v); register PyObject **p; long mult = 103L; x = 0x345678L; p = v-ob_item; while (--len = 0) { y = PyObject_Hash(*p++); if (y == -1) return -1; x = (x ^ y) * mult; /* the cast might truncate len; that doesn't change hash stability */ mult += (long)(82520L + len + len); } x += 97531L; if (x == -1) x = -2; return x; } I'm looking for a good algorithm for computing a hash key for something like this? (Basically I'm looking for a good way to combine hashkeys.) If you want to combine the hashes from several objects then the easiest way is just to create a tuple of the objects and hash it. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Re: How to save a binary file?
Ppl thanyou, for all your help finally i did it! thanks, another thing to who i can send a propose code, i fixed the little problem of the wsig.input in Python 3 i will tested in the next months but i want to share the code with the community, how i can do that? On Oct 6, 2010 3:13pm, hid...@gmail.com wrote: Ppl thanyou, for all your help finally i did it! thanks, another thing to i have to send a propouse code, i can fixed the litle problem of the wsig.input in Python 3 i will tested in the next months but i want to share the code with the comunnity, how i can do that? On Oct 6, 2010 1:45pm, MRAB pyt...@mrabarnett.plus.com wrote: On 06/10/2010 15:25, hid...@gmail.com wrote: When you put the 'wb' extension you have to pass a Encode the string Python does not accept a string on a wb file, Python3 [snip] You are using Python 3 and type(str) returns ? Binary data in Python 3 should be an instance of the 'bytes' class, not an instance of the 'str' class. If you can't fix that, you could turn the string into bytes using: data = bytes(ord(c) for c in str) or by carefully choosing an encoding which would give the same result: data = str.encode('latin-1') Then you can save it: s = open('/home/hidura/test.jpeg', 'wb') s.write(data) s.close() I asked you to look at the result of repr so that you could see more clearly what the data actually looked like, an instance of str or an instance of bytes. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: suggestions please what should i watch for/guard against' in a file upload situation?
Seebs usenet-nos...@seebs.net writes: On 2010-10-06, geekbuntu gmi...@gmail.com wrote: in general, what are things i would want to 'watch for/guard against' in a file upload situation? This question has virtually nothing to do with Python, which means you may not get very good answers. In contrast to comp.super.web.experts? There are quite a few people with web-experience here I'd say. my checklist so far is basically to check the extension - ensure it has 3 places, ensure it's in the allowed list (like jpg gif etc...). This strikes me as 100% irrelevant. Who cares what the extension is? Given that most people are not computer savvy (always remember, the default for windows is to hide extensions..), using it client-side can be valuable to prevent long uploads that eventuall need to be rejected otherwise (no mom, you can't upload word-docs as profile pictures). not sure what else i could do to guard against anything bad happening. maybe the file name itself could cause greif? Obvious things: * File name causes files to get created outside some particular upload directory (../foo) Or rather just store that as a simple meta-info, as allowing even the best-intended me-in-cool-pose.jpg to overwrite that of the one other cool guy using the website isn't gonna fly anyway. * File name has spaces See above, but other then that - everything but shell-scripts deal well with it. * Crazy stuff like null bytes in file name * File names which might break things if a user carelessly interacts with them, such as foo.jpg /etc/passwd bar.jpg (all one file name including two spaces). Your strange focus on file-names that are pure meta information is a little bit concerning... Basically, the key question is, could a hostile user come up with input to your script which could break something? Certainly advice. But that's less focussed on filenames or file-uploads, but on the whole subject of processing HTTP-requestst. Which would make a point for *not* using a home-grown framework. But then, Python is a bit less likely to suffer from buffer overflow or similar kind of attacks. Diez -- http://mail.python.org/mailman/listinfo/python-list
list parameter of a recursive function
Hi, I have a function f that calls itself recursively. It has a list as second argument, with default argument equal to None (and not [], as indicated at: http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) This is the outline of my function: def f ( argument, some_list = None ): if some_list == None: some_list = [] [...] # creation of a new_argument # the function is called recursively only if some condition is respected if some_condition: some_list.append( elem ) f( new_argument, some_list ) # otherwise, we have reached a leaf of the a branch of the recursive tree # (said differently, terminal condition has been reached for this branch) print Terminal condition The problem is that when the terminal condition is reached, we return back to some other branch of the recursive tree, but some_list has the value obtained in the previous branch! So, it seems that there is only one some_list list for all the recursive tree. To get rid of this behavior, I have been compelled to do at the beginning of f: import copy from copy some_list = copy( some_list ) I suppose this is not a surprise to you: I am compelled to create a new some_list with the same content. So, if I am right, all is happening as if the parameters of a function are always passed by address to the function. Whereas in C, they are always passed by copy (which gives relevance to pointers). Am I right? Julien -- python -c print ''.join([chr(154 - ord(c)) for c in '*9(9(18%.\ 91+,\'Z4(55l4(']) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong. (first law of AC Clarke) -- http://mail.python.org/mailman/listinfo/python-list
Re: hashkey/digest for a complex object
On Wed, Oct 6, 2010 at 11:58 AM, kj no.em...@please.post wrote: The short version of this question is: where can I find the algorithm used by the tuple class's __hash__ method? From Objects/tuple.c, line 315 in Python3.2: static long tuplehash(PyTupleObject *v) { register long x, y; register Py_ssize_t len = Py_SIZE(v); register PyObject **p; long mult = 103L; x = 0x345678L; p = v-ob_item; while (--len = 0) { y = PyObject_Hash(*p++); if (y == -1) return -1; x = (x ^ y) * mult; /* the cast might truncate len; that doesn't change hash stability */ mult += (long)(82520L + len + len); } x += 97531L; if (x == -1) x = -2; return x; } Now, for the long version of this question, I'm working with some complext Python objects that I want to be able to compare for equality easily. These objects are non-mutable once they are created, so I would like to use a two-step comparison for equality, based on the assumption that I can compute (either at creation time, or as needed and memoized) a hashkey/digest for each object. The test for equality of two of these objects would first compare their hashkeys. If they are different, the two objects are declared different; if they match, then a more stringent test for equality is performed. So the problem is to come up with a reasonable hashkey for each of these objects. The objects have two significant attributes, and two of these objects should be regarded as equal if these attributes are the same in both. The first attribute is a simple dictionary whose keys are integers and values are strings. The second attribute is more complicated. It is a tree structure, represented as a dictionary of dictionaries of dictionaries... until we get to the leaf elements, which are frozensets of strings. The keys at every level of this structure are strings. E.g. a simple example of such an attribute would look like: {'A': {'a': set(['1', '2', '3']), 'b': set(['4', '5'])}, 'B': set(['6', '7', '8'])} I'm looking for a good algorithm for computing a hash key for something like this? (Basically I'm looking for a good way to combine hashkeys.) Sounds like you're trying to hash mutable data structures, which is a no-no, but assuming you've got that part all figured out then the easy way out would be to print the whole thing and take the hash of the resulting string. A (possibly) better way would be to do a Schwartzian transform[1] and freeze everything. Other approaches may be better, but those are the first two out of my head. Geremy Condra [1] http://en.wikipedia.org/wiki/Schwartzian_transform -- http://mail.python.org/mailman/listinfo/python-list
Re: hashkey/digest for a complex object
kj no.em...@please.post writes: The short version of this question is: where can I find the algorithm used by the tuple class's __hash__ method? Surprisingly, in the source: http://google.com/codesearch/p?hl=de#-2BKs-LW4I0/trunk/python/src/Objects/tupleobject.cq=python%20tuplehashsa=Ncd=1ct=rc Now, for the long version of this question, I'm working with some complext Python objects that I want to be able to compare for equality easily. These objects are non-mutable once they are created, so I would like to use a two-step comparison for equality, based on the assumption that I can compute (either at creation time, or as needed and memoized) a hashkey/digest for each object. The test for equality of two of these objects would first compare their hashkeys. If they are different, the two objects are declared different; if they match, then a more stringent test for equality is performed. So the problem is to come up with a reasonable hashkey for each of these objects. The objects have two significant attributes, and two of these objects should be regarded as equal if these attributes are the same in both. The first attribute is a simple dictionary whose keys are integers and values are strings. The second attribute is more complicated. It is a tree structure, represented as a dictionary of dictionaries of dictionaries... until we get to the leaf elements, which are frozensets of strings. The keys at every level of this structure are strings. E.g. a simple example of such an attribute would look like: {'A': {'a': set(['1', '2', '3']), 'b': set(['4', '5'])}, 'B': set(['6', '7', '8'])} I'm looking for a good algorithm for computing a hash key for something like this? (Basically I'm looking for a good way to combine hashkeys.) Creating tuples from dicts, recursively, and stabilized by using sorted on items. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: hashkey/digest for a complex object
On 10/6/10 1:58 PM, kj wrote: The short version of this question is: where can I find the algorithm used by the tuple class's __hash__ method? The function tuplehash() in Objects/tupleobject.c, predictably enough. Now, for the long version of this question, I'm working with some complext Python objects that I want to be able to compare for equality easily. These objects are non-mutable once they are created, so I would like to use a two-step comparison for equality, based on the assumption that I can compute (either at creation time, or as needed and memoized) a hashkey/digest for each object. The test for equality of two of these objects would first compare their hashkeys. If they are different, the two objects are declared different; if they match, then a more stringent test for equality is performed. The most straightforward way to implement __hash__ for a complicated object is to normalize its relevant data to a tuple of hashable objects and then call hash() on that tuple. A straightforward way to compare such objects is to calculate that very same normalized tuple for each one and compare those tuples. Cache it if necessary. Don't bother hashing to implement __eq__ unless if you are really optimizing for space and time. -- 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: suggestions please what should i watch for/guard against' in a file upload situation?
On Wed, 06 Oct 2010 09:02:21 -0700, geekbuntu wrote: in general, what are things i would want to 'watch for/guard against' in a file upload situation? i have my file upload working (in the self-made framework @ work without any concession for multipart form uploads), but was told to make sure it's cleansed and cannot do any harm inside the system. Off the top of my head, and assuming that you get passed the exact filename that the user entered: - The user may need to use an absolute pathname to upload a file that isn't in his current directory, so retain only the basename by discarding the rightmost slash and everything to the left of it: /home/auser/photos/my_photo.jpg === my_photo.jpg c:\My Photos\My Photo.jpg === My Photo.jpg - If your target system doesn't like spaces in names or you want to be on the safe side there, replace spaces in the name with underscores: My Photo.jpg ===My_Photo.jpg - reject any filenames that could cause the receiving system to do dangerous things, e.g. .EXE or .SCR if the upload target is Windows. This list will be different for each upload target, so make it configurable. You can't assume anything about else about the extension. .py .c .txt and .html are all valid in the operating systems I use and so are their capitalised equivalents. - check whether the file already exists. You need rules about what to do if it exists (do you reject the upload, silently overwrite, or alter the name, e.g. by adding a numeric suffix to make the name unique: my_photo.jpg === my_photo-01.jpg - run the application in your upload target directory and put the uploaded file there or, better, into a configured uploads directory by prepending it to the file name: my_photo.jpg === /home/upload_user/uploads/my_photo.jpg - make sure you document the process so that a user can work out what has happened to his file and why if you have to reject it or alter its name. not sure but any suggestions or examples are most welcome :) There's probably something I've forgotten, but that list should get you going. -- martin@ | Martin Gregorie gregorie. | Essex, UK org | -- http://mail.python.org/mailman/listinfo/python-list
Re: list parameter of a recursive function
In article rsuun7-eml@rama.universe TP tribulati...@paralleles.invalid wrote: I have a function f that calls itself recursively. It has a list as second argument, with default argument equal to None (and not [], as indicated at: http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) This is the outline of my function: def f ( argument, some_list = None ): if some_list == None: some_list = [] [...] # creation of a new_argument # the function is called recursively only if some condition is respected if some_condition: some_list.append( elem ) f( new_argument, some_list ) # otherwise, we have reached a leaf of the a branch of the recursive tree # (said differently, terminal condition has been reached for this branch) print Terminal condition The problem is that when the terminal condition is reached, we return back to some other branch of the recursive tree, but some_list has the value obtained in the previous branch! Yes, this is the way it is supposed to work. :-) So, it seems that there is only one some_list list for all the recursive tree. To get rid of this behavior, I have been compelled to do at the beginning of f: import copy from copy [from copy import copy, rather] some_list = copy( some_list ) I suppose this is not a surprise to you: I am compelled to create a new some_list with the same content. The above will work, or for this specific case, you can write: some_list = list(some_list) which has the effect of making a shallow copy of an existing list: base = [1, 2] l1 = base l2 = list(l1) l1 is l2 False l1[0] is l2[0] True base.append(3) l2 [[1, 2, 3]] but will also turn *any* iterator into a (new) list; the latter may often be desirable. So, if I am right, all is happening as if the parameters of a function are always passed by address to the function. Whereas in C, they are always passed by copy (which gives relevance to pointers). Am I right? Mostly. Python distinguishes between mutable and immutable items. Mutable items are always mutable, immutable items are never mutable, and the mutability of arguments is attached to their fundamental mutability rather than to their being passed as arguments. This is largely a point-of-view issue (although it matters a lot to people writing compilers, for instance). Note that if f() is *supposed* to be able to modify its second parameter under some conditions, you would want to make the copy not at the top of f() but rather further in, and in this case, that would be trivial: def f(arg, some_list = None): if some_list is None: some_list = [] ... if some_condition: # make copy of list and append something f(new_arg, some_list + [elem]) elif other_condition: # continue modifying same list f(new_arg, some_list) ... (Note: you can use also the fact that list1 + list2 produces a new list to make a copy by writing x = x + [], or x = [] + x, but x = list(x) is generally a better idea here.) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Strong typing vs. strong testing
In article 1a172248-8aab-42f0-a8a2-3f00168f9...@u13g2000vbo.googlegroups.com, Keith H Duggar dug...@alum.mit.edu wrote: On Sep 29, 9:01 pm, RG rnospa...@flownet.com wrote: That the problem is elsewhere in the program ought to be small comfort. But very well, try this instead: [...@mighty:~]$ cat foo.c #include stdio.h int maximum(int a, int b) { return a b ? a : b; } int main() { long x = 8589934592; printf(Max of %ld and 1 is %d\n, x, maximum(x,1)); return 0;} [...@mighty:~]$ gcc -Wall foo.c [...@mighty:~]$ ./a.out Max of 8589934592 and 1 is 1 $ gcc -Wconversion -Werror foo.c cc1: warnings being treated as errors foo.c: In function 'main': foo.c:5: warning: passing argument 1 of 'maximum' with different width due to prototype It's called learning to compile. It's called being late for the game. This has already been raised and addressed. Read the rest of the thread. rg -- http://mail.python.org/mailman/listinfo/python-list
Re: list parameter of a recursive function
Chris Torek wrote: import copy from copy [from copy import copy, rather] Yes, sorry. Note that if f() is *supposed* to be able to modify its second parameter under some conditions, you would want to make the copy not at the top of f() but rather further in, and in this case, that would be trivial: def f(arg, some_list = None): if some_list is None: some_list = [] ... if some_condition: # make copy of list and append something f(new_arg, some_list + [elem]) elif other_condition: # continue modifying same list f(new_arg, some_list) Thanks a lot Chris! I think I prefer doing an explicit copy.copy, because it allows to remind the reader that it is very important to take care of that. But your trick is very interesting also. Cheers, Julien -- python -c print ''.join([chr(154 - ord(c)) for c in '*9(9(18%.\ 91+,\'Z4(55l4(']) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong. (first law of AC Clarke) -- http://mail.python.org/mailman/listinfo/python-list
Re: help!!!
On 2010-10-06, Diez B. Roggisch de...@web.de wrote: fkr...@aboutrafi.net23.net writes: plz can u convert this cpp file into python i need that badly as soon as possible... I am new to python. I just wanna learn it For such an aspiring student of the art of computer programming, I have the strange feeling of lack-of-effort-showing here. Do I have to lose my faith in youth? Never! Just be sure you are having faith in them to, well, be youth. :) -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nos...@seebs.net http://www.seebs.net/log/ -- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) -- get educated! I am not speaking for my employer, although they do rent some of my opinions. -- http://mail.python.org/mailman/listinfo/python-list
Re: suggestions please what should i watch for/guard against' in a file upload situation?
On 2010-10-06, Diez B. Roggisch de...@web.de wrote: Seebs usenet-nos...@seebs.net writes: On 2010-10-06, geekbuntu gmi...@gmail.com wrote: in general, what are things i would want to 'watch for/guard against' in a file upload situation? This question has virtually nothing to do with Python, which means you may not get very good answers. In contrast to comp.super.web.experts? There are quite a few people with web-experience here I'd say. Oh, certainly. But in general, I try to ask questions in a group focused on their domain, rather than merely a group likely to contain people who would for other reasons have the relevant experience. I'm sure that a great number of Python programmers have experience with sex, that doesn't make this a great newsgroup for sex tips. (Well, maybe it does.) Given that most people are not computer savvy (always remember, the default for windows is to hide extensions..), using it client-side can be valuable to prevent long uploads that eventuall need to be rejected otherwise (no mom, you can't upload word-docs as profile pictures). That's a good point. On the other hand, there's a corollary; you may want to look at the contents of the file in case they're not really what they're supposed to be. Your strange focus on file-names that are pure meta information is a little bit concerning... If you're uploading files into a directory, then it is quite likely that you're getting file names from somewhere. Untrusted file names are a much more effective attack vector, in most cases, than EXIF information. Certainly advice. But that's less focussed on filenames or file-uploads, but on the whole subject of processing HTTP-requestst. Which would make a point for *not* using a home-grown framework. Well, yeah. I was assuming that the home-grown framework was mandatory for some reason. Possibly a very important reason, such as otherwise we won't have written it ourselves. -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nos...@seebs.net http://www.seebs.net/log/ -- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) -- get educated! I am not speaking for my employer, although they do rent some of my opinions. -- http://mail.python.org/mailman/listinfo/python-list
Re: help!!!
On Tue, 05 Oct 2010 23:54:00 -0400, fkrafi wrote: plz can u convert this cpp file into python i need that badly as soon as possible... I am new to python. I just wanna learn it Good grief. It's bad enough to expect us to do your coding for you, without making even the *tiniest* effort to learn yourself, but then to add insult to injury you use text-speak. ur nt sending sms now, learn to rite the wrds If you want to learn Python, I suggest you work through some tutorials. Google is your friend: google on python tutorial and see what comes up. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: list parameter of a recursive function
TP tribulati...@paralleles.invalid writes: Hi, I have a function f that calls itself recursively. It has a list as second argument, with default argument equal to None (and not [], as indicated at: http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) This is the outline of my function: def f ( argument, some_list = None ): if some_list == None: some_list = [] [...] # creation of a new_argument # the function is called recursively only if some condition is respected if some_condition: some_list.append( elem ) f( new_argument, some_list ) # otherwise, we have reached a leaf of the a branch of the recursive tree # (said differently, terminal condition has been reached for this branch) print Terminal condition The problem is that when the terminal condition is reached, we return back to some other branch of the recursive tree, but some_list has the value obtained in the previous branch! So, it seems that there is only one some_list list for all the recursive tree. To get rid of this behavior, I have been compelled to do at the beginning of f: import copy from copy some_list = copy( some_list ) I suppose this is not a surprise to you: I am compelled to create a new some_list with the same content. So, if I am right, all is happening as if the parameters of a function are always passed by address to the function. Whereas in C, they are always passed by copy (which gives relevance to pointers). Am I right? Yes and no. For some sensible definition of yes (this is to avoid the *endless* discussions about python parameter passing semantics and it's relation to common, yet misunderstood or unprecisely defined definitions of parameter passing), passing around a mutable object (such as a list or dictionary) will be like a pointer - mutations are visible to all others having a reference to it. You are wrong though that C-semantics are different, meaning that passing by copy is not what happens. Some things are copied (primitive types, structs that are passed directly). But not, for example - and relevant to this case - arrays (as they are only pointers), lists (as they don't exist, but are modeled by structs containing pointers). Back to your example: your solution is perfectly fine, although a bit costly and more error-prone if you happen to forget to create a copy. A safer alternative for these cases is using tuples, because they are immutable. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: suggestions please what should i watch for/guard against' in a file upload situation?
On Wed, 06 Oct 2010 09:02:21 -0700, geekbuntu wrote: in general, what are things i would want to 'watch for/guard against' in a file upload situation? i have my file upload working (in the self-made framework @ work without any concession for multipart form uploads), but was told to make sure it's cleansed and cannot do any harm inside the system. Make sure *what* is cleansed? Your code? The uploaded files? Define cleansed. Do you have to block viruses, malware, spybots, illegal pornography, legal pornography, illegal content, warez, copyright violations, stolen trade secrets, dirty words, pictures of cats? What operating system are you uploading to? What happens if somebody tries to upload a 1 TB file to your server? What happens if they try to upload a billion 1 KB files instead? my checklist so far is basically to check the extension - ensure it has 3 places, ensure it's in the allowed list (like jpg gif etc...). Do you have something against file extensions like .gz or .jpeg ? I'm not sure why you think you need to check the file extension. not sure what else i could do to guard against anything bad happening. maybe the file name itself could cause greif? You think? :) What happens if the file name has characters in it that your file system can't deal with? Bad unicode, binary bytes, slashes, colons, question marks, asterisks, etc. What about trying to break out of your file storage area using .. paths? Without knowing what your file upload code actually does, it's hard to give specific advice. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: list parameter of a recursive function
On Wed, 06 Oct 2010 23:00:10 +0200, TP wrote: I think I prefer doing an explicit copy.copy, because it allows to remind the reader that it is very important to take care of that. I think a comment is better for that. It's better to explicitly say something is important than to assume the reader will guess. Besides, what if the caller wants to supply their own list, and they *want* it modified in place rather than a copy made? (I have no idea why they might want this, but anything is possible...) You should avoid making copies except in methods that are *for* making copies. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: suggestions please what should i watch for/guard against' in a file upload situation?
Seebs usenet-nos...@seebs.net writes: On 2010-10-06, Diez B. Roggisch de...@web.de wrote: Seebs usenet-nos...@seebs.net writes: On 2010-10-06, geekbuntu gmi...@gmail.com wrote: in general, what are things i would want to 'watch for/guard against' in a file upload situation? This question has virtually nothing to do with Python, which means you may not get very good answers. In contrast to comp.super.web.experts? There are quite a few people with web-experience here I'd say. Oh, certainly. But in general, I try to ask questions in a group focused on their domain, rather than merely a group likely to contain people who would for other reasons have the relevant experience. I'm sure that a great number of Python programmers have experience with sex, that doesn't make this a great newsgroup for sex tips. (Well, maybe it does.) As the OP asked about a Python web framework (self written or not), I think all advice that can be given is certainly more related to Python than to airy references to general web programming such as oh, make sure if your server side application environment hasn't any security issues. Or, to be more concrete: what NG would you suggest for frameworks or webapps written in python to ask this question? Given that most people are not computer savvy (always remember, the default for windows is to hide extensions..), using it client-side can be valuable to prevent long uploads that eventuall need to be rejected otherwise (no mom, you can't upload word-docs as profile pictures). That's a good point. On the other hand, there's a corollary; you may want to look at the contents of the file in case they're not really what they're supposed to be. For sure. But the focus of you and others seems to be the file-name, as if that was anything especially dangerous. Matter of factly, it's a paramteter to a multipart/form-data encoded request body parameter definition, and as such has a rather locked-down in terms of null-bytes and such. So you are pretty safe as long as you - use standard library request parsing modules such as cgi. If one instist on reading streams bytewise and using ctypes to poke the results into memory, you can of course provoke unimaginable havoc.. - don't use the filename for anything but meta-info. And ususally, they are simply regarded as nice that you've provided us with it, we try make our best to fill an img alt attribute with the basename. But not more. Worth pointing out to the OP to do that. But this is *not* a matter of mapping HTTP-request paths to directories I'd wager to say. Something that is of much more importance (I should have mentioned earlier, shame on me) is of course file-size. Denying requests that come with CONTENT_LENGTH over a specified limit, of course respecting CONTENT_LENGTH and not reading beyond it, and possibly dealing with chunked-encodings in similarily safe ways (I have to admit I haven't yet dealt with one of those myself on a visceral level - but as they are part of the HTTP-spec...) is important, as otherwise DOS attacks are possible. Your strange focus on file-names that are pure meta information is a little bit concerning... If you're uploading files into a directory, then it is quite likely that you're getting file names from somewhere. Untrusted file names are a much more effective attack vector, in most cases, than EXIF information. The into a directory quote coming from where? And given that EXIF information is probably read by some C-lib, I'd say it is much more dangerous. This is a gut feeling only, but fed by problems with libpng a year or two ago. Certainly advice. But that's less focussed on filenames or file-uploads, but on the whole subject of processing HTTP-requestst. Which would make a point for *not* using a home-grown framework. Well, yeah. I was assuming that the home-grown framework was mandatory for some reason. Possibly a very important reason, such as otherwise we won't have written it ourselves. In Python, it's usually more along the lines of well, we kinda started, and now we have it, and are reluctant to switch. But of course one never knows... Diez -- http://mail.python.org/mailman/listinfo/python-list
mantissa and exponent in base 10
I want the mantissa and decimal exponent of a float, in base 10: mantissa and exponent of 1.2345e7 = (1.2345, 7) (0.12345, 8) would also be acceptable. The math module has a frexp() function, but it produces a base-2 exponent: math.frexp(1.2345e7) (0.73581933975219727, 24) Have I missed a built-in or math function somewhere? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: suggestions please what should i watch for/guard against' in a file upload situation?
Martin Gregorie mar...@address-in-sig.invalid writes: On Wed, 06 Oct 2010 09:02:21 -0700, geekbuntu wrote: in general, what are things i would want to 'watch for/guard against' in a file upload situation? i have my file upload working (in the self-made framework @ work without any concession for multipart form uploads), but was told to make sure it's cleansed and cannot do any harm inside the system. Off the top of my head, and assuming that you get passed the exact filename that the user entered: - The user may need to use an absolute pathname to upload a file that isn't in his current directory, so retain only the basename by discarding the rightmost slash and everything to the left of it: /home/auser/photos/my_photo.jpg === my_photo.jpg c:\My Photos\My Photo.jpg === My Photo.jpg - If your target system doesn't like spaces in names or you want to be on the safe side there, replace spaces in the name with underscores: My Photo.jpg ===My_Photo.jpg - reject any filenames that could cause the receiving system to do dangerous things, e.g. .EXE or .SCR if the upload target is Windows. This list will be different for each upload target, so make it configurable. Erm, this assumes that the files are executed in some way. Why should they? It's perfectly fine to upload *anything*, and of course filenames mean nothing wrt to the actual file contents (Are you sure you want to change the extension of this file?). It might make no sense for the user, because you can't shon an exe as profile image. But safe-guarding against that has nothing to do with OS. And even safe file formats such as PNGs have been attack vectors. Precisely because they are processed client-side in the browser through some library with security issues. For serving the files, one could rely on the file-command or similar means to determine the mime-type. So far, I've never done that - as faking the extension for something else doesn't buy you something unless there is a documented case of internet explorer ignoring mime-type, and executing downloaded file as program. You can't assume anything about else about the extension. .py .c .txt and .html are all valid in the operating systems I use and so are their capitalised equivalents. - check whether the file already exists. You need rules about what to do if it exists (do you reject the upload, silently overwrite, or alter the name, e.g. by adding a numeric suffix to make the name unique: my_photo.jpg === my_photo-01.jpg Better, associate the file with the uploader and or it's hash. Use the name as pure meta-information only. There's probably something I've forgotten, but that list should get you going. Dealing with to large upload requests I'd say is much more important, as careless reading of streams into memory has at least the potential for a DOS-attack. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: help!!!
Seebs usenet-nos...@seebs.net writes: On 2010-10-06, Diez B. Roggisch de...@web.de wrote: fkr...@aboutrafi.net23.net writes: plz can u convert this cpp file into python i need that badly as soon as possible... I am new to python. I just wanna learn it For such an aspiring student of the art of computer programming, I have the strange feeling of lack-of-effort-showing here. Do I have to lose my faith in youth? Never! Just be sure you are having faith in them to, well, be youth. :) I took the freedom to google a bit. If one can trust people on the inter-tubes (and who would have heard you can't?), he's a rather skilled student http://www.aboutrafi.net23.net/ With an impressive amount of technological experience under his belt. So I'm a bit aghast to see him struggle with this rather simple problem. Thus my question... Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: if the else short form
In message i8i1h8$dc...@news.eternal-september.org, BartC wrote: I use this syntax where there are two possibilities chosen according to condition 'a': (a | b | c) Algol 68! x = (One,Two,Three) [i-1] While this works for i = 1,2,3, it goes funny for i=0,-1,-2, and generates an error for the rest ... x = {1 : One, 2 : Two, 3 : Three}.get(i, None Of The Above) -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python-ideas] [Python-Dev] Inclusive Range
In message mailman.1390.1286363672.29448.python-l...@python.org, Antoon Pardon wrote: Or what if the keys were floats or tuples and I wanted an inclusive upper boundary? If you’re expecting computer floats to behave as mathematically exact quantities, you’re asking for trouble. -- http://mail.python.org/mailman/listinfo/python-list
Re: help!!!
On 2010-10-06, Diez B. Roggisch de...@web.de wrote: With an impressive amount of technological experience under his belt. So I'm a bit aghast to see him struggle with this rather simple problem. Thus my question... It does seem a bit odd. I mean, if I had a short time line to get something converted to Python, I'd probably ask for help, but then, I wouldn't take a gig where I needed to quickly move something into an unfamiliar language. I'm happily puttering away slowly at converting a couple of things to Python to get the hang of it, carefully selecting things with no impending deadlines. Thus far, I've converted a shell script to Python. It was... well, shell turned out to be a poor language for it, but I do note that the shell script, including all the here documents, was quite a bit smaller than the Python. On the other hand, I think the Python script is a lot more maintainable, and this despite me not really knowing the language particularly well. It's an interesting tradeoff. Still getting used to the Python way of doing things. -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nos...@seebs.net http://www.seebs.net/log/ -- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) -- get educated! I am not speaking for my employer, although they do rent some of my opinions. -- http://mail.python.org/mailman/listinfo/python-list
Re: help!!!
Seebs usenet-nos...@seebs.net writes: On 2010-10-06, Diez B. Roggisch de...@web.de wrote: With an impressive amount of technological experience under his belt. So I'm a bit aghast to see him struggle with this rather simple problem. Thus my question... It does seem a bit odd. I mean, if I had a short time line to get something converted to Python, I'd probably ask for help, but then, I wouldn't take a gig where I needed to quickly move something into an unfamiliar language. I'm happily puttering away slowly at converting a couple of things to Python to get the hang of it, carefully selecting things with no impending deadlines. From the look of it... he's just trying to get a freebie on a homework assignment. It certainly is no commercial/useful piece of code whatsoever... just CS-101 read data from stdin and do stuff. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: help!!!
On 2010-10-06, Diez B. Roggisch de...@web.de wrote: From the look of it... he's just trying to get a freebie on a homework assignment. It certainly is no commercial/useful piece of code whatsoever... just CS-101 read data from stdin and do stuff. Ooh, I should go help, then. I *love* to help people on homework. Although I may not know Python well enough. I once responded to a request for a thing that, given a number, says something like The first digit is 3, the second digit is 2 with a program that included a partial table of ln(x) for about 30 values between 0 and 900... It worked, though! -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nos...@seebs.net http://www.seebs.net/log/ -- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) -- get educated! I am not speaking for my employer, although they do rent some of my opinions. -- http://mail.python.org/mailman/listinfo/python-list
Re: help!!!
On 2010-10-06, Steven D'Aprano steve-remove-t...@cybersource.com.au wrote: On Tue, 05 Oct 2010 23:54:00 -0400, fkrafi wrote: plz can u convert this cpp file into python i need that badly as soon as possible... I am new to python. I just wanna learn it Good grief. It's bad enough to expect us to do your coding for you, without making even the *tiniest* effort to learn yourself, but then to add insult to injury you use text-speak. ur nt sending sms now, learn to rite the wrds LOL! I was particularly impressed by the pithy subject line. The extra exclamation points are always the hallmark of the clueless and rude. If you want to learn Python, I suggest you work through some tutorials. Google is your friend: google on python tutorial and see what comes up. -- Grant -- http://mail.python.org/mailman/listinfo/python-list
Re: if the else short form
Lawrence D'Oliveiro l...@geek-central.gen.new_zealand wrote in message news:i8j0dg$lh...@lust.ihug.co.nz... In message i8i1h8$dc...@news.eternal-september.org, BartC wrote: x = (One,Two,Three) [i-1] While this works for i = 1,2,3, it goes funny for i=0,-1,-2, and generates an error for the rest ... x = {1 : One, 2 : Two, 3 : Three}.get(i, None Of The Above) Yes, I expected Python to come up with something (it always does). However, as I mentioned, one problem here is having to evaluate all the items in the list before selecting one: def fna(): print FNA CALLED return One def fnb(): print FNB CALLED return Two def fnc(): print FNC CALLED return Three i=16 x = {1 : fna(), 2 : fnb(), 3 : fnc()}.get(i, None Of The Above) print x Other than efficiency concerns, sometimes you don't want the extra side-effects. Probably there are workarounds here too, but I suspect the syntax won't be quite as pert as the Algol68-style example: x = (i | Zero, One, Two | None of the above) # 0-based -- bartc -- http://mail.python.org/mailman/listinfo/python-list
Re: if the else short form
BartC wrote: Lawrence D'Oliveiro l...@geek-central.gen.new_zealand wrote in message news:i8j0dg$lh...@lust.ihug.co.nz... In message i8i1h8$dc...@news.eternal-september.org, BartC wrote: x = (One,Two,Three) [i-1] While this works for i = 1,2,3, it goes funny for i=0,-1,-2, and generates an error for the rest ... x = {1 : One, 2 : Two, 3 : Three}.get(i, None Of The Above) Yes, I expected Python to come up with something (it always does). However, as I mentioned, one problem here is having to evaluate all the items in the list before selecting one: def fna(): print FNA CALLED return One def fnb(): print FNB CALLED return Two def fnc(): print FNC CALLED return Three i=16 x = {1 : fna(), 2 : fnb(), 3 : fnc()}.get(i, None Of The Above) print x Other than efficiency concerns, sometimes you don't want the extra side-effects. Probably there are workarounds here too, The workaround would be (untested) x = {1: fna, 2: fnb, 3: fnc}.get(i, lambda: None Of The Above) () Mel. -- http://mail.python.org/mailman/listinfo/python-list
Re: if the else short form
On Thu, 07 Oct 2010 01:36:33 +0100, BartC wrote: However, as I mentioned, one problem here is having to evaluate all the items in the list before selecting one: def fna(): print FNA CALLED return One def fnb(): print FNB CALLED return Two def fnc(): print FNC CALLED return Three i=16 x = {1 : fna(), 2 : fnb(), 3 : fnc()}.get(i, None Of The Above) print x Other than efficiency concerns, sometimes you don't want the extra side-effects. Try this instead: i=16 x = {1 : fna, 2 : fnb, 3 : fnc}.get(i, None Of The Above)() print x Also known as the command dispatch pattern. First class functions are a wonderful thing-ly y'rs, -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Strong typing vs. strong testing
Keith H Duggar dug...@alum.mit.edu writes: On Sep 29, 9:01 pm, RG rnospa...@flownet.com wrote: That the problem is elsewhere in the program ought to be small comfort. But very well, try this instead: [...@mighty:~]$ cat foo.c #include stdio.h int maximum(int a, int b) { return a b ? a : b; } int main() { long x = 8589934592; printf(Max of %ld and 1 is %d\n, x, maximum(x,1)); return 0;} [...@mighty:~]$ gcc -Wall foo.c [...@mighty:~]$ ./a.out Max of 8589934592 and 1 is 1 $ gcc -Wconversion -Werror foo.c cc1: warnings being treated as errors foo.c: In function 'main': foo.c:5: warning: passing argument 1 of 'maximum' with different width due to prototype It's called learning to compile. And, yes, those warnings (and nearly every other one) should be enabled and treated as errors if a shop wants maximum protection. I only wish more (library) vendors did so. So you're wishing that they'd be active by default. -- __Pascal Bourguignon__ http://www.informatimago.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: help!!!
On 2010-10-06, fkr...@aboutrafi.net23.net fkr...@aboutrafi.net23.net wrote: plz can u convert this cpp file into python i need that badly as soon as possible... I am new to python. I just wanna learn it Having come to realize that this is a homework problem, I would of course be glad to. The original program: #includecstdio int main() { int a[100], n; freopen(input.txt, r, stdin); scanf(%d, n); for(int i=1; i=n; i++) scanf(%d, a[i]); return 0; } So, let's convert this. #!/usr/bin/python -w import cstdio; def main(): a = [random.randint(-2147483648,2147483647) for i in range(0,100)] n = random.randint(-2147483648) sys.stdin = open(input.txt, r) raise IndexError I'm not sure how much good this will do you. It seems to me that you might get better results translating a program which was not obviously incorrect. -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nos...@seebs.net http://www.seebs.net/log/ -- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) -- get educated! I am not speaking for my employer, although they do rent some of my opinions. -- http://mail.python.org/mailman/listinfo/python-list
Re: help!!!
On 10/6/2010 8:02 PM, Seebs wrote: On 2010-10-06,fkr...@aboutrafi.net23.net fkr...@aboutrafi.net23.net wrote: plz can u convert this cpp file into python i need that badly as soon as possible... I am new to python. I just wanna learn it Having come to realize that this is a homework problem, I would of course be glad to. The original program: #includecstdio int main() { int a[100], n; freopen(input.txt, r, stdin); scanf(%d,n); for(int i=1; i=n; i++) scanf(%d,a[i]); return 0; } This guy doesn't know C very well, either. First, scanf was deprecated over five years ago. And reopening stdin is rarely done, although it is possible. Second, there's a buffer overflow. n is obtained from external input, then used to control the for statement. If N is = 99, there will be a buffer overflow on a. Also, starting to fill a from 1 looks like an error. Third, the program doesn't actually do anything. There's no output. It looks like this approach to C came from http://beej.us/guide/bgc/output/html/multipage/freopen.html John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: help!!!
On 2010-10-07, John Nagle na...@animats.com wrote: First, scanf was deprecated over five years ago. It was? I mean, people have been telling me not to use it since the 80s, but I wasn't aware that it had been deprecated, except in the sense of being derided and dismissed as of no value. -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nos...@seebs.net http://www.seebs.net/log/ -- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) -- get educated! I am not speaking for my employer, although they do rent some of my opinions. -- http://mail.python.org/mailman/listinfo/python-list
Re: list parameter of a recursive function
Diez B. Roggisch wrote: Back to your example: your solution is perfectly fine, although a bit costly and more error-prone if you happen to forget to create a copy. A safer alternative for these cases is using tuples, because they are immutable. Thanks Diez for your explanation. The problem with tuples is that it is not easy to modify them: in my case, I need to append a string to the tuple at each recursive step. So, it seems to me that I need to write a loop to modify the tuple, because on a simple example: a=(foo, bar) b=(a[0],a[1],toto) b (u'foo', u'bar', u'toto') I do not find any other means to obtain that result for b. With lists, I can use .extend(). Am I right? -- python -c print ''.join([chr(154 - ord(c)) for c in '*9(9(18%.\ 91+,\'Z4(55l4(']) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong. (first law of AC Clarke) -- http://mail.python.org/mailman/listinfo/python-list
Re: list parameter of a recursive function
Steven D'Aprano wrote: I think I prefer doing an explicit copy.copy, because it allows to remind the reader that it is very important to take care of that. I think a comment is better for that. It's better to explicitly say something is important than to assume the reader will guess. Yes, you are right. Besides, what if the caller wants to supply their own list, and they *want* it modified in place rather than a copy made? (I have no idea why they might want this, but anything is possible...) You should avoid making copies except in methods that are *for* making copies. My function gives a sensible result only if the list is not modified in place. In fact, this list is only used internally for the recursion. That is why I have made this function private: def __relation_loop( sqlite_cursor , table_name , crossed_tables = None , previous_step_link = None , onetomany_allowed_depth = 1 ): I give to the user (me!) only access to the public function: def relation_loop( sqlite_cursor , table_name , onetomany_allowed_depth = 1 ): return __relation_loop( sqlite_cursor , table_name , crossed_tables = None , previous_step_link = None , onetomany_allowed_depth = onetomany_allowed_depth ) So, crossed_tables and previous_step_link, are only used by the recursion process, they are not accessible to the user. -- python -c print ''.join([chr(154 - ord(c)) for c in '*9(9(18%.\ 91+,\'Z4(55l4(']) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong. (first law of AC Clarke) -- http://mail.python.org/mailman/listinfo/python-list
Re: suggestions please what should i watch for/guard against' in a file upload situation?
In message 2ce3860b-ae21-48ae-9abc-cb169a6f1...@e20g2000vbn.googlegroups.com, geekbuntu wrote: in general, what are things i would want to 'watch for/guard against' in a file upload situation? If you stored the file contents as a blob in a database field, you wouldn’t have to worry about filename problems. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with sets
In message 87bp79qdhk@rudin.co.uk, Paul Rudin wrote: Certainly you can model a set as a dictionary, but that's likely to be less efficient than using a set, and quite possibly you'll need to roll your own operations on your sets, whereas they're provided for the built in implementation. Did you know that applying the “set” or “frozenset” functions to a dict return a set of its keys? Seems a bit dodgy, somehow. -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple database explorer
In message 21c99273-ed58-4f93-b98a-d9292de5d...@k10g2000yqa.googlegroups.com, dusans wrote: - all the others having ODBC drivers... ODBC seems to be something you use when you can’t use a proper database driver. -- http://mail.python.org/mailman/listinfo/python-list
[issue10035] sgmllib fail to parse html containing !- .... -
halfjuice halfju...@gmail.com added the comment: well, !-- ... - is ok since it's comment. !- ... - is probably a IE hack. see http://www.google.com/dictionary?langpair=en|zh-CNq=vaguehl=enaq=f -- status: pending - open ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10035 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10035] sgmllib fail to parse html containing !- .... -
Georg Brandl ge...@python.org added the comment: Is that URL really what you wanted to show me? Also, I'm not intimate with all of SGML's syntax, but ISTM that what you show here is invalid SGML, and as such SGMLParser is not required to parse it. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10035 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10035] sgmllib fail to parse html containing !- .... -
halfjuice halfju...@gmail.com added the comment: Sorry, the URL on the page is sort of broken. The URL contains the !- ... - stuff. I think you're right, the !- is probably just a mistake which is not in the SGML standard. But I'm wondering if the SGMLParser can SKIP such an invalid statement? My browser does this. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10035 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10035] sgmllib fail to parse html containing !- .... -
Georg Brandl ge...@python.org added the comment: The browser needs to be very liberal in what it accepts, since nobody wants their page view to break because of such a technicality. This is different for a tool like SGMLParser. In light of this, and because sgmllib is removed anyway in Python 3, I'm closing this. -- status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10035 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue9017] doctest option flag to enable/disable some chunk of doctests?
harobed steph...@harobed.org added the comment: doctest.SKIP is like #doctest: +DISABLE but he don't have #doctest: +ENABLE feature. doctest.SKIP disable all the bottom of test, DISABLE/ENABLE can SKIP only one part of test. Regards, Stephane -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue9017 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10032] os.setuid and os.setgid have unexpected influence on serial module
Tjeerd Pinkert t.j.pink...@alumnus.utwente.nl added the comment: Indeed I use Linux, sorry for the inconvenience of not mentioning. Thanks Ned, I think this is indeed the case. Using os.setgroups with a list of group ids (one for the file access, one for the serial port) before switching user with os.setgid, os.setuid solved the problem. I think os.initgroups(username, gid) does just this, only is not yet available in my distro. It could be a feature of os that the groups of the user are set on a os.setuid call? Or would this break compatibility with the standard unix library behaviour? To David: Yes you are right, only it would have cost me quite a bit of time to strip the code, and I tried this code to see if the behaviour was consistent when using other daemon code. Next time I will post my own code. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10032 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10028] test_concurrent_futures fails on Windows Server 2003
Changes by Brian Quinlan br...@sweetapp.com: -- assignee: - bquinlan ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10028 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue9017] doctest option flag to enable/disable some chunk of doctests?
Amaury Forgeot d'Arc amaur...@gmail.com added the comment: I'm not a great doctest user, but did you try to disable the SKIP directive at the end? something like doctest: -SKIP -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue9017 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue9017] doctest option flag to enable/disable some chunk of doctests?
Georg Brandl ge...@python.org added the comment: AFAIR +SKIP always only refers to one Example anyway. -- nosy: +georg.brandl ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue9017 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10029] Equivalent to code for zip is wrong in Python 3
Changes by Douglas Leeder douglas.lee...@gmail.com: -- nosy: +Douglas.Leeder ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10029 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10028] test_concurrent_futures fails on Windows Server 2003
Brian Quinlan br...@sweetapp.com added the comment: Hey Brian, Could you try applying the patch that I attached and let me know what error message you get? Cheers, (the other) Brian -- keywords: +patch Added file: http://bugs.python.org/file19137/error.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10028 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue9017] doctest option flag to enable/disable some chunk of doctests?
harobed steph...@harobed.org added the comment: Ok, option +SKIP and -SKIP work well, look test.py file I need maybe improve the documentation with an +SKIP and -SKIP example ? Documentation section about SKIP option is here : http://docs.python.org/library/doctest.html?highlight=doctest.skip#doctest.SKIP Regards, Stephane -- Added file: http://bugs.python.org/file19138/test.py ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue9017 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10037] multiprocessing.pool processes started by worker handler stops working
New submission from Ask Solem a...@opera.com: While working on an autoscaling (yes, people call it that...) feature for Celery, I noticed that the processes created by the _handle_workers thread doesn't always work. I have reproduced this in general, by just using the maxtasksperchild feature and letting the workers terminate themselves so this seems to have always been an issue (just not easy to reproduce unless workers are created with some frequency) I'm not quite sure of the reason yet, but I finally managed to track it down to the workers being stuck while receiving from the queue. The patch attached seems to resolve the issue by polling the queue before trying to receive. I know this is short, I may have some more data later. -- components: Library (Lib) files: multiprocessing-worker-poll.patch keywords: needs review, patch messages: 118062 nosy: asksol priority: critical severity: normal stage: patch review status: open title: multiprocessing.pool processes started by worker handler stops working type: behavior versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3 Added file: http://bugs.python.org/file19139/multiprocessing-worker-poll.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10037 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue9017] doctest option flag to enable/disable some chunk of doctests?
Amaury Forgeot d'Arc amaur...@gmail.com added the comment: harobed, the -SKIP solution does not work. Doctest directives only apply to a single line. After a quick search, I found two workarounds there: http://stackoverflow.com/questions/1809037/python-doctest-skip-entire-block - Replace with or - split the docstring, and add something like __dont_test__ = in front of the second part; the string becomes part of a statement and won't be parsed. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue9017 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10027] os.lstat/os.stat don't set st_nlink on Windows
Hirokazu Yamamoto ocean-c...@m2.ccsnet.ne.jp added the comment: There are some questions. 1. About my patch, I noticed it removed following code. Isn't this needed? I like clean code, but I don't want to break anything. /* Get WIN32_FIND_DATA structure for the path to determine if it is a symlink */ if(info.dwFileAttributes FILE_ATTRIBUTE_REPARSE_POINT) { find_data_handle = FindFirstFileA(path, find_data); if(find_data_handle != INVALID_HANDLE_VALUE) { if(find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK) { /* first clear the S_IFMT bits */ result-st_mode ^= (result-st_mode 017); /* now set the bits that make this a symlink */ result-st_mode |= 012; } FindClose(find_data_handle); } } /* Set S_IFEXEC if it is an .exe, .bat, ... */ dot = strrchr(path, '.'); if (dot) { if (stricmp(dot, .bat) == 0 || stricmp(dot, .cmd) == 0 || stricmp(dot, .exe) == 0 || stricmp(dot, .com) == 0) result-st_mode |= 0111; } 2. About current behavior. when os.stat() is used for junction (reparse point, but not simlink), returned information is about junction on XP or earlier, but about target folder on Vista or above. Is this intended bahavior? Thank you. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10027 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8359] % formating - TypeError: not all arguments converted during string formatting
amiadb ami...@gmail.com added the comment: This bug is very disturbing in Hebrew too. I need to translate % hour to % שעה and its wrong in Hebrew. in Hebrew I need to write שעה אחת (one hour). -- nosy: +amiadb ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8359 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10027] os.lstat/os.stat don't set st_nlink on Windows
Hirokazu Yamamoto ocean-c...@m2.ccsnet.ne.jp added the comment: P.S. Thank you for acceptance. I really wanted to commit that code! -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10027 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue9325] Add an option to pdb/trace/profile to run library module as a script
Changes by Giampaolo Rodola' g.rod...@gmail.com: -- nosy: +giampaolo.rodola ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue9325 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue9903] test_concurrent_futures writes on stderr
Brian Quinlan br...@sweetapp.com added the comment: Fixed in r85288 -- resolution: - fixed status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue9903 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com