Re: Quickie - Regexp for a string not at the beginning of the line
Rivka Miller wrote: I am looking for a regexp for a string not at the beginning of the line. For example, I want to find $hello$ that does not occur at the beginning of the string, ie all $hello$ that exclude ^$hello$. The begging of the string is zero width character. So you could use negative lookahead (?!^). Then the regular expression looks like: /(?!^)\$hello\$/g var str = '$hello$ should not be selected but', str1 = 'not hello but all of the $hello$ and $hello$ ... $hello$ each one '; str.match(/(?!^)\$hello\$/g); //null str1.match(/(?!^)\$hello\$/g); //[$hello$, $hello$, $hello$] -- http://mail.python.org/mailman/listinfo/python-list
Re: Best way to structure data for efficient searching
Larry.Mart wrote: Since there are duplicates, I can't use a dict. And if I have any extraneous data in the keys (i.e. something to make them unique) then I still have to walk through the entire dict to find the matches. You can use slightly different approach. With double mapping you could simplify the lookup. What I mean? Get the first set and build lookup map as: MAP := { KEY1-VALUE : { KEY2-VALUE : [SET, SET, n-th duplicate SET] } } In the worst case you would have quadratic complexity of the algorithm. Otherwise the lookup would be really fast. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionaries and incrementing keys
Steve Crook wrote: Whilst certainly more compact, I'd be interested in views on how pythonesque this method is. Instead of calling function you could use: d = {} d[key] = (key in d and d[key]) + 1 Regards. -- http://mail.python.org/mailman/listinfo/python-list
Re: Rant on web browsers
Chris Angelico wrote: I've just spent a day coding in Javascript, and wishing browsers supported Python instead (or as well). All I needed to do was take two dates (as strings), figure out the difference in days, add that many days to both dates, and put the results back into DOM Input objects (form entry fields). Pretty simple, right? Javascript has a Date class, it should be fine. But no. First, the date object can't be outputted as a formatted string. The only way to output a date is Feb 21 2011. So I have to get the three components (oh and the month is 0-11, not 1-12) and emit those. And Javascript doesn't have a simple format function that would force the numbers to come out with leading zeroes, so I don't bother with that. Actually there is not Date class. There are not any classes in ECMAScript. What if I want to accept any delimiter in the date - slash, hyphen, or dot? Can I just do a simple translate, turn all slashes and dots into hyphens? Nope. Have to go regular expression if you want to change more than the first instance of something. There's no nice string parse function (like sscanf with %d-%d-%d), so I hope every browser out there has a fast regex engine. When all you have is a half-ton sledgehammer, everything looks like a really REALLY flat nail... function formatDate(date) { return ('000' + date.getFullYear()).slice(-4) + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2); } formatDate(new Date()); Plus, Javascript debugging is annoyingly difficult if you don't have tools handy. I need third-party tools to do anything other than code blind? Thanks. It depends on the environment. It is good idea to read c.l.js and JSMentors. -- http://mail.python.org/mailman/listinfo/python-list
Square bracket and dot notations?
Hi all, I am beginner in Python. What is interesting for me is that Python interpreter treats in different way dot and square bracket notations. I am coming from JavaScript where both notations lead prototype chain lookup. In Python it seems square bracket and dot notations lead lookup in different store. Simple example with dict object: d = {key : value} print d[key] #value print d.key #AttributeError I found an implementation of dict which uses both notations for its keys lookup, which I think is stupid idea when obviously both notations lead different lookup. It will confuse me as a reader of the code. Anyway, I would like to know more about the lookup for key of dict and lookup for property of any object with dot notation. Any materials and explanations are highly appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Square bracket and dot notations?
Francesco Bochicchio wrote: User classes - that is the ones you define with the class statement - can implement support for the squared bracket and dot notations: - the expression myinstance[index] is sort of translated into of myinstance.__getitem__(index) - the expression myinstance.myattribute is sort of translated of myinstance.__getattr__(myattribute) It is exactly what I wanted to know. Thank you. I have not examined classes in Python yet, but when I do it I will understand some new things. One of the most interesting is, can an object inherit items trough the parent class? By items I mean items which are accessible trough square bracket notation. I really like Pythonic way here. Square bracket and dot notations allow me to create an object which can be true hash map and meanwhile to support independent methods from its keys. I could have an item and a property with same names and they won't interfere each other. Classes also exposes a __dict__ attributes that allows to access to instance attributes and methods using dictionary semantics. That is, myistance.__dict__[myattribute] should give the same result as myinstance.myattribute. I believe this is because in the beginning class instances actually had a dictionary storing the instance attributes. Nowadays it is more complex than that, I think, but the interface is kept to allow dynamic access to instance contents, although the reccomended way to do it this is getattr(myinstance, myattribute). Of course it is only useful to use __dict__ or getattr when the parameter is not a constant string but a variable referring to a string computed at run time ( this is what I mean for 'dynamic access' ). Yeah, I agree with that. For example in JS exactly square bracket notation has been invented to dynamic property access. The biggest question here is why do you need dynamic property access? In language as JavaScript which is strongly bounded to browser environment, you could use: function getForm(formName) { return document.forms[formName]; } Another use case is to call a method of object and kept the proper `this' value. E.g. obj.x = 10; obj.method = function () { return this.x; }; function callMethod(obj, method) { return obj[method](); } callMethod(obj, 'method'); //10 Of course it could be achieved in different ways and with dot notation of course. Thank you very much for the answer. -- http://mail.python.org/mailman/listinfo/python-list
Re: Square bracket and dot notations?
Terry Reedy wrote: Right. d.items is a dict method. d['items'] is whatever you assign. Named tuples in the collections modules, which allow access to fields through .name as well as [index], have the name class problem. All the methods are therefore given leading underscore names to avoid this. [But there still could be a clash if someone used field names with leading underscores!] Scripting languages as JavaScript, Python and other so dynamic languages allow user to shoot in his feet. I think the developer should learning the curves of the language before start writing complex applications. That was the goal of this thread. -- http://mail.python.org/mailman/listinfo/python-list
Re: Function declarations ?
Andre Majorel wrote: Is there a way to keep the definitions of the high-level functions at the top of the source ? I don't see a way to declare a function in Python. I am not a Python developer, but Pythonic way of definition not declaration is definitely interesting. Languages with variable and function declarations usually use hoisted environment. JavaScript is the perfect example. Hoisted environment allows you to use call expression before the physical declaration of the function in the source text. e.g. foo(); function foo() {} This code does not throw ReferenceError or TypeError in JavaScript. It calls `foo' exactly because it is used a hoisted environment. More precisely on entering in some context global or function JS engine define all function declarations as local scoped variables. While this has its advantages, it has a really big drawback. It cannot support default arguments which are bound to local variables. x = 10 def f(y = x): print y f() #10 This in hoisted environment cannot be implemented, because assignment is evaluating during the execution of the code, not on entering in specific context. So the interpreter is not able to predict the value for y here. Hope this helps, why Python use definitions instead of declarations. -- http://mail.python.org/mailman/listinfo/python-list