Parsing XML using SAX
Hi, Currently I m writing an XML parser that processes an xml file using sax, and I have it working, however I want to make the code of my parser less cluttered and more readable by other people (including myself). However it is quite messy at the moment. The main reason is that Python doesnt have a switch statement. def startElement(self,name,attributes): if name == "sbml": s = Sbml(attributes['xmlns'], attributes['version'], attributes['level']) self.sbmlDict['sbml'] = s elif name == "model": m = Model(attributes['id'], attributes['name']) self.sbmlDict['model'] = m elif name == "listOfCompartments": self.inListOfCompartments = bool(1) elif name == "compartment" and self.inListOfCompartments: c = Compartment(attributes['id'], attributes['name']) self.tempList.append(c)...snipI would use a dictionary for this, but this would require the use of many extra methods for each tag name, and this would lead to clutter aswell. Does anyone have any suggestions for reducing the number of lines and making my code look neater than a large amount of methods or elif statements. Many ThanksNathan -- http://mail.python.org/mailman/listinfo/python-list
Graph Data Structures
Hi All, Currently I am working on a generic graph library so I can do various graph based analysis for various projects I have ideas for. Currently I am implementing Graph as a wrapper around a dictionary. Currently my implementation works like this: t = Graph() n1 = Node("Node1") n2 = Node("Test2") edge1 = Edge("Test3") t += n1{ n1:{}} t[n1][n2] = edge1{ n1:{n2:edge1} However this isnt actually ending up with the structure I want. I want it to finally end up as ..{ n1:{n2:edge1}, n2:{}}. Is there anyway I can do this simply Also I am looking at having a large graph and was wondering if anyone knew of anyway I could reduce the memory requirements of this structure and improve the speed of queries on it. I m thinking writing a C extension for itis this a good idea and where would I start? Or does Python have some kind of transparent memory access module I can implement. Many Thanks in advance, Nathan PS.Please find my code below: class Graph(object): def __init__(self, g= { } ): self.graph = g def __iadd__(self, p): if p not in self.graph: self.graph[p] = PathsDict() return self def __getitem__(self, p): try: return self.graph[p] except KeyError: raise KeyError( "%s not in graph" %(repr(p)) ) def __str__(self): return str(self.graph) def filter(self, filter): pass class PathsDict(object): def __init__(self): self.paths = { } def __setitem__(self, p, val): if p not in self.paths: self.paths[p] = val def __getitem__(self, p): return self.paths[p] # catch exception here def paths(self): for k, v in self.paths: yield (k, v) def edges(self): return self.paths.values() def __str__(self): return str(self.paths) def __len__(self): return len(self.paths) class Node(object): def __init__(self, name): self.name = name def __str__(self): return self.name class Edge(dict): def __init__(self, name, weight = 1): self["name"] = name self["weight"] = weight def __str__(self): return self["name"] -- http://mail.python.org/mailman/listinfo/python-list
Importing module of data dicts and constants
Hi All, I ve got a single module which I m using to contain a lot of dictionaries, constants, general information, which are used by various other modules. However I can't seem to access them: in data.py _SEQTYPE_DNA = 0 _SEQTYPE_RNA = 1 _SEQTYPE_PROT = 2 _seqType = { "DNA":_SEQTYPE_DNA, "RNA":_SEQTYPE_RNA, "PROTEIN":_SEQTYPE_PROT } but in test.py from data import * class Test(object): def __init__(self, type): self.type = _seqType[type] def test(self): return self.type t = Test("DNA") print t.test() File "test.py", line 24, in __init__ self.type = _seqType[type] NameError: global name '_seqType' is not defined I think I m doing something incredibly wrong/stupid here Can anyone help? Many Thanks Nathan PS. I was wondering if the use of a data module to store constants and dictionaries is good design??? or not? -- http://mail.python.org/mailman/listinfo/python-list
Re: Graph Data Structures
> > https://networkx.lanl.gov/ This was working for me earlier, I managed to get everything from there earlier. It seems a very good package. It seems theres more out there than what I had thought, which unfortunately makes it harder for me to decide what to use (pynetwork and bgl look useful aswell). I m going to do some testing on it later and see what happens with it. Thanks a lot for your help. Has anyone got an idea how I could split the contents of a node and its representation (to save memory in my graph). ie the nodes contain the start and end coordinates and id and the actual representation contains the string. I was going to have : class Node(object): pass class Section(Node): pass class Item(object): pass Where section contains a slice of the Item which im interested. I m just not sure how I can access the contents of item without storing it. ---> If u get what I mean??? Many Thanks in advance Nathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Graph Data Structures
Hi, The idea is that I m going to use it to build graphs for sequence alignment (at the moment), I read a discussion on the corebio (reimplementation of biopython) group about using intervals to represent sequence slices. The idea being that, my graph may contain millions of alignments and storing the sequence (the actual ATGC) is not required. class Node(object): pass class Interval(Node): _id = "gene1" _start = 50 _end = 200 _strand = 1 class Sequence(object): _sequence = "atgtcgtgagagagagttgtgag." So one interval on one sequence would align to another interval from another sequence, but I want changes I make to the interval to be reflected in the representation later. If I reverse complement it i want the interval to store this information but the Sequence only shows this later on when I call use it calling repr or str. Do you get what I mean. Many Thanks Nathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Graph Data Structures
Hi, It seems that by just going through the problem writing out a better explanation for the reply I have figured out a solution and the problem isnt as difficult as I thought it would be. What is a wontok? Thanks Nathan PS --> the start of my reply: class Interval(object): _id = "gene1" _start = 50 _end = 200 _strand = 1 class Sequence(object): _sequence = "atgtcgtgagagagagttgtgag." > Only vaguely. You use several terms which appear to be from your trade > jargon Sequence is a string made from a restricted alphabet (A,T,G,C...). Sequences can be aligned: 1 ATGCTGCAT 2 TAGCTGTTA --- 25 I m trying to represent this as a graph Interval(id=1, start=2, end=6, strand=1) ---edge--Interval(id=2, start=2, end=6, strand=1) The problem is I was planning on storing the sequences in a dictionary {id:Seq}, however each dictionary would represent a different source of sequences. File1, File2... ( STORE THE SOURCES AS A DICT AND HAVE SOURCE IN INTERVAL ASWELL -- http://mail.python.org/mailman/listinfo/python-list
Use of factory pattern in Python?
Hi, Im trying to find the best way to do a certain task and my so far I have found that using something similar to factory pattern might be the best bet. I m parsing a file to be stored as a graph (using NetworkX). Each row in the file corresponds to a node in the graph. However rows have different types and different numbers of attributes. ( and their corresponding nodes in the future will have methods ) eg chr1 SGD gene 5 8 id=1 name=3 dbref=6 chr1 SGD intron 5 6 id=5 notes="spam" chr1 SGD exon 7 8 id=5 so I was thinking of having a factory class to return the individual objects for each row..ie class Factory(): # if passed a gene return a gene object # if passed an intron return an intron object # if passed an exom return an exon object Is this a good way of doing this, or is there a better way to do this in Python, this is probably the way I d do it in Java. Many Thanks Nathan -- http://mail.python.org/mailman/listinfo/python-list
Metaclass uses?
Hi, Recently I posted a question about the factory pattern in python. I ve implemented this following one of the ideas posted. After some reading I ve seem a lot of Aspect Oriented Programming mentioned but I m not really sure what it is. Can anyone help me understand how metaclasses can improve the quality of my code and let me do better things. Also is there anymore interesting OO stuff that Python has apart from Java. Many Thanks Nathan -- http://mail.python.org/mailman/listinfo/python-list
recursion error using setattr and getattr
Hi, I m trying to implement an object which contains lazy" variables. My idea is to alter the getattr and the setattr methods. However I keep on getting a recursion error. My idea is that the lazy variable can be stored in a variety of places, Database, PyTables etc. The lazy variable is a large variable and so I dont want to hold it in memory all of the time, I d rather just get it when needed and then store it for future work. Most of the work will be done using var1 and var2 So .. class LazyBase(object): self.var1 = 0 self.var2 = None def __init__(self, name): self.name = name def __str__(self): return self.name def __setattr__(self, attr, k): if attr == "var1": if self.var1 != k self.var1 = k if self.lazy != None self.lazy = None elif attr == "var2" : if self.var2 != k self.var2 = k if self.lazy != None self.lazy = None else: raise Exception("Attribute") class SQLLazy(LazyBase): def __init__(self, name, table="test_table"): self.table = table LazyBase.__init__(name) self.lazy = None def __getattr__(self, attr): if attr == "lazy": if self.lazy == None: # problem line I think print "Hit Database" # use sqlalchemy to get the variable # set self.lazy = lazy return self.lazy else: return self.lazy else: try: return Lazy.__getattr__(attr) except: raise Exception("Attribute Exception") def __setattr__(self, attr, k): if attr == "lazy" raise Exception("Cannot alter lazy") else: try: Lazy.__setattr__(attr, k) except: raise Exception("Attribute Exception") if __name__=='__main__': spam = SQLLazy("Test") spam.var2 = 10 print spam.lazy print spam.lazy spam.var2 = 5 print spam.lazy I m expecting the output: Hit Database Lazy Variable Lazy Variable Hit Database Lazy Variable However I just get recursion errors. Does anyone have any ideas how I can implement this sort of thing through my current method, or is there a better way to accomplish this. THanks Nathan -- http://mail.python.org/mailman/listinfo/python-list
Type error when using SQLAlchemy and Python
HI, I posted this to sqlalchemy but didnt get a response, so I was wondering if anyone on python-list could help me. I m currently trying to build an api for a database and I really like the way that Djangos manager ( Class_name.objects ) is set up. This seems very intuitive for me. After reading through the some of the django source and getting slightly confused I ve implemented a basic version for one of my tables. The problem is I get an exception, when I m not expecting one. registry.py --> contains table definitions etc. Manager.py from Registry import * class Manager(object): def __init__(self, model, table=None): self.model = model self.table = table def get(self, slice): pass def all(self): print "1" mapper(self.model, interpro_table) print "2" session = create_session() print "3" query = session.query(self.model) print "4" return query.select() Models.py from Manager import * class ModelBase(type): def __new__(cls, name, bases, dict): print cls setattr(cls, 'objects', Manager(cls)) return type.__new__(cls, name, bases, dict) class Model(object): __metaclass__=ModelBase class InterPro(Model): _tableName = interpro_table def __init__(self, interpro_ac): self.interpro_ac = interpro_ac def __str__(self): return "InterPro: %s" %(self.interpro_ac) def __repr__(self): return "InterPro: %s" %(self.interpro_ac) if __name__=='__main__': a = Manager(InterPro) print a print a.all() --> this prints out all of the objects in the database i = InterPro('IPR014697') print InterPro.objects print InterPro.objects.all() --> this fails and produces the exception. Traceback (most recent call last): File "Model.py ", line 28, in ? print InterPro.objects.all() File "/home/skeg/workspace/test/src/Manager.py", line 17, in all return query.select() File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py", line 247, in select return self.select_whereclause(whereclause=arg, **kwargs) File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py", line 252, in select_whereclause return self._select_statement(statement, params=params) File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py", line 378, in _select_statement return self.execute(statement, params=params, **kwargs) File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py", line 310, in execute return self.instances(result, **kwargs) File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py", line 329, in instances self.mapper._instance(context, row, result) File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/mapper.py", line 1213, in _instance instance = self._create_instance(context.session) File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/mapper.py", line 1234, in _create_instance obj = self.class_.__new__(self.class_) TypeError: __new__() takes exactly 4 arguments (1 given) Does anyone know what the problem is? Is it the way I ve programmed this using metaclasses or is it sqlalchemy and the way in instantiates objects or even both? Many Thanks in advance, -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Help!!!
Hi, you could try this: def parse(self, ifile): id="" seq="" for line in open(ifile, 'r'): if '>'==line[0]: if id!="" and len(seq)>0: yield id,seq seq = "" id=line[1:].strip("\n") elif id!="": for word in line.split(): seq += word if id!="" and len(seq)>0: yield id,seq for id, seq in parse("some.fa"): print "%s \n %s" %(id, seq) Its adapted from the fasta parser in PyGr. >From what I understand biopython isnt very active and I think theres a re-factor of it going on at the moment in the form of corebio. Hope this helps; Thanks Nathan -- http://mail.python.org/mailman/listinfo/python-list
Accessing variable from a function within a function
Hi, I m playing around with extended euclids algorithm from Knuth. I m trying to build a function with a function inside it. def exteuclid(m,n): a,a1,b,b1,c,d = 0,1,1,0,m,n def euclid(c,d): q = c /d r = c % d if r == 0: print a,b return d else: print a1,a,b1,b,c,d,q,r t = b1 b = t - q * b a = t - q * a c,d,a1,b1 = d,r,a,b return euclid(c,d) return euclid(c,d) Unfortunately this doesnt work since a,a1,b,b1 arent declared in the function. Is there a way to make these variables accessible to the euclid function. Or is there a better way to design this function? Many Thanks in advance, Nathan -- http://mail.python.org/mailman/listinfo/python-list
Programming Idiomatic Code
Hi, I m sorry but I m bored at work (and no ones looking so I can write some Python) and following a job advertisement post,I decided to write the code to do its for the one entitled Ninjas or something like that. I was wondering what could be done to my following code to make it more idiomatic...or whether it was idiomatic and to be honest what idiomatic really means. All comments greatly appreciated and welcomed. Thanks in advance Nathan import urllib2,sys from elementtree.ElementTree import parse base_url = "http://api.etsy.com/feeds/xml_user_details.php?id="; def read_id_file(filename): """ reads a file and generates a list of ids from it""" ids = [ ] try: id_file = open(filename, "r") for l in id_file: ids.append( l.strip("\n") ) id_file.close() except e: print e os._exit(99) return ids def generate_count(id_list): """ takes a list of ids and returns a dictionary of cities with associated counts""" city_count = {} for i in id_list: url = "%s%s" %(base_url,i) req = urllib2.Request(url) try: xml = parse(urllib2.urlopen(url)).getroot() city = xml.findtext('user/city') except e: print e.reason os._exit(99) try: city_count[city] += 1 except: city_count[city] = 1 return city_count if __name__=='__main__': if len(sys.argv) is 1: id_list = [ 42346, 77290, 729 ] else: try: id_list = read_id_file(sys.argv[1]) except e: print e for k, v in generate_count(id_list).items(): print "%s: %i" %(k, v) -- http://mail.python.org/mailman/listinfo/python-list
Re: Programming Idiomatic Code
HI, Thanks to everyone for your comments ( i never knew "with" existed, but to quote Borat "I like", unfortunately I cant apply for the job as i m in the UK and dont have the experience but hey 10 minutes of programming python beats 12 hours of programming in Clipper-derived unreadable drivel (you dont know how much I appreciate Python atm). I have one question though: Using a module global for this kind of data is usually a bad idea (except eventually for run-once throw-away scripts, and even then...) Why is this a bad idea? Thanks in advance Nathan PS I am very ashamed I wrote: if (len(sys.argv)) is 1 .. I would ask that this is never spoken of again, oh well off to work I go -- http://mail.python.org/mailman/listinfo/python-list
Re: Programming Idiomatic Code
> > Using a module global for this kind of data is usually a bad idea > > (except eventually for run-once throw-away scripts, and even then...) > > > > Why is this a bad idea? > > Don't you have any idea ? > -- Not really.problem with access, using unneeded memory... I grasping at straws here... -- http://mail.python.org/mailman/listinfo/python-list
Re: Programming Idiomatic Code
> "Clipper-derived unreadable drivel" > > I'm intrigued, what language are you working in? > > Clipper v5 was a pretty impressive development language for 1990 - with > code blocks, a flexible pre-processor, garbage collection, exception > handling, decent speed and an API to allow easy integration with > routines written in c. > > It doesn't have to be unreadable at all (unless it's written by someone > who thinks it is dBase). > Whilst I ve never programmed in standard clipper this language is an inhouse language (I would tell you the company but then they would know I wasnt working) developed from Clipper. It has no exception handling, no code blocks, the subroutines arent really subroutines and dont return anything they just write to a global variable which you access after the call. The speed is amazingly slow, it crawls. And dont even start me on GOTOsyou try to make your code easy to read and maintain but it just looks like you ve gone on a 2 day binge and been ill over your VDU. Perl IMO is a 1 day binge problem. They do use real clipper here and it sounds a lot better than this bstard son. C integration, oh to do some real programming would be amazing. I think its just a poor implementation of Clipper and it makes it appreciate the genius of python, the indentation, the ease of reading, ease of maintainingits so perfect. Now if only I could convince them to move to Pythondreams. Nathan -- http://mail.python.org/mailman/listinfo/python-list
Creating logged functions using decorators
Hi, I m thinking about writing some code which logs the input and output of a function/script and stores it in a database using sqlalchemy (although I havent started on this yet). I want to do this via a decorator ( I think this is the best way ). def log(fn): def newfn(*args): print datetime.date.today() print __file__ print fn.__name__ print args return fn(*args) return newfn @log def doAnalysis(a, b): print a, b return a + b doAnalysis(3, 7) I can access the arguments passed to the "logged" function, but is there anyway I can capture the output of the function i.e. 10. Many Thanks in advance, Nathan -- http://mail.python.org/mailman/listinfo/python-list
Converting between objects
Hi, I have being thinking about this and was wondering with built in types you can do things like float(1) or str(200) is there way I can define conversion functions like this: say i have a class A and a class B bobj = B() aobj = a(bobj) in a neater way than just defining a set of methods def a(object_to_convert) # if object_to_convert of type.. # do some stuff return A() def b(object_to_convert) # if object_to_convert of type.. # do some stuff return B() Cause this seems a little verbose and not very OO. Please correct me if I m wrong or offer me hints as to a better way to do it ? Many Thanks nathan -- http://mail.python.org/mailman/listinfo/python-list
Restricting the alphabet of a string
Hi, I ve being thinking about playing around with bit strings but use in some encoding problems I m considering and was trying to decide how to implement a bit string class. Is there a library out there for doing basic things with bit strings already is my first question? I know that I can extend string to bit string, but is there anyway I can force the alphabet to be restricted to 1's and 0's (or even 1, 0 and -1, as an extension to from trinary strings). class Binary_String(String): pass Many Thanks Nathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Restricting the alphabet of a string
Thanks, I might just move my trinary string (if I get that far) to be encoded as 0, 1, 2, thanks for your help. On 30 Apr 2007 11:14:10 -0700, John Machin <[EMAIL PROTECTED]> wrote: > On Apr 30, 9:53 pm, "Nathan Harmston" <[EMAIL PROTECTED]> > wrote: > > Hi, > > > > I ve being thinking about playing around with bit strings but use in > > some encoding problems I m considering and was trying to decide how to > > implement a bit string class. Is there a library out there for doing > > basic things with bit strings already is my first question? > > > > I know that I can extend string to bit string, but is there anyway I > > can force the alphabet to be restricted to 1's and 0's (or even 1, 0 > > and -1, as an extension to from trinary strings). > > > > class Binary_String(String): > > pass > > > > See if you can pick which line below is impractically different to the > others: > > binary: 0, 1 > "trinary": -1, 0, 1 > octal: 0, 1, 2, 3, 4, 5, 6, 7 > decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 > > HTH, > John > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Using the CSV module
Hi, I ve been playing with the CSV module for parsing a few files. A row in a file looks like this: some_id\t|\tsome_data\t|t\some_more_data\t|\tlast_data\t\n so the lineterminator is \t\n and the delimiter is \t|\t, however when I subclass Dialect and try to set delimiter is "\t|\t" it says delimiter can only be a character. I know its an easy fix to just do .strip("\t") on the output I get, but I was wondering a) if theres a better way of doing this when the file is actually being parsed by the csv module b) Why are delimiters only allowed to be one character in length. Many Thanks in advance Nathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Using the CSV module
I ve just finished writing one, I wanted to stay with the batteries included approach as much as possible though. Is there anyway I can request a change to the csv module? Thanks Nathan On 09/05/07, Stefan Sonnenberg-Carstens <[EMAIL PROTECTED]> wrote: > Most of the time I found the CSV module not as useful as it might be - > due to the restrictions you describe. > > Why not write a simple parser class ? > > On Mi, 9.05.2007, 10:40, Nathan Harmston wrote: > > Hi, > > > > I ve been playing with the CSV module for parsing a few files. A row > > in a file looks like this: > > > > some_id\t|\tsome_data\t|t\some_more_data\t|\tlast_data\t\n > > > > so the lineterminator is \t\n and the delimiter is \t|\t, however when > > I subclass Dialect and try to set delimiter is "\t|\t" it says > > delimiter can only be a character. > > > > I know its an easy fix to just do .strip("\t") on the output I get, > > but I was wondering > > a) if theres a better way of doing this when the file is actually > > being parsed by the csv module > > b) Why are delimiters only allowed to be one character in length. > > > > Many Thanks in advance > > Nathan > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > > > > -- http://mail.python.org/mailman/listinfo/python-list
Re: alternative to eclipse [ python ide AND cvs ]
I have had very few problems with eclipse on ubuntu with pydev installed. Is it still running under the gnu jvm, not the sun one? It was crashing on me until I changed them around, detials about changing it around on ubuntu anyway http://ubuntuguide.org/wiki/Ubuntu_Edgy#How_to_install_Java_Integrated_Development_Environment_.28Eclipse.29 Hope this helps, Nathan -- http://mail.python.org/mailman/listinfo/python-list
Complexity of methods etc
Hi, I was wondering if anyone knew of any resources, where I might be able to find information about the complexity of certain python functions or little tips on how to reduce complexity. I mean like the "".join(), kind of thing? I want to see if there are any improvements I can add to my coding in order to reduce time/space usage/ Many Thanks in advance Nathan -- http://mail.python.org/mailman/listinfo/python-list
Problem using Optional pyparsing
Hi, I know this isnt the pyparsing list, but it doesnt seem like there is one. I m trying to use pyparsing to parse a file however I cant get the Optional keyword to work. My file generally looks like this: ALIGNMENT 1020 YS2-10a02.q1k chr09 1295 42141045 142297 C1254 95.06 1295 reject_bad_break 0 or this: ALIGNMENT 36YS2-10a08.q1k chrm 208 165 10745 10788 C 44 95.45 593 reject_low 10,14 and my grammar work well for these lines, however somethings the row looks like: ALIGNMENT 53YS2-10b03.p1k chr12 180 125 1067465 1067520 C 56 98.21 532|5,2 reject_low 25 So I try to parse the 532 using from pyparsing import * integer = Word( nums ) float = Word( nums+".") identifier = Word( alphanums+"-_." ) alignment = Literal("ALIGNMENT ").suppress() row_1 = integer.setResultsName("row_1")#.setParseAction(make_int) src_id = identifier.setResultsName("src_id") dest_id = identifier.setResultsName("dest_id") src_start = integer.setResultsName("src_start")#.setParseAction(make_int) src_stop = integer.setResultsName("src_stop")#.setParseAction(make_int) dest_start = integer.setResultsName("dest_start")#.setParseAction(make_int) dest_stop = integer.setResultsName("dest_stop")#.setParseAction(make_int) row_8 = oneOf("F C").setResultsName("row_8") length = integer.setResultsName("length")#.setParseAction(make_int) percent_id = float.setResultsName("percent_id")#.setParseAction(make_float) row_11 = integer + Optional(Literal("|") + commaSeparatedList ) )#.setResultsName("row_11")#.setParseAction(make_int) result = Word(alphas+"_").setResultsName("result") row_13 = commaSeparatedList.setResultsName("row_13") def make_alilines_status_parser(): return alignment + row_1 + src_id + dest_id + src_start + src_stop + dest_start + dest_stop + row_8 + length + percent_id + row_11 + result + row_13 def parse_alilines_status(ifile): alilines = make_alilines_status_parser() for l in ifile: yield alilines.parseString( l ) However my parser always fails on lines of type 3. Does anyone know why the Optional part is not working. Many Thanks in advance Nathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Tokenizer for python?
Hi, >From your description I think something like PyParsing might be useful to you. http://pyparsing.wikispaces.com/ If not then I guess I m not understanding your question right. Hope this helps Nathan On 21/08/07, Robert Dailey <[EMAIL PROTECTED]> wrote: > Hi, > > I am looking for a sort of "tokenizer" for python. I've taken a look at the > tokenize module, but that seems to parse python code from what I read. I > want a tokenizer that works a little like boost::tokenizer, however for > python. Basically I want to be able to pass in an arbitrary string (or line > from readline()) and specify tokens that cause the string to be separated > into parts, much like the regular expression split() method (I think that's > the name of it). Is there anything that already exists that does this, or do > I need to implement it myself with regular expressions? > > Thanks. > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Adding attributes stored in a list to a class dynamically.
Hi, Sorry if the subject line of post is wrong, but I think that is what this is called. I want to create objects with class Coconuts(object): def __init__(self, a, b, *args, **kwargs): self.a = a self.b = b def spam( l ) return Coconuts( l.a, l.b, l.attributes ) l in a parse line of a file which is a tuple wrapped with attrcol..with attributes a, b and attributes (which is a list of strings in the format key=value ie... [ "id=bar", "test=1234", "doh=qwerty" ] ). I want to add attributes to Coconuts so that I can do print c.id, c.test, c.doh HOwever I m not sure how to do this: how can i assign args, kwargs within the constructor of coconuts and how can I deconstruct the list to form the correct syntax to be able to be used for args, kwargs. HOpe this makes sense, Thanks in advance, Nathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Requirements For A Visualization Software System For 2010
You could look at sage.its all python! http://sage.scipy.org/sage/ I wish Xah Lee would stop trolling.oh well Cheers Nathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Free Python Ebook
Theres a few python ebooks listed on the wiki http://wiki.python.org/moin/PythonBooks Hope this helps, Nathan On 16/09/2007, Marco <[EMAIL PROTECTED]> wrote: > Hi George, > > > Please tell me from which website I will get the free Python Ebook. > which one do you mean? > > I only know this one: > http://diveintopython.org > > Bye, > Marco > > PS: Sorry, hit the wrong button the first time ;-) > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
adding a static class to another class
HI, I m trying to start an api in a similar way to the djangic way of Class.objects.all(). Ie objects is a "Manager" class. So: class Foo(object): def __init__(self): self.test = "NEE" class Manager(object): def __init__(self): pass def all(self): return "COCONUTS" Because of how some of the code is set up I cant use metaclassesso I try to use a decorator: def addto(instance): def decorator(f): import new f = new.instancemethod(f, instance, instance.__class__) setattr(instance, "objects", f) return f return decorator class Manager(object): @addto(Foo) def __init__(self): . however this only binds the init method to the Foo.objects, so not what I want. If I try using classmethod...then it just says the Foo.objects doesnt exist. Does anyone have any ideas how I can accomplish this using decorators? And also preventing more than one Manager instance instantiated at one time. Many Thanks in advance, Nathan -- http://mail.python.org/mailman/listinfo/python-list
Re: adding a static class to another class
Hi, I guess my description was a bit rubbish in retrospec, I dont even think the title of my email made senseit doesnt to me now: class Manager(object): def __init__(self): pass def dosomething(self): return "RESULTS" class Foo(object): def __init__(self): self.a = "NEE" What I m trying to do is end up with the following syntax: f = Foo() # instantiates a Foo object g= Foo.objects.dosomething() # returns "RESULTS" The best way I ve found of doing this is overriding new class Foo(object): def __new__(cls, *args, **kw): cls.objects = Manager() > If I do it like this, I get exactly what I want - especially if I change the new method in Manager to one supplied by Bruno. However, I m playing around with SQLAlchemy (which is going to be used by some of the Managers I create, others will use different things) which means that overriding new is not allowed, I cant assign a mapper to Foo. So I was trying to think of another way of doing cls.objects = Manager()and the only solution I could think of was to use a decorator and try to do it somehow... >From Brunos answer I guess a decorator is not a good way of accomplishing this, so if there anyway of doing this without using a metaclass..or by using a decorator or notany help would be greatly appreciated. Many Thanks in advance Nathan On 17/09/2007, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > Nathan Harmston a écrit : > > HI, > > > > I m trying to start an api in a similar way to the djangic way of > > Class.objects.all(). Ie objects is a "Manager" class. > > > > So: > > > > class Foo(object): > >def __init__(self): > > self.test = "NEE" > > > > class Manager(object): > > def __init__(self): > > pass > >def all(self): > >return "COCONUTS" > > > > Because of how some of the code is set up I cant use > > metaclassesso I try to use a decorator: > > > > def addto(instance): > > def decorator(f): > > import new > > f = new.instancemethod(f, instance, instance.__class__) > > setattr(instance, "objects", f) > > return f > > return decorator > > > > class Manager(object): > > @addto(Foo) > > def __init__(self): > >. > > > > however this only binds the init method to the Foo.objects, so not > > what I want. > > Indeed. > > > If I try using classmethod...then it just says the > > Foo.objects doesnt exist. > > You mean decorating Manager.__init__ with classmethod ??? > > I may be wrong, but I suspect you don't have a clear idea of what you're > doing here. > > > Does anyone have any ideas how I can accomplish this using decorators? > > Yes : don't use a decorator !-) > > Instead of asking for how to implement what you think is the solution, > you might be better explaining the problem you're trying to solve. > > > And also preventing more than one Manager instance instantiated at one > > time. > > Q&D: > > class Singleton(object): > def __new__(cls): > if not hasattr(cls, '_inst'): > cls._inst = object.__new__(cls) > return cls._inst > > > Same remark as above... > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Pb with an AttributeError
Hi, I m not sure what your trying to do, but this is where your problem is: string1 is not a string it is actually a dict because of your eval. When you call string1["param"] inside the string.split() it is returning a dictionary and not a string 'param': {'key': 'SP<136>=', 'value': ['SD:<0> ']} since {'key': 'SP<136>=', 'value': ['SD:<0> ']} is inside curly brackets it is a dictionary. I still dont get what your trying to have as your final result, but hope this helps. Thanks Nathan On 27/09/2007, Eric BOUVIER <[EMAIL PROTECTED]> wrote: > > > > Hello, > > First, sorry for my english. > > I've just been assigned a project written in Python which contains bug(s). > I've never programmed in Python, but I've read the code > and understood basically what the different source codes do. > > I've traced the code and found where the problem is but I don't > know how to solve the problem. > > Here it is : > > I've got a string issued from a CSV file : >string1 = eval(line[4]) >print " * string1 = (", string1 ,") \n" > => * string1 = ( {'header': 'X-Score', 'param': {'key': 'SP<136>=', > 'value': ['SD:<0> ']} ) > > Then, there is a split with the equal : >TheParts = string.split(string1["param"], "=") >print " * TheParts = (", TheParts ,") \n" > > But, running the program returns : > > Traceback (most recent call last): > File "C:\test\helper.py", line 136, in Action_handler > Score_eval(line, OutputFile_SP, This_Host) > File "C:\test\helper.py", line 66, in Score_eval > TheParts = string.split(string1["param"], "=") > File "C:\ActiveState\Python25\lib\string.py", line 290, > in split > return s.split(sep, maxsplit) > AttributeError: 'dict' object has no attribute 'split' !?! > > I've found that it works when I put directly the string in the split > expression : > >TheParts = string.split(" {'key': 'ESP<136>=', 'value': ['SHA:<0> ']} ", > "=") > => * TheParts = ["{'key': 'ESP<136>", "', 'value': ['SHA:<0> ']}"] > > But the code isn't dynamic anymore ! > I think it's a problem with the type, but the different syntax I've tried > didn't work. > Is somebody could help me ? > > Thank you > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Pb with an AttributeError
Did I just help someone with their homework? Hope not :S On 27/09/2007, Nathan Harmston <[EMAIL PROTECTED]> wrote: > Hi, > > I m not sure what your trying to do, but this is where your problem is: > > string1 is not a string it is actually a dict because of your eval. > When you call > string1["param"] inside the string.split() > > it is returning a dictionary and not a string > 'param': {'key': 'SP<136>=', 'value': ['SD:<0> ']} > > since {'key': 'SP<136>=', 'value': ['SD:<0> ']} is inside curly > brackets it is a dictionary. > > I still dont get what your trying to have as your final result, but > hope this helps. > > Thanks > > Nathan > > On 27/09/2007, Eric BOUVIER <[EMAIL PROTECTED]> wrote: > > > > > > > > Hello, > > > > First, sorry for my english. > > > > I've just been assigned a project written in Python which contains bug(s). > > I've never programmed in Python, but I've read the code > > and understood basically what the different source codes do. > > > > I've traced the code and found where the problem is but I don't > > know how to solve the problem. > > > > Here it is : > > > > I've got a string issued from a CSV file : > >string1 = eval(line[4]) > >print " * string1 = (", string1 ,") \n" > > => * string1 = ( {'header': 'X-Score', 'param': {'key': 'SP<136>=', > > 'value': ['SD:<0> ']} ) > > > > Then, there is a split with the equal : > >TheParts = string.split(string1["param"], "=") > >print " * TheParts = (", TheParts ,") \n" > > > > But, running the program returns : > > > > Traceback (most recent call last): > > File "C:\test\helper.py", line 136, in Action_handler > > Score_eval(line, OutputFile_SP, This_Host) > > File "C:\test\helper.py", line 66, in Score_eval > > TheParts = string.split(string1["param"], "=") > > File "C:\ActiveState\Python25\lib\string.py", line 290, > > in split > > return s.split(sep, maxsplit) > > AttributeError: 'dict' object has no attribute 'split' !?! > > > > I've found that it works when I put directly the string in the split > > expression : > > > >TheParts = string.split(" {'key': 'ESP<136>=', 'value': ['SHA:<0> ']} ", > > "=") > > => * TheParts = ["{'key': 'ESP<136>", "', 'value': ['SHA:<0> ']}"] > > > > But the code isn't dynamic anymore ! > > I think it's a problem with the type, but the different syntax I've tried > > didn't work. > > Is somebody could help me ? > > > > Thank you > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling a shared library using C types
Hi, I know this is a pretty simple question but I've spent a while on this and can't see whats wrong. I'm trying to access a shared library which I've created called Simulation.so, its created as such (snip from makefile): all: simulation simulation: Simulation.so Simulation.so: Simulation.o Statistics.o gcc -shared Simulation.o Statistics.o -L/usr/local/lib -lsbml -lstdc++ -lm -o Simulation.so Statistics.o: Statistics.c Statistics.h gcc -fpic -g -O2 -I/usr/include -c Statistics.c Simulation.o: Simulation.c gcc -fpic -g -O2 -I/usr/include -c Simulation.c and I can load it properly in python import ctypes t = ctypes.CDLL('./Simulation.so') this works fine, I have a simple function I ve put in for testing which just returns the integer 4. However when I try to access this function it doesnt work t.test() File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ctypes/__init__.py", line 325, in __getattr__ func = self.__getitem__(name) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ctypes/__init__.py", line 330, in __getitem__ func = self._FuncPtr((name_or_ordinal, self)) AttributeError: dlsym(0x81e6b0, test): symbol not found I've tried using t.__getattr__("test") but still have the same exception, I've tried loading the library with mode = RTLD_GLOBAL aswell and still have no luck. As far I as I can see this should work? But as I am just starting with ctypes I am sure I doing something sorry very stupid. Any pointers would be greatly appreciated, Many thanks in advance, Nathan Im hoping python-list is ok for questions regarding ctypes :S -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling a shared library using C types
On 25/03/2008, Gabriel Genellina <[EMAIL PROTECTED]> wrote: > > En Mon, 24 Mar 2008 19:56:08 -0300, Nathan Harmston > <[EMAIL PROTECTED]> escribió: > > > > import ctypes > > t = ctypes.CDLL('./Simulation.so') > > this works fine, I have a simple function I ve put in for testing which > > just > > returns the integer 4. However when I try to access this function it > > doesnt > > work > > t.test() > > File "", line 1, in > > File > > > "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ctypes/__init__.py", > > line 325, in __getattr__ > > func = self.__getitem__(name) > > File > > > "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ctypes/__init__.py", > > line 330, in __getitem__ > > func = self._FuncPtr((name_or_ordinal, self)) > > AttributeError: dlsym(0x81e6b0, test): symbol not found > > > Looks like the symbol isn't public - probably if you try loading the > library with a C program it won't find it either. How is the function > declared in the source? > Try listing all public symbols with: nm -D Simulation.so Thanks for the quick reply: Running nm lists test as 1760 T _test in the source its declared as: int test(){ return 4; } Sorry, this is the first time I'm making my own shared library and using ctypes, so being a little slow. Thanks again Nathan > Im hoping python-list is ok for questions regarding ctypes :S > > > It's not off topic, although there is a specific list for ctypes-related > questions. But hijacking a thread to post a completely different question > is not good netiquette. > > -- > Gabriel Genellina > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling a shared library using C types
Hi, Just as a follow up to this...I ve discovered that its an issue with building shared libraries on mac os and it works fine on a Linux box :S. Thanks Nathan On 25/03/2008, Nathan Harmston <[EMAIL PROTECTED]> wrote: > > > > On 25/03/2008, Gabriel Genellina <[EMAIL PROTECTED]> wrote: > > > > En Mon, 24 Mar 2008 19:56:08 -0300, Nathan Harmston > > <[EMAIL PROTECTED]> escribió: > > > > > > > import ctypes > > > t = ctypes.CDLL('./Simulation.so') > > > this works fine, I have a simple function I ve put in for testing > > which > > > just > > > returns the integer 4. However when I try to access this function it > > > doesnt > > > work > > > t.test() > > > File "", line 1, in > > > File > > > > > "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ctypes/__init__.py", > > > line 325, in __getattr__ > > > func = self.__getitem__(name) > > > File > > > > > "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ctypes/__init__.py", > > > line 330, in __getitem__ > > > func = self._FuncPtr((name_or_ordinal, self)) > > > AttributeError: dlsym(0x81e6b0, test): symbol not found > > > > > > Looks like the symbol isn't public - probably if you try loading the > > library with a C program it won't find it either. How is the function > > declared in the source? > > Try listing all public symbols with: nm -D Simulation.so > > Thanks for the quick reply: > Running nm lists test as > 1760 T _test > > in the source its declared as: > > int test(){ > return 4; > } > > Sorry, this is the first time I'm making my own shared library and using > ctypes, so being a little slow. > > Thanks again > > Nathan > > > > > > > Im hoping python-list is ok for questions regarding ctypes :S > > > > > > It's not off topic, although there is a specific list for ctypes-related > > questions. But hijacking a thread to post a completely different > > question > > is not good netiquette. > > > > -- > > Gabriel Genellina > > > > > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > > -- http://mail.python.org/mailman/listinfo/python-list
Large regular expressions
Hi, So I m trying to use a very large regular expression, basically I have a list of items I want to find in text, its kind of a conjunction of two regular expressions and a big list..not pretty. However everytime I try to run my code I get this exception: OverflowError: regular expression code size limit exceeded I understand that there is a Python imposed limit on the size of the regular expression. And although its not nice I have a machine with 12Gb of RAM just waiting to be used, is there anyway I can alter Python to allow big regular expressions? Could anyone suggest other methods of these kind of string matching in Python? I m trying to see if my swigged alphabet trie is faster than whats possible in Python! Many thanks, Nathan -- http://mail.python.org/mailman/listinfo/python-list
Generating text from a regular expression
Hi everyone, I have a slightly complicated/medium sized regular expression and I want to generate all possible words that it can match (to compare performance of regex against an acora based matcher). Using the regular expression as a grammar to generate all words in its language. I was wondering if this possible in Python or possible using anything. Google doesnt seem to give any obvious answers. Many thanks in advance, Nathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Generating text from a regular expression
Thanks everyone, the invRegexInf is perfect. Thanks again, Nathan On 1 April 2010 10:17, Gabriel Genellina wrote: > En Wed, 31 Mar 2010 12:23:48 -0300, Paul McGuire > escribió: >> >> On Mar 31, 5:49 am, Nathan Harmston >> wrote: >>> >>> I have a slightly complicated/medium sized regular expression and I >>> want to generate all possible words that it can match (to compare >>> performance of regex against an acora based matcher). >> >> The pyparsing wiki Examples page includes this regex inverter: >> http://pyparsing.wikispaces.com/file/view/invRegex.py >> >>> From the module header: >> >> # Supports: >> # - {n} and {m,n} repetition, but not unbounded + or * repetition >> # - ? optional elements >> # - [] character ranges >> # - () grouping >> # - | alternation > > I took the liberty of modifying your invRegex.py example, adding support > for infinite repeaters. It depends on two other modules: > > mergeinf.py (from http://code.activestate.com/recipes/577041) provides the > infinite merge operation. > > enumre.py provides the basic functions (merge, prod, repeat, closure) > necessary to enumerate the language generated by a given regular > expression, even if it contains unbounded repeaters like *,+. The key is > to generate shorter strings before longer ones, so in 'a*|b*' it doesn't > get stuck generating infinite a's before any b. > > By example, "(a|bc)*d" corresponds to this code: > > prod( > closure( > merge( > 'a', > prod('b','c'))), > 'd') > > which returns an infinite generator starting with: > > d > ad > aad > bcd > aaad > abcd > bcad > d > aabcd > abcad > bcaad > bcbcd > ad > aaabcd > aabcad > ... > > > I got the idea from > http://userweb.cs.utexas.edu/users/misra/Notes.dir/RegExp.pdf > > Finally, invRegexInf.py is based on your original regex parser. I only > modified the generation part, taking advantage of the above > infrastructure; the parser itself remains almost the same. It essentially > saves oneself the very tedious work of converting a regular expression > into the equivalent sequence of function calls as shown above. (I hope I > got it right: I like pyparsing a lot and use it whenever I feel it's > appropriate, but not as often as to remember the details...) > > -- > Gabriel Genellina > -- > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list