Re: Better way to sift parts of URL . . .
In practice, I had to change this: if len(query) > 0 and query[-1] == query[-1].capitalize(): group = query.pop() to this: if len(query) > 0 and query[-1][0] == query[-1].capitalize()[0]: group = query.pop() This is because I only wanted to test the case of the first letter of the string. -- http://mail.python.org/mailman/listinfo/python-list
Re: Better way to sift parts of URL . . .
This is what I ended up with. Slightly different approach: import urlparse def sUrl(s): page = group = '' bits = urlparse.urlsplit(s) url = '//'.join([bits[0],bits[1]]) + '/' query = bits[2].split('/') if '' in query: query.remove('') if len(query) > 1: page = query.pop() if len(query) > 0 and query[-1] == query[-1].capitalize(): group = query.pop() if len(query): url += '/'.join(query) + '/' if page == '': page = 'Main' if group == '': group = 'Main' page = '.'.join([group,page]) print " URL: (%s) PAGE: (%s)" % (url, page) urls = [ "http://www.example.org/test/Main/AnotherPage";, # (page = Main/AnotherPage) "http://www.example.org/test/Main";, # (page = Main + '/' + default_page) "http://www.example.org/test";, # (page = default_group + '/' + default_page) "http://www.example.org/test/";, # (page = default_group + '/' + default_page) "http://www.example.org/";, # (page = default_group + '/' + default_page) "http://www.example.org/Main/AnotherPage";, ] for u in urls: print "Testing:",u sUrl(u) -- http://mail.python.org/mailman/listinfo/python-list
Re: Better way to sift parts of URL . . .
Sorry. I'm writing a python script that retrieves source contents of a wiki page, edits, and re-posts changed content. The wiki breaks pages into groups and pages (e.g. ThisGroup/ThisPage). The sections that are camel cased (or otherwise contain title case) are the group and page for a given page. When a url is passed that is incomplete (i.e., has the base URL and the Group, or only the base URL), the wiki resorts to defaults (e.g. a base URL and Group would return the default page for that group, and a bare URL returns the base page for the base group). I'm playing with urlparse now. Looks like I can do the same thing in a lot fewer steps. I'll post results. Ben On 4/18/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > Ben> I am working on a script that splits a URL into a page and a > Ben> url. > > I couldn't tell quite what you mean to accomplish from your example. (In > particular, I don't know what you mean by "default_group", as it's never > defined, and I don't know why the desired output of examples 1 and 6 is the > same, since the URLs are clearly different.) You don't mention having tried > the urlparse module, so I thought I should ask: have you tried using > urlparse? > > Skip > -- Ben Wilson " Mundus vult decipi, ergo decipiatur" -- http://mail.python.org/mailman/listinfo/python-list
Re: Better way to sift parts of URL . . .
Here is what I came up with: def siftUrl(s): s = s.split('//')[1] bits = s.split('/') if '' in bits: bits.remove('') if len(bits) > 1: group = bits[-2] page = bits[-1] group.strip('/') page.strip('/') else: group = 'test' page = 'test' if group == group.capitalize(): page = '/'.join([group,page]) url = '/'.join(s.split('/')[:-2]) + '/' elif page == page.capitalize(): page = '/'.join(['Main',page]) url = '/'.join(s.split('/')[:-1]) + '/' else: page = '/'.join(['Main','Main']) url = s url = 'http://' + url return url, page -- http://mail.python.org/mailman/listinfo/python-list
Better way to sift parts of URL . . .
I am working on a script that splits a URL into a page and a url. The examples below are the conditions I expect a user to pass to the script. In all cases, "http://www.example.org/test/"; is the URL, and the page comprises parts that have upper case letters (note, 5 & 6 are the same as earlier examples, sans the 'test'). 1. http://www.example.org/test/Main/AnotherPage (page = Main/AnotherPage) 2. http://www.example.org/test/Main (page = Main + '/' + default_page) 3. http://www.example.org/test (page = default_group + '/' + default_page) 4. http://www.example.org/test/ (page = default_group + '/' + default_page) 5. http://www.example.org/ (page = default_group + '/' + default_page) 6. http://www.example.org/Main/AnotherPage (page = Main/AnotherPage) Right now, I'm doing a simple split off condition 1: page = '.'.join(in.split('/')[-2:]) url = '/'.join(in.split('/')[:-2]) + '/' Before I start winding my way down a complex path, I wanted to see if anybody had an elegant approach to this problem. Thanks in advance. Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: Trolling for New Web Host . . .
Thanks for your suggestions. I've Python on both my Win32 and Linux partitions of my laptop. Although, I suppose I should develop my CGI on the laptop (running Apache) first. Thanks, Ben -- http://mail.python.org/mailman/listinfo/python-list
Trolling for New Web Host . . .
I've had the same web host (imhosted.com) for the past three years, and have had decent service. However, I'm also trying to learn Python the hard way--by doing. So, I figured I'd write a few Python CGIs (non-Zope). Unfortunately, my web host does not have mod_python. They only way I can run a python script is by using the .cgi extension. I'm not sure if I want to stick with my host and just call my apps index.cgi, or if I should move to a new server. If I were to move to a new server, which would you recomend, and why? Regards, Ben Wilson -- http://mail.python.org/mailman/listinfo/python-list
Re: editor for Python on Linux
You know, I have that for Perl, but seem never to have set up folding for Python. I must remedy this tonight. Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: editor for Python on Linux
He said "IDE." That means "vim" -- http://mail.python.org/mailman/listinfo/python-list
Re: - Copy dictionary entries to attributes
Perhaps: def dictionary_make_attributes(self, settings): for k,v in settings: setattr(self, k, v) http://ftp.python.org/doc/lib/built-in-funcs.html#l2h-64 -- http://mail.python.org/mailman/listinfo/python-list
Re: how do you pronounce 'tuple'?
Yeah, I was going to say it's "I-66," not "Route 66," which has been replaced in pertainent parts by I-40. tuh-ple. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie Q: dynamically assigning object attribute
Well, my Perl way of doing it would be to have all attributes in a dict (hash), then create the accessor vi a dynamic function. I knew Python would have a right way to do it for Python, but when I went looking I neglected to look at the core of the language. I suppose I'm just too accustomed to the TIMTOWTDY approach to expect the one-ring-to-bind-them-all solution. :-) It's a mental shift on my part, to be certain. What I was actually doing was reading a user configuration file and setting an object's variables--the example I got was a fairly close approximation of how I was trying to approach it before setattr(). Thanks, Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie Q: dynamically assigning object attribute
That's it. I should spend more time R-ingTFM. I kept looking for some class-based solution and there was a built in. Perfectly logical. Thanks, Ben -- http://mail.python.org/mailman/listinfo/python-list
Newbie Q: dynamically assigning object attribute
I would like to dynamically assign object attributes: dict = { a : 1, b : 2, } for key,val in dict : obj.key = val To get: print obj.a 1 I've googled to no effect, or maybe I'm needing to be hit with the appropriately sized clue-by-four. Any assistance would be appreciated. Regards, Ben -- http://mail.python.org/mailman/listinfo/python-list
Ternary Operator Now?
I read somewhere else that Python was getting a ternary operator (e.g. x = (true/false) ? y : z). I read the PEP about it and that the PEP had been approved this past Fall. Has this been released into the wild yet? IIRC, the operator is like: x = y if C : else z -- http://mail.python.org/mailman/listinfo/python-list
Re: Another try at Python's selfishness
"But the point is, the current situation is not newbie-friendly (I can tell, I am a newbie)" I will agree to that, as I consider myself still new. _But_, it's a stumbling stone only briefly. Get enough nagging error messages, and you learn and move on. I agree with the grandparent poster that it is a perfect self-documenting thing, as the use of 'self' is pretty obvious. For a language that one can learn in a short time, this is a tempest in a teacup. I'm just trying to disown my several years of Perl. I like PHP too much and have no experience with Python in a CGI environment. So, I'm a little bit confused linguistically. ;-) -- http://mail.python.org/mailman/listinfo/python-list