473,545 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python's annoyance.

Expand|Select|Wrap|Line Numbers
  1. PEP 292 adds a Template class to the string module that uses "$" to
  2. indicate a substitution. Template is a subclass of the built-in
  3. Unicode type, so the result is always a Unicode string:
  4.  import string
  5.  t = string.Template('$page: $title')
  6.  t.substitute({'page':2, 'title': 'The Best of Times'})
  7. u'2: The Best of Times'
  8.  
  9. If a key is missing from the dictionary, the substitute method will
  10. raise a KeyError. There's also a safe_substitute method that ignores
  11. missing keys:
  12.  t = string.SafeTemplate('$page: $title')
  13.  t.safe_substitute({'page':3})
  14. u'3: $title'
  15.  

Expand|Select|Wrap|Line Numbers
  1. As a slightly more realistic example, the following decorator
  2. checks that the supplied argument is an integer:
  3.  
  4. def require_int (func):
  5. def wrapper (arg):
  6. assert isinstance(arg, int)
  7. return func(arg)
  8.  
  9. return wrapper
  10.  
  11. @require_int
  12. def p1 (arg):
  13. print arg
  14.  
  15. @require_int
  16. def p2(arg):
  17. print arg*2
  18.  


Serioulsy, one of python's main selling points is its elegant syntax,
non perl like, non C like. If it can't live up to it. I guess i might
as well use perl or ruby or server side javascript.

how annoying.
Jul 18 '05 #1
8 1307
caroundw5h wrote:
Serioulsy, one of python's main selling points is its elegant syntax,
non perl like, non C like. If it can't live up to it. I guess i might
as well use perl or ruby or server side javascript.


While I'm not a fan of the decorator syntax or the $syntax in
string.Template , you're not forced to use either of these:

Standard Python formatting vs string.Template formatting:
------------------------------------------------------------
string.Template ('$page: $title').substi tute(dict(page= 2, title='The Best of Times'))
'2: The Best of Times' '%(page)s: %(title)s' % dict(page=2, title='The Best of Times') '2: The Best of Times'

Post-function syntax vs. decorator syntax:
------------------------------------------------------------ def require_int(fun c): .... def wrapper(arg):
.... assert isinstance(arg, int)
.... return func(arg)
.... return wrapper
.... @require_int .... def p1(arg):
.... print arg
.... def p2(arg): .... print arg
.... p2 = require_int(p2)
p1(1) 1 p1('1') Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
File "<interacti ve input>", line 3, in wrapper
AssertionError p2(1) 1 p2('1')

Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
File "<interacti ve input>", line 3, in wrapper
AssertionError
Personally, I never saw the gain of the string.Template class -- in most
of my uses I can write code that's just as clear and concise using
%-style formatting. I do however make use of the decorator syntax --
it's nice to have things like classmethod indicated at the top of the
function instead of at the bottom.

Steve
Jul 18 '05 #2
caroundw5h wrote:
Serioulsy, one of python's main selling points is its elegant syntax,
non perl like, non C like. If it can't live up to it. I guess i might
as well use perl or ruby or server side javascript.


I think it's still better than perl or ruby, but anyway,
here are your two examples in boo: http://boo.codehaus.org/

1.
t = "${page}: ${title}"

2.
def p2(arg as int):
print arg*2

Probably you'll be able to do similar things in Python 3.0, too, but
that is years away.
Jul 18 '05 #3
On Wed, 24 Nov 2004 10:47:11 -0800, caroundw5h wrote:
Serioulsy, one of python's main selling points is its elegant syntax, non
perl like, non C like. If it can't live up to it. I guess i might as well
use perl or ruby or server side javascript.


There's a rather large difference between

@require_int
@dynamic_dispat ch('add', int)
@author("Steven Q. Dude")
@whack_a_hacka_ ding_dong(with_ fruit=True, sledgehammer=5)
@register(zodb & mysql_storage)
def increment(i):
return i + 1

and

@require_int
def advanceCounter( index):
objToAdvance = objStorageArray[index]
obj.prep(prepTy pe = ADVANCE)
try:
obj.advance(1)
except IOError:
obj.lazyAdvance (1)

return obj

One guess which is the more common case.

I can make any language look bad by focusing on the nasty cases. I mean,
why not complain about

p=lambda n,s=2,np=lambda s,f:reduce(lamb da x,y:x and y,map(lambda
x,y:x%y,[s+1]*(s/2),range(2,s/2+2)))and s+1or f(s+1,f
):n>0and[s]+p(n-1,np(s,np))or[]

(http://groups.google.com/groups?hl=e...99%40tim#link3)

Obviously, any language that allows that isn't clean.

You have to take the gestalt of real code written in the language, not
contrive cases focusing on the worst part of the language. Python's near
the top of the heap. Javascript would fare better if so many inexperienced
programmers weren't bashing away in it, and if so many extant programs in
it weren't bogged down by browser checking code brought on by wildly
divergent DOMs.

If you insist on using such faulty metrics, though, hie thee hence to
Perl, that epitome of elegant syntax and more power to you. I leave you
with this closing benediction in honor of the might syntactical
cleanliness of Perl:

@==sort@$=map$_ .shift@=,@@for@ @=/\pL|,/g;$_=@$[$_]

(http://perlgolf.sourceforge.net/cgi-...rtem.cgi?id=11)
(http://perlgolf.sourceforge.net/TPR/0/6/)

Jul 18 '05 #4
Hi !

Just for fun (?) :

A qsort in "J-programming-language" :

qsort =: ]`(($:@:((}.<:{. )#}.)),{.,($:@: ((}.>{.)#}.)))@ .(*@#)

More infos here : http://en.wikipedia.org/wiki/J_programming_language


Jul 18 '05 #5
caroundw5h wrote:
Serioulsy, one of python's main selling points is its elegant syntax,
non perl like, non C like. If it can't live up to it. I guess i might
as well use perl or ruby or server side javascript.
how annoying.


In fact, I find the decorator syntax awful. But I know this was already
discussed to a large extent, so there must be a very good reason to use @.
Which I totally miss, but hey.
--
Giovanni Bajo
Jul 18 '05 #6
"Giovanni Bajo" <no***@sorry.co m> wrote in message news:<iB******* **************@ twister2.libero .it>...
In fact, I find the decorator syntax awful. But I know this was already
discussed to a large extent, so there must be a very good reason to use @.
Which I totally miss, but hey.


Well, there is very good reason: Guido likes it!

<no further comments ...>

Michele Simionato
Jul 18 '05 #7
On Wed, 24 Nov 2004 23:02:01 +0100, Michel Claveau - abstraction
méta-galactique non triviale en fuite perpétuelle. wrote:
Hi !

Just for fun (?) :

A qsort in "J-programming-language" :

qsort =: ]`(($:@:((}.<:{. )#}.)),{.,($:@: ((}.>{.)#}.)))@ .(*@#)

More infos here : http://en.wikipedia.org/wiki/J_programming_language

Well, as J is mostly APL dressed up in ASCII, it's bound to be readable...
I've found it convenient for some numerical problems though. And getting
my head around it definitely broadened my perspective - so much in fact,
that I mostly run screaming from most APL-like languages since I found
Python :-)

--
Christopher

Jul 18 '05 #8
Michele Simionato <mi************ ***@gmail.com> wrote:

In fact, I find the decorator syntax awful. But I know this was
already
discussed to a large extent, so there must be a very good reason to
use @.
Which I totally miss, but hey.


Well, there is very good reason: Guido likes it!

<no further comments ...>

Right.

Giovanni Bajo
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2123
by: John Fitzsimons | last post by:
Hi, I want to search an ordered text file and list web links such as : Input like ; A Folder Freeware ftp://ftp.eunet.bg/pub/simtelnet
12
2818
by: Don Bruder | last post by:
A week or two ago, I asked here about porting Python to C. Got some good answers (Note 1) but now I've got another question. Actually, more a request for clarification of a topic that both the Python tutorial and docs leave a touch murky to my understanding. Dictionaries/"dict" types... Am I understanding/interpreting correctly when I go...
699
33348
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it...
5
2504
by: Paradox | last post by:
In general I love Python for text manipulation but at our company we have the need to manipulate large text values stored in either a SQL Server database or text files. This data is stored in a "text" field type and is definitely not unicode though it is often very strange text since it is either OCR or some kinda electronic file extraction....
17
1620
by: hanumizzle | last post by:
I have used Perl for a long time, but I am something of an experimental person and mean to try something new. Most of my 'work' with Vector Linux entails the use of Perl (a bit of a misnomer as it is not now a paid position -- I am not yet even out of K-12), and there a lot of things I love about it. I can look past a number of misfeatures in...
42
2451
by: redefined.horizons | last post by:
I'm coming from a Java background, so please don't stone me... I see that Python is missing "interfaces". The concept of an interface is a key to good programming design in Java, but I've read that they aren't really necessary in Python. I am wondering what technique I can use in Python to get the same benefits to a program design that I...
267
10530
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at http://www.artima.com/weblogs/viewpost.jsp?thread=147358
38
1870
by: flifus | last post by:
Hi all. I'm learning python these days. I'm going to use this thread to post, from time to time, my annoyances with python. I hope someone will clarify things to me where I have misunderstood them. Annoyances: 1. Underscores! What's the deal with that? Especially those double underscores. The best answer I read on this is that the double...
11
1332
by: wongjoekmeu | last post by:
Are there any completely free developent tools for python scripts like IDLE. I have used IDLE , but I want to try out others also. I saw stuff like PyCrust, but I don't see that it can run the script as well. Thanks, RR
0
7653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7411
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5965
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5322
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4942
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3444
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1871
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.