Re: [Tutor] COM server: cannot assign property
"Yashwin Kanchan" wrote I am trying to create a simple test COM server , but am have trouble assigning any property value to it through excel VBA. I've never tried using properties via COM. Does it work if you disable the property aspect and expose the set/get methods directly? Can you access those methods successfully from VB? If that works it suggests the problem lies in the property definition stuff... HTH, Alan G Please point out where i am going wrong. #COM server class test(object): _reg_clsid_ = "{B5901450-F9A1-4F76-8FCF-2BFFA96ED210}" _reg_progid_ = "Python.Test" _public_methods_ = ["arg"] _public_attrs_ = ["t"] def __init__(self): self._t=0 def arg(self,s,r): return (s,r) def get_t(self): return self._t def set_t(self,value): self._t = str(value) t = property(get_t,set_t) if __name__=='__main__': import win32com.server.register win32com.server.register.UseCommandLine(test) print "done" VBA Code: Sub Button1_Click() Set test = CreateObject("Python.Test") test.arg 2, 5 test.t = "hello" MsgBox test.t End Sub Error; "Object doesnt support this property or method" at test.t = "hello" Thanks Yashwin Kanchan ___ 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] COM server: cannot assign property
Hi Guys I am trying to create a simple test COM server , but am have trouble assigning any property value to it through excel VBA. Please point out where i am going wrong. #COM server class test(object): _reg_clsid_ = "{B5901450-F9A1-4F76-8FCF-2BFFA96ED210}" _reg_progid_ = "Python.Test" _public_methods_ = ["arg"] _public_attrs_ = ["t"] def __init__(self): self._t=0 def arg(self,s,r): return (s,r) def get_t(self): return self._t def set_t(self,value): self._t = str(value) t = property(get_t,set_t) if __name__=='__main__': import win32com.server.register win32com.server.register.UseCommandLine(test) print "done" VBA Code: Sub Button1_Click() Set test = CreateObject("Python.Test") test.arg 2, 5 test.t = "hello" MsgBox test.t End Sub Error; "Object doesnt support this property or method" at test.t = "hello" Thanks Yashwin Kanchan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] importing variables
Stefan Lesicnik wrote: On Thu, Nov 12, 2009 at 6:26 PM, bob gailer wrote: Stefan Lesicnik wrote: Hi guys, Im trying to do something and hit a bit of a wall, potentially im going about this the wrong way. Essentially the problem is: features file contains rt='text''' import features a =rt' print features.rt #this works print features.a #this fails I need to use features.a as i am iterating through a list and a would be the different features i want to check if they exist, and then use the variable. I hope that makes sense, or if anyone has any suggestion on how properly to do this! Even though Alan missed your point, I understood it. So your explanation was OK (for me). print getattr(features, a) Thanks Bob! That works. I hadn't heard about getattr Much appreciated stefan 1) Bob's suggestion is what I would have said as well, but using a 3rd argument to avoid getting exceptions: thisfeature = getattr(features, a, None) But now that I see it, I'd mention other possibilities. 2) Since the features.py is being generated, why not just make sure it has all the required attributes, perhaps using None as a flag to indicate that the feature is disabled. 3) Alternatively, let features.py generate an instance of a class you define in your (non-generated) code. That class can have default behaviors to handle missing features, as well as interrelated features (eg. if feature3 is enabled, then disable feature4, even if the user wants it). import features myfeatures = features.factory() and now myfeatures.rt is guaranteed to exist, because the class makes sure of it. General principle, avoid indexing by string if all the strings could be known in advance - that's what regular attribute notation is for. And if there's a part that's out of your control (say data from bob.txt), isolate knowledge of that from the rest of the code. Avoiding code generation: I don't know cheetah or mako, and can't say I really understand your environment. But if it's CGI stuff for example, I'd beware of generating code, since you then run the risk of multiple clients colliding with each other. If you can do things entirely in data structures, things can be safely made reusable with no risk of two separate instances of the interpreter interfering with each other. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] importing variables
"bob gailer" wrote I need to use features.a as i am iterating through a list and a would be the different features i want to check if they exist, and then use the variable. I hope that makes sense, or if anyone has any suggestion on how properly to do this! Even though Alan missed your point, I understood it. So your explanation was OK (for me). print getattr(features, a) And now that I see Bob's solution I understand the question! :-) Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] django python Version 1.1.1 - Internacionalization
On Thu, 2009-11-12 at 00:05 -0600, Wayne Werner wrote: > > > On Wed, Nov 11, 2009 at 10:10 PM, andré palma > wrote: > Hi all! > I'm developing a django web application but i'm having a > trouble with > the internationalization. > > > I don't know much about Django, and I suspect that's more the norm > here - you may have better luck on the Django users list: > > > http://groups.google.com/group/django-users?pli=1 > > HTH, > Wayne Tanks a lot for your help. But after a try a few many times i finally figured out where the problem was coming from. I don't know exactly why but it seems that you really have to use your manage.py file to create those translation files. I was using "django-admin makemessages -l " command in my project folder( like it is on django-book ) and i couldn't get the translations working. So i did solved this issue by using the following on my project folder. ::: python manage.py makemessages -l It worked for me and i found that it might be useful for someone in the same situation. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] importing variables
On Thu, Nov 12, 2009 at 6:26 PM, bob gailer wrote: > Stefan Lesicnik wrote: >> >> Hi guys, >> >> Im trying to do something and hit a bit of a wall, potentially im >> going about this the wrong way. Essentially the problem is: >> >> features file contains >> rt='''text''' >> >> import features >> >> a = 'rt' >> print features.rt #this works >> print features.a #this fails >> >> I need to use features.a as i am iterating through a list and a would >> be the different features i want to check if they exist, and then use >> the variable. >> >> I hope that makes sense, or if anyone has any suggestion on how >> properly to do this! >> > > Even though Alan missed your point, I understood it. So your explanation was > OK (for me). > > print getattr(features, a) Thanks Bob! That works. I hadn't heard about getattr Much appreciated stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] importing variables
Stefan Lesicnik wrote: Hi guys, Im trying to do something and hit a bit of a wall, potentially im going about this the wrong way. Essentially the problem is: features file contains rt='''text''' import features a = 'rt' print features.rt #this works print features.a #this fails I need to use features.a as i am iterating through a list and a would be the different features i want to check if they exist, and then use the variable. I hope that makes sense, or if anyone has any suggestion on how properly to do this! Even though Alan missed your point, I understood it. So your explanation was OK (for me). print getattr(features, a) -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to call a method with a print statement?
Kent Johnson wrote: On Thu, Nov 12, 2009 at 6:35 AM, Luke Paireepinart wrote: On Thu, Nov 12, 2009 at 5:29 AM, Jeff R. Allen wrote: You are looking for the __str__ method. See http://docs.python.org/reference/datamodel.html#object.__str__ Can't you also implement __repr__? Yes, in fact if you are only going to implement one of __str__ and __repr__, arguably __repr__ is a better choice. __repr__() is called by the interactive interpreter when it displays an object. __str__ is called by print, and if you don't define __str__ it will call __repr__. So defining only __str__ will not give a custom representation unless you print: In [1]: class Foo(): ...: def __str__(self): ...: return "I'm a Foo" In [2]: f = Foo() In [3]: f Out[3]: <__main__.Foo instance at 0x1433468> In [4]: print f I'm a Foo Defining __repr__ will give the custom representation when you just give the name of the object: In [5]: class Foo2(): ...: def __repr__(self): ...: return "I'm a Foo2" ...: ...: In [6]: f2=Foo2() In [7]: f2 Out[7]: I'm a Foo2 In [8]: print f2 I'm a Foo2 Kent And one other important place that uses __repr__() is the printing of containers. So if you have a list of Foo2 objects, and you want to just say print mylist it's better to have __repr__(). ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unexpected iterator
Jeff R. Allen wrote: Hello, I am working my way through the tutorial, and I like trying variations, just to see what expected errors look like, and other ways things could be written. I tried a, b = 0, 0 and that worked. Then I tried this to (maybe) set both a and b to 0: a, b = 0 Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not iterable I understand why it doesn't work, but I don't understand the wording of the exception. Could someone explain how I accidentally introduced iteration into the picture with my syntax? I have a feeling there's an interesting lesson hidden in this example... Thanks. -jeff What you've really got going across the = sign is a list, or something like it. On the left side, you have exactly two items, so on the right side you need a list or tuple of length 2. In your first example, you have a tuple, because of the comma. The generalization implied above is an iterable. Lists and tuples are iterables, but you could also use a generator, for example. Anything that you could iterate over, such as with a for statement. a, b, c, d = xrange(4) Anyway, in your second example, the system is trying to interpret the right side as an iterable, in order to get two items from it. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] importing variables
On Thu, Nov 12, 2009 at 4:38 PM, Alan Gauld wrote: > > "Stefan Lesicnik" wrote > >> features file contains >> rt='''text''' >> >> import features >> >> a = 'rt' >> print features.rt #this works >> print features.a #this fails > > Because you defined a in your current file. > You need to define it in features. > >> I need to use features.a as i am iterating through a list and a would >> be the different features i want to check if they exist, and then use >> the variable. > > So you need to define a in features. > Note that if you define a in features to be an empty list you can add/delete > items to that list from the importing module. > > ## features.py## > a = [] > > ###main.py## > > import features > print features.a > > features.a.append(66) > print features.a > > features.a.remove(66) > print features.a > > > Does that help? > > NB. I don't really recommend this approach, but it is a way to do what I > think you want... > Thanks Alan & Christian, but i think I probably didnt communicate what i was trying to do. Maybe a sample of the actual code will help #!/usr/bin/python import fileinput import re import features client = open('clients/bob.txt', 'r') template = open('temp.txt', 'w') #Read all client data to variable dictionary data = {} for line in client: line = line.strip() line = line.split('=') data[line[0]] = line[1] requestedfeatures = data['features'].split(',') client.close() #Write template and modify all relevant sections for line in fileinput.input('template/noc-template.txt'): line = line.strip() templatereplace = re.match(r'.*\%\%(.*)\%\%.*', line) featurereplace = re.match(r'.*\$\$(.*)\$\$.*', line) if templatereplace: newline = re.sub('\%\%' + templatereplace.group(1) + '\%\%', data[templatereplace.group(1)], line) print newline template.write(newline + '\n') elif featurereplace: if featurereplace.group(1) in requestedfeatures: newline = re.sub('\$\$' + featurereplace.group(1) + '\$\$', features.rt, line) print newline template.write(newline + '\n') else: print line template.write(line + '\n') template.close() So. At the end of the day, i want to generate a page that will be read in a wiki. The requestedfeatures is a variable of features. So the end generated file will have certain extra fields depending on what features were selected. So the noc-template.txt is the main template which looks like this = %%clientname%% = this is the report tech contact: %%technical_contact%% $$rt$$ So my idea was to replace %%clientname%% with the dictionary and replace $$rt$$ with features from the features.py. The issue i have is with the featurereplace. and this line --newline = re.sub('\$\$' + featurereplace.group(1) + '\$\$', features.rt, line)--. Using features.rt works, but i essentially want to use features.featurereplace.group(1) which should be features.rt , but saying features.featurereplace.group(1) seems to look for that variable and doesnt substitute. I've tried things like features. + featuresreplace.group(1) etc but that doesnt work. Furthermore, i think what im trying to do here is a mix of templating, but i looked at cheetah and mako and cant work it out from the docs :(. I am probably doing this completely the wrong way round and appreciate better ideas on 'how to do things (tm)'. thanks!! :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] importing variables
"Stefan Lesicnik" wrote features file contains rt='''text''' import features a = 'rt' print features.rt #this works print features.a #this fails Because you defined a in your current file. You need to define it in features. I need to use features.a as i am iterating through a list and a would be the different features i want to check if they exist, and then use the variable. So you need to define a in features. Note that if you define a in features to be an empty list you can add/delete items to that list from the importing module. ## features.py## a = [] ###main.py## import features print features.a features.a.append(66) print features.a features.a.remove(66) print features.a Does that help? NB. I don't really recommend this approach, but it is a way to do what I think you want... -- 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] importing variables
Hi guys, Im trying to do something and hit a bit of a wall, potentially im going about this the wrong way. Essentially the problem is: features file contains rt='''text''' import features a = 'rt' print features.rt #this works print features.a #this fails I need to use features.a as i am iterating through a list and a would be the different features i want to check if they exist, and then use the variable. I hope that makes sense, or if anyone has any suggestion on how properly to do this! Thanks in advance stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to call a method with a print statement?
On Thu, Nov 12, 2009 at 6:00 AM, Kent Johnson wrote: > > > Defining __repr__ will give the custom representation when you just > give the name of the object: > > In [5]: class Foo2(): > ...: def __repr__(self): > ...: return "I'm a Foo2" > ...: > ...: > > In [6]: f2=Foo2() > > In [7]: f2 > Out[7]: I'm a Foo2 > > In [8]: print f2 > I'm a Foo2 > > Which can be surprisingly useful in certain cases (I've used it for debugging). class Foo2: def __init__(self): self.name = 'This is my name' self.value = 'This is my value' def __repr__(self): return "Name: %s\nValue: %s" % (self.name, self.value) In [2]: a = Foo2() In [3]: a Out[3]: Name: This is my name Value: This is my value In [4]: print a --> print(a) Name: This is my name Value: This is my value HTH, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to call a method with a print statement?
On Thu, Nov 12, 2009 at 6:35 AM, Luke Paireepinart wrote: > > > On Thu, Nov 12, 2009 at 5:29 AM, Jeff R. Allen wrote: >> >> You are looking for the __str__ method. See >> http://docs.python.org/reference/datamodel.html#object.__str__ >> > Can't you also implement __repr__? Yes, in fact if you are only going to implement one of __str__ and __repr__, arguably __repr__ is a better choice. __repr__() is called by the interactive interpreter when it displays an object. __str__ is called by print, and if you don't define __str__ it will call __repr__. So defining only __str__ will not give a custom representation unless you print: In [1]: class Foo(): ...: def __str__(self): ...: return "I'm a Foo" In [2]: f = Foo() In [3]: f Out[3]: <__main__.Foo instance at 0x1433468> In [4]: print f I'm a Foo Defining __repr__ will give the custom representation when you just give the name of the object: In [5]: class Foo2(): ...: def __repr__(self): ...: return "I'm a Foo2" ...: ...: In [6]: f2=Foo2() In [7]: f2 Out[7]: I'm a Foo2 In [8]: print f2 I'm a Foo2 Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] os.environ question
On Wed, Nov 11, 2009 at 10:34 PM, Khalid Al-Ghamdi wrote: > hi, > can anyone tell me why on python 2.6 i can enter os.environ and then get all > the items that that pertain to the os while on python 3 you just get the > following: > > with no items? I think this is perhaps an oversight in Python 3.1. I have filed a bug with an explanation (if you do want to know why) at http://bugs.python.org/issue7310 Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to call a method with a print statement?
On Thu, Nov 12, 2009 at 5:29 AM, Jeff R. Allen wrote: > You are looking for the __str__ method. See > http://docs.python.org/reference/datamodel.html#object.__str__ > > Can't you also implement __repr__? (bottom-posted for Dave) -Luke ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to call a method with a print statement?
You are looking for the __str__ method. See http://docs.python.org/reference/datamodel.html#object.__str__ class Foo(): def __init__(self): pass def __str__(self) return "hello world!" -jeff ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to call a method with a print statement?
On Thu, Nov 12, 2009 at 5:31 AM, Modulok wrote: > List, > > How do I get a print statement to call a class method? I'm using > python 2.5. I did it long ago, but can't remember how, or even where I > learned it from. Something like: > > class Foo(): > def __init__(self): > pass > > def ***part I can't remember goes here*** > print "hello world!" You want the __str__() special method. Note that it returns a string, it doesn't itself print: def __str__(self): return "hello world" Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unexpected iterator
2009/11/12 Jeff R. Allen : > Then I tried this to (maybe) set both a and b to 0: > a, b = 0 > Traceback (most recent call last): > File "", line 1, in > TypeError: 'int' object is not iterable I think you are looking for. >>> a = b = c = 300 Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] os.environ question
Ahoy! On Do, 2009-11-12 at 06:34 +0300, Khalid Al-Ghamdi wrote: > can anyone tell me why on python 2.6 i can enter os.environ and then > get all the items that that pertain to the os while on python 3 you > just get the following: > > with no items? Seems like os.environ has changed a little since Python 2.x. Try this: >>> import os >>> for env in os.environ: ...print('%s: %s' % (env, os.environ[env])) That should do the trick. Cheers, Alan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to call a method with a print statement?
List, How do I get a print statement to call a class method? I'm using python 2.5. I did it long ago, but can't remember how, or even where I learned it from. Something like: class Foo(): def __init__(self): pass def ***part I can't remember goes here*** print "hello world!" # Now use it: bar = Foo() print bar hello world! #<-- Magic! If any of this makes sense, any pointers would be great! -Modulok- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unexpected iterator
Jeff R. Allen wrote: a, b = 0 Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not iterable I understand why it doesn't work, but I don't understand the wording of the exception. Could someone explain how I accidentally introduced iteration into the picture with my syntax? I have a feeling there's an interesting lesson hidden in this example... To upack your variables a and b you need an iterable object on the right side, which returns you exactly 2 variables, e.g you could also write a, b = range(2) or create your own class class X: def __iter__(self): yield 0 yield 0 a,b = X() - Patrick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] moving directories into directories
"Christopher Spears" wrote I'm trying to move a bunch of files and directories into another directory. Unfortunately, I keep getting an error message: OSError: [Errno 17] File exists: './TAPE_ARCHIVES' The problem seems to happen when I try to move a directory into another directory: I think what is happening is that you are trying to copy something that has an equivalently named item already existing in the target. When you do this at a UI the OS asks if you want to replace it, but in a program it can't do that so it throws the exception. You need to handle that case by catching the exception and ignoring it, or stopping or whatever. 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] Unexpected iterator
Hello, I am working my way through the tutorial, and I like trying variations, just to see what expected errors look like, and other ways things could be written. I tried a, b = 0, 0 and that worked. Then I tried this to (maybe) set both a and b to 0: >>> a, b = 0 Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not iterable I understand why it doesn't work, but I don't understand the wording of the exception. Could someone explain how I accidentally introduced iteration into the picture with my syntax? I have a feeling there's an interesting lesson hidden in this example... Thanks. -jeff ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor