Re: flatten a list of list
On Aug 16, 6:47 am, Terry wrote: > Hi, > > Is there a simple way (the pythonic way) to flatten a list of list? > rather than my current solution: > > new_list=[] > for l in list_of_list: > new_list.extend(l) > > or, > > new_list=reduce(lambda x,y:x.extend(y), list_of_list) > > br, Terry Well, This is not simple but it is comprhensive in that it has to do several things. I am using it to decompose deeply nested lists from Pyparsing output that may have strings in a variety of languages. Performance wise I do not know how it stacks up against the other examples but it works for me. :-) def flatten(x): """flatten(sequence) -> list Returns a single, flat list which contains all elements retrieved from the sequence and all recursively contained sub-sequences (iterables). All strings are converted to unicode. """ result = [] for el in x: #if isinstance(el, (list, tuple)): if hasattr(el, "__iter__") and not isinstance(el, basestring): result.extend(flatten(el)) else: result.append(el) # all strings must be unicode rtnlist=[] for x in result: if isinstance(x,str): # replace any brackets so Python doesn't think it's a list and we still have a seperator. x=x.replace('[','_') x=x.replace(']','_') try: x=unicode(x, "utf8") # need more decode types here except UnicodeDecodeError: x=unicode(x, "latin1") except UnicodeDecodeError: x=unicode(x,"iso-8859-1") except UnicodeDecodeError: x=unicode(x,"eucJP") rtnlist.append(x) return rtnlist -- http://mail.python.org/mailman/listinfo/python-list
Re: What text editor is everyone using for Python
On May 26, 9:07 am, Lacrima wrote: > I am new to python. > And now I am using trial version of Wing IDE. > But nobody mentioned it as a favourite editor. > So should I buy it when trial is expired or there are better choices? I use nothing but Wing. Their support is great as well. -- http://mail.python.org/mailman/listinfo/python-list
Abstract Classes
I am implementing a set of specifications that were designed to be OO language neutral. Several classes are specified as being abstract; so therefore there should be no instances of them, correct? However in the classes that are subclasses what is the correct way in Python to implement them? I am using the zope.interface module as well. For example: class IA(Interface): m = Int() p = TextLine() class IB(Interface): x = Bool() s = Text() class A(object): """ This is an abstract class with attributes m is an int and p is a string""" implements(IA) pass class B(A): implements(IB) def __init__(self,m,p,x,s): m=m p=p x=x s=s or should it be like: class A(object): """ This is an abstract class with attributes m is an int and p is a string""" implements(IA) def __init__(self,m,p): m=m p=p class B(A): implements(IB) def __init__(self,m,p,x,s): A.__init__(m,p) x=x s=s or maybe even: class B(A): implements(IB) def __init__(self,m,p,x,s): super(A.__init__(m,p)) x=x s=s Thanks for any pointers. Tim -- Timothy Cook, MSc Health Informatics Research & Development Services LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook Skype ID == timothy.cook ** *You may get my Public GPG key from popular keyservers or * *from this link http://timothywayne.cook.googlepages.com/home* ** -- http://mail.python.org/mailman/listinfo/python-list
Re: IDE Question
On Oct 15, 2:19 pm, "Steve Phillips" <[EMAIL PROTECTED]> wrote: > Hi All, > I am just wondering what seems to be the most popular IDE. Well, you have already had many replies. For some context; I am an serious open source advocate. But for productivity I haven't been able to beat WingIDE. Their support is AWESOME! also. I have tried most of the FOSS offerings and I would like to see them compete. However, when it comes to tools... I have to use what works for me; that is Wing. As said before, they are not open source and not free. But if you only work on FOSS apps, you can get a free license. I chose to pay for a copy after having a FOSS copy because they are so good at support. I do not think you can beat the features. --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: Genital Hair Removal
On Thu, 2008-07-31 at 20:46 -0700, Paul McGuire wrote: > Be careful though, you should not modify a sequence while iterating > over it. > > -- Paul But if I can't remove each hair from the sequence as it's actually removed then how will I ever know when I'm finished? --Tim -- ** Join the OSHIP project. It is the standards based, open source healthcare application platform in Python. Home page: https://launchpad.net/oship/ Wiki: http://www.openehr.org/wiki/display/dev/Python+developer%27s+page ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Correct Attribute Assignment Methodology?
Say I have these classes: class Parent(object): """Parent is abstract""" a=None def showA(): return self.a class Child(Parent): """inherits a and showA from Parent""" def __init__(self,a,b): self.a=a self.b=b def showAB(): return self.a,self.b class GrandChild(Child): """inherits all of the above""" def __init__(self,a,b,c): self.a=a self.b=b """should this be Child.__init__(a,b)? or Child.__init__(b)?"" """if so; why? if not why not?""" self.c=c Thanks for answering these very basic questions but I am not certain about the correct way. I know that in Python, assignment in the GrandChild class will work but is that correct? --Tim -- ** Join the OSHIP project. It is the standards based, open source healthcare application platform in Python. Home page: https://launchpad.net/oship/ Wiki: http://www.openehr.org/wiki/display/dev/Python+developer%27s+page ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Re: Stripping parts of a path
On Sun, 2008-07-27 at 04:32 +, Tim Roberts wrote: > This doesn't do what you think it does. The parameter to rstrip is a set: > as long as the last character is in the set 'abcdhiloprs/', it will remove > it and check the next one. All of the characters in "shop" are in that > set. Thanks for all the replies. You are correct I misunderstood the docs. Finding and slicing works great. Cheers, Tim -- ** Join the OSHIP project. It is the standards based, open source healthcare application platform in Python. Home page: https://launchpad.net/oship/ Wiki: http://www.openehr.org/wiki/display/dev/Python+developer%27s+page ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Stripping parts of a path
Hi All, I just ran into an issue with the rstrip method when using it on path strings. When executing a function I have a need to strip off a portion of the current working directory and add on a path to a log file. Initially this worked great but then I added a branch in SVN which caused the path to contain 'LNCCWorkshop'. The rstrip() then began removing the characters 'shop' leaving an incorrect path to the log file. When I hard coded this path it worked okay but then did the same thing later in the file when I needed to point to a database. The code worked fine with a different path. Here are some code fragments. logfile=os.getcwd().rstrip('src/oship/atbldr')+'/oship/log/at_build_errors.log' this worked when the path was: /home/tim/ref_impl_python/TRUNK/oship/src/oship/atbldr the code above returns: /home/tim/ref_impl_python/TRUNK/oship/log/at_build_errors.log but when I tried a branches version that has the path: /home/tim/ref_impl_python/BRANCHES/LNCCWorkshop/oship/src/oship/atbldr it SHOULD return: /home/tim/ref_impl_python/BRANCHES/LNCCWorkshop/oship/log/at_build_errors.log but I get: /home/tim/ref_impl_python/BRANCHES/LNCCWork/oship/log/at_build_errors.log logfile=os.getcwd() print logfile is correct; but when I add the .rstrip('src/oship/atbldr') it also strips the 'shop' off of LNCCWorkshop and returns /home/tim/ref_impl_python/BRANCHES/LNCCWork/oship/log/at_build_errors.log I had two other people looking at this as we did troubleshooting and we could not determine the cause. It is repeatable with this path name. In resolution I renamed the branch to just LNCC and it works fine. Thoughts? Tim -- ** Join the OSHIP project. It is the standards based, open source healthcare application platform in Python. Home page: https://launchpad.net/oship/ Wiki: http://www.openehr.org/wiki/display/dev/Python+developer%27s+page ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Unicode confusion
Hi All, I'm not clear on how to use the unicode module. I need to be able to use certain characters such as the degree symbol and the mu symbol, i.e.: units = <"°"> if I say units=unicode("°"). I get UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128) If I try x=unicode.decode(x,'utf-8'). I get TypeError: descriptor 'decode' requires a 'unicode' object but received a 'str' What is the correct way to interpret these symbols that come to me as a string? Thanks, Tim -- ** Join the OSHIP project. It is the standards based, open source healthcare application platform in Python. Home page: https://launchpad.net/oship/ Wiki: http://www.openehr.org/wiki/display/dev/Python+developer%27s+page ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Re: Malaysia python user group
Marcus, You should probably contact the Malaysian Public Sector Open Source Competency Centre (OSCC) in Cyberjaya. http://www.oscc.org.my HTH, Tim On Mon, 2008-07-14 at 10:57 +0800, Marcus.CM wrote: > Hi, > > I am thinking of promoting Python for the local developers here in > Malaysia, via Universities , seminars etc . > Is there already a user group here in Malaysia? Any pointers would help. > > Marcus. > > -- > http://mail.python.org/mailman/listinfo/python-list -- ** Join the OSHIP project. It is the standards based, open source healthcare application platform in Python. Home page: https://launchpad.net/oship/ Wiki: http://www.openehr.org/wiki/display/dev/Python+developer%27s+page ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Functional/Best?
I guess I can classify my application(s) as more procedural than anything else. But I have a question about the best way to handle something in Python. When given a mapping of keywords, I want to call a function based on a certain keyword found when parsing a text file. The mapping looks like this: definClassMap={'SECTION':'bldSection','COMPOSITION':'bldComposition','OBSERVATION':'bldObservation','ITEM_TREE':'bldItemTree'} So if the text file contains 'ITEM_TREE' I want to call bldItemTree which creates an instance of the class ItemTree. I currently use an if ..., elif ... construct. Is there a better, more efficient, more Pythonic way of doing this? Thanks, Tim -- ** Join the OSHIP project. It is the standards based, open source healthcare application platform in Python. Home page: https://launchpad.net/oship/ Wiki: http://www.openehr.org/wiki/display/dev/Python+developer%27s+page ** <> signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Re: FOSS projects exhibiting clean/good OOP?
On Wed, 2008-07-09 at 07:38 -0700, Phillip B Oldham wrote: > I'm wondering whether anyone can offer suggestions on FOSS projects/ > apps which exhibit solid OO principles, clean code, good inline > documentation, and sound design principles? > > I'm devoting some time to reviewing other people's code to advance my > skills. Its good to review bad code (of which I have more than enough > examples) as well as good, but I'm lacking in finding good examples. > > Projects of varying sizes would be great. Of course 'I think' mine matches that description. :-) In addition to the two links in the signature below where you can get a description and source code; there is an entry on Ohloh that says it is well documented code. http://www.ohloh.net/projects/oship I would appreciate your feedback. Cheers, Tim PS. The Launchpad and Ohloh repositories lag the openEHR SVN by several hours. -- ** Join the OSHIP project. It is the standards based, open source healthcare application platform in Python. Home page: https://launchpad.net/oship/ Wiki: http://www.openehr.org/wiki/display/dev/Python+developer%27s+page ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Re: start reading from certain line
On Wed, 2008-07-09 at 03:30 -0700, antar2 wrote: > I am a starter in python and would like to write a program that reads > lines starting with a line that contains a certain word. > For example the program starts reading the program when a line is > encountered that contains 'item 1' > > > The weather is nice > Item 1 > We will go to the seaside > ... > > Only the lines coming after Item 1 should be read file=open(filename) while True: line=file.readline() if not line: break if 'Item 1' in line: print line HTH, Tim -- ** Join the OSHIP project. It is the standards based, open source healthcare application platform in Python. Home page: https://launchpad.net/oship/ Wiki: http://www.openehr.org/wiki/display/dev/Python+developer%27s+page ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Re: start reading from certain line
On Wed, 2008-07-09 at 03:30 -0700, antar2 wrote: > I am a starter in python and would like to write a program that reads > lines starting with a line that contains a certain word. > For example the program starts reading the program when a line is > encountered that contains 'item 1' > > > The weather is nice > Item 1 > We will go to the seaside > ... > > Only the lines coming after Item 1 should be read file=open(filename) while True: line=file.readline() if not line: break if 'Item 1' in line: print line HTH, Tim -- ** Join the OSHIP project. It is the standards based, open source healthcare application platform in Python. http://www.openehr.org/wiki/display/dev/Python+developer%27s+page ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Re: a simple 'for' question
On Wed, 2008-07-09 at 00:00 -0400, Ben Keshet wrote: > oops, my mistake, actually it didn't work... > when I tried: > for x in folders: > print x # print the current folder > filename='Folder/%s/myfile.txt' %x > f=open(filename,'r') > > it says: IOError: [Errno 2] No such file or directory: > 'Folder/1/myfile.txt' > I believe it's because x is the position marker what you want instead is the contents of folders at x; therefore folders[x] HTH, Tim -- Timothy Cook, MSc Health Informatics Research & Development Services LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook Skype ID == timothy.cook ** *You may get my Public GPG key from popular keyservers or * *from this link http://timothywayne.cook.googlepages.com/home* ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Re: Hands-on HTML Table Parser/Matrix?
There are couple of HTML examples using Pyparsing here: http://pyparsing.wikispaces.com/Examples --Tim On Sun, 2008-07-06 at 14:40 +0200, robert wrote: > Often I want to extract some web table contents. Formats are > mostly static, simple text & numbers in it, other tags to be > stripped off. So a simple & fast approach would be ok. > > What of the different modules around is most easy to use, stable, > up-to-date, iterator access or best matrix-access (without need > for callback functions,classes.. for basic tasks)? > > > Robert > -- > http://mail.python.org/mailman/listinfo/python-list -- Timothy Cook, MSc Health Informatics Research & Development Services LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook Skype ID == timothy.cook ** *You may get my Public GPG key from popular keyservers or * *from this link http://timothywayne.cook.googlepages.com/home* ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Re: Instance Names
On Thu, 2008-07-03 at 14:20 -0500, Larry Bates wrote: > I suspect there is some "misunderstanding" here. Why exactly do you think > you > need to have your instances named with [] characters in them? > I often misunderstand. :-) But, I am implementing specifications in Python that are already implemented in other languages. http://www.openehr.org/releases/1.0.1/roadmap.html These specifications say that an archetype node id consists of identifiers like [at] and [at0001]. Now these are valid URIs and the associated query language (AQL) used by other services will send queries with those characters in them. For example: FROM EHR [ehr_id/value=$ehrUid] CONTAINS COMPOSITION [openEHR-EHR-COMPOSITION.encounter.v1] CONTAINS OBSERVATION obs [openEHR-EHR-OBSERVATION.blood_pressure.v1] WHERE obs/data[at0001]/events[at0006]/data[at0003]/items[at0004]/value/value >= 140 Since I am also using Zope3 it would be easier on me to name the instances with those characters. But my fall back is to set at.__name__='[at]' and manipulate the query to match __name__ instead of the actual instance ID. Thoughts? --Tim -- Timothy Cook, MSc Health Informatics Research & Development Services LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook Skype ID == timothy.cook ** *You may get my Public GPG key from popular keyservers or * *from this link http://timothywayne.cook.googlepages.com/home* ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Instance Names
Hi All, I have a need (if at all possible) to create instance names using '[' and ']', i.e. [at]=ClassA0(), [at0001]=ClassB2(), etc. Of course Python tries to unpack a sequence when I do that. Is there anyway to do this? I do have a workaround but it is an ugly, nasty URL mangling thing. :-) Cheers, Tim -- Timothy Cook, MSc Health Informatics Research & Development Services LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook Skype ID == timothy.cook ** *You may get my Public GPG key from popular keyservers or * *from this link http://timothywayne.cook.googlepages.com/home* ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Re: IDE on the level of Eclipse or DEVc++?
I highly recommend Wing http://www.wingware.com . There are various licensing levels and even one for open source only developers. The support is AWESOME!. --Tim On Mon, 2008-06-23 at 09:15 -0700, cirfu wrote: > is there an IDE for python of the same quality as Eclipse or DEVC++? > > I am currently using the editor that coems iwth python and it is all > fine but for bigger projects it would be nice to have some way to > easier browse the projectfiles for example. > -- > http://mail.python.org/mailman/listinfo/python-list -- Timothy Cook, MSc Health Informatics Research & Development Services LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook Skype ID == timothy.cook ** *You may get my Public GPG key from popular keyservers or * *from this link http://timothywayne.cook.googlepages.com/home* ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Python Package Construction
Hi All, I would like feedback on the proper/best 'Pythonic' approach. This is a rather subjective question. Where is the trade-off between package name lengths and faithfulness to the specifications? [Discussion follows] I am implementing a set of specifications for healthcare IT for Python programmers to be able to develop interoperable healthcare applications. I am using ZCA (aka.Zope3) extensively. My desire is to implement the specs as faithfully as possible for two reasons: 1) teachability - how easy/difficult is it to teach the framework and specifications to new developers? 2) maintainability - which approach, if either, will make it easier to maintain the framework if/when the specifications change? My first pass was to develop a skeleton of the specs using Interfaces from the ZCA approach and then the implementations following the document structure of the specs. The specs are available via SVN at: http://www.openehr.org/svn/specification/TRUNK/publishing/architecture/ It is best to probably use real examples. Following the document structure for packaging AND using the ZCA convention of having a sub-directory for interfaces caused massive circular import issues due to some classes being used in the interface definition of classes inside the same interface file being imported into the implementation file. If that sounds confusing; it is. It was confusing to write too. :-) If anyone has questions I'll try to expand. It is best to probably use specific, real examples. http://www.openehr.org/svn/specification/TRUNK/publishing/architecture/rm/data_types_im.pdf (note class names are converted from the upper case, underscore separated style to CamelCase) The package openehr.rm.datatypes.text defines the implementation class CodePhrase. The associated interface file openehr.rm.datatypes.interfaces.text needed CodePhrase as an attribute type in DvCodedText and TermMapping needs both CodePhrase and DvCodedText. This quickly got out of control. So my solution to solving the circular imports is to take each interface and implementation and put them into one file. Research tells me that this is probably the second mostly popular ZCA approach. So, ICodePhrase and CodePhrase are now in openehr/rm/datatypes/codephrase.py, DvCodeText and IDvCodedText in openehr/rm/datatypes/dvcodedtext.py, etc. But wait, now I don't have a 'text package'. So if codephrase.py and dvcodedtext.py were in openehr/rm/datatypes/text/ that would solve the problem. BUT! Throughout the specs many of the names are VERY long already. Adding another package name that is from 4 - 15 (or more) characters long adds to the length of already long import statements, i.e. (sorry for the email line wraps) from openehr.am.archetype.creferenceobject import ICReferenceObject,CReferenceObject should really be from openehr.am.archetype.constraintmodel.creferenceobject import ICReferenceObject,CReferenceObject Thoughts, opinions and jeers all gratefully accepted. :-) --Tim -- Timothy Cook, MSc Health Informatics Research & Development Services LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook Skype ID == timothy.cook ** *You may get my Public GPG key from popular keyservers or * *from this link http://timothywayne.cook.googlepages.com/home* ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Python/ZCA Healthcare Project Announcement
will be spending 10 days on health informatics, openEHR and OSHIP. The goal is to actually develop one or more OSHIP applications as examples. There is at least one PhD student that is using these ideas for his project. OSHIP is already considered to be the Python reference implementation of the openEHR specs. (BTW: for anyone interested there is a Ruby implementation underway as well). In order to promote the widest use of the openEHR specifications; OSHIP is licensed under the Mozilla tri-license http://www.mozilla.org/MPL/boilerplate-1.1/mpl-tri-license-txt If you have any interest in helping move this project ahead please join the developer's list at the SourceForge Project site: http://sourceforge.net/projects/oship The sourcecode will be placed on the openehr.org SVN server by 31 May, 2008. I also plan to put an egg on the SF site. This will be 'alpha' level code, though I hope that we can move to a beta stage at a fairly rapid pace (mid July?). I do not envision that the Zope experts will need to do the actual manual labor of fixing a lot of this code. If I can get some helpful suggestions then I will gladly do the work as well as manage others helping out. As an aside, one of the key benefits to this project is that the core documentation is already complete. The openEHR specifications do that for us. We just need to finish the implementation and some top-level ZCA specific docs. Thank you very much for your kind attention to this project that holds such a deep passion for me. Sincerely, --Tim Cook -- Timothy Cook, MSc Health Informatics Research & Development Services LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook Skype ID == timothy.cook ** *You may get my Public GPG key from popular keyservers or * *from this link http://timothywayne.cook.googlepages.com/home* ** signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list