[Tutor] Dynamically generated classes
Hi all, I've been playing with dynamically generated classes. In particular: class ClassMaker(object): def __init__(maker_self, name): maker_self.name = name # class Foo(object): def __init__(self): self.parent = maker_self def whoami(self): print 'I am a Foo instance made by %s' % self.parent.name maker_self.Foo = Foo # class Bar(object): def __init__(self): self.parent = maker_self def whoami(self): print 'I am a Bar instance made by %s' % self.parent.name maker_self.Bar = Bar >>> a = ClassMaker('Alice') >>> b = ClassMaker('Bob') >>> af = a.Foo() >>> ab = a.Bar() >>> bf = b.Foo() >>> bb = b.Bar() >>> >>> af.whoami() I am a Foo instance made by Alice >>> ab.whoami() I am a Bar instance made by Alice >>> bf.whoami() I am a Foo instance made by Bob >>> bb.whoami() I am a Bar instance made by Bob >>> isinstance(bb, b.Bar) True >>> a.Foo is b.Foo False The actual use case is a system where there are multiple databases of essentially the same form, where a is database A, and b is database B, and Foo and Bar represent tables in both the databases. af would be the Foo table in database A, and bf would be the Foo table in database B. My question is: is there a better way? Based on my playing with the above, it all seems to do what I want. My only concern is that I've not seen this idiom before, and perhaps there is a simpler or more standard way? Cheers, Rasjid. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically generated classes
"Rasjid Wilcox" wrote I've been playing with dynamically generated classes. In particular: class ClassMaker(object): def __init__(maker_self, name): maker_self.name = name # class Foo(object): def __init__(self): self.parent = maker_self def whoami(self): print 'I am a Foo instance made by %s' % self.parent.name maker_self.Foo = Foo I'd probably just define the classes in a module rather than in the init method itself. Then pass the required class to the parent init as an argument. But the example code doesn't give enough clue as to how these classes will be used to be sure that will suit your need. a = ClassMaker('Alice') af = a.Foo() ab = a.Bar() af.whoami() I am a Foo instance made by Alice ab.whoami() I am a Bar instance made by Alice a.Foo is b.Foo False The actual use case is a system where there are multiple databases of essentially the same form, where a is database A, and b is database B, and Foo and Bar represent tables in both the databases. Unless you are actually building a database it would be unusual to have a class represent a table, a table normally represents a class - ie its not a two way relation - or the class uses a table to populate attributes. But again we have no clues about what the classes actually do - its normally the methods of the class that define its usage... the Foo table in database A, and bf would be the Foo table in database B. That sounds like you may be trying to create a facade pattern? My question is: is there a better way? Based on my playing with the above, it all seems to do what I want. My only concern is that I've not seen this idiom before, and perhaps there is a simpler or more standard way? I think it will do what you want but I wouldn't bother putting the class definitions inside the init - unless there is a good reason. At the very least I would create two factory methods which are called by init. That would allow you to reset the classes if needed too. class A: def __init__(self, name): self.foo = makeFoo() self.bar = makeBar() def makeFoo(self): class Foo: pass return Foo def makeBar(self): class Bar: pass reurn Bar But I still think its much more flexible to define the classes in a separate module: import foo, bar class A: def __init__(self,foo,bar): self.foo = foo self.bar = bar a = A(foo.Foo(A),bar.Bar(A)) # pass A so Foo/Bar know their parent You can then extend the use of A without changing A by defining new derivatives of Foo and Bar (as you use new databases for example) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problems processing accented characters in ISO-8859-1 encoded texts
I am working with texts that are encoded as ISO-8859-1. I have included the following two lines at the beginning of my python script: !/usr/bin/env python # -*- coding: iso-8859-1 -*- If I'm not mistaken, this should tell Python that accented characters such as 'á', 'Á', 'ö' or 'è' should be considered as alpha-numeric characters and therefore matched with a regular expression of the form [a-zA-Z]. However, when I process my texts, all of the accented characters are matched as non alpha-numeric symbols. What am I doing wrong? I'm not including the whole script because I think the rest of the code is irrelevant. All that's relevant (I think) is that I'm using the regular expression '[^a-zA-Z\t\n\r\f\v]+' to match any string that includes non alpha-numeric characters and that returns 'á', 'Á', 'ö' or 'è' as well as other real non alpha-numeric characters. Has anybody else experienced this problem when working with texts encoded as ISO-8859-1 or UTF-8? Is there any additional flag or parameter that I should add to make the processing of these characters as regular word characters possible? Thanks in advance for your help. Josep M. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically generated classes
Hello Alan, You mentioned the facade pattern. Could you please provide a basic simple implementation of this pattern in python? Regards Karim On 12/23/2010 10:13 AM, Alan Gauld wrote: "Rasjid Wilcox" wrote I've been playing with dynamically generated classes. In particular: class ClassMaker(object): def __init__(maker_self, name): maker_self.name = name # class Foo(object): def __init__(self): self.parent = maker_self def whoami(self): print 'I am a Foo instance made by %s' % self.parent.name maker_self.Foo = Foo I'd probably just define the classes in a module rather than in the init method itself. Then pass the required class to the parent init as an argument. But the example code doesn't give enough clue as to how these classes will be used to be sure that will suit your need. a = ClassMaker('Alice') af = a.Foo() ab = a.Bar() af.whoami() I am a Foo instance made by Alice ab.whoami() I am a Bar instance made by Alice a.Foo is b.Foo False The actual use case is a system where there are multiple databases of essentially the same form, where a is database A, and b is database B, and Foo and Bar represent tables in both the databases. Unless you are actually building a database it would be unusual to have a class represent a table, a table normally represents a class - ie its not a two way relation - or the class uses a table to populate attributes. But again we have no clues about what the classes actually do - its normally the methods of the class that define its usage... the Foo table in database A, and bf would be the Foo table in database B. That sounds like you may be trying to create a facade pattern? My question is: is there a better way? Based on my playing with the above, it all seems to do what I want. My only concern is that I've not seen this idiom before, and perhaps there is a simpler or more standard way? I think it will do what you want but I wouldn't bother putting the class definitions inside the init - unless there is a good reason. At the very least I would create two factory methods which are called by init. That would allow you to reset the classes if needed too. class A: def __init__(self, name): self.foo = makeFoo() self.bar = makeBar() def makeFoo(self): class Foo: pass return Foo def makeBar(self): class Bar: pass reurn Bar But I still think its much more flexible to define the classes in a separate module: import foo, bar class A: def __init__(self,foo,bar): self.foo = foo self.bar = bar a = A(foo.Foo(A),bar.Bar(A)) # pass A so Foo/Bar know their parent You can then extend the use of A without changing A by defining new derivatives of Foo and Bar (as you use new databases for example) HTH, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems processing accented characters in ISO-8859-1 encoded texts
Sorry! Sorry! Sorry! I just found out this question had already been answered by Steven D'Aprano in another thread! The trick was to add '\w' besides [a-zA-Z]. Please, accept my apologies. I devote time to this project whenever I have some free time. I got very busy with other things at some point and I stopped working on that. When I started again today, I had not noticed that there was already an answer to the question I had posted a while ago that actually solved my problem. Thanks again Steven. You can consider the problem solved and this thread closed. Josep M. On Thu, Dec 23, 2010 at 10:25 AM, Josep M. Fontana wrote: > I am working with texts that are encoded as ISO-8859-1. I have > included the following two lines at the beginning of my python script: > > !/usr/bin/env python > # -*- coding: iso-8859-1 -*- > > If I'm not mistaken, this should tell Python that accented characters > such as 'á', 'Á', 'ö' or 'è' should be considered as alpha-numeric > characters and therefore matched with a regular expression of the form > [a-zA-Z]. However, when I process my texts, all of the accented > characters are matched as non alpha-numeric symbols. What am I doing > wrong? > > I'm not including the whole script because I think the rest of the > code is irrelevant. All that's relevant (I think) is that I'm using > the regular expression '[^a-zA-Z\t\n\r\f\v]+' to match any string that > includes non alpha-numeric characters and that returns 'á', 'Á', 'ö' > or 'è' as well as other real non alpha-numeric characters. > > Has anybody else experienced this problem when working with texts > encoded as ISO-8859-1 or UTF-8? Is there any additional flag or > parameter that I should add to make the processing of these characters > as regular word characters possible? > > Thanks in advance for your help. > > Josep M. > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems processing accented characters in ISO-8859-1 encoded texts
Josep M. Fontana wrote: I am working with texts that are encoded as ISO-8859-1. I have included the following two lines at the beginning of my python script: !/usr/bin/env python # -*- coding: iso-8859-1 -*- If I'm not mistaken, this should tell Python that accented characters such as 'á', 'Á', 'ö' or 'è' should be considered as alpha-numeric characters and therefore matched with a regular expression of the form [a-zA-Z]. You are mistaken. a-zA-Z always means the ASCII A to Z, and nothing else. You are conflating three unrelated problems: (1) What encoding is used to convert the bytes on disk of the source code literals into characters? (2) What encoding is used for the data fed to the regex engine? (3) What characters does the regex engine consider to be alphanumeric? The encoding line only tells Python what encoding to use to read the source code. It has no effect on text read from files, or byte strings, or anything else. It is only to allow literals and identifiers to be decoded correctly, and has nothing to do with regular expressions. To match accented characters, you can do two things: (1) explicitly include the accented characters you care about in the regular expression; or (2) i. set the current LOCALE to a locale that includes the characters you care about; ii. search for the \w regex special sequence; and iii. include the ?L flag in the regex. In both cases, don't forget to use Unicode strings, not byte strings. For example: >>> text = u"...aböyz..." >>> re.search(r'[a-zA-Z]+', text).group(0) u'ab' Setting the locale on its own isn't enough: >>> locale.setlocale(locale.LC_ALL, 'de_DE') 'de_DE' >>> re.search(r'[a-zA-Z]+', text).group(0) u'ab' Nor is using the locale-aware alphanumeric sequence, since the regex engine is still using the default C locale: >>> re.search(r'\w+', text).group(0) u'ab' But if you instruct the engine to use the current locale instead, then it works: >>> re.search(r'(?L)\w+', text).group(0) u'ab\xf6yz' (Don't be put off by the ugly printing representation of the unicode string. \xf6 is just the repr() of the character ö.) Oh, and just to prove my point that a-z is always ASCII, even with the locale set: >>> re.search(r'(?L)[a-zA-Z]+', text).group(0) u'ab' Note also that \w means alphanumeric, not just alpha, so it will also match digits. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Weighted Random Choice - Anyone have an efficient algorithm?
On 01/-10/-28163 02:59 PM, Modulok wrote: Does anyone know of an efficient way of doing a weighted random choice? (I don't even know what algorithms like this would be called.) Preferably, something that doesn't grow exponentially with the number of elements in the list, or the size of their respective values. For example: Assume I have a list of integer elements. I want to pick an index from that list, using the value of said elements to control the probability of selecting a given index: w = [5, 20, 75] wrand(w) Where wrand() will return '2', (the index of 75), about 75% of the time. It would return '1', (the index of 20) about 20% of the time, and 0, about 5% of the time. It could return the actual value, but returning the index seems more flexible. The implementation doesn't have to work exactly like this, even if the weight values don't add up to 100, or are arbitrary, that's fine. Hopefully you get the idea. Here's what I tried (it works, but is slow): ### Begin Code Example ### import random random.seed(2) #<-- So we can reproduce the sequence for testing. def wrandom(weights): ''' Return a randomly selected index of the 'weights' list, using the values of that list to affect probability of said index being returned. ''' debug = False flattened = [] for i, w in enumerate(weights): if debug: print "i, w: ", i, w for k in range(w): flattened.append(i) if debug: print "flattened: ", flattened rnd = random.randint(0, (len(flattened) - 1)) return flattened[rnd] # Not test it: print wrandom([5, 20, 75]) print wrandom([5, 20, 75]) print wrandom([5, 20, 75]) ### End Code Example ### It works and is easy enough to understand, but when the number of list items gets large, or the weights get heavy, things get ugly. -Modulok- You're building a temporary list, then discarding it when the function returns. Your speed efficiency is likely to improve if you keep that "flattened list" and only do the conversion once. But the separate problem of that flattened list possibly being much larger than necessary is more interesting. What you probably want to build instead is a list of accumulated probabilities. It would be the same size as the input list, and in this case, it would contain 5, 25, and 100, respectively. The list is always monotonic, since none of your probabilities can legally be negative. Anyway, once you have that new list, you pick a random number in the range from 0 to newlist[-1], and do a binary search of the newlist for the first value > your random value. And if the list is small, you can just scan through it linearly, which is much less code. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems processing accented characters in ISO-8859-1 encoded texts
Hi Steven, On Thu, Dec 23, 2010 at 11:51 AM, Steven D'Aprano wrote: > Note also that \w means alphanumeric, not just alpha, so it will also match > digits. I'm sorry you didn't get to read my next message because in there I said that you yourself had already solved my problem a few weeks ago. Then again, I'm not sorry you didn't read it in time because this way I have benefited again from your pearls of wisdom. In this message you clarified for me some conceptual problems I had and that is even more beneficial than just solving the problem at hand (how true that thing about "Don't give a man a fish. Teach him how to fish." Thanks! Just one more question. You say that \w means alphanumeric, not just alpha. Is there any expression that would mean "just alpha" and (given the appropriate LOCALE setting) would match 'a' and 'ö' but not '9'? Josep M. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems processing accented characters in ISO-8859-1 encoded texts
Josep M. Fontana wrote: Sorry! Sorry! Sorry! I just found out this question had already been answered by Steven D'Aprano in another thread! The trick was to add '\w' besides [a-zA-Z]. Hah ha! I didn't see this before I answered... I thought the question sounded familiar :) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically generated classes
"Karim" wrote You mentioned the facade pattern. Could you please provide a basic simple implementation of this pattern in python? Not without spending more time than I have available. But you can read about it on wikipedia, including Java example code. However I think I actually should have said an Adaptor pattern rather than a Facade... http://en.wikipedia.org/wiki/Adapter_pattern HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Weighted Random Choice - Anyone have an efficient algorithm?
Modulok wrote: Does anyone know of an efficient way of doing a weighted random choice? (I don't even know what algorithms like this would be called.) If you google for "python weighted random choice" you will find a number of hits. Preferably, something that doesn't grow exponentially with the number of elements in the list, or the size of their respective values. Here's one method that is linear on the number of elements: def weighted_choice(weights): total = sum(weights) p = random.uniform(0, total) # random float between 0 and total. assert 0.0 <= p <= total # Do a linear search for the right index value. If you have a # huge number of weights, a binary search may be faster. running_total = 0 for i, weight in enumerate(weights): running_total += weight if p <= running_total: return i And tested: >>> import random >>> weights = [5, 20, 75] >>> counts = {0:0, 1:0, 2:0} >>> for i in xrange(100): ... i = weighted_choice(weights) ... counts[i] += 1 ... >>> counts {0: 50252, 1: 17, 2: 749751} >>> [n*1e6/100 for n in weights] # expected values [5.0, 20.0, 75.0] As you can see, the results are very close to what should be expected, and of course the difference can be chalked up to random chance. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically generated classes
Thanks for the link. I use Adapter already for java to align to different interface. I was curious about the facade implementation. Regards Karim On 12/23/2010 01:25 PM, Alan Gauld wrote: "Karim" wrote You mentioned the facade pattern. Could you please provide a basic simple implementation of this pattern in python? Not without spending more time than I have available. But you can read about it on wikipedia, including Java example code. However I think I actually should have said an Adaptor pattern rather than a Facade... http://en.wikipedia.org/wiki/Adapter_pattern HTH, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically generated classes
On 23 December 2010 20:13, Alan Gauld wrote: > I think it will do what you want but I wouldn't bother putting the class > definitions inside the init - unless there is a good reason. At the very > least I would create two factory methods which are called by init. > That would allow you to reset the classes if needed too. Wonderful! It turns out a single factory method which can be called once for each table per database is all that I need. It has simplified the code significantly. Thank you very much. Rasjid. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically generated classes
On 12/23/2010 10:13 AM, Alan Gauld wrote: "Rasjid Wilcox" wrote I've been playing with dynamically generated classes. In particular: class ClassMaker(object): def __init__(maker_self, name): maker_self.name = name # class Foo(object): def __init__(self): self.parent = maker_self def whoami(self): print 'I am a Foo instance made by %s' % self.parent.name maker_self.Foo = Foo I'd probably just define the classes in a module rather than in the init method itself. Then pass the required class to the parent init as an argument. But the example code doesn't give enough clue as to how these classes will be used to be sure that will suit your need. a = ClassMaker('Alice') af = a.Foo() ab = a.Bar() af.whoami() I am a Foo instance made by Alice ab.whoami() I am a Bar instance made by Alice a.Foo is b.Foo False The actual use case is a system where there are multiple databases of essentially the same form, where a is database A, and b is database B, and Foo and Bar represent tables in both the databases. Unless you are actually building a database it would be unusual to have a class represent a table, a table normally represents a class - ie its not a two way relation - or the class uses a table to populate attributes. But again we have no clues about what the classes actually do - its normally the methods of the class that define its usage... the Foo table in database A, and bf would be the Foo table in database B. That sounds like you may be trying to create a facade pattern? My question is: is there a better way? Based on my playing with the above, it all seems to do what I want. My only concern is that I've not seen this idiom before, and perhaps there is a simpler or more standard way? I think it will do what you want but I wouldn't bother putting the class definitions inside the init - unless there is a good reason. At the very least I would create two factory methods which are called by init. That would allow you to reset the classes if needed too. class A: def __init__(self, name): self.foo = makeFoo() self.bar = makeBar() def makeFoo(self): class Foo: pass return Foo def makeBar(self): class Bar: pass reurn Bar But I still think its much more flexible to define the classes in a separate module: import foo, bar class A: def __init__(self,foo,bar): self.foo = foo self.bar = bar a = A(foo.Foo(A),bar.Bar(A)) # pass A so Foo/Bar know their parent You can then extend the use of A without changing A by defining new derivatives of Foo and Bar (as you use new databases for example) HTH, By the way Alan your solution is simple and beautiful! I like this kind of flexible design. Regards Karim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] paramiko error with put()
Hi I am using paramiko 1.7.6 "fanny" on microsoft windows xp v2002 service pack3 with python 2.4.2 I have the follwing script: *import paramiko hostname='blah' port=22 username='blah' password='blah' fullpath='root\\path\\file.xls' remotepath='/inbox/file.xls' self.client= paramiko.SSHClient() self.client.load_system_host_keys() self.client.connect(hostname,port,username,password) sftp = self.client.open_sftp() sftp.put(fullpath,remotepath)* and i get the following error: *sftp.put(fullpath,remotepath)) File "build\bdist.win32\egg\paramiko\sftp_client.py", line 577, in put File "build\bdist.win32\egg\paramiko\sftp_client.py", line 337, in stat File "build\bdist.win32\egg\paramiko\sftp_client.py", line 628, in _request File "build\bdist.win32\egg\paramiko\sftp_client.py", line 675, in _read_response File "build\bdist.win32\egg\paramiko\sftp_client.py", line 701, in _convert_status IOError: [Errno 2] /inbox/file.xls is not a valid file path** * but the path definitely exists (I can move into it using sftp.chdir('inbox')) I have also tried moving into the folder and using put() but I get the exact same error (did take out inbox prefix) Has anyone had this issue? I would really appreciate you help Thanks Ad ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] paramiko error with put()
I don't really know the answer, but more a question/note (below): > Hi I am using paramiko 1.7.6 "fanny" on microsoft windows xp v2002 service > pack3 with python 2.4.2 > > I have the follwing script: > import paramiko > > hostname='blah' > port=22 > username='blah' > password='blah' > fullpath='root\\path\\file.xls' > remotepath='/inbox/file.xls' > and i get the following error: > IOError: [Errno 2] /inbox/file.xls is not a valid file path > > but the path definitely exists (I can move into it using sftp.chdir('inbox')) > I have also tried moving into the folder and using put() but I get the exact > same error (did take out inbox prefix) chdir('inbox') seems like a relative path, while '/inbox/file.xls' is absolute. Could that be an issue, or does chdir() always translate to absolute path? Because, depending on your sftp server, you may (likely) not land in the root dir when connecting. (I've also never seen /inbox in the root dir, so './inbox/file.xls' does seem the more logical choice to me) As said, no idea really, but this is the only thing that I notice. Cheers, Evert > > Has anyone had this issue? > > > I would really appreciate you help > > Thanks > Ad > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python C API - Defining New Classes with Multiple Inheritance
Hi, I've just started using the Python C API for version 2.7.1, and I've got a question! How do you define a new type which inherits from multiple types? I've been browsing the source code for Python 2.7.1 but I'm having a tough time finding examples. I see that MySQLdb defines low-level classes in the module "_mysql" using the C API, then extends from them using Python, but I want to keep as much of this in C as I can. Any help would be much appreciated! Thanks, Logan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor