Re: Is their an expression to create a class?
Chris Rebert wrote: On Tue, Mar 17, 2009 at 2:24 PM, Robert Kern wrote: On 2009-03-17 16:13, Paddy wrote: We the def statement and the lambda expression. We have the class statement, but is their an expression to create a class? Or: def F(): pass type(F) # Is to: F2 = lambda : none type(F2) # As class O(object): pass type(O) # is to: # type('O', (object,), {}) Further detail from the docs (http://docs.python.org/library/functions.html): type(name, bases, dict) Return a new type object. This is essentially a dynamic form of the class statement. The name string is the class name and becomes the __name__ attribute; the bases tuple itemizes the base classes and becomes the __bases__ attribute; and the dict dictionary is the namespace containing definitions for class body and becomes the __dict__ attribute. For example, the following two statements create identical type objects: >>> class X(object): ... a = 1 ... >>> X = type('X', (object,), dict(a=1)) New in version 2.2. Cheers, Chris Thanks guys. Youve put my mind at rest! - Paddy. -- http://mail.python.org/mailman/listinfo/python-list
Re: Question: How do I format printing in python
Lie wrote: On Jun 24, 12:12 am, [EMAIL PROTECTED] wrote: Hi All, How do I format printed data in python? I could not find this in the Python Reference Manual:http://docs.python.org/ref/print.html Nor could I find it in Matloff's great tutorial:http://heather.cs.ucdavis.edu/~matloff/Python/PythonIntro.pdf For example, how do I turn this: 512 Jun 5 2004 X11r6 22 Jan 17 2005 a2p 22 Jan 17 2005 acctcom 5374 Sep 15 2002 acledit 5664 May 13 2004 aclget 12020 May 13 2004 aclput 115734 Jun 2 2004 adb 46518 Jun 4 2004 admin 66750 Sep 16 2002 ali 1453 Sep 15 2002 alias 28150 Jun 4 2004 alog 15 May 12 2005 alstat into this: 512Jun 5 2004X11r6 22 Jan 17 2005a2p 22 Jan 17 2005acctcom 5374 Sep 15 2002acledit 5664 May 13 2004aclget 12020 May 13 2004aclput 115734 Jun 2 2004adb 46518 Jun 4 2004admin 66750 Sep 16 2002ali 1453 Sep 15 2002alias 28150 Jun 4 2004alog 15 May 12 2005alstat Thank you There is string formatting print formatspecifier_string % data_sequence The format specifier is similar to the one used in C's printf, and data sequence may be tuple or list. Dictionary may also be used for data, but it has its own way to specify string formatting since dictionary is unordered but "indexed" by the dict key. I have attached a prog I wrote to answer someones elses similar problem. - Paddy. from StringIO import StringIO from pprint import pprint as pp left_justified = False debug = False textinfile = '''I$created$a$solution$for$a$program$I$am$writing$that makes$columns$line$up$when$outputted$to$the$command line.$I$am$new$to$Python,$and$am$hoping$I$might$get some$input$on$this$topic.$In$the$text$file$that$the program$reads,$the$fields$(columns)$are$delimited$by 'dollar'$and$the$records$(lines)$are$delimited$by newlines$'\\n'.$So$one$line$looks$like:$''' """ Solution to problem posed at: http://www.kbrandt.com/2008/06/getting-command-line-output-columns-to.html Output is the following if left-justified: # Column-aligned output: Icreated asolution for a program Iam writing that makescolumns line up when outputted tothe command line.I am new toPython, and am hoping I might get some input on this topic.Inthe text filethat the program reads, the fields (columns) are delimited by 'dollar' and the records (lines) are delimited by newlines '\n'. So one line looks like: And like this if not left-justified: # Column-aligned output: I createda solution for a programI am writing that makes columns line up when outputtedto the command line. I am newto Python, and am hoping I might get some input on thistopic.In the textfile that the program reads, the fields (columns) are delimited by 'dollar' and the records (lines) are delimited by newlines '\n'. So one line looks like: """ infile = StringIO(textinfile) fieldsbyrecord= [line.strip().split('$') for line in infile] if debug: print "fieldsbyrecord:"; print (fieldsbyrecord) # pad to same number of fields per record maxfields = max(len(record) for record in fieldsbyrecord) fieldsbyrecord = [fields + ['']*(maxfields - len(fields)) for fields in fieldsbyrecord] if debug: print "padded fieldsbyrecord:"; print (fieldsbyrecord) # rotate fieldsbycolumn = zip(*fieldsbyrecord) if debug: print "fieldsbycolumn:"; print (fieldsbycolumn) # calculate max fieldwidth per column colwidths = [max(len(field) for field in column) for column in fieldsbycolumn] if debug: print "colwidths:"; print (colwidths) # pad fields in columns to colwidth with spaces # (Use -width for left justification,) fieldsbycolumn = [ ["%*s" % (-width if left_justified else width, field) for field in column] for width, column in zip(colwidths, fieldsbycolumn) ] if debug: print "padded fieldsbycolumn:"; print (fieldsbycolumn) # rotate again fieldsbyrecord = zip(*fieldsbycolumn) if debug: print "padded rows and fields, fieldsbyrecord:"; print (fieldsbyrecord) # printit print "\n# Column-aligned output:" for record in fieldsbyrecord: print " ".join(record) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.5 adoption
Joseph Turian wrote: > Basically, we're planning on releasing it as open-source, and don't > want to alienate a large percentage of potential users. Then develop for 2.5 with an eye on what is to come this year in 2.6 with regard to already planned deprecations. - Paddy. -- http://mail.python.org/mailman/listinfo/python-list
pprint module and newer standard types
Hi, When I try and use pprint on standard types I get varying 'quality of output'. Lists will wrap nicely to multiple lines as will dicts, but sets and defaultdicts give one long unreadable line. Is their a chance to get this changed so that more built-in types look pretty when printed with pprint? I just did a small trial on an early version of Python 3 and sets don't seem to obey pprint.pprints width argument, the same way that lists do: Python 3.0a1 (py3k:57844, Aug 31 2007, 16:54:27) [MSC v.1310 32 bit (Intel)] on win32 >>> from pprint import pprint as pp >>> pp(list(range(3)), width=4) [0, 1, 2] >>> pp(set(range(3)), width=4) {0, 1, 2} - Paddy. -- http://mail.python.org/mailman/listinfo/python-list
Re: alternating string replace: Extended input (Long).
cesco wrote: I created some more test strings and ran posters solutions against them. results attached. - Paddy. # alternating_replacements.py tests = " 1 2_ 3_4 5_6_ 7_8_9 10_11_12_ 13_14_15_16 17_18_19_20_" \ " _ _21 _22_ _23_24 _25_26_ _27_28_29 _30_31_32_ _33_34_35_36" \ " __ ___ _".split(" ") def altrep0(s): s1 = s.split("_") s1 = [s1[i]+":"+s1[i+1] for i in range(0,len(s1),2)] s1 = ",".join(s1) return s1 altrep0.author="Frederik Lundh" def altrep1(s): from re import sub def repl(o): repl.n = not repl.n return ":" if repl.n else "," repl.n = False return sub("_", repl, s) altrep1.author="bearophile" def altrep2(s): evenOrOdd = True s2 = "" for i in s: if i == '_': if evenOrOdd: s2 += ':' evenOrOdd = not evenOrOdd else: s2 += ',' evenOrOdd = not evenOrOdd else: s2 += i return s2 altrep2.author="cokofree" def altrep3(s): import re from itertools import cycle return re.sub("_", lambda m, c=cycle(":,").next: c(), s) altrep3.author="Peter Otten" def altrep4(s): from itertools import islice, cycle def interleave(*iterators): iterators = [ iter(i) for i in iterators ] while 1: for i in iterators: yield i.next() def punctuate(s): parts = s.split('_') punctuation = islice(cycle(':,'), len(parts)-1) return ''.join(interleave(parts, punctuation)) return punctuate(s) altrep4.author="Duncan Booth" def altrep5(s): import re return re.sub(r'([^_]+)_([^_]+)_?', r'\1:\2;', s) altrep5.author="Pablo Ziliani" progs = [ altrep0, altrep1, altrep2, altrep3, altrep4, altrep5] def testall(): ''' >>> testall() ## Program by: Frederik Lundh '' RETURNS '' '1' RETURNS '' '2_' RETURNS '2:' '3_4' RETURNS '3:4' '5_6_' RETURNS '' '7_8_9' RETURNS '' '10_11_12_' RETURNS '10:11,12:' '13_14_15_16' RETURNS '13:14,15:16' '17_18_19_20_' RETURNS '' '_' RETURNS ':' '_21' RETURNS ':21' '_22_' RETURNS '' '_23_24' RETURNS '' '_25_26_' RETURNS ':25,26:' '_27_28_29' RETURNS ':27,28:29' '_30_31_32_' RETURNS '' '_33_34_35_36' RETURNS '' '__' RETURNS '' '___' RETURNS ':,:' '' RETURNS '' '_' RETURNS ':,:,:' ## Program by: bearophile '' RETURNS '' '1' RETURNS '1' '2_' RETURNS '2:' '3_4' RETURNS '3:4' '5_6_' RETURNS '5:6,' '7_8_9' RETURNS '7:8,9' '10_11_12_' RETURNS '10:11,12:' '13_14_15_16' RETURNS '13:14,15:16' '17_18_19_20_' RETURNS '17:18,19:20,' '_' RETURNS ':' '_21' RETURNS ':21' '_22_' RETURNS ':22,' '_23_24' RETURNS ':23,24' '_25_26_' RETURNS ':25,26:' '_27_28_29' RETURNS ':27,28:29' '_30_31_32_' RETURNS ':30,31:32,' '_33_34_35_36' RETURNS ':33,34:35,36' '__' RETURNS ':,' '___' RETURNS ':,:' '' RETURNS ':,:,' '_' RETURNS ':,:,:' ## Program by: cokofree '' RETURNS '' '1' RETURNS '1' '2_' RETURNS '2:' '3_4' RETURNS '3:4' '5_6_' RETURNS '5:6,' '7_8_9' RETURNS '7:8,9' '10_11_12_' RETURNS '10:11,12:' '13_14_15_16' RETURNS '13:14,15:16' '17_18_19_20_' RETURNS '17:18,19:20,' '_' RETURNS ':' '_21' RETURNS ':21' '_22_' RETURNS ':22,' '_23_24' RETURNS ':23,24' '_25_26_' RETURNS ':25,26:' '_27_28_29' RETURNS ':27,28:29' '_30_31_32_' RETURNS ':30,31:32,' '_33_34_35_36' RETURNS ':33,34:35,36' '__' RETURNS ':,' '___' RETURNS ':,:' '' RETURNS ':,:,' '_' RETURNS ':,:,:' ## Program by: Peter Otten '' RETURNS '' '1' RETURNS '1' '2_' RETURNS '2:' '3_4' RETURNS '3:4' '5_6_' RETURNS '5:6,' '7_8_9' RETURNS '7:8,9' '10_11_12_' RETURNS '10:11,12:' '13_14_15_16' RETURNS '13:14,15:16' '17_18_19_20_' RETURNS '17:18,19:20,' '_' RETURNS ':' '_21' RETURNS ':21' '_22_' RETURNS ':22,' '_23_24' RETURNS ':23,24' '_25_26_' RETURNS ':25,26:' '_27_28_29' RETURNS ':27,28:29' '_30_31_32_' RETURNS ':30,31:32,' '_33_34_35_36' RETURNS ':33,34:35,36' '__' RETURNS ':,' '___' RETURNS ':,:' '' RETURNS ':,:,' '_' RETURNS ':,:,:' ## Program by: Duncan Booth '' RETURNS '' '1' RETURNS '1' '2_' RETURNS '2:' '3_4' RETURNS '3:4' '5_6_' RETURNS '5:6,' '7_8_9' RETURNS '7:8,9' '10_11_12_' RETURNS '10:11,12:' '13_14_15_16' RETURNS '13:14,15:16' '17_18_19_20_' RETURNS '17:18,19:20,' '_' RETURNS ':' '_
Re: Is Python really a scripting language?
Doug Morse wrote: > although perhaps not a part of the definition of scripting languages per se, > one aspect of them is that they are often used to "glue" a wide variety of > other components together. perl's initial and continued success is in no > small part to all the wrappers and interfaces it has to all sorts of other > software components and applications. part of python's utility, IMHO, is the > ease with which it can be used, like perl, to glue together a lot of disparate > parts. > But here's my problem, most of my coworkers, when they see my apps and learn that they are written in Python ask questions like, "Why would you write that in a scripting language?" Whenever I hear a comment like that I can feel myself boiling inside. I'm with Doug on this. Python *is* a scripting language which is a *good* thing. It's their perceptions of what scripting languages are capable of that are out-of-date. - Paddy. -- http://mail.python.org/mailman/listinfo/python-list
Re: EuroPython vs PyconUK
EuGeNe Van den Bulke wrote: > I do realize that the UK is not really part of Europe (no polemic :P) > but I am nevertheless curious about the logic behind creating another > major Python event in Europe. Wasn't EuroPython enough? > > Like many I am sure, I probably won't be able to attend both (and I > really enjoyed the Geneva experience so definitely want to renew "it"). > How would you go about selecting which conference to attend? > > They are only 2 months apart, 6 would have been easier for the > attendees! Could the organizers liaise one way or another to make > Pythoneers life as easy and fun as the language and give as much > information out as possible as early as possible (early bird early) for > people to make the best decision? > > I know marketing matters but ... > > EuGeNe -- http://www.3kwa.com Growth! -- http://mail.python.org/mailman/listinfo/python-list
Re: Where did my post go?
[EMAIL PROTECTED] wrote: > I posted to this newsgroup earlier about my annoyances with python and > now I can't find the post. What did you do with it? > I notice a gmail address. Google groups was not updated for over a day and is still 'behind'. Try another news reader. - Paddy -- http://mail.python.org/mailman/listinfo/python-list
Re: Python not giving free memory back to the os get's me in real problems ...
[EMAIL PROTECTED] wrote: > So I read quite a few things about this phenomenon in Python 2.4.x but > I can hardly believe that there is really no solution to my problem. > > We use a commercial tool that has a macro functionality. These macros > are written in python. So far nothing extraordinary. > > Our (python-)macro uses massively nested loops which are unfortunately > necessary. These loops perform complex calculations in this commercial > tool. To give you a quick overview how long this macros runs: > > The outer loop takes 5-7 hours for one cycle. Each cycle creates one > outputfile. So we would like to perform 3-5 outer cycles en bloc. > Unfortunately one of our computers (768MB RAM) crashes after just ~10% > of the first cycle with the following error message: > > http://img2.freeimagehosting.net/uploads/7157b1dd7e.jpg > > while another computer (1GB RAM) crashes after ~10% of the fourth > loop. While the virtual memory on the 1gb machine was full to the > limit when it crashed the memory usage of the 768mb machine looked > this this: > > http://img2.freeimagehosting.net/uploads/dd15127b7a.jpg > > The moment I close the application that launched the macro, my > ressources get freed. > > So is there a way to free my memory inside my nested loops? > > thanks in advance, > tim > Could you split the program into one handling the outer loop and calling another program, with data transfer, to handle the inner loops? - Paddy. -- http://mail.python.org/mailman/listinfo/python-list