csjark module

2016-09-21 Thread bezenchu
After setting up csjark (http://csjark.readthedocs.io/), I'm trying to test on 
of my C header files which has following statements:

typedef struct {
   unsigned long X;
   __int64 Y;
} abc;


If I'm changing __int64 to unsigned long I'm not getting this error
For the __int64 i'm getting the mention error. Am I missing something?


In addition, I'm getting following error:
Attribute error("'tuple object has no attibute 'children'",)

I'd be glad to have some assistance.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: csjark module

2016-09-21 Thread Peter Otten
bezen...@gmail.com wrote:

> After setting up csjark (http://csjark.readthedocs.io/), I'm trying to
> test on of my C header files which has following statements:
> 
> typedef struct {
>unsigned long X;
>__int64 Y;
> } abc;
> 
> 
> If I'm changing __int64 to unsigned long I'm not getting this error
> For the __int64 i'm getting the mention error. Am I missing something?
> 
> 
> In addition, I'm getting following error:
> Attribute error("'tuple object has no attibute 'children'",)
> 
> I'd be glad to have some assistance.

It looks like development of csjark has stopped in 2011. Try installing a 
pycparser version from that time frame -- 2.05 should be a good candidate 
according to  so if 
you are using pip after

$ pip install pycparser==2.05

csjark might work.



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


Re: Linear Time Tree Traversal Generator

2016-09-21 Thread Ned Batchelder
On Tuesday, September 20, 2016 at 12:48:55 PM UTC-4, ROGER GRAYDON CHRISTMAN 
wrote:
> I am trying to find a better (i.e. more efficient) way to implement a 
> generator
> that traverses a tree.
> 
> The current model of the code (which is also used by a textbook I am teaching
> from does this)
> 
>def __iter__(node):
>  for x in iter(node._left):
>   yield x
>  yield node._value
>  for x in iter(node._right)
>   yield x
> 
> This is nice, simple, and straightforward, but has an O(n log n) running time,
> since
> values from the leaves of the tree have to be yielded multiple times to the 
> top
> of the tree.
> 
> Now, I could certainly implement a linear-time traversal without the gnerator:
> 
> def to_list(node,result):
>   """append node information to result"""
>   result = to_list(node._left, result)
>   result.append(node._value)
>   return to_list(node,_right, result)
> 
> but then that requires the extra memory space to build the list into, which
> is basically what the generator is supposed to avoid.
> 
> Now, I did see that itertools has a chain method for concatenating
> iterators, so it would be nice to concatenate the results from the 
> recursive calls without the for loops, but I have no idea how to
> squeeze the 'yield node._value' in between them.
> 
> Is there hope for a linear-time tree-traversal generator, or will
> I have just have to settle for an n-log-n generator or a linear time
> behavior with linear extra space?

Another option is linear time, log-n space, by using an explicit stack
in your iterator:

def __iter__(self):
cur = self
stack = []
while True:
if cur:
stack.append(cur)
cur = cur._left
elif not stack:
break
else:
cur = stack.pop()
yield cur._value
cur = cur._right

This replaces the Python call stack with a list variable which tracks
parent nodes we haven't yet iterated, so it will grow as log-n. There's
no Python recursion, so each yield directly produces a value to the
caller.

--Ned.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: csjark module

2016-09-21 Thread bezenchu
On Wednesday, September 21, 2016 at 1:14:14 PM UTC+3, Peter Otten wrote:
> bezen...@gmail.com wrote:
> 
> > After setting up csjark (http://csjark.readthedocs.io/), I'm trying to
> > test on of my C header files which has following statements:
> > 
> > typedef struct {
> >unsigned long X;
> >__int64 Y;
> > } abc;
> > 
> > 
> > If I'm changing __int64 to unsigned long I'm not getting this error
> > For the __int64 i'm getting the mention error. Am I missing something?
> > 
> > 
> > In addition, I'm getting following error:
> > Attribute error("'tuple object has no attibute 'children'",)
> > 
> > I'd be glad to have some assistance.
> 
> It looks like development of csjark has stopped in 2011. Try installing a 
> pycparser version from that time frame -- 2.05 should be a good candidate 
> according to  so if 
> you are using pip after
> 
> $ pip install pycparser==2.05
> 
> csjark might work.

I've installed all the required SW, but still getting the same error
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: csjark module

2016-09-21 Thread Peter Otten
bezen...@gmail.com wrote:

> On Wednesday, September 21, 2016 at 1:14:14 PM UTC+3, Peter Otten wrote:
>> bezen...@gmail.com wrote:
>> 
>> > After setting up csjark (http://csjark.readthedocs.io/), I'm trying to
>> > test on of my C header files which has following statements:
>> > 
>> > typedef struct {
>> >unsigned long X;
>> >__int64 Y;
>> > } abc;
>> > 
>> > 
>> > If I'm changing __int64 to unsigned long I'm not getting this error
>> > For the __int64 i'm getting the mention error. Am I missing something?
>> > 
>> > 
>> > In addition, I'm getting following error:
>> > Attribute error("'tuple object has no attibute 'children'",)
>> > 
>> > I'd be glad to have some assistance.
>> 
>> It looks like development of csjark has stopped in 2011. Try installing a
>> pycparser version from that time frame -- 2.05 should be a good candidate
>> according to  so
>> if you are using pip after
>> 
>> $ pip install pycparser==2.05
>> 
>> csjark might work.
> 
> I've installed all the required SW, but still getting the same error

When you invoke the interactive interpreter what does

>>> import pycparser
>>> pycparser.__version__
'2.05'

produce on your system?

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


Re: Data Types

2016-09-21 Thread BartC

On 21/09/2016 05:03, Cai Gengyang wrote:


Are there any other data types that will give you type(A) or type(B) =  besides True and False?


No types but any variable or expression containing True or False will be 
a bool type (or class bool):


 A = 10<20
 print (type(A))  =>  

 print (10<20)=>  True

 print (type(10<20))  =>  

 --
Bartc
--
https://mail.python.org/mailman/listinfo/python-list


Re: csjark module

2016-09-21 Thread bezenchu
On Wednesday, September 21, 2016 at 3:17:11 PM UTC+3, Peter Otten wrote:
> bezen...@gmail.com wrote:
> 
> > On Wednesday, September 21, 2016 at 1:14:14 PM UTC+3, Peter Otten wrote:
> >> bezen...@gmail.com wrote:
> >> 
> >> > After setting up csjark (http://csjark.readthedocs.io/), I'm trying to
> >> > test on of my C header files which has following statements:
> >> > 
> >> > typedef struct {
> >> >unsigned long X;
> >> >__int64 Y;
> >> > } abc;
> >> > 
> >> > 
> >> > If I'm changing __int64 to unsigned long I'm not getting this error
> >> > For the __int64 i'm getting the mention error. Am I missing something?
> >> > 
> >> > 
> >> > In addition, I'm getting following error:
> >> > Attribute error("'tuple object has no attibute 'children'",)
> >> > 
> >> > I'd be glad to have some assistance.
> >> 
> >> It looks like development of csjark has stopped in 2011. Try installing a
> >> pycparser version from that time frame -- 2.05 should be a good candidate
> >> according to  so
> >> if you are using pip after
> >> 
> >> $ pip install pycparser==2.05
> >> 
> >> csjark might work.
> > 
> > I've installed all the required SW, but still getting the same error
> 
> When you invoke the interactive interpreter what does
> 
> >>> import pycparser
> >>> pycparser.__version__
> '2.05'
> 
> produce on your system?

I have version 2.07 (which is the one used for the development)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: csjark module

2016-09-21 Thread bezenchu
On Wednesday, September 21, 2016 at 4:43:47 PM UTC+3, beze...@gmail.com wrote:
> On Wednesday, September 21, 2016 at 3:17:11 PM UTC+3, Peter Otten wrote:
> > bezen...@gmail.com wrote:
> > 
> > > On Wednesday, September 21, 2016 at 1:14:14 PM UTC+3, Peter Otten wrote:
> > >> bezen...@gmail.com wrote:
> > >> 
> > >> > After setting up csjark (http://csjark.readthedocs.io/), I'm trying to
> > >> > test on of my C header files which has following statements:
> > >> > 
> > >> > typedef struct {
> > >> >unsigned long X;
> > >> >__int64 Y;
> > >> > } abc;
> > >> > 
> > >> > 
> > >> > If I'm changing __int64 to unsigned long I'm not getting this error
> > >> > For the __int64 i'm getting the mention error. Am I missing something?
> > >> > 
> > >> > 
> > >> > In addition, I'm getting following error:
> > >> > Attribute error("'tuple object has no attibute 'children'",)
> > >> > 
> > >> > I'd be glad to have some assistance.
> > >> 
> > >> It looks like development of csjark has stopped in 2011. Try installing a
> > >> pycparser version from that time frame -- 2.05 should be a good candidate
> > >> according to  so
> > >> if you are using pip after
> > >> 
> > >> $ pip install pycparser==2.05
> > >> 
> > >> csjark might work.
> > > 
> > > I've installed all the required SW, but still getting the same error
> > 
> > When you invoke the interactive interpreter what does
> > 
> > >>> import pycparser
> > >>> pycparser.__version__
> > '2.05'
> > 
> > produce on your system?
> 
> I have version 2.07 (which is the one used for the development)


some progress I've found for the 2nd problem is that this error is thrown in 
cparser.py (from the csjark files) in the visit_Enum function of the 
StructVisitor class.

In the .h file I have following definition which tried to be parsed
typedef enum{
   a=0;
   b=1;
} c;
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: get the sum of differences between integers in a list

2016-09-21 Thread Daiyue Weng
Hi, first of all, let me rephase the problem.

For an arbitrary list of integers (the integers in the list are not
necessary to be sequential), e.g. [1,2,3,6,8,9,10,11,13],

if a set of consecutive integers having a difference of 1 between them, put
them in a list, i.e. there are two such lists in this example,

[1,2,3],

[8,9,10,11],

and then put such lists in another list (i.e. [[1,2,3], [8,9,10,11]]). Put
the rest integers (non-sequential) in a separated list, i.e.

`[6, 13]`.

Note that, the problem only considers sequential integers with step_size =
1.

I tried to use itertools.groupby and itertools.count,

from itertools import groupby, count
lst = [1,2,3,6,8,9,10,11,13]
c = count()
result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c))]

but the result is close to the requirement shown as a list of lists,

[[1, 2, 3], [6], [8, 9, 10, 11], [13]]

but it didn't separate sequential lists from non-sequential ones.
Also, I couldn't find a way to put [6] and [13] in a single list.

I have also tried to separate sequential lists from non-sequential ones,

result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c) == 1)] #
tried to extract [1,2,3] and [8,9,10,11] from the list

or

result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c) > 1)] #
tried to extract [6] and [13] from the list

but they all ended up with

[[1, 2, 3], [6, 8, 9, 10, 11, 13]]

So two questions here,

1. How does itertools.groupby key function work in my case?

2. How to improve the program to achieve my goals?


Many thanks

On 21 September 2016 at 00:19, John Pote  wrote:

> On 20/09/2016 12:52, Daiyue Weng wrote:
>
> Hi, I have a list numbers say,
>>
>> [1,2,3,4,6,8,9,10,11]
>>
>> First, I want to calculate the sum of the differences between the numbers
>> in the list.
>>
> At least for this first part a little pencil, paper and algebra yields a
> simple formula of constant and minimal calculation time. I had an intuitive
> guess and wrote down the sum of differences for a couple of examples,
> [1, 2, 5]   => 4
> [9, 11, 17, 19] => 10
> It works for negative differences as well,
> [1, 2, 5, 1]=> 0
> The trick is to spot the relation between the sum of differences and the
> numbers in the list. A few lines of algebra then gave a very simple formula.
>
> As for the rest it's down to code as others have hinted at.
>
> Second, if a sequence of numbers having a difference of 1, put them in a
>> list, i.e. there are two such lists,
>>
>> [1,2,3]
>>
>> [8,9,10,11]
>>
>> and also put the rest numbers in another list, i.e. there is only one such
>> list in the example,
>>
>> [6].
>>
>> Third, get the lists with the max/min sizes from above, i.e. in this
>> example, the max list is,
>>
>> [8,9,10,11]
>>
>> min list is
>>
>> [1,2,3].
>>
>> What's the best way to implement this?
>>
>> cheers
>>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: csjark module

2016-09-21 Thread Peter Otten
bezen...@gmail.com wrote:

> On Wednesday, September 21, 2016 at 3:17:11 PM UTC+3, Peter Otten wrote:
>> bezen...@gmail.com wrote:
>> 
>> > On Wednesday, September 21, 2016 at 1:14:14 PM UTC+3, Peter Otten
>> > wrote:
>> >> bezen...@gmail.com wrote:
>> >> 
>> >> > After setting up csjark (http://csjark.readthedocs.io/), I'm trying
>> >> > to test on of my C header files which has following statements:
>> >> > 
>> >> > typedef struct {
>> >> >unsigned long X;
>> >> >__int64 Y;
>> >> > } abc;
>> >> > 
>> >> > 
>> >> > If I'm changing __int64 to unsigned long I'm not getting this error
>> >> > For the __int64 i'm getting the mention error. Am I missing
>> >> > something?
>> >> > 
>> >> > 
>> >> > In addition, I'm getting following error:
>> >> > Attribute error("'tuple object has no attibute 'children'",)
>> >> > 
>> >> > I'd be glad to have some assistance.
>> >> 
>> >> It looks like development of csjark has stopped in 2011. Try
>> >> installing a pycparser version from that time frame -- 2.05 should be
>> >> a good candidate according to
>> >>  so if you
>> >> are using pip after
>> >> 
>> >> $ pip install pycparser==2.05
>> >> 
>> >> csjark might work.
>> > 
>> > I've installed all the required SW, but still getting the same error
>> 
>> When you invoke the interactive interpreter what does
>> 
>> >>> import pycparser
>> >>> pycparser.__version__
>> '2.05'
>> 
>> produce on your system?
> 
> I have version 2.07 (which is the one used for the development)

For cjshark to work (or at least not fail in the way you observed) you need 
2.5.

2.7 returns 2-tuples where 2.5 does not. Compare for example:

https://github.com/eliben/pycparser/blob/release_v2.07/pycparser/c_ast.py#L149

def children(self):
nodelist = []
if self.name is not None: nodelist.append(("name", self.name))
if self.subscript is not None: nodelist.append(("subscript", 
self.subscript))
return tuple(nodelist)

and

https://github.com/eliben/pycparser/blob/release_v2.05/pycparser/c_ast.py#L136

def children(self):
nodelist = []
if self.name is not None: nodelist.append(self.name)
if self.subscript is not None: nodelist.append(self.subscript)
return tuple(nodelist)

I wonder why you didn't just try what I suggested...

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


strings and ints consistency - isinstance

2016-09-21 Thread Sayth Renshaw
Hi

Trying to clarify why ints and strings arent treated the same.

You can get a valuerror from trying to cast a non-int to an int as in int(3.0) 
however you cannot do a non string with str(a).

Which means that you likely should use try and except to test if a user enters 
a non-int with valuerror. 
However as you can't str () and get a valuerror you use conditional logic with 
strings.

Therefore to try and keep with pythons only one obvious way of doing things 
should i prefer conditional logic for all using isinstance?

That way regardless of input type my code flows the same and more explicitly 
states the intended type.
Sayth
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: get the sum of differences between integers in a list

2016-09-21 Thread Peter Otten
Daiyue Weng wrote:

> Hi, first of all, let me rephase the problem.
> 
> For an arbitrary list of integers (the integers in the list are not
> necessary to be sequential), e.g. [1,2,3,6,8,9,10,11,13],
> 
> if a set of consecutive integers having a difference of 1 between them,
> put them in a list, i.e. there are two such lists in this example,
> 
> [1,2,3],
> 
> [8,9,10,11],
> 
> and then put such lists in another list (i.e. [[1,2,3], [8,9,10,11]]). Put
> the rest integers (non-sequential) in a separated list, i.e.
> 
> `[6, 13]`.
> 
> Note that, the problem only considers sequential integers with step_size =
> 1.
> 
> I tried to use itertools.groupby and itertools.count,
> 
> from itertools import groupby, count
> lst = [1,2,3,6,8,9,10,11,13]
> c = count()
> result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c))]
> 
> but the result is close to the requirement shown as a list of lists,
> 
> [[1, 2, 3], [6], [8, 9, 10, 11], [13]]
> 
> but it didn't separate sequential lists from non-sequential ones.
> Also, I couldn't find a way to put [6] and [13] in a single list.
> 
> I have also tried to separate sequential lists from non-sequential ones,
> 
> result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c) == 1)]
> # tried to extract [1,2,3] and [8,9,10,11] from the list
> 
> or
> 
> result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c) > 1)] #
> tried to extract [6] and [13] from the list
> 
> but they all ended up with
> 
> [[1, 2, 3], [6, 8, 9, 10, 11, 13]]
> 
> So two questions here,
> 
> 1. How does itertools.groupby key function work in my case?
> 
> 2. How to improve the program to achieve my goals?

OK, you did at least try this time. So here's my attempt: 

$ cat consecutive.py 
from itertools import chain, groupby, islice, tee


def lonely(triple):
left, value, right = triple
if left is not None and value - left == 1:
return False
if right is not None and right - value == 1:
return False
return True


def grouped(values):
left, mid, right = tee(values, 3)
triples = zip(
chain([None], left),
mid,
chain(islice(right, 1, None), [None])
)
for key, group in groupby(triples, lonely):
yield key, (value for left, value, right in group)


def main():
consecutive = []
other = []

values = [1, 2, 3, 6, 8, 9, 10, 11, 13, 17, 19]

for is_lonely, group in grouped(values):
if is_lonely:
other.extend(group)
else:
consecutive.append(list(group))

print("input")
print("  ", values)
print()
print("consecutive")
print("  all ", consecutive)
print("  shortest", min(consecutive, key=len))
print("  longest ", max(consecutive, key=len))
print()
print("other")
print("  ", other)


if __name__ == "__main__":
main()
$ python3 consecutive.py 
input
   [1, 2, 3, 6, 8, 9, 10, 11, 13, 17, 19]

consecutive
  all  [[1, 2, 3], [8, 9, 10, 11]]
  shortest [1, 2, 3]
  longest  [8, 9, 10, 11]

other
   [6, 13, 17, 19]
$

This is not as efficient as it should be -- the gaps are calculated twice, 
the check for the first and the last position is repeated on every iteration 
lots of packing and unpacking... -- either groupby() is the wrong tool or 
I'm holding it wrong ;)

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


Re: Linear Time Tree Traversal Generator

2016-09-21 Thread ROGER GRAYDON CHRISTMAN
Since I was asked how I got different running times for my two code
fragments, I'll try to apply them to a very simple tree:
  4
2   6
   1 3 5 7

>def __iter__(node):
>  for x in iter(node._left):
>   yield x
>  yield node._value
>  for x in iter(node._right)
>   yield x

Nodes 1, 3, 5, 7 each yield exactly 1 value -- total 4 yield statements
Node  2  yields  1, 2, 3 3 yield statements
Node  6  yields  5, 6, 7 3 yield statements
Node  4  with its for loops yields all 7 values  7 yield statements

Total number of yields is 17
4 values (approx n/2) had to be yielded 3 times (approx log n)
Hence total time is O(n log n)

> def to_list(node,result):
>   """append node information to result"""
>   result = to_list(node._left, result)
>   result.append(node._value)
>   return to_list(node,_right, result)

Initially called at root (Node 4) with result = []
Empty result is based from Node 4 to Node 2 to Node 1 -> [1]
Node 2 appends 2, then Node 3 appends 3
These are returned to Node 4, who appends 4, etc.

Total number of append operations is 7, far less than 17

Now I suppose my new lists are also being 'sent upward'
with those return statements, but it is also very clear that
the total number of return statements executed is also 7,
far less than 17.

Hence total time is still O(n)

---

I saw one or two responses that said that it was inherently
flawed to do this as a binary tree in the first place.
Well, certainly updating the binary tree is faster than
a regular array or relational database, since it does not
require linear-time insertions and removals.

And for those who favor using dict for its very fast access,
you display your search keys in sorted order while they
are still in the dict.  The sorted() function copies all 
those out into an array and sorts the array, which
is of course, expected to be O(n log n).

Which only highlights my disappointment that my tree
traversal itself was O(n log n), unless I gave up on yield.
As a teacher I'm stuck with either
a) a data structure that can only get its maximal speed
   by not having any iterator in the normal sense
   (which in my mind sounds non-Pythonic)
b) can support iteration, but then is no better than a
   dict in any way, except for maybe some rare operation like
   "identify the minimal value"

Roger Christman
instructor
Pennsylvania State Universtiy


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


Re: strings and ints consistency - isinstance

2016-09-21 Thread Ned Batchelder
On Wednesday, September 21, 2016 at 10:27:15 AM UTC-4, Sayth Renshaw wrote:
> Hi
> 
> Trying to clarify why ints and strings arent treated the same.
> 
> You can get a valuerror from trying to cast a non-int to an int as in 
> int(3.0) however you cannot do a non string with str(a).
> 
> Which means that you likely should use try and except to test if a user 
> enters a non-int with valuerror. 
> However as you can't str () and get a valuerror you use conditional logic 
> with strings.
> 
> Therefore to try and keep with pythons only one obvious way of doing things 
> should i prefer conditional logic for all using isinstance?
> 
> That way regardless of input type my code flows the same and more explicitly 
> states the intended type.
> Sayth

How you do your check depends a lot on why you are checking, how strange a
value you expecting to be checking, and what you want to do if the value
isn't good for you.  Can you give us specifics of the check you are doing,
and the values you want to accept/reject, and the behavior you want on
rejection?

int() and str() are not casts, they are conversions, or constructors, and
they have different ideas of the kinds of things they can turn into ints
and strs.

One Python philosophy is to not check types just to reject values, but
instead to work with the value you are given. This gives the caller the
flexibility to provide a value of a type perhaps you didn't expect, but
which will work fine anyway.

--Ned.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: get the sum of differences between integers in a list

2016-09-21 Thread Peter Otten
Peter Otten wrote:

> This is not as efficient as it should be -- the gaps are calculated twice,
> the check for the first and the last position is repeated on every
> iteration lots of packing and unpacking... -- 

To address some of my own criticism:

def lonely(triple):
left, value, right = triple
return left and right


def grouped(values):
a, b, mid = tee(values, 3)
gaps = (y - x != 1 for x, y in zip(a, islice(b, 1, None)))
left, right = tee(gaps)

triples = zip(
chain([True], left),
mid,
chain(right, [True])
)
for key, group in groupby(triples, lonely):
yield key, (value for left, value, right in group)

> either groupby() is the
> wrong tool or I'm holding it wrong ;)

Most certainly still true. 


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


Re: automated comparison tool

2016-09-21 Thread Andrew Clark
On Tuesday, September 20, 2016 at 7:48:20 PM UTC-5, Steve D'Aprano wrote:
> On Wed, 21 Sep 2016 07:20 am, Andrew Clark wrote:
> 
> > I've restarted my code so many times i no longer have a working version of
> > anything.
> 
> 
> *Restarting* your code doesn't change it and cannot possibly stop it from
> working. My guess is that you actually mean that you've made so many random
> edits to your code, without understanding or thinking about what you are
> doing, that you've broken it irreversibly.
> 
> I think you've learned a few things:
> 
> (1) Debugging by making random perturbations to code should be a last
> resort, and even then, only done with the greatest of care.
> 
> (2) Use version control so you can roll back changes.
> 
> (3) Or at least make a backup copy of a known working version of your code.
> 
> (4) Most importantly, never make so many changes to your ONLY copy of the
> code in one go that you can break it so badly that you can't go back.
> 
> 
> You've described your problem quite well, and nicely broken it up into
> pieces. I suggest you take each piece, and try to write the code for it
> independently of the others. Start with the first piece:
> 
> "access remote directories"
> 
> 
> Okay, how are they accessible? Are they just network drives? Then that's
> simple: you can access them as if they were local directories. What's the
> path name of the network drive(s)? Simply use that. Problem solved.
> 
> If not, then you need to decide how to access them: over SSH, FTP,
> sneaker-net, whatever. The choice you make here is going to impact the
> complexity of your code. Think about carefully.
> 
> Once you have working code that can list the remote directory, you can use
> it to list your three sets of files:
> 
> 
> startupfiles = listdir(...StartupConfig)
> runningfiles = listdir(...RunningConfig)
> archivefiles = listdir(...ArchiveConfig)
> 
> 
> now you can move onto step 2:
> 
> "run through startup, running and archive to find files 
>  with same hostname(do this for all files)"
> 
> 
> Now you can forget all about remote file systems (at least for now) and just
> work with the three lists of file names. How do you decide which files
> belong to which file name? I don't know, because you don't say. Is the
> hostname in the file name? Or do you have to open the file and read the
> information from the file contents?
> 
> However you do it, start by generating a mapping of hostname: list of file
> names.
> 
> 
> mapping = {}
> for filename in list_of_filenames:
> hostname = get_hostname(filename)
> if hostname in mapping:
> mapping[hostname].append(filename)
> else:
> mapping[hostname] = [filename]  # start a new list with one entry
> 
> 
> 
> Once you've done that for all three directories, then you can collect all
> the file names for each host from all three directories, and compare the
> files.

> -- 
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.

Thank you all for the comments. I've been working on the part to remote into 
the server. Unfortunately it appears that the drives are network accessible and 
i have to connect to them via SFTP to retrieve the files. I've been working on 
the below code to do this. However i keep getting an error.

import os
import sys
from contextlib import closing
from paramiko import SSHConfig, SSHClient

# Specify hostname to connect to and the path
hostname, remote_dirname, destdir = sys.argv[1:]

# Load parameters to setup ssh connection
config = SSHConfig()
with open(os.path.expanduser('~/.ssh/config')) as config_file:
config.parse(config_file)
d = config.lookup(hostname)

# Connect
with closing(SSHClient()) as ssh:
ssh.load_system_host_keys() #NOTE: no AutoAddPolicy()
ssh.connect(d['hostname'], username=d.get('user'))
with closing(ssh.open_sftp()) as sftp:
# CD into remote directory
sftp.chdir(remote_dirname)
# CD to local destination directory
os.chdir(destdir)
# Download all files in it to destdir directory
for filename in sftp.listdir():
sftp.get(filename, filename)

Traceback (most recent call last):
  File "C:\Users\ac40935\workspace\AutoCompare\filecmp.py", line 6, in 
from paramiko import SSHConfig, SSHClient
  File "C:\Python35-32\paramiko-master\paramiko\__init__.py", line 30, in 

from paramiko.transport import SecurityOptions, Transport
  File "C:\Python35-32\paramiko-master\paramiko\transport.py", line 32, in 

from cryptography.hazmat.backends import default_backend
ImportError: No module named 'cryptography'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: get the sum of differences between integers in a list

2016-09-21 Thread marco . nawijn
On Wednesday, September 21, 2016 at 4:14:10 PM UTC+2, Daiyue Weng wrote:
> Hi, first of all, let me rephase the problem.
> 
> For an arbitrary list of integers (the integers in the list are not
> necessary to be sequential), e.g. [1,2,3,6,8,9,10,11,13],
> 
> if a set of consecutive integers having a difference of 1 between them, put
> them in a list, i.e. there are two such lists in this example,
> 
> [1,2,3],
> 
> [8,9,10,11],
> 
> and then put such lists in another list (i.e. [[1,2,3], [8,9,10,11]]). Put
> the rest integers (non-sequential) in a separated list, i.e.
> 
> `[6, 13]`.
> 
> Note that, the problem only considers sequential integers with step_size =
> 1.
> 
> I tried to use itertools.groupby and itertools.count,
> 
> from itertools import groupby, count
> lst = [1,2,3,6,8,9,10,11,13]
> c = count()
> result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c))]
> 
> but the result is close to the requirement shown as a list of lists,
> 
> [[1, 2, 3], [6], [8, 9, 10, 11], [13]]
> 
> but it didn't separate sequential lists from non-sequential ones.
> Also, I couldn't find a way to put [6] and [13] in a single list.
> 
> I have also tried to separate sequential lists from non-sequential ones,
> 
> result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c) == 1)] #
> tried to extract [1,2,3] and [8,9,10,11] from the list
> 
> or
> 
> result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c) > 1)] #
> tried to extract [6] and [13] from the list
> 
> but they all ended up with
> 
> [[1, 2, 3], [6, 8, 9, 10, 11, 13]]
> 
> So two questions here,
> 
> 1. How does itertools.groupby key function work in my case?
> 
> 2. How to improve the program to achieve my goals?
> 
> 
> Many thanks
> 
> On 21 September 2016 at 00:19, John Pote  wrote:
> 
> > On 20/09/2016 12:52, Daiyue Weng wrote:
> >
> > Hi, I have a list numbers say,
> >>
> >> [1,2,3,4,6,8,9,10,11]
> >>
> >> First, I want to calculate the sum of the differences between the numbers
> >> in the list.
> >>
> > At least for this first part a little pencil, paper and algebra yields a
> > simple formula of constant and minimal calculation time. I had an intuitive
> > guess and wrote down the sum of differences for a couple of examples,
> > [1, 2, 5]   => 4
> > [9, 11, 17, 19] => 10
> > It works for negative differences as well,
> > [1, 2, 5, 1]=> 0
> > The trick is to spot the relation between the sum of differences and the
> > numbers in the list. A few lines of algebra then gave a very simple formula.
> >
> > As for the rest it's down to code as others have hinted at.
> >
> > Second, if a sequence of numbers having a difference of 1, put them in a
> >> list, i.e. there are two such lists,
> >>
> >> [1,2,3]
> >>
> >> [8,9,10,11]
> >>
> >> and also put the rest numbers in another list, i.e. there is only one such
> >> list in the example,
> >>
> >> [6].
> >>
> >> Third, get the lists with the max/min sizes from above, i.e. in this
> >> example, the max list is,
> >>
> >> [8,9,10,11]
> >>
> >> min list is
> >>
> >> [1,2,3].
> >>
> >> What's the best way to implement this?
> >>
> >> cheers
> >>
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
On Wednesday, September 21, 2016 at 4:14:10 PM UTC+2, Daiyue Weng wrote:
> Hi, first of all, let me rephase the problem.
> 
> For an arbitrary list of integers (the integers in the list are not
> necessary to be sequential), e.g. [1,2,3,6,8,9,10,11,13],
> 
> if a set of consecutive integers having a difference of 1 between them, put
> them in a list, i.e. there are two such lists in this example,
> 
> [1,2,3],
> 
> [8,9,10,11],
> 
> and then put such lists in another list (i.e. [[1,2,3], [8,9,10,11]]). Put
> the rest integers (non-sequential) in a separated list, i.e.
> 
> `[6, 13]`.
> 
> Note that, the problem only considers sequential integers with step_size =
> 1.
> 
> I tried to use itertools.groupby and itertools.count,
> 
> from itertools import groupby, count
> lst = [1,2,3,6,8,9,10,11,13]
> c = count()
> result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c))]
> 
> but the result is close to the requirement shown as a list of lists,
> 
> [[1, 2, 3], [6], [8, 9, 10, 11], [13]]
> 
> but it didn't separate sequential lists from non-sequential ones.
> Also, I couldn't find a way to put [6] and [13] in a single list.
> 
> I have also tried to separate sequential lists from non-sequential ones,
> 
> result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c) == 1)] #
> tried to extract [1,2,3] and [8,9,10,11] from the list
> 
> or
> 
> result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c) > 1)] #
> tried to extract [6] and [13] from the list
> 
> but they all ended up with
> 
> [[1, 2, 3], [6, 8, 9, 10, 11, 13]]
> 
> So two questions here,
> 
> 1. How does itertools.groupby key function work in my case?
> 
> 2. How to improve the program to achieve my goals?
> 
> 
> Many thanks
> 
> On 21 September 2016 at 00:19, John Pote  wrote:

Re: automated comparison tool

2016-09-21 Thread Andrew Clark
I reinstalled paramiko and now i'm getting a slighty different error but still 
says no cryptography.

Traceback (most recent call last):
  File "C:\Users\ac40935\workspace\AutoCompare\filecmp.py", line 6, in 
from paramiko import SSHConfig, SSHClient
  File 
"C:\Python35-32\lib\site-packages\paramiko-2.0.2-py3.5.egg\paramiko\__init__.py",
 line 30, in 
  File 
"C:\Python35-32\lib\site-packages\paramiko-2.0.2-py3.5.egg\paramiko\transport.py",
 line 32, in 
ImportError: No module named 'cryptography'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Linear Time Tree Traversal Generator

2016-09-21 Thread Chris Angelico
On Thu, Sep 22, 2016 at 12:39 AM, ROGER GRAYDON CHRISTMAN  wrote:
> Which only highlights my disappointment that my tree
> traversal itself was O(n log n), unless I gave up on yield.
> As a teacher I'm stuck with either
> a) a data structure that can only get its maximal speed
>by not having any iterator in the normal sense
>(which in my mind sounds non-Pythonic)
> b) can support iteration, but then is no better than a
>dict in any way, except for maybe some rare operation like
>"identify the minimal value"

c) Reject the notion that speed is somehow linked to "number of yield
operations", until such time as you actually have proof of that

d) Use "yield from" and declare and/or assume that it is optimized,
which it quite probably will be.

The cost of a yield action is pretty minimal. Don't worry about it.

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


Re: pypy on windows much slower than linux/mac when using complex number type?

2016-09-21 Thread Irmen de Jong
On 21-9-2016 1:20, Chris Kaynor wrote:

> 
> Regarding the performance decrease, it may be worthwhile to push the report
> to a PyPy specific forum - a PyPy developer will probably see it here, but
> you may get a faster response on a forum specific to PyPy.


You're right.
I don't know the best place for that, but I've made a bug report about it here:
https://bitbucket.org/pypy/pypy/issues/2401


Irmen

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


Re: Linear Time Tree Traversal Generator

2016-09-21 Thread Random832
On Wed, Sep 21, 2016, at 10:39, ROGER GRAYDON CHRISTMAN wrote:
> Which only highlights my disappointment that my tree
> traversal itself was O(n log n), unless I gave up on yield.

Or you can give up on recursion. Recursive tree traversal is generally
associated with passing in a callback in rather than implementing an
iterable-like interface that can be used with a caller's for-loop
anyway.

def __iter__(node):
stack = []
while stack or node:
if node:
stack.append(node)
node = node._left
else:
node = stack.pop()
yield node._value
node = node._right
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Linear Time Tree Traversal Generator

2016-09-21 Thread Ian Kelly
On Tue, Sep 20, 2016 at 11:03 AM, Rob Gaddi
 wrote:
> The only thing that's O(N log N) in that is the number of actual yield
> calls.  If you're doing pretty much anything with those values as
> they're being iterated over then they'll dominate the timing, and that
> is O(N).

It's fair to say that for sufficiently small values of N, the time
taken by the actual work will likely dominate the time taken by the
yields. It's not quite correct though to say that a O(N) piece will
dominate a O(N log N) piece, because the whole point of measuring the
asymptotic complexity is the observation that as N grows, the O(N log
N) component will *eventually* become dominant. If you're only talking
about "sufficiently small" values of N, then big O complexity is
irrelevant.

> I think there's a little bit of optimization that goes on using yield
> from.  That said, all this has a serious stink of premature
> optimization.

Agreed.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Linear Time Tree Traversal Generator

2016-09-21 Thread Chris Angelico
On Thu, Sep 22, 2016 at 3:15 AM, Random832  wrote:
> Or you can give up on recursion. Recursive tree traversal is generally
> associated with passing in a callback in rather than implementing an
> iterable-like interface that can be used with a caller's for-loop
> anyway.
>

Maybe in languages that don't _have_ good generators, but in Python,
the obvious way to iterate over something is with an iterator, not a
callback.

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


Re: Linear Time Tree Traversal Generator

2016-09-21 Thread Chris Angelico
On Thu, Sep 22, 2016 at 3:32 AM, Ian Kelly  wrote:
> On Tue, Sep 20, 2016 at 11:03 AM, Rob Gaddi
>  wrote:
>> The only thing that's O(N log N) in that is the number of actual yield
>> calls.  If you're doing pretty much anything with those values as
>> they're being iterated over then they'll dominate the timing, and that
>> is O(N).
>
> It's fair to say that for sufficiently small values of N, the time
> taken by the actual work will likely dominate the time taken by the
> yields. It's not quite correct though to say that a O(N) piece will
> dominate a O(N log N) piece, because the whole point of measuring the
> asymptotic complexity is the observation that as N grows, the O(N log
> N) component will *eventually* become dominant. If you're only talking
> about "sufficiently small" values of N, then big O complexity is
> irrelevant.

Yeah but if your O(N) actual work takes fifty times as long as the
O(N log N) yields, the latter won't dominate until you have 2**50
elements, and you're unlikely to have that. I don't know exactly what
the cost of a yield is, but most definitely...

>> That said, all this has a serious stink of premature
>> optimization.
> Agreed.
... this.

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


Re: Another å, ä, ö question

2016-09-21 Thread Martin Schöön
Den 2016-09-20 skrev Chris Angelico :
> On Wed, Sep 21, 2016 at 6:21 AM, Martin Schöön  
> wrote:
>> Den 2016-09-19 skrev Lawrence D’Oliveiro :
>>> On Tuesday, September 20, 2016 at 8:21:25 AM UTC+12, Martin Schöön wrote:
 But -- now I tested using emacs instead using C-c C-c to execute.
 Noting happens so I try to run the program from command line and
 find that now Python can't stand my å, ä and ö.
>>>
>>> What version of Python? Python 3 accepts Unicode UTF-8 as a matter of 
>>> course.
>>
>> Python 2.7.
>> I just tried running my code in Python 3 and that worked like charm.
>
> Then you've found the solution. Py2's Unicode support is weaker than
> Py3's, and it often depends on encodings.
>
Yes, so it seems. I have told emacs to invoke python3 instead of python
and then it worked like just fine.

I assume there is a setting somewhere in Geany that tells it to run
python3 rather than python but I have failed to find it.

Case closed as far as I am concerned.

Thanks for the help and patience.

/Martin
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Another å, ä, ö question

2016-09-21 Thread Martin Schöön
Den 2016-09-20 skrev Peter Otten <__pete...@web.de>:
> Martin Schöön wrote:
>
>> Den 2016-09-19 skrev Christian Gollwitzer :
>>> Am 19.09.16 um 22:21 schrieb Martin Schöön:
 I am studying some of these tutorials:
 https://pythonprogramming.net/matplotlib-intro-tutorial/

>>>
>>> Assuming that you use UTF-8 (you should check with an emacs expert, I am
>>> not an emacs user), try putting the header
>>>
>>> #!/usr/bin/python
>>> # -*- coding: utf-8 -*-
>>>
>>> on top of your files.
>>>
>> I already have this and since it doesn't work from command line
>> either it can't be an emacs unique problem.
>> 
>
> Are all non-ascii strings unicode? I. e.
>
> u"Schöön" rather than just "Schöön"
>
> ?

I am not sure I answer your questions since I am not quite sure I
understand it but here goes: The complete file is UTF-8 encoded as
that is how Geany is set up to create new files (I just checked).

/Martin
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: csjark module

2016-09-21 Thread bezenchu
On Wednesday, September 21, 2016 at 5:15:38 PM UTC+3, Peter Otten wrote:
> bezen...@gmail.com wrote:
> 
> > On Wednesday, September 21, 2016 at 3:17:11 PM UTC+3, Peter Otten wrote:
> >> bezen...@gmail.com wrote:
> >> 
> >> > On Wednesday, September 21, 2016 at 1:14:14 PM UTC+3, Peter Otten
> >> > wrote:
> >> >> bezen...@gmail.com wrote:
> >> >> 
> >> >> > After setting up csjark (http://csjark.readthedocs.io/), I'm trying
> >> >> > to test on of my C header files which has following statements:
> >> >> > 
> >> >> > typedef struct {
> >> >> >unsigned long X;
> >> >> >__int64 Y;
> >> >> > } abc;
> >> >> > 
> >> >> > 
> >> >> > If I'm changing __int64 to unsigned long I'm not getting this error
> >> >> > For the __int64 i'm getting the mention error. Am I missing
> >> >> > something?
> >> >> > 
> >> >> > 
> >> >> > In addition, I'm getting following error:
> >> >> > Attribute error("'tuple object has no attibute 'children'",)
> >> >> > 
> >> >> > I'd be glad to have some assistance.
> >> >> 
> >> >> It looks like development of csjark has stopped in 2011. Try
> >> >> installing a pycparser version from that time frame -- 2.05 should be
> >> >> a good candidate according to
> >> >>  so if you
> >> >> are using pip after
> >> >> 
> >> >> $ pip install pycparser==2.05
> >> >> 
> >> >> csjark might work.
> >> > 
> >> > I've installed all the required SW, but still getting the same error
> >> 
> >> When you invoke the interactive interpreter what does
> >> 
> >> >>> import pycparser
> >> >>> pycparser.__version__
> >> '2.05'
> >> 
> >> produce on your system?
> > 
> > I have version 2.07 (which is the one used for the development)
> 
> For cjshark to work (or at least not fail in the way you observed) you need 
> 2.5.
> 
> 2.7 returns 2-tuples where 2.5 does not. Compare for example:
> 
> https://github.com/eliben/pycparser/blob/release_v2.07/pycparser/c_ast.py#L149
> 
> def children(self):
> nodelist = []
> if self.name is not None: nodelist.append(("name", self.name))
> if self.subscript is not None: nodelist.append(("subscript", 
> self.subscript))
> return tuple(nodelist)
> 
> and
> 
> https://github.com/eliben/pycparser/blob/release_v2.05/pycparser/c_ast.py#L136
> 
> def children(self):
> nodelist = []
> if self.name is not None: nodelist.append(self.name)
> if self.subscript is not None: nodelist.append(self.subscript)
> return tuple(nodelist)
> 
> I wonder why you didn't just try what I suggested...

Thanks.

I've tried it @home on the test files of csjark.
It worked fine for the 2nd issue (while at my work computer, there was error).
For the first one, still have to check
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: csjark module

2016-09-21 Thread bezenchu
On Wednesday, September 21, 2016 at 5:15:38 PM UTC+3, Peter Otten wrote:
> bezen...@gmail.com wrote:
> 
> > On Wednesday, September 21, 2016 at 3:17:11 PM UTC+3, Peter Otten wrote:
> >> bezen...@gmail.com wrote:
> >> 
> >> > On Wednesday, September 21, 2016 at 1:14:14 PM UTC+3, Peter Otten
> >> > wrote:
> >> >> bezen...@gmail.com wrote:
> >> >> 
> >> >> > After setting up csjark (http://csjark.readthedocs.io/), I'm trying
> >> >> > to test on of my C header files which has following statements:
> >> >> > 
> >> >> > typedef struct {
> >> >> >unsigned long X;
> >> >> >__int64 Y;
> >> >> > } abc;
> >> >> > 
> >> >> > 
> >> >> > If I'm changing __int64 to unsigned long I'm not getting this error
> >> >> > For the __int64 i'm getting the mention error. Am I missing
> >> >> > something?
> >> >> > 
> >> >> > 
> >> >> > In addition, I'm getting following error:
> >> >> > Attribute error("'tuple object has no attibute 'children'",)
> >> >> > 
> >> >> > I'd be glad to have some assistance.
> >> >> 
> >> >> It looks like development of csjark has stopped in 2011. Try
> >> >> installing a pycparser version from that time frame -- 2.05 should be
> >> >> a good candidate according to
> >> >>  so if you
> >> >> are using pip after
> >> >> 
> >> >> $ pip install pycparser==2.05
> >> >> 
> >> >> csjark might work.
> >> > 
> >> > I've installed all the required SW, but still getting the same error
> >> 
> >> When you invoke the interactive interpreter what does
> >> 
> >> >>> import pycparser
> >> >>> pycparser.__version__
> >> '2.05'
> >> 
> >> produce on your system?
> > 
> > I have version 2.07 (which is the one used for the development)
> 
> For cjshark to work (or at least not fail in the way you observed) you need 
> 2.5.
> 
> 2.7 returns 2-tuples where 2.5 does not. Compare for example:
> 
> https://github.com/eliben/pycparser/blob/release_v2.07/pycparser/c_ast.py#L149
> 
> def children(self):
> nodelist = []
> if self.name is not None: nodelist.append(("name", self.name))
> if self.subscript is not None: nodelist.append(("subscript", 
> self.subscript))
> return tuple(nodelist)
> 
> and
> 
> https://github.com/eliben/pycparser/blob/release_v2.05/pycparser/c_ast.py#L136
> 
> def children(self):
> nodelist = []
> if self.name is not None: nodelist.append(self.name)
> if self.subscript is not None: nodelist.append(self.subscript)
> return tuple(nodelist)
> 
> I wonder why you didn't just try what I suggested...

Thanks,
This one solved the 2nd problem.

Do you have any suggestions for the 1st one?

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


Re: list or dictionary

2016-09-21 Thread Ganesh Pal
Thanks Steve  for the clues , quickly tried  out # Version 1 doesn't seen
to work.


>>> for line in hostname:
... regex = r'(.*) is array with id {}'.format(devid)
... mo = re.search(regex, line)
... print line, regex, mo
... if mo is not None:
... print mo.group(1)
...
RX-145-1 is array id 1 (.*) is array with id 3 None
RX-145-2 is array id 2 (.*) is array with id 3 None
RX-145-3 is array id 3 (.*) is array with id 3 None
>>> hostname
['RX-145-1 is array id 1', 'RX-145-2 is array id 2', 'RX-145-3 is array id
3']
>>> type(devid)

>>> devid
3
---
>>> devid = '3'
>>> for line in hostname:
... regex = r'(.*) is array with id {}'.format(devid)
... mo = re.search(regex, line)
... print line, regex, mo
... if mo is not None:
...print mo.group(1)
...
RX-145-1 is array id 1 (.*) is array with id 3 None
RX-145-2 is array id 2 (.*) is array with id 3 None
RX-145-3 is array id 3 (.*) is array with id 3 None
>>> type(devid)

--
>>> for line in hostname:
... regex = r'(.*) is array with id %d' % (devid)
... mo = re.search(regex, line)
... print line, regex, mo
... if mo is not None:
...print mo.group(1)
...
RX-145-1 is array id 1 (.*) is array with id 3 None
RX-145-2 is array id 2 (.*) is array with id 3 None
RX-145-3 is array id 3 (.*) is array with id 3 None
---

Looks like Iam missing something ?



Regards,
Ganesh


On Wed, Sep 21, 2016 at 5:57 AM, Steve D'Aprano 
wrote:

> On Wed, 21 Sep 2016 04:04 am, Ganesh Pal wrote:
>
> > I am on python 2.7 and Linux
> >
> > I have the stdout in the below form , I need to write a function to get
> > hostname for the given id.
> >
> >
> > Example:
> >
>  stdout
> > 'hostname-1 is array with id 1\nhostname-2 is array with  id
> 2\nhostname-3
> > is array with id 3\n'
> >
> >
> > def get_hostname(id)
> >return id
> >
> > what's a better option here
> >
> > 1. store it in list and grep for id and return
> > 2. store it in dict as key and value i.e hostname = { 'hostname1': 1} and
> > return key
> > 3. any other simple options.
>
>
> Why don't you write a function for each one, and see which is less work?
>
> # Version 1: store the hostname information in a list as strings
>
> hostnames = ['hostname-1 is array with id 1',
>  'hostname-2 is array with id 2',
>  'hostname-842 is array with id 842']
>
> def get_hostname(id):
> for line in hostnames:
> regex = r'(.*) is array with id %d' % id
> mo = re.match(regex, line)
> if mo is not None:
> return mo.group(1)
> raise ValueError('not found')
>
>
> # Version 2: store the hostname information in a dict {hostname: id}
>
> hostnames = {'hostname1': 1, 'hostname2': 2, 'hostname842': 842}
>
> def get_hostname(id):
> for key, value in hostnames.items():
> if value == id:
> return key
> raise ValueError('not found')
>
>
> # Version 3: use a dict the way dicts are meant to be used
>
> hostnames = {1: 'hostname1', 2: 'hostname2', 842: 'hostname842'}
>
> def get_hostname(id):
> return hostnames[id]
>
>
>
>
>
>
>
>
> --
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: list or dictionary

2016-09-21 Thread MRAB

On 2016-09-21 19:35, Ganesh Pal wrote:

Thanks Steve  for the clues , quickly tried  out # Version 1 doesn't seen
to work.



for line in hostname:

... regex = r'(.*) is array with id {}'.format(devid)
... mo = re.search(regex, line)
... print line, regex, mo
... if mo is not None:
... print mo.group(1)
...
RX-145-1 is array id 1 (.*) is array with id 3 None
RX-145-2 is array id 2 (.*) is array with id 3 None
RX-145-3 is array id 3 (.*) is array with id 3 None

hostname

['RX-145-1 is array id 1', 'RX-145-2 is array id 2', 'RX-145-3 is array id
3']

type(devid)



devid

3
---

devid = '3'
for line in hostname:

... regex = r'(.*) is array with id {}'.format(devid)
... mo = re.search(regex, line)
... print line, regex, mo
... if mo is not None:
...print mo.group(1)
...
RX-145-1 is array id 1 (.*) is array with id 3 None
RX-145-2 is array id 2 (.*) is array with id 3 None
RX-145-3 is array id 3 (.*) is array with id 3 None

type(devid)


--

for line in hostname:

... regex = r'(.*) is array with id %d' % (devid)
... mo = re.search(regex, line)
... print line, regex, mo
... if mo is not None:
...print mo.group(1)
...
RX-145-1 is array id 1 (.*) is array with id 3 None
RX-145-2 is array id 2 (.*) is array with id 3 None
RX-145-3 is array id 3 (.*) is array with id 3 None
---

Looks like Iam missing something ?


[snip]
The lines have "array id", but the regex has "array with id".

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


Re: list or dictionary

2016-09-21 Thread Ganesh Pal
Thanks  , and it has to be re.match()

On Thu, Sep 22, 2016 at 12:18 AM, MRAB  wrote:

> On 2016-09-21 19:35, Ganesh Pal wrote:
>
>> Thanks Steve  for the clues , quickly tried  out # Version 1 doesn't seen
>> to work.
>>
>>
>> for line in hostname:
>
 ... regex = r'(.*) is array with id {}'.format(devid)
>> ... mo = re.search(regex, line)
>> ... print line, regex, mo
>> ... if mo is not None:
>> ... print mo.group(1)
>> ...
>> RX-145-1 is array id 1 (.*) is array with id 3 None
>> RX-145-2 is array id 2 (.*) is array with id 3 None
>> RX-145-3 is array id 3 (.*) is array with id 3 None
>>
>>> hostname
>
 ['RX-145-1 is array id 1', 'RX-145-2 is array id 2', 'RX-145-3 is array
>> id
>> 3']
>>
>>> type(devid)
>
 
>>
>>> devid
>
 3
>> 
>> ---
>>
>>> devid = '3'
> for line in hostname:
>
 ... regex = r'(.*) is array with id {}'.format(devid)
>> ... mo = re.search(regex, line)
>> ... print line, regex, mo
>> ... if mo is not None:
>> ...print mo.group(1)
>> ...
>> RX-145-1 is array id 1 (.*) is array with id 3 None
>> RX-145-2 is array id 2 (.*) is array with id 3 None
>> RX-145-3 is array id 3 (.*) is array with id 3 None
>>
>>> type(devid)
>
 
>> 
>> --
>>
>>> for line in hostname:
>
 ... regex = r'(.*) is array with id %d' % (devid)
>> ... mo = re.search(regex, line)
>> ... print line, regex, mo
>> ... if mo is not None:
>> ...print mo.group(1)
>> ...
>> RX-145-1 is array id 1 (.*) is array with id 3 None
>> RX-145-2 is array id 2 (.*) is array with id 3 None
>> RX-145-3 is array id 3 (.*) is array with id 3 None
>> 
>> ---
>>
>> Looks like Iam missing something ?
>>
>> [snip]
> The lines have "array id", but the regex has "array with id".
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: csjark module

2016-09-21 Thread Peter Otten
bezen...@gmail.com wrote:

> On Wednesday, September 21, 2016 at 5:15:38 PM UTC+3, Peter Otten wrote:
>> bezen...@gmail.com wrote:
>> 
>> > On Wednesday, September 21, 2016 at 3:17:11 PM UTC+3, Peter Otten
>> > wrote:
>> >> bezen...@gmail.com wrote:
>> >> 
>> >> > On Wednesday, September 21, 2016 at 1:14:14 PM UTC+3, Peter Otten
>> >> > wrote:
>> >> >> bezen...@gmail.com wrote:
>> >> >> 
>> >> >> > After setting up csjark (http://csjark.readthedocs.io/), I'm
>> >> >> > trying to test on of my C header files which has following
>> >> >> > statements:
>> >> >> > 
>> >> >> > typedef struct {
>> >> >> >unsigned long X;
>> >> >> >__int64 Y;
>> >> >> > } abc;
>> >> >> > 
>> >> >> > 
>> >> >> > If I'm changing __int64 to unsigned long I'm not getting this
>> >> >> > error For the __int64 i'm getting the mention error. Am I missing
>> >> >> > something?
>> >> >> > 
>> >> >> > 
>> >> >> > In addition, I'm getting following error:
>> >> >> > Attribute error("'tuple object has no attibute 'children'",)
>> >> >> > 
>> >> >> > I'd be glad to have some assistance.
>> >> >> 
>> >> >> It looks like development of csjark has stopped in 2011. Try
>> >> >> installing a pycparser version from that time frame -- 2.05 should
>> >> >> be a good candidate according to
>> >> >>  so if you
>> >> >> are using pip after
>> >> >> 
>> >> >> $ pip install pycparser==2.05
>> >> >> 
>> >> >> csjark might work.
>> >> > 
>> >> > I've installed all the required SW, but still getting the same error
>> >> 
>> >> When you invoke the interactive interpreter what does
>> >> 
>> >> >>> import pycparser
>> >> >>> pycparser.__version__
>> >> '2.05'
>> >> 
>> >> produce on your system?
>> > 
>> > I have version 2.07 (which is the one used for the development)
>> 
>> For cjshark to work (or at least not fail in the way you observed) you
>> need 2.5.
>> 
>> 2.7 returns 2-tuples where 2.5 does not. Compare for example:
>> 
>> 
https://github.com/eliben/pycparser/blob/release_v2.07/pycparser/c_ast.py#L149
>> 
>> def children(self):
>> nodelist = []
>> if self.name is not None: nodelist.append(("name", self.name))
>> if self.subscript is not None: nodelist.append(("subscript",
>> self.subscript))
>> return tuple(nodelist)
>> 
>> and
>> 
>> 
https://github.com/eliben/pycparser/blob/release_v2.05/pycparser/c_ast.py#L136
>> 
>> def children(self):
>> nodelist = []
>> if self.name is not None: nodelist.append(self.name)
>> if self.subscript is not None: nodelist.append(self.subscript)
>> return tuple(nodelist)
>> 
>> I wonder why you didn't just try what I suggested...
> 
> Thanks,
> This one solved the 2nd problem.
> 
> Do you have any suggestions for the 1st one?

Here's what I see from your original post:

https://mail.python.org/pipermail/python-list/2016-September/714323.html

So I have no idea what your "1st problem" might be. Can you restate it? 

You have to use text, pictures are removed.

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


Obtain the raw line of text read by CSVDictReader when reporting errors?

2016-09-21 Thread Malcolm Greene
Looking for ideas on how I can obtain the raw line of text read by a
CSVDictReader. I've reviewed the CSV DictReader documentation and there
are no public attributes that expose this type of data.

My use case is reporting malformed lines detected when my code
validates the dict of data returned by this object. I would like to log
the actual line read by the CSVDictReader, not the processed data
returned in the dict.

Thank you,
Malcolm
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to automate java application in window using python

2016-09-21 Thread Emile

On 09/18/2016 06:37 PM, Lawrence D’Oliveiro wrote:

On Monday, September 19, 2016 at 11:32:25 AM UTC+12, Michael Torrie wrote:

One I've used is AutoIt.




Like I said, this kind of thing can never work reliably...



Hmm, then I'll have to wait longer to experience the unreliability as 
the handful of automated gui tools I'm running has only been up 10 to 12 
years or so.


Emile


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


Re: Obtain the raw line of text read by CSVDictReader when reporting errors?

2016-09-21 Thread MRAB

On 2016-09-21 21:28, Malcolm Greene wrote:

Looking for ideas on how I can obtain the raw line of text read by a
CSVDictReader. I've reviewed the CSV DictReader documentation and there
are no public attributes that expose this type of data.

My use case is reporting malformed lines detected when my code
validates the dict of data returned by this object. I would like to log
the actual line read by the CSVDictReader, not the processed data
returned in the dict.


You could put something between the file and the reader, like this:

import csv

class MyFile:
def __init__(self, file):
self.file = file

def __iter__(self):
return self

def __next__(self):
self.line = next(self.file)

return self.line

path = ''

with open(path) as file:
my_file = MyFile(file)
reader = csv.DictReader(my_file)

for row in reader:
print(repr(my_file.line), '=>', row)

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


Re: Obtain the raw line of text read by CSVDictReader when reporting errors?

2016-09-21 Thread Oscar Benjamin
On 21 September 2016 at 21:28, Malcolm Greene  wrote:
> Looking for ideas on how I can obtain the raw line of text read by a
> CSVDictReader. I've reviewed the CSV DictReader documentation and there
> are no public attributes that expose this type of data.
>
> My use case is reporting malformed lines detected when my code
> validates the dict of data returned by this object. I would like to log
> the actual line read by the CSVDictReader, not the processed data
> returned in the dict.

You can wrap the file passed into DictReader so that it yelds each
line along with the dict like so:

import csv

def csv_with_line(infile):

last_line = [None]

def line_rememberer():
for line in infile:
last_line[0] = line
yield line

rows = csv.DictReader(line_rememberer())
for lineno, row in enumerate(rows, 2):
yield lineno, last_line[0], row

infile = ['a,b,c', '1,2,3', '4,5,6']

for lineno, line, row in csv_with_line(infile):
print(lineno, line, row)


-- 
Oscar
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: strings and ints consistency - isinstance

2016-09-21 Thread John Gordon
In  Sayth Renshaw 
 writes:

> Trying to clarify why ints and strings arent treated the same.

Because they are not the same.

> You can get a valuerror from trying to cast a non-int to an int as in
> int(3.0) however you cannot do a non string with str(a).

Anything you type can be represented as a string*.  The same is
not true for integers.

* Okay, python 2.7 does have some issues with Unicode.

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: automated comparison tool

2016-09-21 Thread Kalos Kalyre
On 2016-09-21 08:55, Andrew Clark wrote:
> I reinstalled paramiko and now i'm getting a slighty different error but 
> still says no cryptography.
> 
> Traceback (most recent call last):
>   File "C:\Users\ac40935\workspace\AutoCompare\filecmp.py", line 6, in 
> 
> from paramiko import SSHConfig, SSHClient
>   File 
> "C:\Python35-32\lib\site-packages\paramiko-2.0.2-py3.5.egg\paramiko\__init__.py",
>  line 30, in 
>   File 
> "C:\Python35-32\lib\site-packages\paramiko-2.0.2-py3.5.egg\paramiko\transport.py",
>  line 32, in 
> ImportError: No module named 'cryptography'

Have you tried installing the cryptography module? I just did a quick
web search for "python cryptography module" and found this site:

https://cryptography.io/en/latest/

which says the module can be installed with pip:

pip install cryptography

-- 
Microblog: https://identi.ca/jdea
Blog: https://theopensource.ninja/



  Cut away the five (lower fetters), abandon the five (remaining fetters),
  and then develop the five (faculties). The bhikkhu who has transcended
  the five fetters is said to be "crossed over the flood". 370

>From the Dhammapada

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


Re: strings and ints consistency - isinstance

2016-09-21 Thread Steve D'Aprano
On Thu, 22 Sep 2016 12:26 am, Sayth Renshaw wrote:

> Hi
> 
> Trying to clarify why ints and strings arent treated the same.

Because ints and strings are different? :-)


> You can get a valuerror from trying to cast a non-int to an int as in
> int(3.0) however you cannot do a non string with str(a).

Now, that's incorrect. int(3.0) succeeds, it doesn't raise ValueError.

int([]) will raise TypeError.

int("hello world") will raise ValueError, because int() can convert strings,
but only strings with the right value. If the string has an invalid value,
you get a ValueError.

But str(a) should succeed for any object, any value. It should (in
principle) *always* succeed. (If it fails for some object, that object is
buggy.)


> Which means that you likely should use try and except to test if a user
> enters a non-int with valuerror. However as you can't str () and get a
> valuerror you use conditional logic with strings.

What sort of conditional logic do you think you will use to determine
whether or not str(x) will fail?

 
> Therefore to try and keep with pythons only one obvious way of doing
> things should i prefer conditional logic for all using isinstance?

No. There is no connection between the One Obvious Way principle and this.
The principle of One Obvious Way is a design principle that applies to
Python the language and its standard library. (You can also apply it to
your own libraries, because it is a good principle to use.) But it has no
connection to how you test for invalid arguments.

> That way regardless of input type my code flows the same and more
> explicitly states the intended type. Sayth

That is an over-generalisation.

Nobody should care if, in one part of your application, you test for error
conditions using try...except, and in a completely different part of your
application you use an explicit test using if. They are different parts of
code, and there is nothing wrong with them being different.

One Obvious Way does not mean "Only One Way". It means that there must be an
obvious way to solve the problem, not that all problems must be solved the
same way.

When converting arbitrary objects to ints, the obvious way is to try the
conversion and catch TypeError or ValueError if it fails. (And then what?
What will you do with the exception once you have caught it? How do you
recover from this? If you have no way to recover, you probably shouldn't
bother catching the exception.)

When converting arbitrary objects to strings, the obvious way is to assume
that it will succeed.

When dealing with operations that might fail, there are generally two ways
to handle it:

Look Before You Leap (an explicit test using if)

Easier to Ask Forgiveness than Permission (try...except)

https://docs.python.org/3/glossary.html#term-lbyl

https://docs.python.org/3/glossary.html#term-eafp

Use whichever is appropriate for the situation. There is no need to always
use one or the other.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: automated comparison tool

2016-09-21 Thread Steve D'Aprano
On Thu, 22 Sep 2016 01:55 am, Andrew Clark wrote:

> I reinstalled paramiko and now i'm getting a slighty different error but
> still says no cryptography.

[...]

> ImportError: No module named 'cryptography'


You appear to be missing a dependency of paramiko. You need to identify
which "cryptography" module it relies on, and install it. Start by reading
the paramiko documentation and especially look for "requirements"
or "dependencies".




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: get the sum of differences between integers in a list

2016-09-21 Thread Steve D'Aprano
On Thu, 22 Sep 2016 01:51 am, marco.naw...@colosso.nl wrote:

> And here is a straightforward one without any helpers:


Please don't do people's homework for them.





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Data Types

2016-09-21 Thread Steve D'Aprano
On Wed, 21 Sep 2016 10:25 pm, BartC wrote:

> On 21/09/2016 05:03, Cai Gengyang wrote:
> 
>> Are there any other data types that will give you type(A) or type(B) =
>>  besides True and False?
> 
> No types but any variable or expression containing True or False will be
> a bool type (or class bool):

"Containing" True or False? Certainly not:

py> type( [1, 2, True] )

py> type( False or 99 )


Based on your examples, I think you mean any expression *evaluating to* True
or False will be a bool. Um, yeah, of course it will. Because it evaluates
to one of the only two bool values, True or False.

>   A = 10<20
>   print (type(A))  =>  

That's because the value of A is True.

>   print (10<20)=>  True
>   print (type(10<20))  =>  

10<20 shouldn't be thought of as some alternative value which is a bool, any
more than we should think of 1+1 as being a different value to 2.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: csjark module

2016-09-21 Thread bezenchu
On Wednesday, September 21, 2016 at 10:09:25 PM UTC+3, Peter Otten wrote:
> bezen...@gmail.com wrote:
> 
> > On Wednesday, September 21, 2016 at 5:15:38 PM UTC+3, Peter Otten wrote:
> >> bezen...@gmail.com wrote:
> >> 
> >> > On Wednesday, September 21, 2016 at 3:17:11 PM UTC+3, Peter Otten
> >> > wrote:
> >> >> bezen...@gmail.com wrote:
> >> >> 
> >> >> > On Wednesday, September 21, 2016 at 1:14:14 PM UTC+3, Peter Otten
> >> >> > wrote:
> >> >> >> bezen...@gmail.com wrote:
> >> >> >> 
> >> >> >> > After setting up csjark (http://csjark.readthedocs.io/), I'm
> >> >> >> > trying to test on of my C header files which has following
> >> >> >> > statements:
> >> >> >> > 
> >> >> >> > typedef struct {
> >> >> >> >unsigned long X;
> >> >> >> >__int64 Y;
> >> >> >> > } abc;
> >> >> >> > 
> >> >> >> > 
> >> >> >> > If I'm changing __int64 to unsigned long I'm not getting this
> >> >> >> > error For the __int64 i'm getting the mention error. Am I missing
> >> >> >> > something?
> >> >> >> > 
> >> >> >> > 
> >> >> >> > In addition, I'm getting following error:
> >> >> >> > Attribute error("'tuple object has no attibute 'children'",)
> >> >> >> > 
> >> >> >> > I'd be glad to have some assistance.
> >> >> >> 
> >> >> >> It looks like development of csjark has stopped in 2011. Try
> >> >> >> installing a pycparser version from that time frame -- 2.05 should
> >> >> >> be a good candidate according to
> >> >> >>  so if you
> >> >> >> are using pip after
> >> >> >> 
> >> >> >> $ pip install pycparser==2.05
> >> >> >> 
> >> >> >> csjark might work.
> >> >> > 
> >> >> > I've installed all the required SW, but still getting the same error
> >> >> 
> >> >> When you invoke the interactive interpreter what does
> >> >> 
> >> >> >>> import pycparser
> >> >> >>> pycparser.__version__
> >> >> '2.05'
> >> >> 
> >> >> produce on your system?
> >> > 
> >> > I have version 2.07 (which is the one used for the development)
> >> 
> >> For cjshark to work (or at least not fail in the way you observed) you
> >> need 2.5.
> >> 
> >> 2.7 returns 2-tuples where 2.5 does not. Compare for example:
> >> 
> >> 
> https://github.com/eliben/pycparser/blob/release_v2.07/pycparser/c_ast.py#L149
> >> 
> >> def children(self):
> >> nodelist = []
> >> if self.name is not None: nodelist.append(("name", self.name))
> >> if self.subscript is not None: nodelist.append(("subscript",
> >> self.subscript))
> >> return tuple(nodelist)
> >> 
> >> and
> >> 
> >> 
> https://github.com/eliben/pycparser/blob/release_v2.05/pycparser/c_ast.py#L136
> >> 
> >> def children(self):
> >> nodelist = []
> >> if self.name is not None: nodelist.append(self.name)
> >> if self.subscript is not None: nodelist.append(self.subscript)
> >> return tuple(nodelist)
> >> 
> >> I wonder why you didn't just try what I suggested...
> > 
> > Thanks,
> > This one solved the 2nd problem.
> > 
> > Do you have any suggestions for the 1st one?
> 
> Here's what I see from your original post:
> 
> https://mail.python.org/pipermail/python-list/2016-September/714323.html
> 
> So I have no idea what your "1st problem" might be. Can you restate it? 
> 
> You have to use text, pictures are removed.

Clarification with example:

I'm taking the complex_union_test.h file (which is one of the test files of 
csjark) and changing on line 8 from int to __int64 so the code is

#include "union_test.h"
#include "cenum_test.h"

typedef signed int BOOL;
typedef enum {TRUE, FALSE} bool_t;

typedef union {
__int64 int_member;
struct cenum_test cenum_test_member;
long long long_long_member;
BOOL bool_member;
bool_t bool_t_member;
short short_member;
} complex_union;

struct struct_with_complex_union {
complex_union complex_union_member;
bool_t bool_t_member;
union union_test union_test_member;
};

Now trying to execute the parsing and getting following error:
Skipped "headers\complex_union_test.":Win32 as it raised 
ParseError('headers\\complex_union_test.h:8: before: __int64',)

I have similar error with my .h file.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: csjark module

2016-09-21 Thread bezenchu
On Thursday, September 22, 2016 at 5:51:43 AM UTC+3, beze...@gmail.com wrote:
> On Wednesday, September 21, 2016 at 10:09:25 PM UTC+3, Peter Otten wrote:
> > bezen...@gmail.com wrote:
> > 
> > > On Wednesday, September 21, 2016 at 5:15:38 PM UTC+3, Peter Otten wrote:
> > >> bezen...@gmail.com wrote:
> > >> 
> > >> > On Wednesday, September 21, 2016 at 3:17:11 PM UTC+3, Peter Otten
> > >> > wrote:
> > >> >> bezen...@gmail.com wrote:
> > >> >> 
> > >> >> > On Wednesday, September 21, 2016 at 1:14:14 PM UTC+3, Peter Otten
> > >> >> > wrote:
> > >> >> >> bezen...@gmail.com wrote:
> > >> >> >> 
> > >> >> >> > After setting up csjark (http://csjark.readthedocs.io/), I'm
> > >> >> >> > trying to test on of my C header files which has following
> > >> >> >> > statements:
> > >> >> >> > 
> > >> >> >> > typedef struct {
> > >> >> >> >unsigned long X;
> > >> >> >> >__int64 Y;
> > >> >> >> > } abc;
> > >> >> >> > 
> > >> >> >> > 
> > >> >> >> > If I'm changing __int64 to unsigned long I'm not getting this
> > >> >> >> > error For the __int64 i'm getting the mention error. Am I missing
> > >> >> >> > something?
> > >> >> >> > 
> > >> >> >> > 
> > >> >> >> > In addition, I'm getting following error:
> > >> >> >> > Attribute error("'tuple object has no attibute 'children'",)
> > >> >> >> > 
> > >> >> >> > I'd be glad to have some assistance.
> > >> >> >> 
> > >> >> >> It looks like development of csjark has stopped in 2011. Try
> > >> >> >> installing a pycparser version from that time frame -- 2.05 should
> > >> >> >> be a good candidate according to
> > >> >> >>  so if you
> > >> >> >> are using pip after
> > >> >> >> 
> > >> >> >> $ pip install pycparser==2.05
> > >> >> >> 
> > >> >> >> csjark might work.
> > >> >> > 
> > >> >> > I've installed all the required SW, but still getting the same error
> > >> >> 
> > >> >> When you invoke the interactive interpreter what does
> > >> >> 
> > >> >> >>> import pycparser
> > >> >> >>> pycparser.__version__
> > >> >> '2.05'
> > >> >> 
> > >> >> produce on your system?
> > >> > 
> > >> > I have version 2.07 (which is the one used for the development)
> > >> 
> > >> For cjshark to work (or at least not fail in the way you observed) you
> > >> need 2.5.
> > >> 
> > >> 2.7 returns 2-tuples where 2.5 does not. Compare for example:
> > >> 
> > >> 
> > https://github.com/eliben/pycparser/blob/release_v2.07/pycparser/c_ast.py#L149
> > >> 
> > >> def children(self):
> > >> nodelist = []
> > >> if self.name is not None: nodelist.append(("name", self.name))
> > >> if self.subscript is not None: nodelist.append(("subscript",
> > >> self.subscript))
> > >> return tuple(nodelist)
> > >> 
> > >> and
> > >> 
> > >> 
> > https://github.com/eliben/pycparser/blob/release_v2.05/pycparser/c_ast.py#L136
> > >> 
> > >> def children(self):
> > >> nodelist = []
> > >> if self.name is not None: nodelist.append(self.name)
> > >> if self.subscript is not None: nodelist.append(self.subscript)
> > >> return tuple(nodelist)
> > >> 
> > >> I wonder why you didn't just try what I suggested...
> > > 
> > > Thanks,
> > > This one solved the 2nd problem.
> > > 
> > > Do you have any suggestions for the 1st one?
> > 
> > Here's what I see from your original post:
> > 
> > https://mail.python.org/pipermail/python-list/2016-September/714323.html
> > 
> > So I have no idea what your "1st problem" might be. Can you restate it? 
> > 
> > You have to use text, pictures are removed.
> 
> Clarification with example:
> 
> I'm taking the complex_union_test.h file (which is one of the test files of 
> csjark) and changing on line 8 from int to __int64 so the code is
> 
> #include "union_test.h"
> #include "cenum_test.h"
> 
> typedef signed int BOOL;
> typedef enum {TRUE, FALSE} bool_t;
> 
> typedef union {
> __int64 int_member;
> struct cenum_test cenum_test_member;
> long long long_long_member;
> BOOL bool_member;
> bool_t bool_t_member;
> short short_member;
> } complex_union;
> 
> struct struct_with_complex_union {
> complex_union complex_union_member;
> bool_t bool_t_member;
> union union_test union_test_member;
> };
> 
> Now trying to execute the parsing and getting following error:
> Skipped "headers\complex_union_test.":Win32 as it raised 
> ParseError('headers\\complex_union_test.h:8: before: __int64',)
> 
> I have similar error with my .h file.

seems that I have a solution for it:
instead of __int64 to use "long long" statement
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: strings and ints consistency - isinstance

2016-09-21 Thread Sayth Renshaw
To answer all the good replies.

I adapted a simple vector example from the Springer book practical primer on 
science with python.

Having solved the actual problem I thought checking with the user they had the 
correct entries or would like to ammend them would be a good addition. This 
leads to the question Y or N response which isn't hard but if someone 
accidentally types 4, then you get where I got stuck can't test an int for 
ValueError if you expect a string. 

This was my code

import sys

v0 = float(input("What velocity would you like? "))
g = float(input("What gravity would you like? "))
t = float(input("What time decimal would you like? "))

print("""
We have the following inputs.
   v0 is %d
   g is  %d
   t is  %d
Is this correct? [Y/n]
""" % (v0, g, t))
while True:
try:
answer = input("\t >> ").isalpha()
print(v0 * t - 0.5 * g * t ** 2)
except ValueError as err:
print("Not a valid entry", err.args)
sys.exit()
finally:
print("would you like another?")
break

___

When I look at this SO question it splits the votes half choose try except the 
other conditional logic, neither are wrong but which is the more obvious python 
way. 
https://stackoverflow.com/questions/2020598/in-python-how-should-i-test-if-a-variable-is-none-true-or-false

___

I actually thought this would have resolved my issues and still returned error 
if ints entered however it still passes through.

answer = input("\t >> ")
if isinstance(answer, str) is True:
print(v0 * t - 0.5 * g * t ** 2)
elif int(answer) is True:
raise ValueError("Ints aren't valid input")
sys.exit()
else:
print("Ok please ammend your entries")

Thanks

Sayth
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cython taking more time than regular Python

2016-09-21 Thread Lawrence D’Oliveiro
On Wednesday, September 21, 2016 at 6:05:07 PM UTC+12, Gregory Ewing wrote:
>
> Christian Gollwitzer wrote:
>
>> It may take a microsecond to call a functino from Python.
> 
> Obviously the compiler used was a "dump" compiler. :-)

Don’t forget “functino”. :)

For some reason it awakens half-remembered memories of undergrad physics 
lectures ... is that the gauge boson for the function field? Or is that a 
“functon”? One of them might be spin-½ ...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: strings and ints consistency - isinstance

2016-09-21 Thread Sayth Renshaw
This ends being the code I can use to get it to work, seems clear and pythonic, 
open to opinion on that :-)


answer = input("\t >> ")
if isinstance(int(answer), int) is True:
raise ValueError("Ints aren't valid input")
sys.exit()
elif isinstance(answer, str) is True:
print(v0 * t - 0.5 * g * t ** 2)
else:
print("Ok please ammend your entries")

Cheers

Sayth
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Data Types

2016-09-21 Thread Sayth Renshaw

> > 
> >> Are there any other data types that will give you type(A) or type(B) =
> >>  besides True and False?
> > 
> > No types but any variable or expression containing True or False will be
> > a bool type (or class bool):
> 
> "Containing" True or False? Certainly not:
> 
> py> type( [1, 2, True] )
> 
> py> type( False or 99 )
> 
> 
> Based on your examples, I think you mean any expression *evaluating to* True
> or False will be a bool. Um, yeah, of course it will. Because it evaluates
> to one of the only two bool values, True or False.
> 
> >   A = 10<20
> >   print (type(A))  =>  
> 
> That's because the value of A is True.
> 
> >   print (10<20)=>  True
> >   print (type(10<20))  =>  
> 
> 10<20 shouldn't be thought of as some alternative value which is a bool, any
> more than we should think of 1+1 as being a different value to 2.
> 
> 

What about 0 or 1 they are true and false like no other numbers? what category 
do they fall in with regards to booleans?

In [6]: 0 == False
Out[6]: True

In [7]: 1 == True
Out[7]: True

In [8]: 2 == True
Out[8]: False

In [9]: 3 == True
Out[9]: False

In [10]: 3 == False
Out[10]: False

Sayth
-- 
https://mail.python.org/mailman/listinfo/python-list


How to import all things defined the files in a module directory in __init__.py?

2016-09-21 Thread Peng Yu
Hi,

Suppose that I have file1.py, ..., filen.py in a module directory.

I want to import all the thing (or the ones available in the
respective __all__) defined in each of the file by putting the
following lines in __init__.py

from file1 import *

from filen import *

However, I don't want to hardcode the file names in __init__.py. Is
there an automatic way of doing it?

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Is there any currently-working 3rd party snapchat API for python?

2016-09-21 Thread hhkieu
Which one do you use?
I want to build a snapchat bot that downloads video sent to my account, and 
uploads it to my story!
-- 
https://mail.python.org/mailman/listinfo/python-list


Where is import defined in the source code? (python 2)

2016-09-21 Thread Peng Yu
Hi, I want know where import is defined in the source code. Is it
implemented using __import__?

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to import all things defined the files in a module directory in __init__.py?

2016-09-21 Thread Ben Finney
Peng Yu  writes:

> I want to import all the thing (or the ones available in the
> respective __all__) defined in each of the file by putting the
> following lines in __init__.py
>
> from file1 import *
> 
> from filen import *
>
> However, I don't want to hardcode the file names in __init__.py. Is
> there an automatic way of doing it?

Why do you want to do that? It will defeat static analysis of the code,
which is one of the more important tools to make your code reliable.

It will also make the names in the code impossible to automatically
match against where they came from. Explicit is better than implicit;
you are proposing to make an unknown horde of names in the code implicit
and untraceable.

Why? What problem are you trying to solve that you believe needs this
approach?

-- 
 \   “Anything that we scientists can do to weaken the hold of |
  `\religion should be done and may in the end be our greatest |
_o__)  contribution to civilization.” —Steven Weinberg |
Ben Finney

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


Pasting code into the cmdline interpreter

2016-09-21 Thread Veek M
I wanted to test this piece of code which is Kate (editor) on the cmd 
line python >>> prompt:

tex_matches = re.findall(r'(\\\w+{.+?})|(\\\w+)', msg)
for tex_word in tex_matches:
repl = unicode_tex.tex_to_unicode_map.get(tex_word)

if repl is None:
  repl = 'err'

msg = re.sub(re.escape(tex_word), repl, msg)


There are a number of difficulties i encountered.
1. I had to turn on  highlighting to catch mixed indent (which is a 
good thing anyways so this was resolved - not sure how tabs got in 
anyhow)
2. Blank lines in my code within the editor are perfectly acceptable for 
readability but they act as a block termination on cmd line. So if i 
paste:
tex_matches = re.findall(r'(\\\w+{.+?})|(\\\w+)', msg)
for tex_word in tex_matches:
repl = unicode_tex.tex_to_unicode_map.get(tex_word)

if repl is None:

I get:
 IndentationError: unexpected indent

How do i deal with this - what's the best way to achieve what I'm trying 
to do.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pasting code into the cmdline interpreter

2016-09-21 Thread Ben Finney
Veek M  writes:

> 1. I had to turn on  highlighting to catch mixed indent (which is a 
> good thing anyways so this was resolved - not sure how tabs got in 
> anyhow)

The EditorConfig system is a growing consensus for configuring a code
base to instruct text editors not to mangle it. See the EditorConfig
site http://editorconfig.org/> for more information.

Sadly, it seems Kate does not yet recognise EditorConfig instructions
https://bugs.kde.org/show_bug.cgi?id=330843>. In the meantime,
manually configure Kate to only ever insert spaces to indent lines.

> 2. Blank lines in my code within the editor are perfectly acceptable
> for readability but they act as a block termination on cmd line.

Yes. That is a deliberate compromise to make it easy to write code
interactively at the interactive prompt.

> I get:
>  IndentationError: unexpected indent
>
> How do i deal with this - what's the best way to achieve what I'm
> trying to do.

Since you are writing code into a module file, why not just run the
module from that file with the non-interactive Python interpreter?

-- 
 \“The restriction of knowledge to an elite group destroys the |
  `\   spirit of society and leads to its intellectual |
_o__)impoverishment.” —Albert Einstein |
Ben Finney

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


Re: Cython taking more time than regular Python

2016-09-21 Thread Gregory Ewing

Lawrence D’Oliveiro wrote:


Don’t forget “functino”. :)

... is that the gauge boson for the function field? Or is that a
“functon”? One of them might be spin-½ ...


A functino would be the supersymmetric partner of a bosonic
function. For a fermionic function, its partner would be
a sfunction.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.5.1 C API, the global available available is not destroyed when delete the module

2016-09-21 Thread dieter
dl l  writes:
> I understood call PyGC_collect to release object cycles. But in my python
> test code, seems there is NO reference cycle on the Simple object in the
> python test code. Why needs to call PyGC_Collect() here?
>
> =
>
> class Simple:
>  def __init__( self ):
>  print('Simple__init__')
>  def __del__( self ):
>  print('Simple__del__')
>
> simple = None
>
> def run():
> global simple
> simple = Simple()
>
> if __name__ == '__main__':
>   run()

I do not see a cycle in this example. Thus, maybe, Python itself
has introduced one (maybe via "__builtins__").

To further analyse the case, I would start by checking the "gc" module's
documentation to find out, whether it provides a way to get information
about the cycles it has found (and would have deleted if it had
not generated references for this analysis).

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


Re: Pasting code into the cmdline interpreter

2016-09-21 Thread Christian Gollwitzer

Am 22.09.16 um 07:12 schrieb Veek M:

I wanted to test this piece of code which is Kate (editor) on the cmd
line python >>> prompt:
2. Blank lines in my code within the editor are perfectly acceptable for
readability but they act as a block termination on cmd line. So if i
paste:
tex_matches = re.findall(r'(\\\w+{.+?})|(\\\w+)', msg)
for tex_word in tex_matches:
repl = unicode_tex.tex_to_unicode_map.get(tex_word)

if repl is None:

I get:
 IndentationError: unexpected indent


IPython has a %paste and %cpaste magic to deal with this and other 
problems. It won't resolve the tab issue. However, Python code in 
general (due to indentation) is not very friendly for pasting and 
interactive usage. Better write the code to a file, then (again from 
IPython) you can read this by %run filename.py


Christian

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


Re: Pasting code into the cmdline interpreter

2016-09-21 Thread Veek M
Ben Finney wrote:

> Veek M  writes:
> 
>> 1. I had to turn on  highlighting to catch mixed indent (which
>> is a good thing anyways so this was resolved - not sure how tabs got
>> in anyhow)
> 
> The EditorConfig system is a growing consensus for configuring a code
> base to instruct text editors not to mangle it. See the EditorConfig
> site http://editorconfig.org/> for more information.
> 
> Sadly, it seems Kate does not yet recognise EditorConfig instructions
> https://bugs.kde.org/show_bug.cgi?id=330843>. In the meantime,
> manually configure Kate to only ever insert spaces to indent lines.
> 
>> 2. Blank lines in my code within the editor are perfectly acceptable
>> for readability but they act as a block termination on cmd line.
> 
> Yes. That is a deliberate compromise to make it easy to write code
> interactively at the interactive prompt.
> 
>> I get:
>>  IndentationError: unexpected indent
>>
>> How do i deal with this - what's the best way to achieve what I'm
>> trying to do.
> 
> Since you are writing code into a module file, why not just run the
> module from that file with the non-interactive Python interpreter?
> 
It's part of a hexchat plugin/addon.. like below (basically lots of 
hexchat crud interspersed. 

Anyway, i should hive it off into a separate function, and hide the 
import hexchat - still, bit of a pain.

import hexchat

def send_message(word, word_eol, userdata):
if not(word[0] == "65293"):
return

msg = hexchat.get_info('inputbox')
if msg is None:
return

x = re.match(r'(^\\help)\s+(\w+)', msg)
if x:
filter = x.groups()[1]
for key, value in unicode_tex.tex_to_unicode_map.items():
if filter in key:
print_help(value + ' ' + key)
hexchat.command("settext %s" % '')
return

# findall returns: [(,), (,), ...] or []
tex_matches = re.findall(r'((\\\w+){(.+?)})|(\\\w+)', msg)
if len(tex_matches) == 0: 
  return
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pasting code into the cmdline interpreter

2016-09-21 Thread Ben Finney
Veek M  writes:

> Ben Finney wrote:
>
> > Since you are writing code into a module file, why not just run the
> > module from that file with the non-interactive Python interpreter?
> > 
> It's part of a hexchat plugin/addon..

Which tells me that it still isn't appropriate to copy-paste the code
directly into the *interactive* Python interpreter. Instead, it's a
module that should be imported from file, by Hexchat.

Unless I misunderstand what you're talking about?

-- 
 \ “Capitalism has destroyed our belief in any effective power but |
  `\  that of self interest backed by force.” —George Bernard Shaw |
_o__)  |
Ben Finney

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