Re: proposal to allow to set the delimiter in str.format to something other than curly bracket
Peter Otten wrote: > You could automatically convert from a custom format like that in your > original post... Here's a class wrapping your functionality: import re class Template(object): '''uses double brackets e.g [[ob.attr]] as delims to get around curly bracket ({}) collisions when generating code ''' _pattern = re.compile(r"\[\[|]]|\{|\}") _lookup = { "[[": "{", "]]": "}", "{": "{{", "}": "}}", } def __init__(self, txt): self.txt = txt self.type = type def _substitute(self, m): return self._lookup[m.group()] def render(self, *args, **kwds): return self._pattern.sub( self._substitute, self.txt).format(*args, **kwds) def test_Template(): class P: pass p = P() p.name = 'peter' txt = 'hello there [[o.name]]' t = Template(txt) assert t.render(o=p) == 'hello there peter' -- http://mail.python.org/mailman/listinfo/python-list
Re: proposal to allow to set the delimiter in str.format to something other than curly bracket
Peter Otten wrote: > You could automatically convert from a custom format like that in your > original post: > > import re > > _lookup = { > "[[": "{", > "]]": "}", > "{": "{{", > "}": "}}", > > } > > def _substitute(m): > return _lookup[m.group()] > > def custom_format(template, *args, **kw): > return (re.compile(r"\[\[|]]|\{|\}") > .sub(_substitute, template) > .format(*args, **kw)) > > code = "class [[0]]Model { public bool IsModel(){ return a[42] || true; } }" > print custom_format(code, "My") Nice! I didn't think of that. I guess I could get some additional performance by taking the re.compile step out of the function. Thanks for the tip! AK AK -- http://mail.python.org/mailman/listinfo/python-list
Re: proposal to allow to set the delimiter in str.format to something other than curly bracket
Terry Reedy wrote: > Just double the brackets, just as one doubles '\\' to get '\' in a string. > > >>> "class {0}Model {{ public bool IsModel(){{ returntrue; ".format('My') > > 'class MyModel { public bool IsModel(){ returntrue; } }' > Indeed, I tried that, but it means I have to double bracket all the csharp code which just creates more work for me. Here is my solution (aka Cheetah ultra-light) ripped from stdlib.string: it involves hacking the string.Template class so that you get attribute lookups. I know it uses eval, and it's probably highly insecure, but it's for one off batch jobs, and I hope it doesn't hurt anyone: import re class _multimap: """Helper class for combining multiple mappings. Used by .substitute() to combine the mapping and keyword arguments. """ def __init__(self, primary, secondary): self._primary = primary self._secondary = secondary def __getitem__(self, key): try: return self._primary[key] except KeyError: return self._secondary[key] class _TemplateMetaclass(type): pattern = r""" %(delim)s(?: (?P%(delim)s) | # Escape sequence of two delimiters (?P%(id)s) | # delimiter and a Python identifier {(?P%(id)s)} | # delimiter and a braced identifier (?P) # Other ill-formed delimiter exprs ) """ def __init__(cls, name, bases, dct): super(_TemplateMetaclass, cls).__init__(name, bases, dct) if 'pattern' in dct: pattern = cls.pattern else: pattern = _TemplateMetaclass.pattern % { 'delim' : re.escape(cls.delimiter), 'id': cls.idpattern, } cls.pattern = re.compile(pattern, re.IGNORECASE | re.VERBOSE) class Template: """A string class for supporting $-substitutions.""" __metaclass__ = _TemplateMetaclass delimiter = '$' idpattern = r'[_a-z][_a-z0-9\.]*' def __init__(self, template): self.template = template # Search for $$, $identifier, ${identifier}, and any bare $'s def _invalid(self, mo): i = mo.start('invalid') lines = self.template[:i].splitlines(True) if not lines: colno = 1 lineno = 1 else: colno = i - len(''.join(lines[:-1])) lineno = len(lines) raise ValueError('Invalid placeholder in string: line %d, col %d' % (lineno, colno)) def substitute(self, *args, **kws): if len(args) > 1: raise TypeError('Too many positional arguments') if not args: mapping = kws elif kws: mapping = _multimap(kws, args[0]) else: mapping = args[0] # Helper function for .sub() def convert(mo): # Check the most common path first. named = mo.group('named') or mo.group('braced') if named is not None: # We use this idiom instead of str() because the latter will # fail if val is a Unicode containing non-ASCII characters. # here is the probably dangerous eval hack XX if '.' in named: return eval(named, kws) else: val = mapping[named] return '%s' % (val,) if mo.group('escaped') is not None: return self.delimiter if mo.group('invalid') is not None: self._invalid(mo) raise ValueError('Unrecognized named group in pattern', self.pattern) return self.pattern.sub(convert, self.template) def test_templates(): class P: pass p = P() p.name = 'ak' txt = 'hello there ${o.name}' t = Template(txt) assert t.substitute(o=p) == 'hello there ak' if __name__ == '__main__': test_templates() -- http://mail.python.org/mailman/listinfo/python-list
Re: proposal to allow to set the delimiter in str.format to something other than curly bracket
On Apr 3, 1:53 pm, Corey Richardson wrote: > On 04/03/2011 06:07 AM, Alia Khouri wrote: > > > Hi folks, > > > I've been using ironpython2.7 in a project, and I was generating some > > csharp code when i discovered that I couldn't use use str.format > > because the interference with the brackets-aplenty situation in > > csharp. > > Roll your ownhttp://docs.python.org/library/string.html#string.Formatter > (-: Thanks for the tip, looks I can subclass string.Template to get what I need. Kicking myself for posting too early... AK -- http://mail.python.org/mailman/listinfo/python-list
proposal to allow to set the delimiter in str.format to something other than curly bracket
Hi folks, I've been using ironpython2.7 in a project, and I was generating some csharp code when i discovered that I couldn't use use str.format because the interference with the brackets-aplenty situation in csharp. In [1]: code = "class {0}Model { public bool IsModel(){ return true; } }" In [2]: code.format('My') --- KeyError Traceback (most recent call last) KeyError: ' public bool IsModel(){ return true; } ' Please note I had no trouble with the trusty old % interpolation method. Now given that using str.format in this use case (code generation) is equally difficult for other bracketed languages, and that str.format is supposed to eventually become 'best-practice' for python string formatting, can I get some feedback on a proposal that for the odd case when brackets don't cut it, there is a way to redefine the delimiter for str.format so that one could do something like this: str.format_delimiter = ("[[", "]]") and we could write something like this: In [1]: code = "class [[0]]Model { public bool IsModel(){ return true; } }" In [2]: code.format('My') Out[3]: 'class MyModel { public bool IsModel(){ return true; } }' Please give your +N or -N and reasons below. AK -- http://mail.python.org/mailman/listinfo/python-list
playing with berp: python3 to haskell compiler in haskell
In case anyone finds this worthwhile: there is a pretty impressive python3 to haskell compiler written in haskell called berp that looks very interesting: http://github.com/bjpop/berp/wiki I highly recommend reading through the berp implementation code for a fascinating (but currently incomplete) attempt at a purely functional implementation of python3. The current version doesn't do floats and imports and other things but includes callcc continuations and tail- call optimization, and it is likely these things will be added relatively quickly as the code is extremely well-structured. Even the generated code (minus the underscore) is surprisingly readable: class Person: def __init__(self, name): self.name = name def say_hello(self): print('hello, my name is '+self.name) def main(): p = Person('sam') p.say_hello() main() to module Main where import Berp.Base import qualified Prelude main = runStmt init init = do _s_Person <- var "Person" _s_main <- var "main" klass "Person" _s_Person [] (do _s___init__ <- var "__init__" _s_say_hello <- var "say_hello" def _s___init__ 2 none (\ [_s_self, _s_name] -> do _t_0 <- read _s_name _t_1 <- read _s_self setattr _t_1 (621807930, "_s_name") _t_0) def _s_say_hello 1 none (\ [_s_self] -> do _t_2 <- read _s_print _t_3 <- read _s_self _t_4 <- _t_3 . (621807930, "_s_name") _t_5 <- string "hello, my name is " + _t_4 _t_2 @@ [_t_5]) pure [((-277916325, "_s___init__"), _s___init__), ((175397596, "_s_say_hello"), _s_say_hello)]) def _s_main 0 none (\ [] -> do _s_p <- var "p" _t_6 <- read _s_Person _t_7 <- _t_6 @@ [string "sam"] _s_p =: _t_7 _t_8 <- read _s_p _t_9 <- _t_8 . (175397596, "_s_say_hello") _t_9 @@ []) _t_10 <- read _s_main _t_10 @@ [] Also see: others ways to interface to haskell code (from ctypes for example, see my entry in http://wiki.python.org/moin/PythonVsHaskell) AK -- http://mail.python.org/mailman/listinfo/python-list
Re: Is Python a functional programming language?
Paul Rubin: > I like learnyouahaskell.com if you want to get some exposure to Haskell, > probably the archetypal functional language these days. I've been > fooling with it on and off for the past couple years. I'm still not > convinced that it's that good a vehicle for practical general purpose > software development, but there are some specific areas where it works > out just beautifully. And in terms of the challenges it presents and > the amount I've learned from it, it's one of the most interesting things > I've done as a programmer in as long as I can remember. It really is > mind altering. Completely agree with you. Learnyouahaskell.com is as good as it gets to learn haskell: haven't had so much fun learning a language since I picked up python :-) For similarly mind-altering pleasure, have a look at pure-lang [http:// code.google.com/p/pure-lang/] which describes itself as: "Pure is a modern-style functional programming language based on term rewriting. It offers equational definitions with pattern matching, full symbolic rewriting capabilities, dynamic typing, eager and lazy evaluation, lexical closures, built-in list and matrix support and an easy-to-use C interface. The interpreter uses LLVM as a backend to JIT- compile Pure programs to fast native code." Enjoy! AK -- http://mail.python.org/mailman/listinfo/python-list
interfacing python & haskell code with ctypes on linux
Hi Folks, Just in case anyone is interested, I've just added a very simple example for linux showing how to access haskell functions from python code using ctypes. It's on the wiki: http://wiki.python.org/moin/PythonVsHaskell AK -- http://mail.python.org/mailman/listinfo/python-list
python version in snow leopard?
Does anyone know what version of python will appear in snow leopard which is apparently being released in September? -- http://mail.python.org/mailman/listinfo/python-list
survey: cool but not so well-known python apps
This mini-survey's purpose is to raise awareness of certain apps that perhaps don't have the marketing profile/momentum of some of the better known python apps out there, but are cool and interesting nonetheless. To this end, please post 3-5+ apps that you think deserve more attention: Here's my list (unordered): - leo-editor (a literate programming and outlining editor) http://webpages.charter.net/edreamleo/front.html - nodebox (the python programmer as artist) http://nodebox.net - orange (makes datamining/machine learning fun and accessible) http://www.ailab.si/Orange/ - functional (for the inner functional programmer in you) http://pypi.python.org/pypi/functional - ubigraph (dynamic graph visualization) http://ubietylab.net/ubigraph/ -- http://mail.python.org/mailman/listinfo/python-list
recursive outline numbering for object trees
Hi, Here my problem description: Given the following class: class Node(object): def __init__(self, name, children=[], parent=None): self.name = name self.level = '' self.children = children self.parent = parent def __repr__(self): name = self.__class__.__name__ return "<%s %s>" % (self.name, self.level) def __iter__(self): yield self for child in self.children: child.parent = self for subchild in iter(child): yield subchild and the following example: tree1 = Node('root[1] ', [ Node('branch [1.1]', [ Node('leaf [1.1.1]', []), Node('leaf [1.1.2]', []), ]), Node('branch [1.2]', [ Node('leaf [1.2.1]', []), Node('leaf [1.2.2]', []), ]) ]) I would like to create a walk function which when given the root node (tree1) automatically sets the correct outline numbering for all its subnodes. such that for i in walk(tree): print i.level should give the following output: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 Now, I have scoured the web and found this (http:// www.opensubscriber.com/message/python-list@python.org/9056939.html) solution from castironpi, which seems to work nicely: class up( Exception ): pass class down( Exception ): pass def outline(): stack = [1] while True: try: # print 'next' yield '.'.join(str(s) for s in stack) stack[-1]+= 1 except down: # print 'down' stack.append(1) except up: # print 'up' stack.pop(-1) stack[-1] += 1 def test_outline(): print o = outline() print o.next() print o.throw(down) print o.throw(down) print o.next() print o.throw(up) print o.throw(down) print o.next() running test_outline() generates the required output so, so I tried to incorporate the above into my walk function with mixed results (warning: it's ugly): from itertools import count def walk(node, level=outline(), last=None, i=count(1), root=True): '''tree walker assigns parent attribute, creates numbered outline ''' if root: node.level = level.next() yield node if last: node.level = last last = None if node.children: next = level.throw(down) elif i.next() == 5: # length of nodes (hardcoded here for tsting) raise StopIteration else: next = level.throw(up) for child in node.children: child.parent = node child.level = next or level.next() if child == node.children[-1]: last = child.level for subchild in walk(child, level, last, i, root=False): yield subchild works for tree = Node('root[1] ', [ Node('branch [1.1]', [ Node('leaf [1.1.1]', []), ]), Node('branch [1.2]', [ Node('leaf [1.2.1]', []), ]) ]) but barfs for the required tree1 (above). I've kinda hit a wall here, so any help would be appreciated. As an aside, if a solution is possible as an external walk function would it be possible to work in __iter__? Best, AliaK -- http://mail.python.org/mailman/listinfo/python-list
sorting tasks by importance and urgency
I recently considered the apparently simple problem is of how to algorithmically sort a set of business tasks which have an associated a value and a due_date, such that the most important and urgent are pushed to the top of the stack. The two example task types I am posing here are: (1) a bid on a contract and (2) leaverequest from an employee. To sort by importance: I just consider the proportional value of each task in relation to the total value of tasks. To sort by urgency: I used something along the lines of excel's percentrank function (http://office.microsoft.com/en-us/excel/ HP052092121033.aspx) to rank the tasks in percentage terms of the set of days_to delivery. The code is pretty self-explanatory, and I think the algorithm seems to work ok so far, but in case I have missed something or there exists some better way of doing this... from datetime import date weights = { 'Tender' : 1.0, 'LeaveRequest': 0.1 } rnd = lambda x: round(x,2) class Task(object): """a simple prioritizing Task class""" def __init__(self, kind, value, due_date=None): self.kind = kind self.value = value self.due_date = due_date if due_date else date.today() self.weight = weights[kind] self.urgency = 0.0 self.importance = 0.0 def __repr__(self): return '' % ( rnd(self.priority), rnd(self.importance), rnd (self.urgency), self.kind, self.days_to, self.value) def __cmp__(self, other): return cmp(self.priority, other.priority) @property def days_to(self): return (self.due_date - date.today()).days @property def priority(self): return self.weight * (self.importance + self.urgency) def relative_urgency(self, N, due_days): rank = due_days.index(self.days_to) return float(rank) / (N - 1) def relative_importance(self, total_value): return self.value / total_value @staticmethod def prioritize(tasks): print ("\n") N = len(tasks) total_value = sum(t.value for t in tasks) due_days = sorted([t.days_to for t in tasks], reverse=True) for i in tasks: i.importance = i.relative_importance(total_value) i.urgency = i.relative_urgency(N, due_days) for i in sorted(tasks, reverse=True): print i tasks = [ # name value due_date Task("Tender", 100.0, date(2009,4,1)), Task("Tender", 500400.0, date(2009,5,1)), Task("Tender", 100.0, date(2009,6,1)), Task("LeaveRequest", 0.0,date(2009,7,1)), Task("LeaveRequest", 0.0,date(2009,8,1)), ] if __name__ == '__main__': Task.prioritize(tasks) regards, AK -- http://mail.python.org/mailman/listinfo/python-list
python contextmanagers and ruby blocks
As an exercise, I recently translated one of my python scripts (http:// code.activestate.com/recipes/576643/) to haskell (a penultimate version exists at http://groups.google.com/group/comp.lang.haskell/browse_thread/thread/fb1ebd986b44244e# in case anyone is interested) with the result that haskell has now become my second favourite language (after python of course :-) Just to change mental gears a bit, I'd now like to do the same and create a ruby version. As I've progressed on the latter, I've been struck by how pervasive the use of blocks is in ruby. For example: class Builder attr_accessor :name def machine &block @name = "m1" block.call end def build(x, &block) puts x block.call end end builder = Builder.new builder.machine do puts "hello #{builder.name}" end builder.build "hello" do puts "world" end which should print out: hello m1 hello world Now, python's relatively new contextmanagers seem to provide something similar such that one can write: from __future__ import with_statement from contextlib import contextmanager class Builder: @contextmanager def machine(self): self.name = "m1" yield @contextmanager def build(self, x): print x yield builder = Builder() with builder.machine(): print 'hello %s' % builder.name with builder.build("hello"): print 'world' Which brings me to my questions: 1. To what extent are python's contextmanagers similar or equivalent to ruby's blocks? 2. If there is a gap in power or expressiveness in python's context managers relative to ruby's blocks, what are possible (syntactic and non-syntactic) proposals to bridge this gap? Thank you for your responses. AK -- http://mail.python.org/mailman/listinfo/python-list
Re: Source code generation using Python
> Any suggestions? I've happily used Cheetah with Leo (http://webpages.charter.net/ edreamleo/front.html) to organise and script my code generation needs, but you may also be happy with cog (http://nedbatchelder.com/code/ cog/). AK -- http://mail.python.org/mailman/listinfo/python-list
Re: Will MySQLdb, the Python shim, be supported for Python 2.6 or 3.x?
> > John Nagle wrote: > Whoever did the port somehow created a dependency on the > Intel math library, "libguide40.dll" and "libmmd.dll". That shouldn't > be needed in the MySQL Python shim. It's not freely distributable, > either; you have to buy the Intel C++ compiler to get it. There are > versions of those DLLs available on the web, but they may or may not > be legitimate or current. I'm aware of that, as I went through the same process, actually discovered that dependency myself, and ended up having to use dlls off the net. Eventhough I got this to work in the end, I ultimately found that this isn't the only extension lib with 'issues', in 2.6 (numpy, etc..) I finally ditched 2.6 for my app and reverted back to 2.5. My recommendation is to do the same until things improve in extension land. AK -- http://mail.python.org/mailman/listinfo/python-list
Re: Will MySQLdb, the Python shim, be supported for Python 2.6 or 3.x?
John Nagle wrote: > MySQLdb, the Python shim for MySQL, still supports Python only to > Python 2.5. See "http://sourceforge.net/projects/mysql-python";. Are there > any plans to support Python 2.6 or 3.x? Are you running windows? If so, check the forums of the group above, some nice chap has posted a 2.6 version. -- http://mail.python.org/mailman/listinfo/python-list
Re: Midi manipulation
On Nov 16, 10:17 pm, Massi <[EMAIL PROTECTED]> wrote: > Hi everyone, I'm searching for "something" which allows me to write > scripts which handle midi files. I'm totally a newbie in audio > manipulation, therefore any suggestion or link related to this field > is welcome. Thanks in advance. http://wiki.python.org/moin/PythonInMusic -- http://mail.python.org/mailman/listinfo/python-list
doctesting practices
I was wondering the other day how other pythonistas incorporate doctests into their coding practices. I have acquired the habit of keeping an editor open in one window and an ipython instance open in another and then using something similar to the format of the module below. In this case, I incorporate a switch in the _test function whereby covered=False means the doctest is still being written (and is easy to test using the command line) and covered=True means it is somewhat complete and ready to be incorporated into a test suite. This still seems to me to be somewhat hackish and convoluted (execing into globals() and all), and one wonders if there are better coding workflows out there that specifically incorporate doctests as the primary means of testing. Thanks in advance for any feedback AK ''' Simple doctest of a module usage:: >>> fib(0) 0 >>> fib(1) 1 >>> fib(10) 55 >>> fib(15) 610 ''' def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2) def _test(covered=False): import doctest if covered: doctest.testmod() else: exec doctest.script_from_examples(__doc__) in globals() if __name__ == '__main__': _test() -- http://mail.python.org/mailman/listinfo/python-list
Re: You advice please
Hussein B wrote: > I'm a Java/Java EE developer and I'm playing with Python these days. > I like the Python language so much and I like its communities and the > Django framework. Sounds familiar... (-: > My friends are about to open a Ruby/Rails shop and they are asking me > to join them. In this case, I think you have to make decision that is not technology- centric but application-centric, and you should also consider closely the opportunity set and capability set available to you and your friends. > I don't know what, sure I'm not leaving Java, but they are asking me > to stop learning Python and concentrate on Ruby/Rails. I don't think you should stop learning anything that rings your bell.. I love learning other languages (e.g. Haskell, Lua, Ruby, C#, Java, boo, etc..) and I will code projects as per the requirements at the time, but I tend to Python because, like you, I like the language and the community. In _addition_ to your love for Python and Django, why not learn Ruby/ Rails? It's not a bad framework at all, and Ruby is quite fun to program in as well...? > The sad fact (at least to me), Ruby is getting a lot of attention > these days. Not a sad fact, What's good for ruby is good for python and vice versa... Friendly competition is always positive and usually good ideas cross-pollinate across the languages... > Why Python isn't getting this attention although is a much more mature > language and it is used by many big, big names? Who says Python is not getting attention? Last time I checked, Python's popularity was at all time high, and the big guns in the industry favor (witness Google AppEngine, Microsoft Ironpython preceding Ironruby, etc..) > And do I dare to say it is much more charming? That is an aesthetic judgement... (-: > What do you think of Ruby/Rails? do they worth learning and working > with? (see above) > Any way, I'm not leaving Python and I will try to study it every time > I get a chance... Good for you (-: > Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Jason Orendorff's path module
andybak wrote: > Which rather makes me wonder - lot's of people liked this module > or rather disliked os.path. Is there anything salvageable from > the BDFL's objections worthy of a PEP? Funny you should bring this up today.. I was just thinking the same thing (-: and I looked at the pep (http://www.python.org/dev/peps/ pep-0355/) and the final (pep) version of the path module, which is at http://wiki.python.org/moin/PathModule If the primary issue that killed this pep was due to inheritance from the str type, i.e. the fact that a lot of unnecessary string functions would be inherited into an already functionally-fat path class, then perhaps that problem had to be addressed first, as it would probably crop up in other areas. Perhaps I'm wrong, but I presume ABCs (abstract base classes) were intended to help in such cases? AK -- http://mail.python.org/mailman/listinfo/python-list
Re: snippet to update local (bazaar, mercurial, svn) versioned source
On Jul 16, 8:34 am, Alia Khouri <[EMAIL PROTECTED]> wrote: > Here's a very simple snippet I use to automatically keep my versioned > sources fresh.. Posted here in case it may be of use to anybody... > > > #!/usr/local/bin/python > import os, sys > > src = '/Users/ak/Code/src' > > # utility functions > join, isdir, listdir = os.path.join, os.path.isdir > def run(cmd): > print cmd > os.system(cmd) > > ops = { > '.bzr': ['bzr pull', 'bzr update'], > '.hg': ['hg pull', 'hg update'], > '.svn': ['svn update'] > > } > > for folder in os.listdir(src): > target = os.path.join(src,folder) > if os.path.isdir(target): > internal = os.listdir(target) > for f in internal: > if f in ops: > print > os.chdir(target) > cmds = ops[f] > print > print target, '-->', > for cmd in cmds: > run(cmd) > > My bad, here's the one that actually (-: #!/usr/local/bin/python import os, sys src = '/Users/sa/Code/src' # utility functions join, isdir = os.path.join, os.path.isdir def run(cmd): print cmd os.system(cmd) ops = { '.bzr': ['bzr pull', 'bzr update'], '.hg': ['hg pull', 'hg update'], '.svn': ['svn update'] } for folder in os.listdir(src): target = join(src,folder) if isdir(target): internal = os.listdir(target) for f in internal: if f in ops: print # print f, target os.chdir(target) cmds = ops[f] print print target, '-->', for cmd in cmds: run(cmd) -- http://mail.python.org/mailman/listinfo/python-list
snippet to update local (bazaar, mercurial, svn) versioned source
Here's a very simple snippet I use to automatically keep my versioned sources fresh.. Posted here in case it may be of use to anybody... #!/usr/local/bin/python import os, sys src = '/Users/ak/Code/src' # utility functions join, isdir, listdir = os.path.join, os.path.isdir def run(cmd): print cmd os.system(cmd) ops = { '.bzr': ['bzr pull', 'bzr update'], '.hg': ['hg pull', 'hg update'], '.svn': ['svn update'] } for folder in os.listdir(src): target = os.path.join(src,folder) if os.path.isdir(target): internal = os.listdir(target) for f in internal: if f in ops: print os.chdir(target) cmds = ops[f] print print target, '-->', for cmd in cmds: run(cmd) -- http://mail.python.org/mailman/listinfo/python-list
Re: python's setuptools (eggs) vs ruby's gems survey/discussion
Might as well answer the survey myself: > A few questions (please add more) so far are: > (1) Should setuptools be standard? Not at this stage. What pythonistas need is a cross-platform package manager that is included in the stdlib. Setuptools is simply not mature enough nor pythonic enough to be standard in its current form. > (2) What bugs you most about the current featureset? - no uninstall so I have to spend time manually removing old egg files/ directories - pollutes my site-packages directory with lots of sparse egg-info files - uglifies my sys.path - the egg directory structure is unpythonically deep - confusing documentation - easy_install is a mouthful (why not 'python -m egg.install') > (3) Which features do you need the most (list in order of need)? - uninstall - clean up location of egg files - better dependency management (doesn't break) - works with multiple versions of python - list available packages / versions / dependencies - local database of packages (why not use sqlite?) > (4) Shouldn't we just port gems to python? I don't think that's bad idea... > (5) What's the best community process to improve setuptools? Perhaps we can all contribute some cash/time/effort to improve setuptools or even rewrite the python package manager of our collective dreams... > (6) What's your ideal conception of the 'standard python package > manager? Pure Python. Combines the best features of port/apt-get/gems while being in the stdlib AK -- http://mail.python.org/mailman/listinfo/python-list
python's setuptools (eggs) vs ruby's gems survey/discussion
Can we open up the discussion here about how to improve setuptools which has become the de facto standard for distributing / installing python software. I've been playing around with ruby's gems which seems to be more more mature and usable. >From my perspective, the relative immaturity of setuptools and its simultaneous widespread use is a clear python weakness and can make python less easy to absorb than it should be. A few questions (please add more) so far are: (1) Should setuptools be standard? (2) What bugs you most about the current featureset? (3) Which features do you need the most (list in order of need)? (4) Shouldn't we just port gems to python? (5) What's the best community process to improve setuptools? (6) What's your ideal conception of the 'standard python package manager? To give this discussion some ammunition, I will post the output of the different '--help' for either tool: == SETUPTOOLS == C:\TMP>easy_install --help Global options: --verbose (-v) run verbosely (default) --quiet (-q)run quietly (turns verbosity off) --dry-run (-n) don't actually do anything --help (-h) show detailed help message Options for 'easy_install' command: --prefix installation prefix --zip-ok (-z) install package as a zipfile --multi-version (-m) make apps have to require() a version --upgrade (-U) force upgrade (searches PyPI for latest versions) --install-dir (-d) install package to DIR --script-dir (-s) install scripts to DIR --exclude-scripts (-x) Don't install scripts --always-copy (-a) Copy all needed packages to install dir --index-url (-i) base URL of Python Package Index --find-links (-f) additional URL(s) to search for packages --delete-conflicting (-D) no longer needed; don't use this --ignore-conflicts-at-my-risk no longer needed; don't use this --build-directory (-b) download/extract/build in DIR; keep the results --optimize (-O)also compile with optimization: -O1 for "python -O", -O2 for "python -OO", and -O0 to disable [default: -O0] --record filename in which to record list of installed files --always-unzip (-Z)don't install as a zipfile, no matter what --site-dirs (-S) list of directories where .pth files work --editable (-e)Install specified packages in editable form --no-deps (-N) don't install dependencies --allow-hosts (-H) pattern(s) that hostnames must match --local-snapshots-ok (-l) allow building eggs from local checkouts usage: easy_install-script.py [options] requirement_or_url ... or: easy_install-script.py --help == GEMS == C:\TMP>gem --help RubyGems is a sophisticated package manager for Ruby. This is a basic help message containing pointers to more information. Usage: gem -h/--help gem -v/--version gem command [arguments...] [options...] Examples: gem install rake gem list --local gem build package.gemspec gem help install Further help: gem help commandslist all 'gem' commands gem help examplesshow some examples of usage gem help platforms show information about platforms gem helpshow help on COMMAND (e.g. 'gem help install') Further information: http://rubygems.rubyforge.org C:\TMP>gem help commands GEM commands are: build Build a gem from a gemspec cert Manage RubyGems certificates and signing settings check Check installed gems cleanup Clean up old versions of installed gems in the local repository contents Display the contents of the installed gems dependencyShow the dependencies of an installed gem environment Display information about the RubyGems environment fetch Download a gem and place it in the current directory generate_indexGenerates the index files for a gem server directory help Provide help on the 'gem' command install Install a gem into the local repository list Display gems whose name starts with STRING lock Generate a lockdown list of gems mirrorMirror a gem repository outdated Display all gems that need updates pristine Restores installed gems to
Re: Python 3.0 migration plans?
Couldn't agree with you more. What would be fantastic is if I could drop into the Pypi (and/or use easy_install) and download automatically compiled versions of extension modules for different versions of python. I'm sure the community at large would be happy to chip in an annual fee to help build/maintain this infrastructure: I know my firm would. PS: IMHO The growth of Ruby is, to a large extent, due to easy installation of modules via its gem system... On Sep 28, 6:53 pm, John Nagle <[EMAIL PROTECTED]> wrote: > Insofar as Python has an organization, it's not adequately managing > extension modules. Each extension module has its own infrastructure, > with its own build procedures, its own bug list, and its own maintainers. > There's not even an archive. Unlike CPAN, Cheese Shop is just a directory of > URLs. > > Take a look at how Perl does it. Here are the instructions on > how to contribute to CPAN: > >http://www.cpan.org/modules/04pause.html > > There's a way to get your module into the system, a standardized format, > build, and installation procedure, and an archive which is mirrored. > There's a common bug reporting system. Modules abandoned by their > original developers are not lost, and can be "adopted" by someone else. > > Python doesn't have any of this. And that's far more of a problem > than Python 3.x. > > John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: problems with logging module
> You are not going beyond basicConfig - I'd write the above as: Intentionally didn't go beyond basicConfig. The problem is global level configuration vs. easy local (in function or in class) configuration. > logging.basicConfig(...) > > def __init__(self, x): > self.x = x > self.log = logging.getLogger("a.nice.name") > > If you *do* need more than a handler, or different formatters for > different loggers, then you must write your own setup anyway; perhaps > using logging.config files, or perhaps writing your own code. I can't > think of a simple and generic approach (beyond what basicConfig provides) > but if you can write something that other people find useful, feel free to > submit a patch. Why not indeed... let's see what happens. AK -- http://mail.python.org/mailman/listinfo/python-list
Re: problems with logging module
On Jul 30, 8:01 am, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > To see what's going on, try using this old-fashioned debug technique: a > few print statements. My bad, problem was solved, the shortcoming was one of my variables pointing to nothingness (-; But I'm not letting the logging module off, it's still not the easiest or most intuitive module to work with. For example: a basic Log class that can be programatically configured would be quicker to work with then messing around with three different classes types if you want to get beyond basicConfig functionality: (Logger, handlers, formatters) To be able to do this would be nice: from logging import Log class MyClass: def __init__(self, x): self.x = x self.log = Log( level=logging.DEBUG, format="%(asctime)s %(levelname)s: %(message)s", datefmt = '%H:%M:%S', filename='app.log', filemode='a' ) Thanks for your help and time. AK -- http://mail.python.org/mailman/listinfo/python-list
problems with logging module
I've been struggling with the logging module in the stdlib which seems to me rather counter-intuitive: For some reason it refuses to recognize configuration options when they are set inside a class so I have had to initialize logging and set the configuration options in the global scope of my module with logging.basicConfig. Here's what I did within the class setup method: self.log = logging.getLogger() # format log_format= self.local.format date_format = self.local.date_format or "%d.%m.%y %H:%M:%S" self.logfile= self.local.logfile if self.local.log_to_file else None if self.logfile: handler = logging.FileHandler( self.logfile, self.local.logfile_mode) else: stream = None # can be sys.st something or other stream handler = logging.StreamHandler() format = logging.Formatter(log_format, date_format) handler.setFormatter(format) self.log.addHandler(handler) self.log.setLevel(self.local.log_level or logging.DEBUG) self.log gets initialized but the formatting options do not get recognized... this is a pain... What I do want is something like the log4r module in Ruby: e.g. require 'log4r' require 'getoptlong' require 'pathname' class Common def init_log @log = Log4r::Logger.new(self.class.name) @log.add Log4r::Outputter.stdout @log.info 'initialized' end end class Builder < Common def initialize(path, options) init_log if File.exist?(path) @path = Pathname.new(path) @options = options else @log.error "not a valid file or directory" exit end end def build() case @path.ftype when 'file' filehandlers = { '.txt' => TxtHandler, '.java' => JavaHandler, '.c'=> CHandler, '.cpp' => CppHandler, '.py' => PyHandler, '.pyx' => PyxHandler, '.exe' => ExeHandler, '.hs' => HaskellHandler, '.rb' => RubyHandler, '.dot' => DotHandler, '.mp3' => MP3Handler, '.wav' => WavHandler, '.csd' => CSoundHandler, '.orc' => CSoundHandler, '.sco' => CSoundHandler, [EMAIL PROTECTED](@path, @options).handle() when 'directory' @log.info "[EMAIL PROTECTED] is a directory" end end end etc... still to prefer to code in python though Just my 2c... AK -- http://mail.python.org/mailman/listinfo/python-list
Re: recursively expanding $references in dictionaries
Ok. I've reached a nice little conclusion here. Time to go to bed, but before that I thought I'd share the results (-; I can now read a yaml file which natively produces a dict tree and convert it into an object tree with attribute read/write access, dump that back into a readable yaml string, and then expand references within that using cheetah in a very nifty self-referential way.* (see: http://pyyaml.org/wiki/PyYAML) and cheetah templates (http:// www.cheetahtemplate.org/) Enjoy! AK from Cheetah.Template import Template from pprint import pprint import yaml class Object(dict): def __getattr__(self, name): if name in self: return self[name] #~ if name in self.__dict__: return getattr(self, name) def __setattr__(self, name, value): self[name] = value def getTree(tree, to_dict=False): _tree = Object() if not to_dict else dict() def recurse(targetDict, sourceDict): for key, value in sourceDict.items(): if isinstance(value, dict): value = Object(value) if not to_dict else dict(value) new_target = targetDict.setdefault(key, value) recurse(new_target, value) else: targetDict[key] = value recurse(_tree, tree) return _tree config = ''' app: name: appname copyright: me.org 2007 dir: src: /src/$app.name ''' # from yml dict tree to obj tree root = getTree(yaml.load(config)) print root print assert root.app.name == root.app['name'] root.app.name = "the_monster" # from obj tree to dict tree root = getTree(root, to_dict=True) # from dict tree to yaml string s = yaml.dump(root) print s # use cheetah templates to expand references print str(Template(s, searchList=[root])) -- http://mail.python.org/mailman/listinfo/python-list
Re: recursively expanding $references in dictionaries
Oops, I left some redundant cruft in the function... here it is slightly cleaner: def expand(dikt): names = {} output = {} def _search(_, sourceDict): for key, value in sourceDict.items(): if isinstance(value, dict): _search({}, value) if not '$' in value: names[key] = value _search({}, dikt) def _substitute(targetDict, sourceDict): for key, value in sourceDict.items(): if isinstance(value, dict): new_target = targetDict.setdefault(key, {}) _substitute(new_target, value) else: targetDict[key] = Template(value).substitute(names) _substitute(output, dikt) return output print expand(d2) -- http://mail.python.org/mailman/listinfo/python-list
recursively expanding $references in dictionaries
I was kind of wondering what ways are out there to elegantly expand '$name' identifiers in nested dictionary value. The problem arose when I wanted to include that kind of functionality to dicts read from yaml files such that: def func(input): # do something return output where: input = {'firstname': 'John', 'lastname': 'Smith', 'src': 'c:/tmp/ file', 'dir': {'path': '$src', 'fullname': '$firstname $lastname'}} output = {'firstname': 'John', 'lastname': 'Smith', 'src': 'c:/tmp/ file', 'dir': {'path': 'c:/tmp/file', 'fullname': 'John Smith'}} Doing this substitution easy done when you have a flat dict, but when they got nested, I had to resort to an undoubtedly ugly function with two recursive passes and obvious deficiencies. Is there a better way to do this? Thanks for any help... AK # test_recurse.py from string import Template from pprint import pprint def expand(dikt): ''' >>> d = expand2({'firstname': 'John', 'lastname': 'Smith', 'fullname': '$firstname $lastname'}) >>> d == {'lastname': 'Smith', 'fullname': 'John Smith', 'firstname': 'John'} True ''' subs = {} for key, value in dikt.items(): if '$' in value: subs[key] = Template(value).substitute(dikt) dikt.update(subs) return dikt dikt = {'firstname': 'John', 'lastname': 'Smith', 'fullname': '$firstname $lastname'} #~ print expand(dikt) d1 = {'firstname': 'John', 'lastname': 'Smith', 'dir': {'fullname': '$firstname $lastname'} } d2 = {'firstname': 'John', 'lastname': 'Smith', 'src': 'c:/tmp/file', 'dir': {'fullname': '$firstname $lastname', 'path': '$src'} } def rexpand(dikt): subs = {} names = {} # pass 1 def recurse(_, sourceDict): for key, value in sourceDict.items(): if isinstance(value, dict): recurse({}, value) elif '$' in value: subs[key] = value else: names[key] = value recurse({}, dikt) print 'subs', subs print 'names', names print 'dikt (before):', dikt for key, value in subs.items(): subs[key] = Template(value).substitute(names) # - # pass 2 output = {} def substitute(targetDict, sourceDict): for key, value in sourceDict.items(): if isinstance(value, dict): new_target = targetDict.setdefault(key, {}) substitute(new_target, value) else: targetDict[key] = Template(value).substitute(names) substitute(output, dikt) print 'output:', output return output rexpand(d2) -- http://mail.python.org/mailman/listinfo/python-list
Ableton Live Python API is out!
I have been waiting for this ages and it's finally happened! Python meet Live, Live meet Python! There's now a wonderful (public) bridge between (arguably) the most exciting and innovative and easy-to-use realtime software sequencer and (arguably) the most exciting and innovative and easy-to-use computer programming language out there. I know the love will flow both ways (btw, they're having problems with the mac os x port, hint, hint... ) I rushed to update http://wiki.python.org/moin/PythonInMusic but lo and behold someone beat me to it. I love it when that happens especially since I kicked off the earliest version of that page back in the day ;-) Some important links: Ableton Live is at http://www.ableton.com The forum thread announcing the api is at : http://www.ableton.com/forum/viewtopic.php?t=66118&postdays=0&postorder=asc&start=0 And last but not least the Live python SDK sits at http://www.liveapi.org/ Enjoy! Alia K -- http://mail.python.org/mailman/listinfo/python-list
Re: Which Python web framework is most like Ruby on Rails?
In http://subway.python-hosting.com/ticket/216 Peter Mere writes: "Subway has a lot of ideas TurboGears lacks. Everything from the @client ajax-in-python to CherryFlow. On technical merits, Subway should eat TurboGears' dinner. But we all know market outcomes are not based on technical merit. They're based on marketing. And as an open-source marketing project, TurboGears is eating Subway's dinner. And not just those low-fat subs, either - they're pouring it on thick! It is a truism that python is the language with more web app frameworks than keywords. Of this LaoTse wrote, "Conquest is a method of union. The smaller submits to the larger to obtain custom. The larger submits to the smaller to obtain service. If both would endure, both must submit." So it's time to stop trumpeting subway as "the best framework", and by uniting with TurboGears definitively create the best web app framework. The combination of the two would become an unstoppable juggernaut of python mindshare, and all the rest of the frameworks would either componentize or be dismissed as toys. If Subway does not unite, chaos will continue in python web app land, and ruby will become ascendant. This is more than a critical issue - don't dismiss it without understanding that doing so will have severe repercussions for subway (and by a process of horsshoe nail extraction, the whole world!)" All I can say is HEAR HEAR AK -- http://mail.python.org/mailman/listinfo/python-list
Re: improving pypi / setuptools
It is still early days for setuptools... still, we are lagging behind the ruby world in this regards. I definitely agree with you that the so-called megaframeworks need to be packaged better (especially all versions of the different components being updated on almost daily basis). AK -- http://mail.python.org/mailman/listinfo/python-list
improving pypi / setuptools
Due to a recent site overhaul by sourceforge certain files linked to from pypi won't install. For example, in trying to install all the dependencies for the subway web framework, I got this kind of error: . . . Searching for CherryPy>=2.1.0-rc1 Reading http://www.python.org/pypi/CherryPy/ Reading http://www.cherrypy.org Reading http://sourceforge.net/project/showfiles.php?group_id=56099 Best match: CherryPy 2.1.0-rc2 Downloading http://prdownloads.sourceforge.net/cherrypy/CherryPy-2.1.0-rc2.tar.g z?download error: Unexpected HTML page found at http://prdownloads.sourceforge.net/cherrypy /CherryPy-2.1.0-rc2.tar.gz?download Unless I am mistaken, this seems like a problem due to a dependency on screen scraping to get the location of the file. If this is the case, IMHO it is too fragile. What ideas do people out there have for making the installation of python module more reliable? Just to get the ball rolling, here are two suggestions: [1] If setuptools encounters an error in retrieving a dependency from its registered location (off python.org), it falls back to a canonical URL that serves it from pypi, an email is then dispatched to uploader / module maintainer, etc... So I when I register the location of my python module on pypi, I also upload a copy to pypi. This can serve as a permanent repository for all python modules and versions, but because it is only used as a fallback server, bandwidth use and therefore cost will be manageable. [2] Create an alia for easy_install.py : egg.py This not really a reliability tweek, its just a memory tweek. The advantage of this is to save the installer a few keystrokes, and to relate the installation command to that which is installed. Furthermore, I won't be jealous of ruby's little "gem" command anymore. (-; AK -- http://mail.python.org/mailman/listinfo/python-list
Re: survey of modules to be added to stdlib
It would be amazing if you could add the feature to do combo package installs like : - a scientific python combo (which would include scipy, numarray, Numpy, plotting libs, etc) - an AI python combo(orange, constraint programming modules, agent libs, etc) - a game development python combo (pygame, pysonic, 3D libs, etc...) - a musical python combo you get the idea (-; AK -- http://mail.python.org/mailman/listinfo/python-list
Re: generating audio signals
http://www.python.org/moin/PythonInMusic -- http://mail.python.org/mailman/listinfo/python-list
survey of modules to be added to stdlib
This is an informal survey to gauge the community's interest in adding popular modules to the python standard library. In no particular order, here's my personal list of favourites: path.py - Jason Orendorff elementree - Fredrik Lundh ctypes - Thomas Heller psyco - Armin Rigo IPython - Fernando Pérez BTW is there an official set of conditions that have to be met before a module can be accepted into the stdlib? AK -- http://mail.python.org/mailman/listinfo/python-list
adding a path module to stdlib
This may have been discussed ad nauseaum before, but every time I use os.path manipulations I miss something like Jason Orrendorf's path.py being in the standard library. [http://www.jorendorff.com/articles/python/path/] Ruby has it: require 'pathname' path = Pathname.new("/tmp/f.log") What are we missing to make something similarly useful happen for python? AK -- http://mail.python.org/mailman/listinfo/python-list
Re: Sound and music libraries?
http://www.python.org/moin/PythonInMusic AK -- http://mail.python.org/mailman/listinfo/python-list
Re: why no python setup.py uninstall?
I'm guessing that this is a non-issue for most people (-; -- http://mail.python.org/mailman/listinfo/python-list
why no python setup.py uninstall?
If the cannonical way to install a python app is python setup.py install one would expect the following to uninstall it python setup.py uninstall However, distutils doesn't automatically take care of that for you. Is this by design? Or is this something that could/should be addressed in a future version of distutils? The reason this came up for me is that I have a scheduled script that downloads the cvs/svn versions of certain python apps (w/extensions), and then builds and installs them to site-packages automatically by 'python setup.py install'. Just to be extra clean about it, I would prefer to uninstall first, and then install. AK PS: I also have to manually uninstall apps from Mac OS X panther: searching for .plists etc. I wonder if that was deliberate or otherwise too... hmm... -- http://mail.python.org/mailman/listinfo/python-list