Re: Custom string joining
On May 9, 1:25 pm, Claudiu Popa wrote: > Hello Karim, > > > You just have to implement __str__() python special method for your > > "custom_objects". > > Regards > > Karim > >> Cheers, > >> Chris > >> -- > >>http://rebertia.com > > I already told in the first post that I've implemented __str__ function, but > it doesn't > seems to be automatically called. > For instance, the following example won't work: > > >>> class a: > > def __init__(self, i): > self.i = i > def __str__(self): > return "magic_function_{}".format(self.i)>>> t = a(0) > >>> str(t) > 'magic_function_0' > >>> "".join([t]) > > Traceback (most recent call last): > File "", line 1, in > "".join([t]) > TypeError: sequence item 0: expected str instance, a found > > -- > Best regards, > Claudiu The built-in str join() method just doesn't do the conversion to string automatically -- instead it assume it has been given a sequence of string. You can achieve the effect you want by modifying the optimized version of my earlier post that @Ian Kelly submitted. def join(seq, sep): it = iter(seq) sep = str(sep) try: first = it.next() except StopIteration: return [] def add_sep(r, v): r += [sep, str(v)] return r return ''.join(reduce(add_sep, it, [str(first)])) class A: def __init__(self, i): self.i = i def __str__(self): return "magic_function_{}".format(self.i) lst = [A(0),A(1),A('b'),A(3)] print repr( join(lst, '|') ) Output: 'magic_function_0|magic_function_1|magic_function_b|magic_function_3' -- http://mail.python.org/mailman/listinfo/python-list
Re: Custom string joining
On May 7, 5:31 am, Claudiu Popa wrote: > Hello Python-list, > > I have an object which defines some methods. I want to join a list or > an iterable of those objects like this: > > new_string = "|".join(iterable_of_custom_objects) > > What is the __magic__ function that needs to be implemented for > this case to work? I though that __str__ is sufficient but it doesn't > seems to > work. Thanks in advance. > > PC Instead of join() here's a function that does something similar to what the string join() method does. The first argument can be a list of any type of objects and the second separator argument can likewise be any type. The result is list of the various objects. (The example usage just uses a list of string objects and separator to illustrate what it does.) def tween(seq, sep): return reduce(lambda r,v: r+[sep,v], seq[1:], seq[:1]) lst = ['a','b','c','d','e'] print tween(lst, '|') print ''.join(tween(lst, '|')) Output: ['a', '|', 'b', '|', 'c', '|', 'd', '|', 'e'] a|b|c|d|e It could be made a little more memory efficient by applying the itertools module's islice() generator to the first 'seq' argument passed to reduce(): def tween(seq, sep): return reduce(lambda r,v: r+[sep,v], itertools.islice(seq,1,None), seq[:1]) -- Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.7 released
On Jul 5, 5:53 pm, David Robinow wrote: > On Mon, Jul 5, 2010 at 8:15 PM, Steven > D'Aprano wrote: > > On Mon, 05 Jul 2010 12:59:00 -0700, Martineau wrote: > > >> I'd like to view the contents of the help file without actually > >> installing the release which would wipe out any currently installed > >> version (I'm one of those rare people who actually reads manuals > >> *before* using or installing most things.) > ... > > Are there any Windows users out there who can confirm that the installer > > does or doesn't leave existing versions in place? > > The installer does leave existing versions in place. I have no idea > what the OP is referring to. Some clarification. I meant installed 2.7 on top of 2.6.x. Doing so would have interfered with the currently installed version because I always install Python in the same directory, one named just "Python", to minimize the number of changes I have to make to to other parts of the system. Some trivial examples are desktop shortcuts I've set up which point to the commandline version of the interpreter and another for the help file. I also believe the Windows installer makes registry changes that also involve paths to the currently installed version, which again, is something I wanted to avoid until I'm actually ready to commit to upgrading. If there are better ways on Windows to accomplish this, I'd like to hear about them. I suppose I could use hardlinks or junctions but they're not well supported on most versions of Windows. BTW, my original problem -- getting a copy of the Windows format compiled help file fro v2/7 without installing it has been taken care by suggestions from other, so this discussion is starting to way off- topic... Thanks, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.7 released
On Jul 5, 1:31 pm, Alexander Kapps wrote: > Martineau wrote: > > Perhaps it's hidden somewhere, but I couldn't find the .chm help file > > in the python-2.7.msi file using 7-zip, nor saw anything that looked > > like a Doc folder embedded within it -- so I doubt installing it on a > > Windows machine would work any better. > > I don't know much about the .msi format or how 7-Zip handles it, but > on my XP box, 7-Zip lists a "python" sub-archive (a 7-Zip > "compound"). Within is the python27.chm My mistake -- you're quite right the .chm *is* in the .msi where you indicated. FWIW I actually did look in that sub-section before posting yet somehow missed it. Sorry about that and thanks to all involved for your help. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.7 released
On Jul 5, 1:12 am, Benjamin Kaplan wrote: > On Sun, Jul 4, 2010 at 7:58 PM, John Machin wrote: > > On Jul 5, 12:27 pm, Martineau wrote: > >> On Jul 4, 8:34 am, Benjamin Peterson wrote: > > >> > On behalf of the Python development team, I'm jocund to announce the > >> > second > >> > release candidate of Python 2.7. > > >> > Python 2.7 will be the last major version in the 2.x series. However, it > >> > will > >> > also have an extended period of bugfix maintenance. > > >> > 2.7 includes many features that were first released in Python 3.1. The > >> > faster io > >> > module, the new nested with statement syntax, improved float repr, set > >> > literals, > >> > dictionary views, and the memoryview object have been backported from > >> > 3.1. Other > >> > features include an ordered dictionary implementation, unittests > >> > improvements, a > >> > new sysconfig module, auto-numbering of fields in the str/unicode format > >> > method, > >> > and support for ttk Tile in Tkinter. For a more extensive list of > >> > changes in > >> > 2.7, seehttp://doc.python.org/dev/whatsnew/2.7.htmlorMisc/NEWSin the > >> > Python > >> > distribution. > > >> > To download Python 2.7 visit: > > >> > http://www.python.org/download/releases/2.7/ > > >> > 2.7 documentation can be found at: > > >> > http://docs.python.org/2.7/ > > >> > This is a production release and should be suitable for all libraries and > >> > applications. Please report any bugs you find, so they can be fixed in > >> > the next > >> > maintenance releases. The bug tracker is at: > > >> > http://bugs.python.org/ > > >> > Enjoy! > > >> > -- > >> > Benjamin Peterson > >> > Release Manager > >> > benjamin at python.org > >> > (on behalf of the entire python-dev team and 2.7's contributors) > > >> Benjamin (or anyone else), do you know where I can get the Compiled > >> Windows Help file -- python27.chm -- for this release? In the past > >> I've been able to download it from the Python web site, but have been > >> unable to locate it anywhere for this new release. I can't build it > >> myself because I don't have the Microsoft HTML help file compiler. > > >> Thanks in advance. > > > If you have a Windows box, download the .msi installer for Python 2.7 > > and install it. The chm file will be in C:\Python27\Doc (if you choose > > the default installation directory). Otherwise ask a friendly local > > Windows user for a copy. > > -- > > Or you can just use 7-zip or cabextract on the MSi. Saves you from > having to uninstall it later, and it works on non-Windows machines. Perhaps it's hidden somewhere, but I couldn't find the .chm help file in the python-2.7.msi file using 7-zip, nor saw anything that looked like a Doc folder embedded within it -- so I doubt installing it on a Windows machine would work any better. I'd like to view the contents of the help file without actually installing the release which would wipe out any currently installed version (I'm one of those rare people who actually reads manuals *before* using or installing most things.) So my original question stands -- where can one get the Windows Help file for v2.7? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.7 released
On Jul 4, 8:34 am, Benjamin Peterson wrote: > On behalf of the Python development team, I'm jocund to announce the second > release candidate of Python 2.7. > > Python 2.7 will be the last major version in the 2.x series. However, it will > also have an extended period of bugfix maintenance. > > 2.7 includes many features that were first released in Python 3.1. The faster > io > module, the new nested with statement syntax, improved float repr, set > literals, > dictionary views, and the memoryview object have been backported from 3.1. > Other > features include an ordered dictionary implementation, unittests > improvements, a > new sysconfig module, auto-numbering of fields in the str/unicode format > method, > and support for ttk Tile in Tkinter. For a more extensive list of changes in > 2.7, seehttp://doc.python.org/dev/whatsnew/2.7.htmlor Misc/NEWS in the Python > distribution. > > To download Python 2.7 visit: > > http://www.python.org/download/releases/2.7/ > > 2.7 documentation can be found at: > > http://docs.python.org/2.7/ > > This is a production release and should be suitable for all libraries and > applications. Please report any bugs you find, so they can be fixed in the > next > maintenance releases. The bug tracker is at: > > http://bugs.python.org/ > > Enjoy! > > -- > Benjamin Peterson > Release Manager > benjamin at python.org > (on behalf of the entire python-dev team and 2.7's contributors) Benjamin (or anyone else), do you know where I can get the Compiled Windows Help file -- python27.chm -- for this release? In the past I've been able to download it from the Python web site, but have been unable to locate it anywhere for this new release. I can't build it myself because I don't have the Microsoft HTML help file compiler. Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list