[Tutor] tree/node class/module
hello, I'm wanting to do a GUI for to help people using rsync, du, tar . it will provide the --exclude and --include of them. And I thought that the best way to store data relative to the hierarchy filesystem was to store them in a tree structure. But after browsing ages on the web, I didn't found any class/modules which implements trees, with some comments, and small examples. Am I just blind ??? I'm looking for a tree of object which will give me stuff like: - sibling - parent - children - find - insert Do you have any pointers ?? Ced. -- Cedric BRINER Geneva - Switzerland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tree/node class/module
usefull page http://www.velocityreviews.com/forums/t355467-tree-and-graph-structures-in-python.html thanks again Ced. -- Cedric BRINER Geneva - Switzerland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] uncomprehension on RE
Kent Johnson wrote: cedric briner wrote: To let you know, I'm writing a script to generate bind9 configuration from a nis hosts table. So I was trying in a one re to catch from this: ip hostname [ cname ...] [# comment] e.g: 10.12.23.45 hostname1 alias1 alias2 alias3 # there is a nice comment 37.64.86.23 hostname2 35.25.89.34 hostname3 alias5 .. .. so I was wishing to write an re expresion which will do in a one step all the job ! maybe am'I expecting too much from re :) You can use an re to get the four primary fields, then use split() to break up the aliases. Try this: import re data = '''10.12.23.45 hostname1 alias1 alias2 alias3 # there is a nice comment 37.64.86.23 hostname2 35.25.89.34 hostname3 alias5 '''.splitlines() for line in data: match = re.search(r'^(\S+)\s+(\S+)([^#]*)(#.*)?$', line) if match: ip, host, aliases, comment = match.group(1, 2, 3, 4) print 'ip:', ip print 'host:', host if aliases: aliases = aliases.strip().split() for alias in aliases: print 'alias:', alias if comment: print comment print I had done that before writing my first email. I knew how to do this in few python steps. But some how I was expecting RE beeing able to do this in an one shoot. Thanks for your precious help. Ced. Kent -- Cedric BRINER Geneva - Switzerland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] uncomprehension on RE
Stephen Nelson-Smith wrote: On 9/20/07, cedric briner [EMAIL PROTECTED] wrote: To let you know, I'm writing a script to generate bind9 configuration from a nis hosts table. So I was trying in a one re to catch from this: ip hostname [ cname ...] [# comment] e.g: 10.12.23.45 hostname1 alias1 alias2 alias3 # there is a nice comment 37.64.86.23 hostname2 35.25.89.34 hostname3 alias5 I'm not sure I follow. Could you state explicitly what the input is (I think the above), and what output you want? S. the input is 10.12.23.45 hostname1 alias1 alias2 alias3 # there is a nice comment 37.64.86.23 hostname2 35.25.89.34 hostname3 alias5 the ouput is ip hostname [ cname ...] [# comment] -- Cedric BRINER Geneva - Switzerland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] uncomprehension on RE
Kent Johnson wrote: cedric briner wrote: Hello, I do not understand the behaviour of this: import re re.search('(a)*','aaa').groups() ('a',) I was thinking that the ``*'' will operate on the group delimited by the parenthesis. And so, I was expecting this result: ('a', 'a', 'a') Is there something I'am missing ? It just doesn't work that way. that's sad :( The returned group is the last string matched by the group: In [17]: re.search('(a.)*','azabac').groups() Out[17]: ('ac',) Somewhere I find re really too difficult. I was never able to write an re expression rightly from the first shoot ! Happily there is kodos and redet. Use re.findall() instead: In [18]: re.findall('a.', 'azabac') Out[18]: ['az', 'ab', 'ac'] that's what I found, that I've to do the job with many python steps than just one If you want overlapping matches you have to search in a loop and collect them yourself, findall() gives only non-overlapping matches. To let you know, I'm writing a script to generate bind9 configuration from a nis hosts table. So I was trying in a one re to catch from this: ip hostname [ cname ...] [# comment] e.g: 10.12.23.45 hostname1 alias1 alias2 alias3 # there is a nice comment 37.64.86.23 hostname2 35.25.89.34 hostname3 alias5 .. .. so I was wishing to write an re expresion which will do in a one step all the job ! maybe am'I expecting too much from re :) Ced. -- Cedric BRINER Geneva - Switzerland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] uncomprehension on RE
Hello, I do not understand the behaviour of this: import re re.search('(a)*','aaa').groups() ('a',) I was thinking that the ``*'' will operate on the group delimited by the parenthesis. And so, I was expecting this result: ('a', 'a', 'a') Is there something I'am missing ? Ced. -- Cedric BRINER Geneva - Switzerland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] ldap client
debian/sarge hello, I'm wanting to interogate an ldap server to fetch informations. so I'm looking for a python2.4 ldap client. And the only I've found is python2.4-ldaptor. Does some of you knows an good ldap client which use SSL ? Ced. -- Cedric BRINER Geneva - Switzerland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] cas and python
hi, does one of you knows if there is a python module which implements CAS(Central Authentication Service): https://clearinghouse.ja-sig.org/wiki/display/CAS/Home thanks in advance Ced. -- Cedric BRINER Geneva - Switzerland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python DB
I love sqlobject.org it this a wrapper between many sql implementation (mysql, postgresql..) and python. get a look. Ced. -- Cedric BRINER Geneva - Switzerland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDEs
what about eric3 ? http://www.die-offenbachs.de/detlev/eric3.html Ced. -- Cedric BRINER Geneva - Switzerland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] python learning
hi, 1)OB I'm learning for almost a year and half python. this is my first language. I'm fine with classes, definition... But now I'm trying to understand better things as: static method class method and the use of metaclass. Does some one of you knows about a compilations of small snippets showing how things works! an url ? because I'm trying to work with sqlobject, and I have to tell my self that I'm far away of a good comprehension of it.. : ( 2) Sometimes, I do think that it will help a lot, to have such compilations. So that people can scan it, test them very easily. Such snippet shall have some use of them (case that work, case that do not work) some explanation in which can we can use them and so on. For example, what I've learned recently. module.py class test(object): def __init__(self): self.dico={} def setname(self,name, value): self.dico[name]=value def dico(self): return printDico testInstance=test()# * setname=testInstance.setname # ** dico=testInstance.dico # *** in the python interpreter. import module module.setname('key1','value1') module.dico() del module # so I've supposed that the garbage collector will destruct this. from module import dico dico() # tintintammm and badaboum there are still there So I imagine, that when you import this module. ``testInstance'' is created in the object space, but it is not assigned to the namespace. So the only way to destruct such module, will be within the module. I found that such module act has a singleton Ced. -- Cedric BRINER Geneva - Switzerland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SQL headache with column
did you heard about sqlobject ? give a try...http://www.sqlobject.org/ Ced. -- Cedric BRINER ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] module with static global
give an eyes to sqlobject.org. -- Cedric BRINER ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] interactif or not
If you want to know if your program has been launched from an interactive terminal or from a system background process like cron or init, then you look to see if stdin is a tty. Unix is usually very careful about who has a controlling tty and who does not. In Python, file objects have an isatty() method that will return True or False. import sys isinteractive = sys.stdin.isatty() if isinteractive: ... else: ... tested and it works ! Ced. -- Cedric BRINER ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Looking for HOWTO's
Maybe this is Off-Topic but I found recently a perfect repository of python tutorials at: http://www.awaretek.com/tutorials.html Ced. -- Cedric BRINER ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] interactif or not
hi, How can I know if a script is launched interactively or not because I'd like to make a script verbose or not depending if it is executed as interactive or not. eg. If I invoke it in a shell.. then it can be verbose If it is launched from a crontab.. then it is less verbose. Ced. -- Cedric BRINER ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creation of a module
Hi Alan Hi Cedric, Reading through this I can't help but think you are going to a lot of trouble to make things very complicated for yourself - and for anyone else who has to work on your code. The Python module system is very simple and helpful in tracking down where the various things live. You can see by the prefix name which package/file provides which service. It appears that you are trying to hide that information? Is there a special reason why you are trying to introduce this kind of obfuscation? {a,b,c}.py contains respecetively classA,classB,classC more some unittest and bunch.py will contains some usefull function using the class{A,B,C} I'd like that when I import MyModule (`import MyModule')OB I'll see from it: MyModule.classA .classB .classC .function1 in numch.py .function2 in numch.py ... without seeing MyModule.{a,b,c} Why do you want to conceal the information about what lives inside the package? How will users be able to debug the package? If you hide a,b and c you will make it harder to do things like I thought that it will be more convenient. But as I can read from your email: it seems a really __not_so_good__ idea . dir(MyModule) dir (MyModule.a) etc to explore the structure and find the code. never done such kind of things. I've done it. But is it normal that I'see also the filename Actually you don;t see the filename you see the sub moduile names. a.py becomes a module called a within your package. the idea was to automate the : from MyModule.a import classA from MyModule.b import classB from MyModule.c import classC What is the advantage - apart from a minimal amount of typing? You still need to maintain a list of the class/module mappings. no the idea was to give a prefix to all my files depending on the way I'd like to import them. In fact, my files are named class_foo.py and the class inside this file is named CFoo. with a loop snip lclassname=['classA', 'classB', 'classC'] #in the future I can even automate the creation of classname list for classname in lclassname: #filename='a', classname='classA' filename=classname.replace('class','').lower() Maybe you should use a dictionary as Kent suggested: classes = {'classA': 'a', 'classB': 'b', 'classC : 'c' } filename = classes[classname] #from filename import classname module=__import__(filename, gloabls(), locals(), [classname]) classDefinition=getattr(module,classname) assign(classname,classDefinition) snip But this is a lot of work to sabve a very small amount of typing and could slow down your program initialisation significantly. In this way, when I'll do an import FWobs I'll get: MyModule.classA .classB .classC .function1 in numch.py .function2 in numch.py It looks as if all you really want to do is avoid having the sub-module names visible when you import the package. As I explained above I think thats a fundamentally bad idea. Good, I'll follow your recommendation If you do just want to export a single set of services from a single import it would be much simpler to just put them all in a single module! And then you can hide the exposed names using the Python naming convention of prefixing with '_' or ' __'. Of course if its a very large set of services a package is better, but then you probably should expose the sub modules too. NAmespaces are one of Pythons best features - they are simple to use and very effective both in controlling program structure and in aiding debugging, its probabnly best to use them as intended rather than trying to force them to work differently. All IMHO of course! :-) Alan G thanks for your time. Ced. -- Cedric BRINER ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Testing for commandline args
Also, I hear that optparse is much better than getopt. this is a true pleasure to work with optparse. It was included in python2.3 and was primarly called python-optik Ced. -- Cedric BRINER ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] creation of a module
hi, 1) I'm trying to create my _first_ own module. I've decided to write each class into a separate file. /MyModule|-bunch.py |-a.py |-b.py `-c.py {a,b,c}.py contains respecetively classA,classB,classC more some unittest and bunch.py will contains some usefull function using the class{A,B,C} I'd like that when I import MyModule (`import MyModule')OB I'll see from it: MyModule.classA .classB .classC .function1 in numch.py .function2 in numch.py ... without seeing MyModule.{a,b,c} 2) does someone now how to do this: x=3 with a function like: assign('x',3) -- Cedric BRINER ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creation of a module
hi, 1) I'm trying to create my _first_ own module. I've decided to write each class into a separate file. OB /MyModule|-bunch.py |-a.py |-b.py `-c.py You also need MyModule/__init__.py to signal to Python that MymModule is a package. (Actually you are creating a package containing several modules.) thank you to enlight me.. :).. that I was creating a package containing many modules. Maybe I'm smarter than I thought. {a,b,c}.py contains respecetively classA,classB,classC more some unittest and bunch.py will contains some usefull function using the class{A,B,C} I'd like that when I import MyModule (`import MyModule')OB I'll see from it: MyModule.classA .classB .classC .function1 in numch.py .function2 in numch.py ... without seeing MyModule.{a,b,c} In MyModule/__init__.py put from MyModule.a import classA from MyModule.b import classB from MyModule.c import classC I've done it. But is it normal that I'see also the filename MyModule.classA .classB .classC .a not expected .b --' .c -' .function1 in numch.py .function2 in numch.py can I not import them ? This creates package-level attributes for the classes. 2) does someone now how to do this: x=3 with a function like: assign('x',3) Can you say why you want to do this? It is possible but generally it is better to use a dict: values = {} values['x'] = 3 the idea was to automate the : from MyModule.a import classA from MyModule.b import classB from MyModule.c import classC with a loop snip lclassname=['classA', 'classB', 'classC'] #in the future I can even automate the creation of classname list for classname in lclassname: #filename='a', classname='classA' filename=classname.replace('class','').lower() #from filename import classname #can't do this module=__import__(filename, gloabls(), locals(), [classname]) classDefinition=getattr(module,classname) assign(classname,classDefinition) snip In this way, when I'll do an import FWobs I'll get: MyModule.classA .classB .classC .function1 in numch.py .function2 in numch.py thanks for your help ! -- Cedric BRINER ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] incomprehension in type of classes.
from (I): class CYear: def __init__(self, year): self.__year=year print str(self.__year) ---(*) print 'dir of: '+str(dir(self)) print 'type of: '+str(type(self)) def __str__(self): return we are in +str(self.year) to (IIOB): class CYearDerived(int): def __init__(self, year): super(CYearDerived,self).__init__(year) self.__year=year print str(self.__year)-(*OB) print 'dir of: '+str( dir(self) ) print 'type of: '+str(type(self)) def __str__(self): return we are in +super(CYearDerived,self).__str__() why type of (CYear(2005)) type 'instance' and type(CYearDerived) class '__main__.CYearDerived' doesn't give the same type ??? It's a difference between new-style and old-style classes. CYear is an old-style class because it doesn't inherit from object. CYearDerived is a new-style class because it inherits from int which is a subtype of object: class C: pass # old-style class ... type(C()) type 'instance' C().__class__ class __main__.C at 0x008CE7B0 class C2(object): pass # new-style class ... type(C2()) class '__main__.C2' C2().__class__ class '__main__.C2' int.__bases__ # int inherits from object (type 'object',) I'm not sure *why* there is this difference between the two types of classes, but there is... ah... I din't know that there was two kinds of classes. So you mean that, now the new style object should be like: class A(object): pass class A: pass ... class B(object): pass ... a=A() b=B() I see that dir (b) compare to dir(a) provides more stuff. So the new style of a class, is not only about syntax but also about properties ??? ok, I'll try to find more about this ! By the way extending int does not work the way you have done it here. int is an immutable type so you have to initialize it in the __new__() method, not in __init__(). Read the details here: http://www.python.org/2.2/descrintro.html#__new__ this link is very usefull thanks Cedric BRINER ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] incomprehension in type of classes.
hello, I have 2 questions. the first one is maybe not directly related to python (it's about eric3), and the second oneis about what the command `type' give back for two instances in my python code I have changed some classes like the following from (I): class CYear: def __init__(self, year): self.__year=year print str(self.__year) ---(*) print 'dir of: '+str(dir(self)) print 'type of: '+str(type(self)) def __str__(self): return we are in +str(self.year) to (IIOB): class CYearDerived(int): def __init__(self, year): super(CYearDerived,self).__init__(year) self.__year=year print str(self.__year)-(*OB) print 'dir of: '+str( dir(self) ) print 'type of: '+str(type(self)) def __str__(self): return we are in +super(CYearDerived,self).__str__() Question 1 (about eric3) -- with the change from I to II, when I debug the code, I don't see the self arborescence (that you find in Window Debug BrowserLocal Variables) which shows you the attributes of the class that you are currently debugging in. Quesion 2 - why type of (CYear(2005)) type 'instance' and type(CYearDerived) class '__main__.CYearDerived' doesn't give the same type ??? -- Cedric BRINER ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor