Re: What I've just learned from Vitalije

2017-12-14 Thread vitalije

>
> BTW In your defaultdict-example there is a known_classes0 which is not 
> part of the code. And it's not clear how that (again overcomplicated) code 
> is better than a regular defaultdict(dict).
>
>
function known_classes0 just returns handmade dictionary of known classes 
like c, p, g, v,... There is nothing special about it. Returning value is 
just used to fill in initial values in regular defaultdict. 
Vitalije

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: What I've just learned from Vitalije

2017-12-14 Thread vitalije
| ast.walk chaotic order

It certainly has its drawbacks. However, it can help to shift programmers 
mind more towards sets and pure functions and away from procedural way of 
thinking.

As you can see in given example I had to operate on ClassDefs and 
FunctionDefs in more strict ordering, so operations on them are done in 
separate walks. It is possible to use ast.walk on narrower scopes 
(ClassDef, or FunctionDef).

Vitalije

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: What I've just learned from Vitalije

2017-12-14 Thread vitalije
What I was hoping to share in my code example was not about generators. It 
is true that python2 doesn't allow to return values from generator 
functions, but plain return statement is allowed and is equivalent to 
`return None`.

Generators that I used in my code were just leftovers of my previous 
 trials and experiments. They are not essential part of my idea. In fact in 
other thread I have already posted that their usage in this particular case 
isn't so good idea at all. Generators can be invaluable in situations when 
we need to operate on long lists, and here they return lists of one or zero 
elements. The same effect would be to replace yield statement with return 
[data...] and the other return with `return []`.

I was hoping to pass the idea of using small functions that can be reasoned 
about in isolation instead of using set of classes which make code 
extremely hard to reason about.

My toy example was written in about few hours, while I was simultaneously 
learning about ast module.

I'll try to explain in more detail my point of view on classes/objects vs. 
functions in another thread with possibly more code examples.
Vitalije 

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: What I've just learned from Vitalije

2017-12-14 Thread 'Marcel Franke' via leo-editor


Am Donnerstag, 14. Dezember 2017 11:05:46 UTC+1 schrieb Edward K. Ream:

​In our code, bool(node) is always True, so this comment is a bit off the 
> mark.
>

Well that's a chance to fix this. You are also free to enhance the 
check.Checking expliciet for None is popular with other DOM-libs.

BTW In your defaultdict-example there is a known_classes0 which is not part 
of the code. And it's not clear how that (again overcomplicated) code is 
better than a regular defaultdict(dict).

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: What I've just learned from Vitalije

2017-12-14 Thread Edward K. Ream
On Thu, Dec 14, 2017 at 3:22 AM, 'Marcel Franke' via leo-editor <
leo-editor@googlegroups.com> wrote:

>
>
> Am Mittwoch, 13. Dezember 2017 22:28:50 UTC+1 schrieb Edward K. Ream:
>
> def a_generator():
>> if 0:
>> yield 'abc' # Makes the function a generator
>> return None
>>
>
> This only works with Python 3. Python 2  does not support mixing return
> and yield.
>

​Thanks for this.  I thought I tested this with Python 2, but apparently
not.
​

>  It's also overcomplicated.
>

​It was just a test of the idea.​

Proper way to handle this is
>
> classes_list.extend(do_class(node if node else []))
>

​In our code, bool(node) is always True, so this comment is a bit off the
mark.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: What I've just learned from Vitalije

2017-12-14 Thread 'Marcel Franke' via leo-editor


Am Mittwoch, 13. Dezember 2017 22:28:50 UTC+1 schrieb Edward K. Ream:

def a_generator():
> if 0:
> yield 'abc' # Makes the function a generator
> return None
>

This only works with Python 3. Python 2  does not support mixing return and 
yield.
 It's also overcomplicated. a_generator is just an empty function, doing 
nothing to the list. But in this case you could also just use an empty 
list. aList.extend([]) or aList.extend(list()) are doing the exact same, 
while working out of the box everywhere.

for node in ast.walk(root):
> classes_list.extend(do_class(node))
>

Proper way to handle this is

classes_list.extend(do_class(node if node else []))

or

for node in ast.walk(root):
if node:
classes_list.extend(do_class(node))

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: What I've just learned from Vitalije

2017-12-13 Thread Edward K. Ream
On Wednesday, December 13, 2017 at 3:28:50 PM UTC-6, Edward K. Ream wrote:

There are several coding patterns in Vitalije's prototype that deserve 
> mention.
>

*Generators as predicates*
>
> Vitalije's prototype (as modified in leoCheck.py) contains this:
>
> for node in ast.walk(root):
> classes_list.extend(do_class(node))
>

Similar code works for dicts. This code:

def gen():
for i in range(2):
s = str(i)
yield ('file'+s, 'val'+s)
return None

d = {}
d.update(gen())
g.printDict(d)

prints:

{
  file0:'val0',
  file1:'val1'
}

This is likely to come in handy in rewriting the ShowData class.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


What I've just learned from Vitalije

2017-12-13 Thread Edward K. Ream
There are several coding patterns in Vitalije's prototype that deserve 
mention.

*Generators*

['abc'].extend(None) throws a TypeError.  However, the following works:

def a_generator():
if 0:
yield 'abc' # Makes the function a generator
return None

aList = ['abc']
aList.extend(a_generator())
print(aList) # prints ['abc']

Returning None from a generator is, iirc, equivalent to raising 
StopIteration.

*Generators as predicates*

Vitalije's prototype (as modified in leoCheck.py) contains this:

for node in ast.walk(root):
classes_list.extend(do_class(node))

This is exquisite code.  It's perfect for summarizing code.  I shall 
rewrite the ShowData class using this pattern. It should be possible to 
eliminate the ShowDataTraverser class, a big collapse in complexity. Note 
that in this case the fact that ast.walk traverses the tree in "no 
particular order" does not matter.

*Using defaultdict*

This code is elegant. The first arg to defaultdict is a default factory 
function. All other args are passed to the dict constructor.

from collections import defaultdict
default_factory = lambda:dict(ivars={}, methods={})
known_classes = defaultdict(default_factory, **known_classes0())

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.