Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

syntax for code blocks

 

 

First page Previous page 1 2 Next page Last page  View All Python python RSS feed   Index | Next | Previous | View Threaded


kiuhnm03.4t.yahoo.it at mail

Apr 27, 2012, 4:24 AM

Post #1 of 49 (3108 views)
Permalink
syntax for code blocks

I'd like to change the syntax of my module 'codeblocks' to make it more
pythonic.

Current Syntax:

with res << func(arg1) << 'x, y':
print(x, y)

with res << func(arg1) << block_name << 'x, y':
print(x, y)

New Syntax:

with res == func(arg1) .taking_block (x, y):
print(x, y)

with res == func(arg1) .taking_block (x, y) as block_name:
print(x, y)

The full form is equivalent to

def anon_func(x, y):
print(x, y)
res = func(arg1, block_name = anon_func)

Suggestions?

Kiuhnm
--
http://mail.python.org/mailman/listinfo/python-list


steve+comp.lang.python at pearwood

Apr 27, 2012, 7:09 AM

Post #2 of 49 (3046 views)
Permalink
Re: syntax for code blocks [In reply to]

On Fri, 27 Apr 2012 13:24:35 +0200, Kiuhnm wrote:

> I'd like to change the syntax of my module 'codeblocks' to make it more
> pythonic.
>
> Current Syntax:
>
> with res << func(arg1) << 'x, y':
> print(x, y)
>
> with res << func(arg1) << block_name << 'x, y':
> print(x, y)


I'm sorry, I don't see how this is a code block. Where is the code in the
block, and how can you pass it to another object to execute it?



--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


kiuhnm03.4t.yahoo.it at mail

Apr 27, 2012, 8:03 AM

Post #3 of 49 (3047 views)
Permalink
Re: syntax for code blocks [In reply to]

On 4/27/2012 16:09, Steven D'Aprano wrote:
> On Fri, 27 Apr 2012 13:24:35 +0200, Kiuhnm wrote:
>
>> I'd like to change the syntax of my module 'codeblocks' to make it more
>> pythonic.
>>
>> Current Syntax:
>>
>> with res << func(arg1) << 'x, y':
>> print(x, y)
>>
>> with res << func(arg1) << block_name << 'x, y':
>> print(x, y)
>
>
> I'm sorry, I don't see how this is a code block. Where is the code in the
> block, and how can you pass it to another object to execute it?

Maybe if you read the entire post...

Kiuhnm
--
http://mail.python.org/mailman/listinfo/python-list


steve+comp.lang.python at pearwood

Apr 27, 2012, 9:07 AM

Post #4 of 49 (3044 views)
Permalink
Re: syntax for code blocks [In reply to]

On Fri, 27 Apr 2012 17:03:19 +0200, Kiuhnm wrote:

> On 4/27/2012 16:09, Steven D'Aprano wrote:
>> On Fri, 27 Apr 2012 13:24:35 +0200, Kiuhnm wrote:
>>
>>> I'd like to change the syntax of my module 'codeblocks' to make it
>>> more pythonic.
>>>
>>> Current Syntax:
>>>
>>> with res << func(arg1) << 'x, y':
>>> print(x, y)
>>>
>>> with res << func(arg1) << block_name << 'x, y':
>>> print(x, y)
>>
>>
>> I'm sorry, I don't see how this is a code block. Where is the code in
>> the block, and how can you pass it to another object to execute it?
>
> Maybe if you read the entire post...

No, I read the entire post. It made no sense to me. Let me give one
example. You state:

The full form is equivalent to
def anon_func(x, y):
print(x, y)
res = func(arg1, block_name = anon_func)

but this doesn't mean anything to me. What's func? Where does it come
from? What's arg1? Why does something called block_NAME have a default
value of a function instead of a NAME?

How about you give an actual working example of what you mean by a code
block and how you use it?


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


ian.g.kelly at gmail

Apr 27, 2012, 10:00 AM

Post #5 of 49 (3046 views)
Permalink
Re: syntax for code blocks [In reply to]

On Fri, Apr 27, 2012 at 10:07 AM, Steven D'Aprano
<steve+comp.lang.python [at] pearwood> wrote:
> How about you give an actual working example of what you mean by a code
> block and how you use it?

He wrote a full blog post about it last week:

http://mtomassoli.wordpress.com/2012/04/20/code-blocks-in-python/
--
http://mail.python.org/mailman/listinfo/python-list


kiuhnm03.4t.yahoo.it at mail

Apr 28, 2012, 3:34 AM

Post #6 of 49 (3046 views)
Permalink
Re: syntax for code blocks [In reply to]

On 4/27/2012 18:07, Steven D'Aprano wrote:
> On Fri, 27 Apr 2012 17:03:19 +0200, Kiuhnm wrote:
>
>> On 4/27/2012 16:09, Steven D'Aprano wrote:
>>> On Fri, 27 Apr 2012 13:24:35 +0200, Kiuhnm wrote:
>>>
>>>> I'd like to change the syntax of my module 'codeblocks' to make it
>>>> more pythonic.
>>>>
>>>> Current Syntax:
>>>>
>>>> with res<< func(arg1)<< 'x, y':
>>>> print(x, y)
>>>>
>>>> with res<< func(arg1)<< block_name<< 'x, y':
>>>> print(x, y)
>>>
>>>
>>> I'm sorry, I don't see how this is a code block. Where is the code in
>>> the block, and how can you pass it to another object to execute it?
>>
>> Maybe if you read the entire post...
>
> No, I read the entire post. It made no sense to me. Let me give one
> example. You state:
>
> The full form is equivalent to
> def anon_func(x, y):
> print(x, y)
> res = func(arg1, block_name = anon_func)
>
> but this doesn't mean anything to me. What's func? Where does it come
> from? What's arg1? Why does something called block_NAME have a default
> value of a function instead of a NAME?
>
> How about you give an actual working example of what you mean by a code
> block and how you use it?

The rewriting rules are the following, where X ---> Y means that X is rewritten as Y on the fly:

1)
with res << func(args) << 'x, y':
<code>

--->

def anon_func(x, y):
<code>
res = func(args, anon_func)

2)
with res << func(args) << block_name << 'x, y':
<code>

--->

def anon_func(x, y):
<code>
res = func(args, block_name = anon_func)

That's all.
func is some function which takes a function as a positional argument or as a keyword parameter neamed block_name.

Some examples:

1)
text = "Anyone should be able to read this message!"
with ris << re.sub(r'(\w)(\w+)(\w)', string = text) << repl << 'm':
inner_word = list(m.group(2))
random.shuffle(inner_word)
_return (m.group(1) + "".join(inner_word) + m.group(3))
print(ris)

which prints (something like):

Aynnoe shluod be albe to read tihs msgseae!

2)
numbers = [random.randint(1, 100) for i in range(30)]

with sorted1 << sorted(numbers) << key << 'x':
if x <= 50:
_return(-x)
else:
_return(x)

print(sorted1)

which prints (something like):
[.50, 47, 46, 28, 28, 25, 24, 23, 21, 19, 16, 15, 14, 3, 52, 52, 53, 54, 58, 62,
63, 69, 70, 72, 74, 78, 84, 86, 90, 97]

Kiuhnm
--
http://mail.python.org/mailman/listinfo/python-list


ethan at stoneleaf

Apr 28, 2012, 1:52 PM

Post #7 of 49 (3041 views)
Permalink
Re: syntax for code blocks [In reply to]

Kiuhnm wrote:
> I'd like to change the syntax of my module 'codeblocks' to make it more
> pythonic.
>
> Current Syntax:
>
> with res << func(arg1) << 'x, y':
> print(x, y)
>
> with res << func(arg1) << block_name << 'x, y':
> print(x, y)
>
> New Syntax:
>
> with res == func(arg1) .taking_block (x, y):
> print(x, y)
>
> with res == func(arg1) .taking_block (x, y) as block_name:
> print(x, y)
>
> The full form is equivalent to
>
> def anon_func(x, y):
> print(x, y)
> res = func(arg1, block_name = anon_func)
>
> Suggestions?

I don't find either the current syntax nor the new syntax pythonic.

~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


ppearson at nowhere

Apr 29, 2012, 9:05 AM

Post #8 of 49 (3043 views)
Permalink
Re: syntax for code blocks [In reply to]

On Fri, 27 Apr 2012 13:24:35 +0200, Kiuhnm <kiuhnm03.4t.yahoo.it> wrote:
> I'd like to change the syntax of my module 'codeblocks' to make it more
> pythonic.
>
> Current Syntax:
>
> with res << func(arg1) << 'x, y':
> print(x, y)
>
> with res << func(arg1) << block_name << 'x, y':
> print(x, y)
>
> New Syntax:
>
> with res == func(arg1) .taking_block (x, y):
> print(x, y)
>
> with res == func(arg1) .taking_block (x, y) as block_name:
> print(x, y)
[snip]

Hey, guys, am I the only one here who can't even guess what
this code does? When did Python become so obscure?

--
To email me, substitute nowhere->spamcop, invalid->net.
--
http://mail.python.org/mailman/listinfo/python-list


lamialily at cleverpun

Apr 29, 2012, 9:32 AM

Post #9 of 49 (3047 views)
Permalink
Re: syntax for code blocks [In reply to]

>> Current Syntax:
>>
>> with res << func(arg1) << 'x, y':
>> print(x, y)
>>
>> with res << func(arg1) << block_name << 'x, y':
>> print(x, y)
>>
>> New Syntax:
>>
>> with res == func(arg1) .taking_block (x, y):
>> print(x, y)
>>
>> with res == func(arg1) .taking_block (x, y) as block_name:
>> print(x, y)
>[snip]
>
>Hey, guys, am I the only one here who can't even guess what
>this code does? When did Python become so obscure?
>
>--
>To email me, substitute nowhere->spamcop, invalid->net.

No, it's pretty impenetratable to me at a passing glance too. Not sure
if I'd get anywhere handtracing it, though.

~Temia
--
When on earth, do as the earthlings do.
--
http://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Apr 29, 2012, 8:30 PM

Post #10 of 49 (3056 views)
Permalink
Re: syntax for code blocks [In reply to]

Peter Pearson <ppearson [at] nowhere> writes:

> On Fri, 27 Apr 2012 13:24:35 +0200, Kiuhnm <kiuhnm03.4t.yahoo.it> wrote:

> > I'd like to change the syntax of my module 'codeblocks' to make it
> > more pythonic.

The “chained callable” style isn't very Pythonic, IMO. Even worse is the
penchant for ‘foo .bar()’, the space obscures the fact that this is
attribute access.

Far from Pythonic, this seems to be an attempt to write some other
language in Python code.

> > Current Syntax:
> >
> > with res << func(arg1) << 'x, y':
> > print(x, y)
> >
> > with res << func(arg1) << block_name << 'x, y':
> > print(x, y)
> >
> > New Syntax:
> >
> > with res == func(arg1) .taking_block (x, y):
> > print(x, y)
> >
> > with res == func(arg1) .taking_block (x, y) as block_name:
> > print(x, y)
> [snip]
>
> Hey, guys, am I the only one here who can't even guess what
> this code does? When did Python become so obscure?

No, you're not alone; I think that code is pretty poor at communicating
the intent.

AFAICT the above code is a proposal. I don't know who (other than the
original poster) thinks it's any good.

--
\ “I cannot be angry at God, in whom I do not believe.” —Simone |
`\ De Beauvoir |
_o__) |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


storchaka at gmail

Apr 29, 2012, 8:36 PM

Post #11 of 49 (3042 views)
Permalink
Re: syntax for code blocks [In reply to]

29.04.12 19:05, Peter Pearson написав(ла):
> Hey, guys, am I the only one here who can't even guess what
> this code does? When did Python become so obscure?

This isn't Python at all. It's Ruby.

--
http://mail.python.org/mailman/listinfo/python-list


mwilson at the-wire

Apr 30, 2012, 7:17 AM

Post #12 of 49 (3046 views)
Permalink
Re: syntax for code blocks [In reply to]

Ben Finney wrote:

> [ ... ] Even worse is the
> penchant for ‘foo .bar()’, the space obscures the fact that this is
> attribute access.

I like the style sometimes when it helps to break the significantly different parts out of
boilerplate:

libbnem. BN_add .argtypes = [ctypes.POINTER (BignumType), ctypes.POINTER (BignumType),
ctypes.POINTER (BignumType)]
libbnem. BN_add .restype = ctypes.c_int
libbnem. BN_add_word .argtypes = [ctypes.POINTER (BignumType), ctypes.c_ulong]
libbnem. BN_add_word .restype = ctypes.c_int

libbnem. BN_sub .argtypes = [ctypes.POINTER (BignumType), ctypes.POINTER (BignumType),
ctypes.POINTER (BignumType)]
libbnem. BN_sub .restype = ctypes.c_int
libbnem. BN_sub_word .argtypes = [ctypes.POINTER (BignumType), ctypes.c_ulong]
libbnem. BN_sub_word .restype = ctypes.c_int

(there were a lot more in the original program where those came from.) Another take-away
might be don't use boilerplate, but in the situation I didn't see a simple way to avoid it.

Mel.
--
http://mail.python.org/mailman/listinfo/python-list


kiuhnm03.4t.yahoo.it at mail

Apr 30, 2012, 8:02 AM

Post #13 of 49 (3043 views)
Permalink
Re: syntax for code blocks [In reply to]

On 4/30/2012 16:17, mwilson [at] the-wire wrote:
> Ben Finney wrote:
>
>> [ ... ] Even worse is the
>> penchant for foo .bar(), the space obscures the fact that this is
>> attribute access.
>
> I like the style sometimes when it helps to break the significantly different parts out of
> boilerplate:
>
> libbnem. BN_add .argtypes = [ctypes.POINTER (BignumType), ctypes.POINTER (BignumType),
> ctypes.POINTER (BignumType)]
> libbnem. BN_add .restype = ctypes.c_int
> libbnem. BN_add_word .argtypes = [ctypes.POINTER (BignumType), ctypes.c_ulong]
> libbnem. BN_add_word .restype = ctypes.c_int
>
> libbnem. BN_sub .argtypes = [ctypes.POINTER (BignumType), ctypes.POINTER (BignumType),
> ctypes.POINTER (BignumType)]
> libbnem. BN_sub .restype = ctypes.c_int
> libbnem. BN_sub_word .argtypes = [ctypes.POINTER (BignumType), ctypes.c_ulong]
> libbnem. BN_sub_word .restype = ctypes.c_int
>
> (there were a lot more in the original program where those came from.) Another take-away
> might be don't use boilerplate, but in the situation I didn't see a simple way to avoid it.
>
> Mel.

BignumTypePtr = ctypes.POINTER(BignumType)

for op, op_word in ((libbnem.BN_add, libbnem.BN_add_word),
(libbnem.BN_sub, libbnem.BN_sub_word)):
op.argtypes = [BignumTypePtr] * 3
op_word.argtypes = [BignumTypePtr, ctypes.c_ulong]
op.restype = op_word.restype = ctypes.c_int

Kiuhnm
--
http://mail.python.org/mailman/listinfo/python-list


kiuhnm03.4t.yahoo.it at mail

Apr 30, 2012, 8:24 AM

Post #14 of 49 (3044 views)
Permalink
Re: syntax for code blocks [In reply to]

On 4/30/2012 17:02, Kiuhnm wrote:
> On 4/30/2012 16:17, mwilson [at] the-wire wrote:
>> Ben Finney wrote:
>>
>>> [ ... ] Even worse is the
>>> penchant for foo .bar(), the space obscures the fact that this is
>>> attribute access.
>>
>> I like the style sometimes when it helps to break the significantly
>> different parts out of
>> boilerplate:
>>
>> libbnem. BN_add .argtypes = [ctypes.POINTER (BignumType),
>> ctypes.POINTER (BignumType),
>> ctypes.POINTER (BignumType)]
>> libbnem. BN_add .restype = ctypes.c_int
>> libbnem. BN_add_word .argtypes = [ctypes.POINTER (BignumType),
>> ctypes.c_ulong]
>> libbnem. BN_add_word .restype = ctypes.c_int
>>
>> libbnem. BN_sub .argtypes = [ctypes.POINTER (BignumType),
>> ctypes.POINTER (BignumType),
>> ctypes.POINTER (BignumType)]
>> libbnem. BN_sub .restype = ctypes.c_int
>> libbnem. BN_sub_word .argtypes = [ctypes.POINTER (BignumType),
>> ctypes.c_ulong]
>> libbnem. BN_sub_word .restype = ctypes.c_int
>>
>> (there were a lot more in the original program where those came from.)
>> Another take-away
>> might be don't use boilerplate, but in the situation I didn't see a
>> simple way to avoid it.
>>
>> Mel.
>
> BignumTypePtr = ctypes.POINTER(BignumType)
>
> for op, op_word in ((libbnem.BN_add, libbnem.BN_add_word),
> (libbnem.BN_sub, libbnem.BN_sub_word)):
> op.argtypes = [BignumTypePtr] * 3
> op_word.argtypes = [BignumTypePtr, ctypes.c_ulong]
> op.restype = op_word.restype = ctypes.c_int

On second thought, BignumPtrType is probably the right name.

Kiuhnm
--
http://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Apr 30, 2012, 7:40 PM

Post #15 of 49 (3028 views)
Permalink
Re: syntax for code blocks [In reply to]

mwilson [at] the-wire writes:

> Another take-away might be don't use boilerplate, but in the situation
> I didn't see a simple way to avoid it.

It seems we agree, then, that avoiding boilerplate code is preferable to
writing bad boilerplate code.

--
\ “Computer perspective on Moore's Law: Human effort becomes |
`\ twice as expensive roughly every two years.” —anonymous |
_o__) |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


wuwei23 at gmail

Apr 30, 2012, 8:27 PM

Post #16 of 49 (3029 views)
Permalink
Re: syntax for code blocks [In reply to]

On Apr 30, 2:05am, Peter Pearson <ppear...@nowhere.invalid> wrote:
> Hey, guys, am I the only one here who can't even guess what
> this code does? When did Python become so obscure?

Thankfully it hasn't. The most Pythonic way to pass around a code
block is still to use a function.

--
http://mail.python.org/mailman/listinfo/python-list


kiuhnm03.4t.yahoo.it at mail

May 1, 2012, 7:18 AM

Post #17 of 49 (3024 views)
Permalink
Re: syntax for code blocks [In reply to]

On 5/1/2012 5:27, alex23 wrote:
> On Apr 30, 2:05 am, Peter Pearson<ppear...@nowhere.invalid> wrote:
>> Hey, guys, am I the only one here who can't even guess what
>> this code does? When did Python become so obscure?
>
> Thankfully it hasn't. The most Pythonic way to pass around a code
> block is still to use a function.

"Most Pythonic" doesn't mean better, unfortunately.

For instance, assume that you want to write a function that accepts a
dictionary of callbacks:
func(some_args, callbacks)

Pythonic way
------------

def when_odd(n):
pass

def when_prime(n):
pass

def before_check():
pass

def after_check():
pass

func(some_args, {'when_odd' : when_odd,
'when_prime' : when_prime,
'before_check' : before_check,
'after_check' : after_check})

def when_prime(n):
pass

def when_perfect(n):
pass

def before_reduction()
pass

def after_reduction():
pass

func(some_args, {'when_prime' : when_prime,
'when_perfect' : when_perfect,
'before_reduction' : before_reduction,
'after_reduction' : after_reduction})

My way
------

with func(some_args) << ':dict':
with when_odd as 'n':
pass
with when_prime as 'n':
pass
with before_check as '':
pass
with after_check as '':
pass

with func(some_args) << ':dict':
with when_prime as 'n':
pass
with when_perfect as 'n':
pass
with before_reduction as '':
pass
with after_reduction as '':
pass

Kiuhnm
--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

May 1, 2012, 7:33 AM

Post #18 of 49 (3022 views)
Permalink
Re: syntax for code blocks [In reply to]

On Wed, May 2, 2012 at 12:18 AM, Kiuhnm
<kiuhnm03.4t.yahoo.it [at] mail> wrote:
> "Most Pythonic" doesn't mean better, unfortunately.
>
> For instance, assume that you want to write a function that accepts a
> dictionary of callbacks:
> func(some_args, callbacks)
>
> Pythonic way
> ------------
>
> def when_odd(n):
> pass
>
> def when_prime(n):
> pass
>
> def before_check():
> pass
>
> def after_check():
> pass
>
> func(some_args, {'when_odd' : when_odd,
> 'when_prime' : when_prime,
> 'before_check' : before_check,
> 'after_check' : after_check})

My way:

func(some_args, {'when_odd': lambda: 1,
'when_prime': lambda: 2,
'before_check': lambda: 3,
'after_check': lambda: 4})

In a language where lambda isn't restricted to a single expression (eg
Javascript, Pike), this works even better, but it's still plausible
for many situations.

ChrisA
--
http://mail.python.org/mailman/listinfo/python-list


steve+comp.lang.python at pearwood

May 1, 2012, 8:11 AM

Post #19 of 49 (3026 views)
Permalink
Re: syntax for code blocks [In reply to]

On Tue, 01 May 2012 16:18:03 +0200, Kiuhnm wrote:

> "Most Pythonic" doesn't mean better, unfortunately.

Perhaps. But this example is not one of those cases.


> For instance, assume that you want to write a function that accepts a
> dictionary of callbacks:
> func(some_args, callbacks)
>
> Pythonic way
> ------------
>
> def when_odd(n):
> pass
[snip multiple function definitions]

> func(some_args, {'when_odd' : when_odd,
> 'when_prime' : when_prime,
> 'before_check' : before_check,
> 'after_check' : after_check})

> My way
> ------
>
> with func(some_args) << ':dict':
> with when_odd as 'n':
> pass
> with when_prime as 'n':
> pass


If you actually try that, you will see that it cannot work. You get:

SyntaxError: can't assign to literal


Have you actually tried to use these code blocks of yours? I asked you
for a *working* example earlier, and you replied with examples that
failed with multiple NameErrors and no hint as to how to fix them. And
now you give an example that fails with SyntaxError. I'm reluctantly
coming to the conclusion that these "code blocks" of yours simply do not
work.


> with before_check as '':
> pass
> with after_check as '':
> pass

You have a bug in one or more of those callbacks.

Of course you do -- all non-trivial software has bugs. The question is,
how are you going to find it? You can't unit-test the individual
callbacks, because they don't exist in a form that can be tested.

So in this case, even though Python is slightly more verbose, and forces
you to have the discipline of writing named functions ahead of time, this
is actually a *good* thing because it encourages you to test them.

If the callbacks are trivial functions, the Pythonic way is to use
lambdas:

func(some_args, {'when_odd': lambda n: n-1,
'when_prime': lambda n: n**2 - 1,
...})

although I would discourage that unless they are *really* trivial. But
for callbacks of any complexity, they will need to be tested, otherwise
how do you know they do what you want them to do?

Your code blocks make unit testing of the callbacks impossible, and for
that reason the Pythonic way is better.


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

May 1, 2012, 8:28 AM

Post #20 of 49 (3021 views)
Permalink
Re: syntax for code blocks [In reply to]

On Wed, May 2, 2012 at 1:11 AM, Steven D'Aprano
<steve+comp.lang.python [at] pearwood> wrote:
> So in this case, even though Python is slightly more verbose, and forces
> you to have the discipline of writing named functions ahead of time, this
> is actually a *good* thing because it encourages you to test them.
>
> If the callbacks are trivial functions, the Pythonic way is to use
> lambdas:
>
> func(some_args, {'when_odd': lambda n: n-1,
> 'when_prime': lambda n: n**2 - 1,
> ...})
>
> although I would discourage that unless they are *really* trivial. But
> for callbacks of any complexity, they will need to be tested, otherwise
> how do you know they do what you want them to do?

Agreed. At work, I'm currently shoveling through a pile of Javascript
code (about 500KB of it, one file) that largely looks like this:

FOO={}
FOO.bar={
init:function() {
// ... initialization code
},
quux:function(a,b) {
//code for this function
},
ajaxrequest:function() {
var blah,blah;
blah;
return {
foo:function() {
blah;
}
};
}(),
bleh:blah
}

It's all anonymous functions assigned to member variables. It's not
easy to debug, and refactoring the code into something simpler is a
weeks-long job. I know it is because I've already spent two on it. Oh,
and notice how ajaxrequest isn't actually declaring a function at all,
it's calling a function and using its return value? Important
information like that is buried away instead of being in the function
signature. Plus, to make matters worse, many of the function and
object names are reused, so FOO.bar.ajaxrequest.foo() is completely
different from FOO.something.else.foo() and both have just
"foo:function()" as their signature.

Write your code out of line unless it REALLY wants to be inline. Name
the functions unless they're trivial.

And yes, I did say that I would do them as in-line lambdas. I don't
really see much value in the callback system you have here unless
either they ARE that trivial, or it's patently obvious that they need
to be functions, so I still stand by what I said.

ChrisA
--
http://mail.python.org/mailman/listinfo/python-list


kiuhnm03.4t.yahoo.it at mail

May 1, 2012, 10:07 AM

Post #21 of 49 (3021 views)
Permalink
Re: syntax for code blocks [In reply to]

On 5/1/2012 17:11, Steven D'Aprano wrote:
>> My way
>> ------
>>
>> with func(some_args)<< ':dict':
>> with when_odd as 'n':
>> pass
>> with when_prime as 'n':
>> pass
>
>
> If you actually try that, you will see that it cannot work. You get:
>
> SyntaxError: can't assign to literal

If you had read the module's docstring you would know that the public
version uses
with when_odd << 'n':
pass

> Have you actually tried to use these code blocks of yours? I asked you
> for a *working* example earlier, and you replied with examples that
> failed with multiple NameErrors and no hint as to how to fix them. And
> now you give an example that fails with SyntaxError.

The examples I gave you work perfectly. It's clear that you don't even
have the vaguest idea of how my module works or, otherwise, you'd know
what you're doing wrong. Again, the module's docstring is your friend.

> You have a bug in one or more of those callbacks.
>
> Of course you do -- all non-trivial software has bugs. The question is,
> how are you going to find it? You can't unit-test the individual
> callbacks, because they don't exist in a form that can be tested.

It's easy to come up with a solution, in fact those functions /do/
exist. You would know that if you had read the documentation or even my
reply to a post of yours.

> So in this case, even though Python is slightly more verbose, and forces
> you to have the discipline of writing named functions ahead of time, this
> is actually a *good* thing because it encourages you to test them.
>
> If the callbacks are trivial functions, the Pythonic way is to use
> lambdas:
>
> func(some_args, {'when_odd': lambda n: n-1,
> 'when_prime': lambda n: n**2 - 1,
> ...})
>
> although I would discourage that unless they are *really* trivial. But
> for callbacks of any complexity, they will need to be tested, otherwise
> how do you know they do what you want them to do?
>
> Your code blocks make unit testing of the callbacks impossible, and for
> that reason the Pythonic way is better.

Talking with you is a real pain. You're always partial in your opinions
and this urge of yours to criticize other's work makes you look dumb or
hopefully just lazy.
I can't stand people like you who don't even have the decency of taking
the time to read the documentation of a project and just run their mouth
without any concern for facts.
What I can't stand is that if I won't reply to your posts other lazy
people will believe the nonsense you say, but I'll have to live with
that because I've wasted enough time with you.

Kiuhnm
--
http://mail.python.org/mailman/listinfo/python-list


lamialily at cleverpun

May 1, 2012, 10:31 AM

Post #22 of 49 (3025 views)
Permalink
Re: syntax for code blocks [In reply to]

Holy crap.

Easy there, tiger.

I understand you're frustrated, but the people here are trying to
help, even if they've decided the means of helping is trying to
explain why they feel your style of development isn't the best way to
do things.

You're going to wear out your welcome and not get any help at all if
you go ballistic like that. :/

~Temia
--
When on earth, do as the earthlings do.
--
http://mail.python.org/mailman/listinfo/python-list


malaclypse2 at gmail

May 1, 2012, 10:39 AM

Post #23 of 49 (3021 views)
Permalink
Re: syntax for code blocks [In reply to]

On Tue, May 1, 2012 at 1:07 PM, Kiuhnm
<kiuhnm03.4t.yahoo.it [at] mail> wrote:
> If you had read the module's docstring you would know that the public
> version uses

Are you aware that you've never posted a link to your module, nor it's
docstrings? Are you also aware that your module is essentially
unfindable on google? Certainly nothing on the first two pages of
google results for 'python codeblocks' jumps out at me as a python
module of that name.

Perhaps you would have better luck if you either post the actual code
you want people to critique, or posted a link to that code.

--
Jerry
--
http://mail.python.org/mailman/listinfo/python-list


arnodel at gmail

May 1, 2012, 10:55 AM

Post #24 of 49 (3022 views)
Permalink
Re: syntax for code blocks [In reply to]

(sent from my phone)
On May 1, 2012 6:42 PM, "Jerry Hill" <malaclypse2 [at] gmail> wrote:
>
> On Tue, May 1, 2012 at 1:07 PM, Kiuhnm
> <kiuhnm03.4t.yahoo.it [at] mail> wrote:
> > If you had read the module's docstring you would know that the public
> > version uses
>
> Are you aware that you've never posted a link to your module, nor it's
> docstrings? Are you also aware that your module is essentially
> unfindable on google? Certainly nothing on the first two pages of
> google results for 'python codeblocks' jumps out at me as a python
> module of that name.
>
> Perhaps you would have better luck if you either post the actual code
> you want people to critique, or posted a link to that code.

He did post a link to a blog post describing his module and also a link to
the actual code, on bitbucket IIRC.

Arnaud


ethan at stoneleaf

May 1, 2012, 11:15 AM

Post #25 of 49 (3024 views)
Permalink
Re: syntax for code blocks [In reply to]

Arnaud Delobelle wrote:
> On May 1, 2012 6:42 PM, "Jerry Hill" wrote:
>> On Tue, May 1, 2012 at 1:07 PM, Kiuhnm wrote:
>>> If you had read the module's docstring you would know that the public
>>> version uses
>>
>> Are you aware that you've never posted a link to your module, nor it's
>> docstrings? Are you also aware that your module is essentially
>> unfindable on google? Certainly nothing on the first two pages of
>> google results for 'python codeblocks' jumps out at me as a python
>> module of that name.
>>
>> Perhaps you would have better luck if you either post the actual code
>> you want people to critique, or posted a link to that code.
>
> He did post a link to a blog post describing his module and also a link
> to the actual code, on bitbucket IIRC.

Actually, it was Ian Kelly that posted the blog reference.

~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list

First page Previous page 1 2 Next page Last page  View All Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.