Re: Clever hack or code abomination?
Arnaud Delobelle wrote: > On 1 December 2011 03:15, Roy Smith wrote: >> I need to try a bunch of names in sequence until I find one that works >> (definition of "works" is unimportant). The algorithm is: >> >> 1) Given a base name, "foo", first see if just plain "foo" works. >> >> 2) If not, try "foo-1", "foo-2", and so on >> >> 3) If you reach "foo-20", give up. >> >> What would you say if you saw this: >> >> for suffix in [''] + [str(i) for i in xrange(-1, -20, -1)]: >> > > It's a little obfuscated ;) I would go for the simple: > > for i in xrange(21): > suffix = "-%s" % i if i else "" > > obfuscated for obfuscated, you can merge the two ideas: for suffix in ('-%s' % i if i else '' for i in xrange(21)): ... -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
FYI
http://scummos.blogspot.com/2011/09/kdev-python-argument-type-guessing.html I'm not used to big ide/rad for python... but I think this work is excellent! Are there alternatives (pydev? others?) capable of this sort of thinks (I mean "guessing the type" and method autocomplete) -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: allow line break at operators
Yingjie Lan wrote: > :The trouble of dealing with long lines can be avoided by a smart > :editor. It's called line wrap. > > Yeah, usually they just wrap it pretty arbitrarily, > and you don't have any control, isn't it? umm... besides "notepad" pretty much any other serious "programmer editor" program try to do its best to deal with line wrap: the minimal I found is the wrapped line is "indented" at the same level of the flow, but I found editors where you can specify what to do (generally something like "indent the wrapped part 2 levels" or something like that) -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: how to separate a list into two lists?
David Robinow wrote: > On Sat, Aug 6, 2011 at 1:23 PM, Kabie wrote: >> No. >> L1, L2 = zip(*L) > > Not quite. That makes L1 & L2 tuples. > > L1, L2 = zip(*L) > L1 = list(L1) > L2 = list(L2) > ??? L1, L2 = map(list, zip(*L)) -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: What is this syntax ?
Steven D'Aprano wrote: >> and you can achieve php interpolation via locals() >> >>>>> a = 'b' >>>>> print("%(a)s" % locals()) >> b > > You can do that, but when reading code I consider any direct use of > locals() (and globals() for that matter) to be a code smell: well you're right, me neither like very much to touch locals() and (worse) globals(), but 1) this is the "php interpolation" Roy Smith asked for: print "$scheme://$host:$port/$route#$fragment" where are defined scheme, host, port, route and fragment? or you think also this is "code smell"? 2) I'm in no way modifying the dict, just accessing in read only. 3) I'm restricting to locals() :D btw I never used dict to format strings, so I learned how old this feature is :D -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: What is this syntax ?
Roy Smith wrote: > There's something nice about building up strings in-line, as > opposed to having to look somewhere to see what's being interpolated. > To give a more complex example, consider: > > print "$scheme://$host:$port/$route#$fragment" > > That certainly seems easier to me to read than: > > print "%s://%s:%s/%s#%s" % (scheme, > port, > host, > route, > fragment) > > because I don't have to line up the nth format specifier with the nth > data item. well, in python3 you can use dict to format strings >>> print("%(a)s" % {'a':'b'}) b and you can achieve php interpolation via locals() >>> a = 'b' >>> print("%(a)s" % locals()) b -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: announcing: dmangame: an ai game. maybe.
could be the ACM queue challenge and google's ai challenge, they've had games in the past with somewhat similar mechanics On Jun 5, 11:10 pm, James Mills wrote: > On Mon, Jun 6, 2011 at 3:50 PM, okay zed wrote: > > the link:http://okayzed.github.com/dmangame/introduction.html > > > dmangame is a game about writing AI for a simple strategy game. > > > an example > > game:http://okayzed.github.com/dmangame/circleblaster_vs_expand.html > > This is really cool. I haven't looked at the source code yet... > > There was another game very similar to this - much more basic though > and when I saw it back then I wanted to do something similar! > > Nice job! > > cheers > James > > -- > -- James Mills > -- > -- "Problems are solved by method" -- http://mail.python.org/mailman/listinfo/python-list
announcing: dmangame: an ai game. maybe.
the link: http://okayzed.github.com/dmangame/introduction.html dmangame is a game about writing AI for a simple strategy game. an example game: http://okayzed.github.com/dmangame/circleblaster_vs_expand.html there are some example / more advanced AI in the dmanai repository (http://github.com/okayzed/dmanai). you can fork the repository and put your AI on the ladder - it'll get ranked and compete in regularly scheduled matches on http://dmangame-app.appspot.com. i'm looking for feedback, ideas and criticisms on the mechanics & gameplay; keeping in mind that the overall design goal is to keep the game simple. -- http://mail.python.org/mailman/listinfo/python-list
Re: Lambda question
jyoun...@kc.rr.com wrote: > I was surfing around looking for a way to split a list into equal > sections. non-recursive, same-unreadeable (worse?) one liner alternative: def chunks(s, j): return [''.join(filter(None,c))for c in map(None,*(s[i::j]for i in range(j)))] -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: True lists in python?
Steven D'Aprano wrote: > I can't see any way to go from this linked list: > > node1 -> node2 -> node3 -> node4 -> node5 -> node6 -> node7 > > to this: > > node1 -> node6 -> node5 -> node4 -> node3 -> node2 -> node7 > > in constant time. You have to touch each of the nodes being reversed. very crude example: class Node(object): def __init__(self, value, list): self.value = value self.next = self.previous = None self.list = list def nextNode(self): if not self.list.reversed: return self.next else: return self.previous class LinkedList(object): def __init__(self): self.head = None self.reversed = False def insertAsHead(self, value): n = Node(value, self) n.next = self.head if self.head is not None: self.head.previous = n self.head = n def main(): ll = LinkedList() ll.insertAsHead(4) ll.insertAsHead(3) ll.insertAsHead(2) n = ll.head while n is not None: n_previous = n print n.value n = n.nextNode() print 'reversed' ll.reversed = True n = n_previous while n is not None: print n.value n = n.nextNode() if __name__ == '__main__': main() -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: True lists in python?
Dmitry Groshev wrote: > Is there any way to use a true lists (with O(c) insertion/deletion and > O(n) search) in python? For example, to make things like reversing > part of the list with a constant time. if you're interested just in "reverse" a collection maybe you can take a look at the deque[0] module. If you want "true lists" (um... "linked list"?) there are is this recipe[1] you might look. [0] http://docs.python.org/library/collections.html#collections.deque [1] http://code.activestate.com/recipes/577355-python-27-linked-list-vs- list/ -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: How on Factorial
Lawrence D'Oliveiro wrote: > You know what, I think I actually prefer the trick to Python’s > backwards-if syntax... fact = lambda x: x*fact(x-1) if x>1 else 1 naa, it's not too bad... -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Request For Comments: Learn Python The Hard Way
Hi Everyone, I rarely post to the list, but I'm getting near the end of the book I wrote to help people learn Python and I would like some feedback on it if you please: * Web: http://learnpythonthehardway.org/ * PDF: http://learnpythonthehardway.org/static/LearnPythonTheHardWay.pdf The book so far has been successful in teaching Python to people who couldn't learn it previously, but I have to still test it with real learners more extensively. It is also in rough draft form, so any grammar errors, spelling, or just plain wrong code/concepts/terms are possible. I'd appreciate if Python experts and newbies could read through the book or choice sections and submit tickets to me if you find errors: http://learnpythonthehardway.org/tktnew Which will require an anonymous login to cut down on spam in the tickets. I'd also appreciate discussion here on the list about the approach I took with the book and what you think. Thanks for your time. Zed P.S. The book is totally free so long as you don't charge for it and you don't change it or break it up. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: bug in python documentation?
Benjamin Kaplan wrote: > You're looking at the 2.7 documentation. Are you using 2.7? whoops, no: 2.6.5 :\ (but the "new in python X.Y.Z" disclaimer does not apply to the example snippets?) -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
bug in python documentation?
from http://docs.python.org/library/unittest.html -->8>8>8>8>8>8>8>8>8>8>8>8-- Here is a short script to test three functions from the random module: import random import unittest class TestSequenceFunctions(unittest.TestCase): def setUp(self): self.seq = range(10) def test_shuffle(self): # make sure the shuffled sequence does not lose any elements random.shuffle(self.seq) self.seq.sort() self.assertEqual(self.seq, range(10)) # should raise an exception for an immutable sequence self.assertRaises(TypeError, random.shuffle, (1,2,3)) def test_choice(self): element = random.choice(self.seq) self.assertTrue(element in self.seq) def test_sample(self): with self.assertRaises(ValueError): random.sample(self.seq, 20) for element in random.sample(self.seq, 5): self.assertTrue(element in self.seq) if __name__ == '__main__': unittest.main() --8<8<8<8<8<8<8<8<8<8<8<8<-- but test_sample() it's a strange method: what's that "with self.assertRaises(ValueErrorr)"? infact, running the script I have $ python test_unittest.py .E. == ERROR: test_sample (__main__.TestSequenceFunctions) -- Traceback (most recent call last): File "./test_data_manip.py", line 23, in test_sample with self.assertRaises(ValueError): TypeError: failUnlessRaises() takes at least 3 arguments (2 given) ------ Ran 3 tests in 0.001s FAILED (errors=1) $ -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Beyond the moratorium
Hi all. I know, maybe I'm just lazily speculating, but I'm curious about what's next, in python, when GvR will stop the moratorium and will let changes in the language. I don't know what to expect... some syntax sugar about concurrent programming? static types? an erlang-style "bang" (!) process message passing? -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: Python "why" questions
Default User wrote: >>From "the emperor's new clothes" department: > > 1) Why do Python lists start with element [0], instead of element [1]? > "Common sense" would seem to suggest that lists should start with [1]. http://userweb.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html > 2) In Python 3, why is print a function only, so that: print "Hello, > World" > is not okay, but it must be print("Hello, World") instead? (Yeah, I know: > picky, picky . . . ) "There should be one-- and preferably only one --obvious way to do it." > 3) In Python 3, why does 2.0 / 3.0 display as 0., but 8 * > 3.57 displays as 28.56 (rounded off to 2 decimal places)? And yet, in > Python 2.6, 8 * 3.57 displays as 28.559? http://mail.python.org/pipermail/python-dev/2009-October/092958.html and replies -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: easy question on parsing python: "is not None"
Richard D. Moores wrote: > So there would be a different implementation for each operating > system? One for Windows, one for linux? Or one for Vista and one for > XP? I'm just trying to clarify what is meant by "implementation". there are dozillions of "implementation" of python: one for each release of CPython (2.0, 2.1.2, 2.6.1, 3.x.y...), multiplied for each OS multiplied for IronPython, Jython, Pypy... etc... (obviously the "implementation details" between, say, CPython 2.6.1 and 2.6.2 are really minor vs Pypy X.Y.Z and IronPython A.B.C) -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: python command line manual
Peng Yu wrote: > I mainly check online python manual. But I feel that it would be nice > if there is command line manual available (just like perl command line > manual). Could you please let me know if such command line manual > available? pydoc? -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: condition and True or False
Peter Otten wrote: > def f(): > big_beast = list(range(10**100)) > return big_beast and True or False > x = f() > > it would prevent that a big_beast reference becomes visible outside the > function and allow for immediate release of its memory. what's wrong in bool(big_beast)? -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: subtraction is giving me a syntax error
Grant Edwards wrote: >> As for not being able to see the difference between a hyphen and an >> EN- dash, or minus sign, or whatever it is, yes but they are very >> similar looking glyphs in virtually ever modern font. It would take a >> good eye to see the difference between (say) ??? ??? and -. > > My point is that if it's an ASCII file, source files aren't (necessary) ASCII files -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: detect interactivity
Steven D'Aprano wrote: >> if I run 'python <<< "import sys; print(sys.ps1)"', I get an error. > > Of course you do, because you're not running interactively Excuse me, why do you say that? -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
]ANN[ Vellum 0.16: Lots Of Documentation and Watching
Hi Everyone, Just putting out an announcement that I've released a new version of Vellum numbered 0.16. This version should be ready for people to use as not much of the internal structure has changed in a great many commits and it contains all the latest bug fixes. It also has the beginning of an extensive PDF document describing how to use it. This is still an in-progress work, so it has grammar mistakes and spelling errors, but it does show off some nice documentation wizardy I'm experimenting with in LaTeX. Finally there's a new "watch" feature which is very handy described further down. It simply watches a file and reruns your targets when the file changes. GETTING IT Honestly, the easiest way is just: sudo easy_install zapps vellum If you want to build the book yourself and have all of TeX Live installed then you'd also need: sudo easy_install pygments idiopidae You can also hit the http://launchpad.net/vellum/ page to grab source tarballs and other goodies. THE BOOK OF VELLUM You can grab the most recent draft of the book at: http://zedshaw.com/projects/vellum/manual-final.pdf Any corrections or comments on how it is written are more than welcome. Suggestions for improving the TeX are also helpful since I'm not a TeX expert yet. The software I'm using to build that book is fully available to anyone looking to document their projects. You can grab Idiopidae, Pygments, and TeX from the interwebs. You can then grab the whole source and all the LaTeX goodness from my Bazaar repository: bzr pull http://zedshaw.com/repository/vellum/ Look in the doc/ directory for all the fun. You'll notice that the .tex files have *no* code in them, and that it's all imported by Idiopidae. Look at the doc/book.vel file to see how it's all merged and massaged together, and you can reuse this book.vel file to start your own books. CRAZY NICE WATCH FEATURE I added a feature to Vellum that is one of those "duh" features. You can tell Vellum to watch a file, and if it changes Vellum will rerun some targets. When I work on the manual.tex file, I do this: vellum -w doc/manual.tex book.draft book.view It just keeps looping, and if you hit CTRL-C you can force a build with ENTER or quit with CTRL-C. Then I use the evince PDF viewer under linux, which has similar Vim key bindings and reloads the PDF when it changes. The net effect of this is, whenever I change my manual.tex file, Vellum runs my build for the manual and evince redisplays it for me to see. You could use this simple feature to also continually run unit tests whenever a file changes that you are working on. GPLv3? How do people feel about Vellum's GPLv3 status? It actually doesn't impact anyone unless you embed Vellum into a project/product or you create commands (which you should give back anyway). Even then if you never release your build to your users then you don't need to release the commands. However, I'm curious to get other people's thoughts. Thanks a bunch folks. -- Zed A. Shaw - Hate: http://savingtheinternetwithhate.com/ - Good: http://www.zedshaw.com/ - Evil: http://yearofevil.com/ -- http://mail.python.org/mailman/listinfo/python-list
[ANN] Vellum 0.13: Simple Python Build Tool (usable now)
Hello Everyone, Insomnia has blessed me with the ability to make another release of Vellum for all to play with and try using. Features in this release: * The ability to make your own commands for your build specs in plain old Python as a Python module. * The docstring comments on your vellum commands become dynamic documentation for vellum via: vellum -C. * Lots of documentation, comments, cleaned up code, and a more complete test suite. * Importing other build specs as recipes and importing python modules are all unified and cleaned up. * Still only 620 lines of Python code which is damn amazing. DOCUMENTATION I wrote a bunch of documentation tonight, and will be doing a more complete manual soon: http://zedshaw.com/projects/vellum/ This page is cleaned up and lays out how to use vellum, write a build.vel file, and write your own commands. DOWNLOADS First, you should grab Zapps and install that: http://zedshaw.com/projects/zapps/ Then you can grab Vellum with easy_install: $ sudo easy_install vellum Or, get it from http://launchpad.net/vellum/ ANNOUNCMENTS You can subscribe to Vellum's RSS feed at: https://launchpad.net/vellum/+announcements FEEDBACK Let me know if you run into anything, and if you like it or hate it. Otherwise, enjoy the gear. -- Zed A. Shaw - Hate: http://savingtheinternetwithhate.com/ - Good: http://www.zedshaw.com/ - Evil: http://yearofevil.com/ -- http://mail.python.org/mailman/listinfo/python-list
[ANN] Vellum 0.8: No More YAML, Some Python
Hello Everyone, This is a very fast announcement to say that I've managed to remove YAML from Vellum, but also to remove Python while still giving you Python. :-) Check out the latest Vellum: http://www.zedshaw.com/projects/vellum/ https://launchpad.net/vellum And you'll notice that the build descriptions are all Python, that you load other build descriptions with import statements, and yet it's still not actually running these build scripts. What the latest Vellum does is use a minimalist parser (vellum/parser.g) that is able to parse just that much syntax and build the data structure. This makes the build files safe from having injected code, but still gives you a full Python build description. This release is still in development, but if you want to install it, first install Zapps: http://www.zedshaw.com/projects/zapps/ And then you can install Vellum. Comments are welcome, especially about the actual safety of the code in vellum/parser.g and vellum/parser.py (generated by Zapps). Have fun. -- Zed A. Shaw - Hate: http://savingtheinternetwithhate.com/ - Good: http://www.zedshaw.com/ - Evil: http://yearofevil.com/ -- http://mail.python.org/mailman/listinfo/python-list
[ANN] Vellum 0.7: Simple Python Can Build Many Things
Hi All, This is another announce for my fun little make,Rake,Scons alternative named Vellum. The goal of Vellum is to create a complete build system in the least amount of clean Python as possible, while keeping the build mechanism safe from code injection (if you need that). == STATUS I went a little nuts today and did three releases adding tons of features but very little code. It currently can be used to build most anything, but there's little documentation. I'm using to build about 6 projects of mine and it's great stuff. The big changes from today is that, if you hate YAML, then you don't have to use it at all. Period. It's all python syntax all the time with none of the >>>,<--,-->,<<< characters. This seems to make people smile. You can download 0.7 from here: https://launchpad.net/vellum/0.0/0.7 Which will also be the main project distribution page for everyone. You can get the source from there via bzr or you can go to: http://www.zedshaw.com/projects/vellum == WHAT'S IN VELLUM 0.7? This release features complete Python integration that's on equal footing with the YAML support. You don't even need to install YAML to use Vellum with just Python only builds. It also adds a bit of Pythonic syntactic sugar for the build files so that people don't freak about the "weird" syntax used in the YAML files. Now you can write this for a target: from vellum.python import * targets = {"sample": [sh("echo i did something")]} Or any combination of sh(), log(), py(), given(), and target() to describe a target. == BUILD SAFETY One problem with using Python to describe a build is that it is an executable, which means that bad things can be put in the build. That's fine if it's your build, since you should be careful. But, if you get the build from someone else you'd want to analyze the build and load it without causing anything to happen. The YAML syntax lets you do this, but the Python does not. Therefore, for the security minded there's a new option -Y that only allows YAML loading so you can restrict your builds to only trust YAML. I'll be adding more features for the build safety that will hopefully let people use Vellum to safely (or reasonably safely) build other people's stuff without getting hurt (or at least get yelled at a lot). == TINY CODE So far Vellum is pushing around 300 lines of Python. That is just awesome. Anyone interested in helping feel free to branch the bzr and register it with Launchpad. Email me when you got stuff I might be interested in and I'll pull from you, or send me patches. Suggested features that don't involve major changes are more than welcome. == NEXT FEATURES * A few recipes to make building common stuff easy, like generating setup.py and so on. * A way to load modules from trusted locations. * Documentation, built with Vellum and Idiopidae. Fun. And that should be it. -- Zed A. Shaw - Hate: http://savingtheinternetwithhate.com/ - Good: http://www.zedshaw.com/ - Evil: http://yearofevil.com/ -- http://mail.python.org/mailman/listinfo/python-list
[ANN] Vellum 0.4 (it builds things, mostly books)
Hi All, I'd like to just announce that I've cranked out a fun little Python project named Vellum. It is an attempt at a Python make alternative written in about 200 lines of Python: http://www.zedshaw.com/projects/vellum/ It currently actually works, and can take a YAML or Python file as the description of the build, including namespaces, recipes from other files, and two samples for running nose and distutiles (respectively). I'm using it to automate most of my Python project builds and my website right now and it's doing great. The defining difference between it and other make-alikes is that each target is just a multi-line string, with different 3-char prefixes to determine how that line should be run: - >>> means it's Python, so run the python with exec. - <-- means eval a test and stop processing that target if False. - <<< means output some text. - --> means jump to another target, with an optional {} has of different options. - Nothing means it's a shell command, run it with os.system(). As the target is processed, each line is passed in as a string % options so you can do replacement on settings you've made. So, you can do this: <<< My name is %(name)s And if options: name: "fred", then it says it's name is fred. I'm pretty sure it's turing complete. :-) All information about the project, getting at the bzr, several sample files, and even the whole source to the core of the project are on the above page. Since I'm a total Python newbie, I'd love any advice about my code I can get. Thanks much. -- Zed A. Shaw - Hate: http://savingtheinternetwithhate.com/ - Good: http://www.zedshaw.com/ - Evil: http://yearofevil.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Cheat sheet
Michele Simionato wrote: > Python 2.4.4 (#2, Oct 4 2007, 22:02:31) file is open > True > > Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32) file is open > False > > Nowadays file is no more an alias for open. curious... maybe it's me, but I can't find a "What's New in Python" where this is said... also, in 2.6 docs (and also in 2.5) remains this footer: http://docs.python.org/dev/library/stdtypes.html#id14 http://www.python.org/doc/2.5/lib/bltin-file-objects.html#foot4449 file() is new in Python 2.2. The older built-in open() is an alias for file(). -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops and things
Jair Trejo wrote: > I was wondering how and if it's possible to write a > loop in python > which updates two or more variables at a time. For > instance, something > like this in C: > > for (i = 0, j = 10; i < 10 && j < 20; i++, j++) { > printf("i = %d, j = %d\n", i, j); > } >>> for i,j in zip(range(0, 10), range(10, 20)): ... print("i = %d, j = %d" % (i, j)) ... i = 0, j = 10 i = 1, j = 11 i = 2, j = 12 i = 3, j = 13 i = 4, j = 14 i = 5, j = 15 i = 6, j = 16 i = 7, j = 17 i = 8, j = 18 i = 9, j = 19 >>> -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: eof
Grant Edwards wrote: > The user-defined xor is operates on "logical" boolean values. > The one in the operator module is a bitwise operator. def xor(a, b): return bool(a) ^ bool(b) seems more explicit to me. maybe, to make "more" explicit (too much, onestly...) from operator import xor as bitwise_xor def logical_xor(a, b): return bitwise_xor(bool(a), bool(b)) -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: sorting a list numbers stored as strings
thebjorn wrote: >> >>> int("020") >> 20 >> >>> 020 >> 16 > > You can get the latter behavior using eval: why using eval when int has the "base" optional parameter? >>> int("020") 20 >>> int("020", 8) 16 >>> int("09", 8) Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 8: '09' >>> -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: generate list of partially accumulated values
cesco wrote: > The list is composed of objects: > l = [obj1, obj2, obj3, obj4] > and I need to call a method (say method1) on each object as follow: > l1 = [obj1.method1(obj2), obj2.method1(obj3), obj3.method1(obj4), > obj4] to me it sounds a bit different from the original request, but... > Is there a clean way of doing this? l = [obj1, obj2, obj3, obj4] l1 = [a.method1(b) for a,b in zip(l, l[1:)] + l[-1:] -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: List Comprehension Question: One to Many Mapping?
Boris Borcic wrote: >>> For example, if I have x=[ [1,2], [3,4] ] >>> What I want is a new list of list that has four sub-lists: >>> [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] >> [[a, map(f,a)] for a in x] > [map(g,a) for a in x for g in [None,f]] > will do it. > > ...a bit too cleverly, but there's worse : > list((yield a) or map(f,a) for a in x) worse (in *many* ways) solutions: l = [[a, map(f,a)] for a in x] 1) s = sum(l, []) 2) from operator import add r = reduce(add, l, []) 3) a = [] for e in l: a.extend(e) -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: alternative to eclipse [ python ide AND cvs ]
yomgui wrote: > I use eclipse for python and cvs, what is "the" good alternative ? "the" good alternative, I dont know. But a good solution is eric3 (http://www.die-offenbachs.de/detlev/eric.html) -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 3131: Supporting Non-ASCII Identifiers
Neil Hodgson wrote: > Ada 2005 allows Unicode identifiers and even includes the constant > '?' in Ada.Numerics. this. is. cool. (oh, and +1 for the pep) -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: track cpu usage of linux application
On Mon, 14 May 2007 20:56:20 + Fabian Braennstroem <[EMAIL PROTECTED]> wrote: > Hi, > > I would like to track the cpu usage of a couple of > programs using python. Maybe it works somehow with > piping 'top' to python read the cpu load for a greped > application and clocking the the first and last > appearence. Is that a good approach or does anyone have > a more elegant way to do that? Look at the /proc filesystem instead. For example, you can do this: cat /proc/49595/status To get information about that process. Using this you can find out anything you need with just basic file operations. Use: man proc to find our more. -- Zed A. Shaw - Hate: http://savingtheinternetwithhate.com/ - Good: http://www.zedshaw.com/ - Evil: http://yearofevil.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Standardizing XML
ZeeGeek wrote: > in the code returned by Live Space, they use instead of so > that Blogger will complain that this tag is not valid because it > doesn't have a closing tag. Another example is that the contents of a > lot of the tag attributes like "color" and "size" are not surrounded > by quotes. Are you sure it's xml? It sounds to me like "old" HTML -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: How to find all the same words in a text?
Johny wrote: >> >Let suppose I want to find a number 324 in the text >> >> >'45 324 45324' >> >> >there is only one occurrence of 324 word but string.find() finds 2 >> >occurrences ( in 45324 too) >> >> >>> '45 324 45324'.split().count('324') >> 1 >> >>> >> >> ciao > Marco, > Thank you for your help. > It works perfectly but I forgot to say that I also need to find the > possition of each word's occurrence.Is it possible that >>> [i for i, e in enumerate('45 324 45324'.split()) if e=='324'] [1] >>> -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: Splitting lines from a database query
Peter Machell wrote: > I have an application where I need to take a query from an existing > database and send it to a web api. [...] > There are always 5 values, but some are blank and some are 'None'. > I'd like to split the lines so I get something resembling XML, like this: > Frank > Spencer > > Dr I Feelgood > 2006-12-27 16:00:00 quick and dirty solution: bar = ( ("Jane Mary","SIMPSON","0411231234","Dr I Feelgood","2006-12-27 15:00:00"), ("John","DOE","None","Dr I Feelgood","2006-12-27 15:30:00"), ("Frank","SPENCER","","Dr I Feelgood","2006-12-27 16:00:00") ) spam ="FNAME", "SNAME", "PHONE", "DR","TIME" for x in bar: i=0 while i<5: if x[i] != 'None': print "<%s>%s" % (spam[i], x[i], spam[i]) else: print "<%s>" % (spam[i], spam[i]) i+=1 print -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: problem about list indexing
hollowspook wrote: > how about indexing 1-7, 10 > [range(1:8),10] will generate [[1, 2, 3, 4, 5, 6, 7], 10], instead of > [1, 2, 3, 4, 5, 6, 7, 10] >>> range(1,8)+[10] [1, 2, 3, 4, 5, 6, 7, 10] -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: tab compleation input
Eli Criffield wrote: > Here is what i want to do. I have a question in my program, and i want > to do tab completion for the valid answers. I think you may "play" width curses library: readline() read... while you input a "newline", but you need to catch single keys (the "TAB" key, foremost) -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting by item_in_another_list
Paul Rubin wrote: >> A = [0,1,2,3,4,5,6,7,8,9,10] >> B = [2,3,7,8] >> >> desired_result = [2,3,7,8,0,1,4,5,6,9,10] > > How about: > > desired_result = B + sorted(x for x in A if x not in B) this. is. cool. -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: Input from the same file as the script
Dennis Lee Bieber wrote: >> Can the input to the python script be given from the same file as the >> script itself. e.g., when we execute a python script with the command >> 'python > > Redirecting? Ugh... > > Off-hand, I'd say NO > > There is no way to tell the python interpreter where the program > ends and the "run data" begins. maybe, as an UGLY hack he coud use some the comments -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: #!/usr/bin/python or #!/usr/bin/env python?
Erik Max Francis wrote: > The file _is_ a /bin/sh executable. You're just having that /bin/sh > executable run something else -- how could `file` figure that out > without a ridiculously complicated set of rules that rise to the level > of a sh interpreter -- thereby, defeating the purpose? but... $ cat test.py #!/usr/bin/env python print "Hello, world" $ file test.py file.py: a python script text executable following what you said, test.py is a /usr/bin/env script, not a python one. -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: #!/usr/bin/python or #!/usr/bin/env python?
Stephan Kuhagen wrote: >> #!/bin/sh >> """exec" python "$0" "$@""" > > Wow, cool... I like that! yeah, but... $ cat test.py #!/bin/sh """exec" python "$0" "$@""" print "Hello, world" $ file test.py test.py: Bourne shell script text executable -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: Accessors in Python (getters and setters)
Bruno Desthuilliers wrote: >> I decided to change the name of an attribute. Problem is I've used the >> attribute in several places spanning thousands of lines of code. If I >> had encapsulated the attribute via an accessor, I wouldn't need to do >> an unreliable and tedious search and replace > find and grep are usually mostly reliable for this kind of tasks. you mean sed :) sed 's/oldName/newName/g' oldFile > newFile -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: Initializing a list of lists
Ciao, [EMAIL PROTECTED] Che stavi dicendo? > Is there a simple way to create a list of independent lists? N=3 x=[[0] for e in range(N)] -- Up da 1 giorno, 3 ore, 43 minuti e 10 secondi -- http://mail.python.org/mailman/listinfo/python-list
Re: how to write a C-style for loop?
Ciao, John Salerno! Che stavi dicendo? > for (int i = 0; i < 50; i += 5) > > How would that go in Python, in the simplest and most efficient way? i=0 while i<50: #... i+=5 about range()/xrange(): what if you want to traslate this c-loop? for (int i=1; i<50; i*=2) -- Evangelion e' la storia yaoi di un angelo che vuole portarsi a letto un ragazzo che si intreccia con la storia di un maturo professionista con il complesso delle lolite... fatte in casa. -- Lennier, 2006 -- http://mail.python.org/mailman/listinfo/python-list
Re: line wrapping problem
Ciao, Juho Schultz! Che stavi dicendo? > should work. IMO file.write() is self-explanatory but "print >> file" is > a bit obscure. is obscure only if you have never used a shell :) -- Evangelion e' la storia yaoi di un angelo che vuole portarsi a letto un ragazzo che si intreccia con la storia di un maturo professionista con il complesso delle lolite... fatte in casa. -- Lennier, 2006 -- http://mail.python.org/mailman/listinfo/python-list
Re: zip unzip?
Ciao, KraftDiner! Che stavi dicendo? > I have two lists... > > a=[1,2,3] > b=[4,5,6] > > n=0 > for i in a: >print i, b[n] >n=n+1 > > how can i have two iterators on my for loop? for i,j in zip(a,b): print i,j -- Quante sono le persone che sanno leggere il codice esadecimale, se ci sono de11e persone che sanno leggere il codice esadecimale? -- http://mail.python.org/mailman/listinfo/python-list
Re: python coding contest
Ciao, Shane Hathaway! Che stavi dicendo? > I'm down to 133 characters (counted according to 'wc -c') on a single > line. It contains about 11 whitespace characters (depending on what you > consider whitespace.) $ wc -c seven_seg.py 137 seven_seg.py $ sed 's/ //g' seven_seg.py|wc -c 120 (yeah, too much spaces, I think) > It's way too tricky for my taste, but it's fun to > play anyway. Has anyone done better so far? Here's a hint on my > strategy: the code contains three large integers. :-) why using 3 longint is better than one?! Trying to "split" my BIG int in 3 just make my code longer... ok, ok... I will wait the end of the contest... :) -- Firma in costruzione -- http://mail.python.org/mailman/listinfo/python-list