how to set timeout for os.popen

2018-04-15 Thread Ho Yeung Lee
while 1:
runner = os.popen("tracert -d www.hello.com")
o=runner.read()
print(o)
runner.close()
runner = os.popen("tracert -d www.hello.com")
o=runner.read()
print(o)
runner.close()
runner = os.popen("tracert -d www.hello.com")
o=runner.read()
print(o)
runner.close()


after running over 1 hour, it stop and not return in one of tracert

how to set timeout and know that this is timeout?
-- 
https://mail.python.org/mailman/listinfo/python-list


joblib AttributeError: 'module' object has no attribute

2018-04-14 Thread Ho Yeung Lee
Process PoolWorker-1:
Traceback (most recent call last):
  File "C:\Python27\lib\multiprocessing\process.py", line 258, in _bootstrap
self.run()
  File "C:\Python27\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
  File "C:\Python27\lib\multiprocessing\pool.py", line 102, in worker
task = get()
  File "C:\Python27\lib\site-packages\joblib\pool.py", line 362, in get
return recv()
AttributeError: 'module' object has no attribute 'easysearch'


how to solve this bug?

import re
import string
from itertools import permutations
from itertools import combinations
from joblib import Parallel, delayed
import multiprocessing
from multiprocessing import Process, freeze_support

all_normal_characters = string.ascii_letters + string.digits
def is_special(character):
return character not in all_normal_characters

def easysearch(content):
if len(str(content[0]).strip()) == 0 or len(str(content[1]).strip()) == 0:
return ""
row1 = content[0]
keywords = content[1]
...

num_cores = multiprocessing.cpu_count()

def segmentsearch(content, findwhat, lensize):
chunks, chunk_size = len(content), lensize
result = 
Parallel(n_jobs=num_cores)(delayed(easysearch)([content[j:j+chunk_size], 
findwhat]) for j in range(0, chunks, chunk_size))
print(result)
return result

def main():
result = segmentsearch("search key$  @   $  wrds today", "key words", 
77)
print(result)


if __name__=="__main__":
freeze_support()
main()


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


Re: why it append one more letter after decode?

2017-10-29 Thread Ho Yeung Lee

i fixed by replace("","\\")


On Monday, October 30, 2017 at 11:16:02 AM UTC+8, Igor Korot wrote:
> Hi,
> 
> On Sun, Oct 29, 2017 at 10:03 PM, Ho Yeung Lee <jobmatt...@gmail.com> wrote:
> > i discover when
> > it
> > decode("\\",1,2)
> > in interactive console
> > is [
> >
> > but decode in big project
> > it see as
> > decode(r"\\",1,2)
> > [[
> >
> > it double
> 
> Those 2 lines are different.
> 
> Thank you.
> 
> >
> > On Monday, October 30, 2017 at 9:51:01 AM UTC+8, Ho Yeung Lee wrote:
> >> if run these function to decode in python interactive console,
> >> it can decode correct,
> >>
> >> but when run with a big project, it append a letter Y
> >>
> >>
> >> On Monday, October 30, 2017 at 9:48:36 AM UTC+8, Ho Yeung Lee wrote:
> >> > def leftrotate(l, n):
> >> > return l[n:] + l[:n]
> >> >
> >> > def rightrotate(l, n):
> >> > return l[-n:] + l[:-n]
> >> >
> >> > def encode(s, k, kk):
> >> > l = [ord(i) for i in s]
> >> > return leftrotate(''.join([chr(i + k) for i in l]), kk)
> >> >
> >> > def decode(s, k, kk):
> >> > l = [ord(i) for i in rightrotate(s, kk)]
> >> > return ''.join([chr(i - k) for i in l])
> >> >
> >> >
> >> > yesterday i add above code and run it with batch file
> >> > it can decode a correct password
> >> >
> >> > then i install cx_freeze to produce executable file
> >> >
> >> > but today when i run it, i see the source of encrypted password is 
> >> > correct
> >> > but the decode one, it append one more letter Y at the end of string
> >> >
> >> > why?
> >> > is cx_freeze change the source python 2.7?
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list

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


Re: why it append one more letter after decode?

2017-10-29 Thread Ho Yeung Lee
it seems that it read csv file
from "\\" to ""

On Monday, October 30, 2017 at 11:03:57 AM UTC+8, Ho Yeung Lee wrote:
> i discover when 
> it 
> decode("\\",1,2)
> in interactive console
> is [
> 
> but decode in big project
> it see as
> decode(r"\\",1,2)
> [[
> 
> it double
> 
> On Monday, October 30, 2017 at 9:51:01 AM UTC+8, Ho Yeung Lee wrote:
> > if run these function to decode in python interactive console,
> > it can decode correct, 
> > 
> > but when run with a big project, it append a letter Y
> > 
> > 
> > On Monday, October 30, 2017 at 9:48:36 AM UTC+8, Ho Yeung Lee wrote:
> > > def leftrotate(l, n):
> > > return l[n:] + l[:n]
> > > 
> > > def rightrotate(l, n):
> > > return l[-n:] + l[:-n]
> > > 
> > > def encode(s, k, kk):
> > > l = [ord(i) for i in s]
> > > return leftrotate(''.join([chr(i + k) for i in l]), kk)
> > > 
> > > def decode(s, k, kk):
> > > l = [ord(i) for i in rightrotate(s, kk)]
> > > return ''.join([chr(i - k) for i in l])
> > > 
> > > 
> > > yesterday i add above code and run it with batch file 
> > > it can decode a correct password
> > > 
> > > then i install cx_freeze to produce executable file
> > > 
> > > but today when i run it, i see the source of encrypted password is correct
> > > but the decode one, it append one more letter Y at the end of string
> > > 
> > > why?
> > > is cx_freeze change the source python 2.7?

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


Re: why it append one more letter after decode?

2017-10-29 Thread Ho Yeung Lee
i discover when 
it 
decode("\\",1,2)
in interactive console
is [

but decode in big project
it see as
decode(r"\\",1,2)
[[

it double

On Monday, October 30, 2017 at 9:51:01 AM UTC+8, Ho Yeung Lee wrote:
> if run these function to decode in python interactive console,
> it can decode correct, 
> 
> but when run with a big project, it append a letter Y
> 
> 
> On Monday, October 30, 2017 at 9:48:36 AM UTC+8, Ho Yeung Lee wrote:
> > def leftrotate(l, n):
> > return l[n:] + l[:n]
> > 
> > def rightrotate(l, n):
> > return l[-n:] + l[:-n]
> > 
> > def encode(s, k, kk):
> > l = [ord(i) for i in s]
> > return leftrotate(''.join([chr(i + k) for i in l]), kk)
> > 
> > def decode(s, k, kk):
> > l = [ord(i) for i in rightrotate(s, kk)]
> > return ''.join([chr(i - k) for i in l])
> > 
> > 
> > yesterday i add above code and run it with batch file 
> > it can decode a correct password
> > 
> > then i install cx_freeze to produce executable file
> > 
> > but today when i run it, i see the source of encrypted password is correct
> > but the decode one, it append one more letter Y at the end of string
> > 
> > why?
> > is cx_freeze change the source python 2.7?

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


ValueError: Error 3 while encrypting in ECB mode

2017-10-29 Thread Ho Yeung Lee
>>> from Crypto.Cipher import AES
>>>
>>> key = 'mysecretpassword'
>>> plaintext = 'Secret Message A'
>>> encobj = AES.new(key, AES.MODE_ECB)
>>> ciphertext = encobj.encrypt("hello")
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python27\lib\site-packages\Crypto\Cipher\_mode_ecb.py", line 124, in 
encrypt
raise ValueError("Error %d while encrypting in ECB mode" % result)
ValueError: Error 3 while encrypting in ECB mode
>>> ciphertext = encobj.encrypt("hellojalsdjflkdsjflds")
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python27\lib\site-packages\Crypto\Cipher\_mode_ecb.py", line 124, in 
encrypt
raise ValueError("Error %d while encrypting in ECB mode" % result)
ValueError: Error 3 while encrypting in ECB mode
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why it append one more letter after decode?

2017-10-29 Thread Ho Yeung Lee

if run these function to decode in python interactive console,
it can decode correct, 

but when run with a big project, it append a letter Y


On Monday, October 30, 2017 at 9:48:36 AM UTC+8, Ho Yeung Lee wrote:
> def leftrotate(l, n):
> return l[n:] + l[:n]
> 
> def rightrotate(l, n):
> return l[-n:] + l[:-n]
> 
> def encode(s, k, kk):
> l = [ord(i) for i in s]
> return leftrotate(''.join([chr(i + k) for i in l]), kk)
> 
> def decode(s, k, kk):
> l = [ord(i) for i in rightrotate(s, kk)]
> return ''.join([chr(i - k) for i in l])
> 
> 
> yesterday i add above code and run it with batch file 
> it can decode a correct password
> 
> then i install cx_freeze to produce executable file
> 
> but today when i run it, i see the source of encrypted password is correct
> but the decode one, it append one more letter Y at the end of string
> 
> why?
> is cx_freeze change the source python 2.7?

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


why it append one more letter after decode?

2017-10-29 Thread Ho Yeung Lee
def leftrotate(l, n):
return l[n:] + l[:n]

def rightrotate(l, n):
return l[-n:] + l[:-n]

def encode(s, k, kk):
l = [ord(i) for i in s]
return leftrotate(''.join([chr(i + k) for i in l]), kk)

def decode(s, k, kk):
l = [ord(i) for i in rightrotate(s, kk)]
return ''.join([chr(i - k) for i in l])


yesterday i add above code and run it with batch file 
it can decode a correct password

then i install cx_freeze to produce executable file

but today when i run it, i see the source of encrypted password is correct
but the decode one, it append one more letter Y at the end of string

why?
is cx_freeze change the source python 2.7?
-- 
https://mail.python.org/mailman/listinfo/python-list


how to create root with treelib?

2017-08-19 Thread Ho Yeung Lee
http://treelib.readthedocs.io/en/latest/examples.html

tree = Tree()
#create root
tree.create_node((0,0), "root")
result = [aa[0]]
previousnode = (0,0)

>>> #create root
... tree.create_node((0,0), "root")
Traceback (most recent call last):
  File "", line 2, in 
  File "C:\Python27\lib\site-packages\treelib\node.py", line 142, in __repr__
"tag=%r" % self.tag,
TypeError: not all arguments converted during string formatting
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why k means do not rectangle the red area drawn by harris corner?

2017-08-14 Thread Ho Yeung Lee
my code can run without error,

you can try to download one of face from search keyword "face"
and try this script


On Tuesday, August 15, 2017 at 4:33:51 AM UTC+8, jlad...@itu.edu wrote:
> On Monday, August 14, 2017 at 12:30:21 PM UTC-7, Ho Yeung Lee wrote:
> > https://gist.github.com/hoyeunglee/df7e6cb9b76c576b26fd2bb2b26bfe2f
> > 
> > sample image
> > https://drive.google.com/file/d/0Bxs_ao6uuBDUY09qM1JMWS1Ob0k/view?usp=sharing
> > 
> > would like to rectangle bound the eye  ,mouth corner etc
> > which detected by harris corner
> > 
> > but using kmeans can not rectangle the red area drawn by harris corner
> 
> Does your code run without errors?  You appear to be asking more general 
> question, about machine learning and image processing algorithms, than about 
> Python.

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


Re: why k means do not rectangle the red area drawn by harris corner?

2017-08-14 Thread Ho Yeung Lee

i would like to ask whether 

at line
dd = dst>0.01*dst.max()

and
the part of code below from script, whether write wrong in array access or 
assignment

testing1 = zip(*np.where(dd == 1))
locations = testing1
testing1 = [list((float(elem[0]),float(elem[1]))) for elem in testing1]
centroids,_ = kmeans(testing1,10)
row,col = vq(testing1,centroids)
locationsgroup = {}
keyone = len(locationsgroup)+1
for jj in range(0, len(row)):
for ii in range(0, len(np.nonzero(row == jj)[0])):
locationsgroup = addtogroupkey(locationsgroup, keyone, 
locations[np.nonzero(row == jj)[0][ii]])
keyone = len(locationsgroup)+1
#for ii in range(0, len(np.nonzero(row == jj)[0])):
#locationsgroup = addtogroupkey(locationsgroup, keyone, 
locations[np.nonzero(row == jj)[0][ii]])
#keyone = len(locationsgroup)+1
colortolocation[eachcolor] = locationsgroup





On Tuesday, August 15, 2017 at 4:33:51 AM UTC+8, jlad...@itu.edu wrote:
> On Monday, August 14, 2017 at 12:30:21 PM UTC-7, Ho Yeung Lee wrote:
> > https://gist.github.com/hoyeunglee/df7e6cb9b76c576b26fd2bb2b26bfe2f
> > 
> > sample image
> > https://drive.google.com/file/d/0Bxs_ao6uuBDUY09qM1JMWS1Ob0k/view?usp=sharing
> > 
> > would like to rectangle bound the eye  ,mouth corner etc
> > which detected by harris corner
> > 
> > but using kmeans can not rectangle the red area drawn by harris corner
> 
> Does your code run without errors?  You appear to be asking more general 
> question, about machine learning and image processing algorithms, than about 
> Python.

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


why k means do not rectangle the red area drawn by harris corner?

2017-08-14 Thread Ho Yeung Lee
https://gist.github.com/hoyeunglee/df7e6cb9b76c576b26fd2bb2b26bfe2f

sample image
https://drive.google.com/file/d/0Bxs_ao6uuBDUY09qM1JMWS1Ob0k/view?usp=sharing

would like to rectangle bound the eye  ,mouth corner etc
which detected by harris corner

but using kmeans can not rectangle the red area drawn by harris corner
-- 
https://mail.python.org/mailman/listinfo/python-list


ValueError: operands could not be broadcast together with shapes

2017-08-06 Thread Ho Yeung Lee
def mse(imageA, imageB):
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
return err

original = cv2.imread("C:\\Users\\hello\\Documents\\words\\" + xx)
later = cv2.imread("C:\\Users\\hello\\Documents\\words\\"+ yy)
mserr = mse(original, later)

Traceback (most recent call last):
  File "words29.py", line 135, in 
mserr = mse(original, later)
  File "words29.py", line 27, in mse
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
ValueError: operands could not be broadcast together with shapes (7,29,3) 
(7,37,3)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to sort a list of tuples with custom function

2017-08-04 Thread Ho Yeung Lee
On Friday, August 4, 2017 at 10:08:56 PM UTC+8, Ho Yeung Lee wrote:
> i had changed to use kmeans
> 
> https://gist.github.com/hoyeunglee/2475391ad554e3d2b2a40ec24ab47940
> 
> i do not know whether write it correctly
> but it seems can cluster to find words in window, but not perfect
> 
> 
> On Wednesday, August 2, 2017 at 3:06:40 PM UTC+8, Peter Otten wrote:
> > Glenn Linderman wrote:
> > 
> > > On 8/1/2017 2:10 PM, Piet van Oostrum wrote:
> > >> Ho Yeung Lee <jobmatt...@gmail.com> writes:
> > >>
> > >>> def isneighborlocation(lo1, lo2):
> > >>>  if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
> > >>>  return 1
> > >>>  elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
> > >>>  return 1
> > >>>  elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
> > >>>  return 1
> > >>>  else:
> > >>>  return 0
> > >>>
> > >>>
> > >>> sorted(testing1, key=lambda x: (isneighborlocation.get(x[0]), x[1]))
> > >>>
> > >>> return something like
> > >>> [(1,2),(3,3),(2,5)]
> > 
> > >> I think you are trying to sort a list of two-dimensional points into a
> > >> one-dimensiqonal list in such a way thet points that are close together
> > >> in the two-dimensional sense will also be close together in the
> > >> one-dimensional list. But that is impossible.
> > 
> > > It's not impossible, it just requires an appropriate distance function
> > > used in the sort.
> > 
> > That's a grossly misleading addition. 
> > 
> > Once you have an appropriate clustering algorithm
> > 
> > clusters = split_into_clusters(items) # needs access to all items
> > 
> > you can devise a key function
> > 
> > def get_cluster(item, clusters=split_into_clusters(items)):
> > return next(
> > index for index, cluster in enumerate(clusters) if item in cluster
> > )
> > 
> > such that
> > 
> > grouped_items = sorted(items, key=get_cluster)
> > 
> > but that's a roundabout way to write
> > 
> > grouped_items = sum(split_into_clusters(items), [])
> > 
> > In other words: sorting is useless, what you really need is a suitable 
> > approach to split the data into groups. 
> > 
> > One well-known algorithm is k-means clustering:
> > 
> > https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html
> > 
> > Here is an example with pictures:
> > 
> > https://dzone.com/articles/k-means-clustering-scipy


i use number of clusters = 120

https://gist.github.com/hoyeunglee/2475391ad554e3d2b2a40ec24ab47940
https://drive.google.com/file/d/0Bxs_ao6uuBDUZFByNVgzd0Jrdm8/view?usp=sharing

using my previous is not suitable for english words
but using kmeans is better, however not perfect to cluster words, 
some words are missing
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to group by function if one of the group has relationship with another one in the group?

2017-08-04 Thread Ho Yeung Lee
On Wednesday, August 2, 2017 at 5:25:21 AM UTC+8, Piet van Oostrum wrote:
> Ho Yeung Lee <jobmatt...@gmail.com> writes:
> 
> > which function should be used for this problem?
> >
> I think it is a kind if clustering, or a connectivity problem. There are 
> special algorithms for that, not just a simple function. Maybe scikit-learn 
> has a suitable algorithm for it.
> -- 
> Piet van Oostrum <pie...@vanoostrum.org>
> WWW: http://piet.vanoostrum.org/
> PGP key: [8DAE142BE17999C4]


i use number of clusters = 120

https://gist.github.com/hoyeunglee/2475391ad554e3d2b2a40ec24ab47940
https://drive.google.com/file/d/0Bxs_ao6uuBDUZFByNVgzd0Jrdm8/view?usp=sharing

seems better, but still has a long distance to be perfect
is there any more that can do for perfect
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] Re: how to guess the number of cluster when do not know?

2017-08-04 Thread Ho Yeung Lee
i use number of clusters = 120

https://drive.google.com/file/d/0Bxs_ao6uuBDUZFByNVgzd0Jrdm8/view?usp=sharing

seems better, but still has a long distance to be perfect

On Friday, August 4, 2017 at 10:09:59 PM UTC+8, Ho Yeung Lee wrote:
> actually i am using python's kmeans library. it is relevant in python
> 
> i had changed to use kmeans
> 
> https://gist.github.com/hoyeunglee/2475391ad554e3d2b2a40ec24ab47940
> 
> i do not know whether write it correctly
> but it seems can cluster to find words in window, but not perfect
> 
> 
> 
> On Friday, August 4, 2017 at 8:24:54 PM UTC+8, Alain Ketterlin wrote:
> > Ho Yeung Lee <jobmatt...@gmail.com> writes:
> > 
> > > i find kmeans has to input number of cluster
> > [...]
> > 
> > https://en.wikipedia.org/wiki/Determining_the_number_of_clusters_in_a_data_set
> > 
> > Completely off-topic on this group/list, please direct your questions
> > elsewhere.
> > 
> > -- Alain.

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


Re: [OT] Re: how to guess the number of cluster when do not know?

2017-08-04 Thread Ho Yeung Lee
actually i am using python's kmeans library. it is relevant in python

i had changed to use kmeans

https://gist.github.com/hoyeunglee/2475391ad554e3d2b2a40ec24ab47940

i do not know whether write it correctly
but it seems can cluster to find words in window, but not perfect



On Friday, August 4, 2017 at 8:24:54 PM UTC+8, Alain Ketterlin wrote:
> Ho Yeung Lee <jobmatt...@gmail.com> writes:
> 
> > i find kmeans has to input number of cluster
> [...]
> 
> https://en.wikipedia.org/wiki/Determining_the_number_of_clusters_in_a_data_set
> 
> Completely off-topic on this group/list, please direct your questions
> elsewhere.
> 
> -- Alain.

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


Re: how to sort a list of tuples with custom function

2017-08-04 Thread Ho Yeung Lee
i had changed to use kmeans

https://gist.github.com/hoyeunglee/2475391ad554e3d2b2a40ec24ab47940

i do not know whether write it correctly
but it seems can cluster to find words in window, but not perfect


On Wednesday, August 2, 2017 at 3:06:40 PM UTC+8, Peter Otten wrote:
> Glenn Linderman wrote:
> 
> > On 8/1/2017 2:10 PM, Piet van Oostrum wrote:
> >> Ho Yeung Lee <jobmatt...@gmail.com> writes:
> >>
> >>> def isneighborlocation(lo1, lo2):
> >>>  if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
> >>>  return 1
> >>>  elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
> >>>  return 1
> >>>  elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
> >>>  return 1
> >>>  else:
> >>>  return 0
> >>>
> >>>
> >>> sorted(testing1, key=lambda x: (isneighborlocation.get(x[0]), x[1]))
> >>>
> >>> return something like
> >>> [(1,2),(3,3),(2,5)]
> 
> >> I think you are trying to sort a list of two-dimensional points into a
> >> one-dimensiqonal list in such a way thet points that are close together
> >> in the two-dimensional sense will also be close together in the
> >> one-dimensional list. But that is impossible.
> 
> > It's not impossible, it just requires an appropriate distance function
> > used in the sort.
> 
> That's a grossly misleading addition. 
> 
> Once you have an appropriate clustering algorithm
> 
> clusters = split_into_clusters(items) # needs access to all items
> 
> you can devise a key function
> 
> def get_cluster(item, clusters=split_into_clusters(items)):
> return next(
> index for index, cluster in enumerate(clusters) if item in cluster
> )
> 
> such that
> 
> grouped_items = sorted(items, key=get_cluster)
> 
> but that's a roundabout way to write
> 
> grouped_items = sum(split_into_clusters(items), [])
> 
> In other words: sorting is useless, what you really need is a suitable 
> approach to split the data into groups. 
> 
> One well-known algorithm is k-means clustering:
> 
> https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html
> 
> Here is an example with pictures:
> 
> https://dzone.com/articles/k-means-clustering-scipy

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


how to guess the number of cluster when do not know?

2017-08-04 Thread Ho Yeung Lee
i find kmeans has to input number of cluster
but i do not know how many words in photo before recognization in application 
of robots vision recognition

if separate high, will it become letters instead of word?


from pylab import plot,show
from numpy import vstack,array
from numpy.random import rand
from scipy.cluster.vq import kmeans,vq

# data generation
data = vstack((rand(150,2) + array([.5,.5]),rand(150,2)))

# computing K-Means with K = 2 (2 clusters)
centroids,_ = kmeans(data,2)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to sort a list of tuples with custom function

2017-08-03 Thread Ho Yeung Lee
I remove red line 
and capture another version

https://gist.github.com/hoyeunglee/99bbe7999bc489a79ffdf0277e80ecb6

it can capture words in windows, but since window words some are black
and some gray, some are not exactly black, 
so I only choose notepad , since it is using black words

but some words are splitted, I have already sorted by x[0] and x[1]

can it improver to a consecutively a few words

"檔案" <- File is succeed
but "另存新檔"  failed since words are splitted

On Thursday, August 3, 2017 at 3:54:13 PM UTC+8, Ho Yeung Lee wrote:
> https://gist.github.com/hoyeunglee/3d340ab4e9a3e2b7ad7307322055b550
> 
> I updated again
> 
> how to do better because some words are stored in different files
> 
> On Thursday, August 3, 2017 at 10:02:01 AM UTC+8, Ho Yeung Lee wrote:
> > https://gist.github.com/hoyeunglee/f371f66d55f90dda043f7e7fea38ffa2
> > 
> > I am near succeed in another way, please run above code
> > 
> > when so much black words, it will be very slow
> > so I only open notepad and maximum it without any content
> > then capture screen and save as roster.png
> > 
> > and run it, but I discover it can not circle all words with red rectangle
> > and only part of words
> > 
> > 
> > On Wednesday, August 2, 2017 at 3:06:40 PM UTC+8, Peter Otten wrote:
> > > Glenn Linderman wrote:
> > > 
> > > > On 8/1/2017 2:10 PM, Piet van Oostrum wrote:
> > > >> Ho Yeung Lee <jobmatt...@gmail.com> writes:
> > > >>
> > > >>> def isneighborlocation(lo1, lo2):
> > > >>>  if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
> > > >>>  return 1
> > > >>>  elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
> > > >>>  return 1
> > > >>>  elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
> > > >>>  return 1
> > > >>>  else:
> > > >>>  return 0
> > > >>>
> > > >>>
> > > >>> sorted(testing1, key=lambda x: (isneighborlocation.get(x[0]), x[1]))
> > > >>>
> > > >>> return something like
> > > >>> [(1,2),(3,3),(2,5)]
> > > 
> > > >> I think you are trying to sort a list of two-dimensional points into a
> > > >> one-dimensiqonal list in such a way thet points that are close together
> > > >> in the two-dimensional sense will also be close together in the
> > > >> one-dimensional list. But that is impossible.
> > > 
> > > > It's not impossible, it just requires an appropriate distance function
> > > > used in the sort.
> > > 
> > > That's a grossly misleading addition. 
> > > 
> > > Once you have an appropriate clustering algorithm
> > > 
> > > clusters = split_into_clusters(items) # needs access to all items
> > > 
> > > you can devise a key function
> > > 
> > > def get_cluster(item, clusters=split_into_clusters(items)):
> > > return next(
> > > index for index, cluster in enumerate(clusters) if item in cluster
> > > )
> > > 
> > > such that
> > > 
> > > grouped_items = sorted(items, key=get_cluster)
> > > 
> > > but that's a roundabout way to write
> > > 
> > > grouped_items = sum(split_into_clusters(items), [])
> > > 
> > > In other words: sorting is useless, what you really need is a suitable 
> > > approach to split the data into groups. 
> > > 
> > > One well-known algorithm is k-means clustering:
> > > 
> > > https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html
> > > 
> > > Here is an example with pictures:
> > > 
> > > https://dzone.com/articles/k-means-clustering-scipy

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


Re: how to sort a list of tuples with custom function

2017-08-03 Thread Ho Yeung Lee
https://gist.github.com/hoyeunglee/3d340ab4e9a3e2b7ad7307322055b550

I updated again

how to do better because some words are stored in different files

On Thursday, August 3, 2017 at 10:02:01 AM UTC+8, Ho Yeung Lee wrote:
> https://gist.github.com/hoyeunglee/f371f66d55f90dda043f7e7fea38ffa2
> 
> I am near succeed in another way, please run above code
> 
> when so much black words, it will be very slow
> so I only open notepad and maximum it without any content
> then capture screen and save as roster.png
> 
> and run it, but I discover it can not circle all words with red rectangle
> and only part of words
> 
> 
> On Wednesday, August 2, 2017 at 3:06:40 PM UTC+8, Peter Otten wrote:
> > Glenn Linderman wrote:
> > 
> > > On 8/1/2017 2:10 PM, Piet van Oostrum wrote:
> > >> Ho Yeung Lee <jobmatt...@gmail.com> writes:
> > >>
> > >>> def isneighborlocation(lo1, lo2):
> > >>>  if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
> > >>>  return 1
> > >>>  elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
> > >>>  return 1
> > >>>  elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
> > >>>  return 1
> > >>>  else:
> > >>>  return 0
> > >>>
> > >>>
> > >>> sorted(testing1, key=lambda x: (isneighborlocation.get(x[0]), x[1]))
> > >>>
> > >>> return something like
> > >>> [(1,2),(3,3),(2,5)]
> > 
> > >> I think you are trying to sort a list of two-dimensional points into a
> > >> one-dimensiqonal list in such a way thet points that are close together
> > >> in the two-dimensional sense will also be close together in the
> > >> one-dimensional list. But that is impossible.
> > 
> > > It's not impossible, it just requires an appropriate distance function
> > > used in the sort.
> > 
> > That's a grossly misleading addition. 
> > 
> > Once you have an appropriate clustering algorithm
> > 
> > clusters = split_into_clusters(items) # needs access to all items
> > 
> > you can devise a key function
> > 
> > def get_cluster(item, clusters=split_into_clusters(items)):
> > return next(
> > index for index, cluster in enumerate(clusters) if item in cluster
> > )
> > 
> > such that
> > 
> > grouped_items = sorted(items, key=get_cluster)
> > 
> > but that's a roundabout way to write
> > 
> > grouped_items = sum(split_into_clusters(items), [])
> > 
> > In other words: sorting is useless, what you really need is a suitable 
> > approach to split the data into groups. 
> > 
> > One well-known algorithm is k-means clustering:
> > 
> > https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html
> > 
> > Here is an example with pictures:
> > 
> > https://dzone.com/articles/k-means-clustering-scipy

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


Re: how to sort a list of tuples with custom function

2017-08-02 Thread Ho Yeung Lee
https://gist.github.com/hoyeunglee/f371f66d55f90dda043f7e7fea38ffa2

I am near succeed in another way, please run above code

when so much black words, it will be very slow
so I only open notepad and maximum it without any content
then capture screen and save as roster.png

and run it, but I discover it can not circle all words with red rectangle
and only part of words


On Wednesday, August 2, 2017 at 3:06:40 PM UTC+8, Peter Otten wrote:
> Glenn Linderman wrote:
> 
> > On 8/1/2017 2:10 PM, Piet van Oostrum wrote:
> >> Ho Yeung Lee <jobmatt...@gmail.com> writes:
> >>
> >>> def isneighborlocation(lo1, lo2):
> >>>  if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
> >>>  return 1
> >>>  elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
> >>>  return 1
> >>>  elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
> >>>  return 1
> >>>  else:
> >>>  return 0
> >>>
> >>>
> >>> sorted(testing1, key=lambda x: (isneighborlocation.get(x[0]), x[1]))
> >>>
> >>> return something like
> >>> [(1,2),(3,3),(2,5)]
> 
> >> I think you are trying to sort a list of two-dimensional points into a
> >> one-dimensiqonal list in such a way thet points that are close together
> >> in the two-dimensional sense will also be close together in the
> >> one-dimensional list. But that is impossible.
> 
> > It's not impossible, it just requires an appropriate distance function
> > used in the sort.
> 
> That's a grossly misleading addition. 
> 
> Once you have an appropriate clustering algorithm
> 
> clusters = split_into_clusters(items) # needs access to all items
> 
> you can devise a key function
> 
> def get_cluster(item, clusters=split_into_clusters(items)):
> return next(
> index for index, cluster in enumerate(clusters) if item in cluster
> )
> 
> such that
> 
> grouped_items = sorted(items, key=get_cluster)
> 
> but that's a roundabout way to write
> 
> grouped_items = sum(split_into_clusters(items), [])
> 
> In other words: sorting is useless, what you really need is a suitable 
> approach to split the data into groups. 
> 
> One well-known algorithm is k-means clustering:
> 
> https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html
> 
> Here is an example with pictures:
> 
> https://dzone.com/articles/k-means-clustering-scipy

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


Re: how to sort a list of tuples with custom function

2017-08-01 Thread Ho Yeung Lee
how to write this distance function in sort
there are the syntax error 


On Wednesday, August 2, 2017 at 6:03:13 AM UTC+8, Glenn Linderman wrote:
> On 8/1/2017 2:10 PM, Piet van Oostrum wrote:
> > Ho Yeung Lee <jobmatt...@gmail.com> writes:
> >
> >> def isneighborlocation(lo1, lo2):
> >>  if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
> >>  return 1
> >>  elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
> >>  return 1
> >>  elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
> >>  return 1
> >>  else:
> >>  return 0
> >>
> >>
> >> sorted(testing1, key=lambda x: (isneighborlocation.get(x[0]), x[1]))
> >>
> >> return something like
> >> [(1,2),(3,3),(2,5)]
> > I think you are trying to sort a list of two-dimensional points into a
> > one-dimensiqonal list in such a way thet points that are close together
> > in the two-dimensional sense will also be close together in the
> > one-dimensional list. But that is impossible.
> It's not impossible, it just requires an appropriate distance function 
> used in the sort.

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


Re: how to sort a list of tuples with custom function

2017-08-01 Thread Ho Yeung Lee
i tried with
testing1.sort(key=lambda x: x[0])
but only first element of tuple are grouped

then i expect to sort with custom function if difference between first element 
of tuple and another first element of tuple is less than some value
and do for second element too,

goal to segmentation of black words from photo

from PIL import Image
from functools import partial 
ma = Image.open("roster.png")
color1 = ma.load()
print ma.size
print color1[1,1] 
color1 = ma.load()
print ma.size
print color1[1,1] 
colortolocation = {}
def addtogroupkey(keyandmemory, key1, memorycontent):
k = key1
if k in keyandmemory: 
keyandmemory[k].append(memorycontent) 
else: 
keyandmemory[k] = [memorycontent]
return keyandmemory

for ii in range(0, ma.size[0]):
for jj in range(0, ma.size[1]):
colortolocation = addtogroupkey(colortolocation, color1[ii,jj], (ii,jj))

def isneighborlocation(lo1, lo2):
if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
return 1
elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
return 1
elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
return 1
else:
return 0

for eachcolor in colortolocation:
testing1 = list(colortolocation[eachcolor])
#testing1.sort(key=lambda x: x[1])
#custom_list_indices = {v: i for i, v in enumerate(custom_list)}
testing1.sort(key=lambda x: x[0]-x[1])
locations = testing1
locationsgroup = {}
continueconnect = 0
for ii in range(0,len(locations)-1):
if isneighborlocation(locations[ii], locations[ii+1]) == 1:
if continueconnect == 0:
keyone = len(locationsgroup)+1
if keyone in locationsgroup:
if locations[ii] not in locationsgroup[keyone]:
locationsgroup = addtogroupkey(locationsgroup, keyone, 
locations[ii])
if locations[ii+1] not in locationsgroup[keyone]:
locationsgroup = addtogroupkey(locationsgroup, keyone, 
locations[ii+1])
else:
locationsgroup = addtogroupkey(locationsgroup, keyone, 
locations[ii])
locationsgroup = addtogroupkey(locationsgroup, keyone, 
locations[ii+1])
continueconnect = 1
else:
if len(locationsgroup) > 0:
if locations[ii] not in locationsgroup[len(locationsgroup)]:
locationsgroup = addtogroupkey(locationsgroup, 
len(locationsgroup)+1, locations[ii])
else:
locationsgroup = addtogroupkey(locationsgroup, 
len(locationsgroup)+1, locations[ii])
continueconnect = 0
colortolocation[eachcolor] = locationsgroup

for kk in colortolocation[(0,0,0)]:
if len(colortolocation[(0,0,0)][kk]) > 7:
print kk
print colortolocation[(0,0,0)][kk]




On Wednesday, August 2, 2017 at 3:50:52 AM UTC+8, Ho Yeung Lee wrote:
> def isneighborlocation(lo1, lo2):
> if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
> return 1
> elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
> return 1
> elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
> return 1
> else:
> return 0
> 
> 
> sorted(testing1, key=lambda x: (isneighborlocation.get(x[0]), x[1]))
> 
> return something like
> [(1,2),(3,3),(2,5)]

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


how to sort a list of tuples with custom function

2017-08-01 Thread Ho Yeung Lee


def isneighborlocation(lo1, lo2):
if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
return 1
elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
return 1
elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
return 1
else:
return 0


sorted(testing1, key=lambda x: (isneighborlocation.get(x[0]), x[1]))

return something like
[(1,2),(3,3),(2,5)]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to group by function if one of the group has relationship with another one in the group?

2017-07-30 Thread Ho Yeung Lee
which function should be used for this problem?

On Saturday, July 29, 2017 at 11:02:30 PM UTC+8, Piet van Oostrum wrote:
> Peter Otten <__pete...@web.de> writes:
> 
> > Ho Yeung Lee wrote:
> >
> >> from itertools import groupby
> >> 
> >> testing1 = [(1,1),(2,3),(2,4),(3,5),(3,6),(4,6)]
> >> def isneighborlocation(lo1, lo2):
> >> if abs(lo1[0] - lo2[0]) == 1  or lo1[1] == lo2[1]:
> >> return 1
> >> elif abs(lo1[1] - lo2[1]) == 1  or lo1[0] == lo2[0]:
> >> return 1
> >> else:
> >> return 0
> >> 
> >> groupda = groupby(testing1, isneighborlocation)
> >> for key, group1 in groupda:
> >> print key
> >> for thing in group1:
> >> print thing
> >> 
> >> expect output 3 group
> >> group1 [(1,1)]
> >> group2 [(2,3),(2,4]
> >> group3 [(3,5),(3,6),(4,6)]
> >
> > groupby() calculates the key value from the current item only, so there's 
> > no 
> > "natural" way to apply it to your problem.
> >
> > Possible workarounds are to feed it pairs of neighbouring items (think 
> > zip()) or a stateful key function. Below is an example of the latter:
> >
> > $ cat sequential_group_class.py
> > from itertools import groupby
> >
> > missing = object()
> >
> > class PairKey:
> > def __init__(self, continued):
> > self.prev = missing
> > self.continued = continued
> > self.key = False
> >
> > def __call__(self, item):
> > if self.prev is not missing and not self.continued(self.prev, item):
> > self.key = not self.key
> > self.prev = item
> > return self.key
> >
> > def isneighborlocation(lo1, lo2):
> > x1, y1 = lo1
> > x2, y2 = lo2
> > dx = x1 - x2
> > dy = y1 - y2
> > return dx*dx + dy*dy <= 1
> >
> > items = [(1,1),(2,3),(2,4),(3,5),(3,6),(4,6)]
> >
> > for key, group in groupby(items, key=PairKey(isneighborlocation)):
> > print key, list(group)
> >
> > $ python sequential_group_class.py 
> > False [(1, 1)]
> > True [(2, 3), (2, 4)]
> > False [(3, 5), (3, 6), (4, 6)]
> 
> That only works if
> (a) The elements in the list are already clustered on group (i.e. all
> elements of a group are adjacent)
> (b) In a group the order is such that adjacent elements are direct
> neigbours, i.e. their distance is at most 1.
> 
> So 'groupby' is not a natural solution for this problem.
> -- 
> Piet van Oostrum <pie...@vanoostrum.org>
> WWW: http://piet.vanoostrum.org/
> PGP key: [8DAE142BE17999C4]

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


Re: how to group by function if one of the group has relationship with another one in the group?

2017-07-28 Thread Ho Yeung Lee
actually i used in this application
if same color is neighbor like connected then group them

i use for segmentation of words in screen capture

https://stackoverflow.com/questions/45294829/how-to-group-by-function-if-any-one-of-the-group-members-has-neighbor-relationsh

i asked here too, but i do not know how to use partial and do not know what 
center is.


On Tuesday, July 25, 2017 at 5:00:25 PM UTC+8, Peter Otten wrote:
> Ho Yeung Lee wrote:
> 
> > from itertools import groupby
> > 
> > testing1 = [(1,1),(2,3),(2,4),(3,5),(3,6),(4,6)]
> > def isneighborlocation(lo1, lo2):
> > if abs(lo1[0] - lo2[0]) == 1  or lo1[1] == lo2[1]:
> > return 1
> > elif abs(lo1[1] - lo2[1]) == 1  or lo1[0] == lo2[0]:
> > return 1
> > else:
> > return 0
> > 
> > groupda = groupby(testing1, isneighborlocation)
> > for key, group1 in groupda:
> > print key
> > for thing in group1:
> > print thing
> > 
> > expect output 3 group
> > group1 [(1,1)]
> > group2 [(2,3),(2,4]
> > group3 [(3,5),(3,6),(4,6)]
> 
> groupby() calculates the key value from the current item only, so there's no 
> "natural" way to apply it to your problem.
> 
> Possible workarounds are to feed it pairs of neighbouring items (think 
> zip()) or a stateful key function. Below is an example of the latter:
> 
> $ cat sequential_group_class.py
> from itertools import groupby
> 
> missing = object()
> 
> class PairKey:
> def __init__(self, continued):
> self.prev = missing
> self.continued = continued
> self.key = False
> 
> def __call__(self, item):
> if self.prev is not missing and not self.continued(self.prev, item):
> self.key = not self.key
> self.prev = item
> return self.key
> 
> def isneighborlocation(lo1, lo2):
> x1, y1 = lo1
> x2, y2 = lo2
> dx = x1 - x2
> dy = y1 - y2
> return dx*dx + dy*dy <= 1
> 
> items = [(1,1),(2,3),(2,4),(3,5),(3,6),(4,6)]
> 
> for key, group in groupby(items, key=PairKey(isneighborlocation)):
> print key, list(group)
> 
> $ python sequential_group_class.py 
> False [(1, 1)]
> True [(2, 3), (2, 4)]
> False [(3, 5), (3, 6), (4, 6)]

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


Re: how to group by function if one of the group has relationship with another one in the group?

2017-07-28 Thread Ho Yeung Lee
actually i used in this application
if same color is neighbor like connected then group them

i use for segmentation of words in screen capture

https://stackoverflow.com/questions/45294829/how-to-group-by-function-if-any-one-of-the-group-members-has-neighbor-relationsh

i asked here too, but i do not know how to use partial and do not know what 
center is.



On Wednesday, July 26, 2017 at 2:06:08 PM UTC+8, ast wrote:
> "Ho Yeung Lee" <jobmatt...@gmail.com> a écrit dans le message de 
> news:ef0bd11a-bf55-42a2-b016-d93f3b831...@googlegroups.com...
> > from itertools import groupby
> >
> > testing1 = [(1,1),(2,3),(2,4),(3,5),(3,6),(4,6)]
> > def isneighborlocation(lo1, lo2):
> >if abs(lo1[0] - lo2[0]) == 1  or lo1[1] == lo2[1]:
> >return 1
> >elif abs(lo1[1] - lo2[1]) == 1  or lo1[0] == lo2[0]:
> >return 1
> >else:
> >return 0
> >
> > groupda = groupby(testing1, isneighborlocation)
> > for key, group1 in groupda:
> >print key
> >for thing in group1:
> >print thing
> >
> > expect output 3 group
> > group1 [(1,1)]
> > group2 [(2,3),(2,4]
> > group3 [(3,5),(3,6),(4,6)]
> 
> Its not clear to me how you build the groups
> 
> Why (1,1) is not in group2 since (1,1) is
> a neighbor to both (2,3) and (2,4) ?

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


how to group by function if one of the group has relationship with another one in the group?

2017-07-24 Thread Ho Yeung Lee
from itertools import groupby

testing1 = [(1,1),(2,3),(2,4),(3,5),(3,6),(4,6)]
def isneighborlocation(lo1, lo2):
if abs(lo1[0] - lo2[0]) == 1  or lo1[1] == lo2[1]:
return 1
elif abs(lo1[1] - lo2[1]) == 1  or lo1[0] == lo2[0]:
return 1
else:
return 0

groupda = groupby(testing1, isneighborlocation)
for key, group1 in groupda:
print key
for thing in group1:
print thing

expect output 3 group
group1 [(1,1)]
group2 [(2,3),(2,4]
group3 [(3,5),(3,6),(4,6)]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to create this dependency table from ast?

2017-07-02 Thread Ho Yeung Lee
On Sunday, July 2, 2017 at 9:59:48 PM UTC+8, Ho Yeung Lee wrote:
> On Sunday, July 2, 2017 at 9:53:50 PM UTC+8, Ho Yeung Lee wrote:
> > On Sunday, July 2, 2017 at 9:32:36 PM UTC+8, ad...@python.org wrote:
> > > ad...@python.org:
> > > > Hi, Ho!
> > > 
> > > 
> > > it is crucial that you dump that fucking Windows of yours and become
> > > real pythonic under Linux !
> > 
> > i do not understand what is difference in result if run in window and linux
> > 
> > goal is to create a table 
> > 
> > graph = {'A': ['C'], 
> >  'B': ['C'], 
> >  'C': ['D'], 
> >  'C': ['E']} 
> > 
> > from 
> > 
> > a = 1 
> > b = 1 
> > c = a + b 
> > d = c 
> > e = c 
> > 
> > Python 2.7.6 (default, Oct 26 2016, 20:30:19)
> > [GCC 4.8.4] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> import ast
> > >>> parseprint("a = 1")
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > NameError: name 'parseprint' is not defined
> > 
> > actually running in bash subsystem of window
> > there is no parseprint to see which attributes for craft the goal
> 
> 
> in fact, i had myself only. i do not know others. of course dump my own 
> window result.


goal is to create dependency graph for variables
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to create this dependency table from ast?

2017-07-02 Thread Ho Yeung Lee
On Sunday, July 2, 2017 at 9:53:50 PM UTC+8, Ho Yeung Lee wrote:
> On Sunday, July 2, 2017 at 9:32:36 PM UTC+8, ad...@python.org wrote:
> > ad...@python.org:
> > > Hi, Ho!
> > 
> > 
> > it is crucial that you dump that fucking Windows of yours and become
> > real pythonic under Linux !
> 
> i do not understand what is difference in result if run in window and linux
> 
> goal is to create a table 
> 
> graph = {'A': ['C'], 
>  'B': ['C'], 
>  'C': ['D'], 
>  'C': ['E']} 
> 
> from 
> 
> a = 1 
> b = 1 
> c = a + b 
> d = c 
> e = c 
> 
> Python 2.7.6 (default, Oct 26 2016, 20:30:19)
> [GCC 4.8.4] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import ast
> >>> parseprint("a = 1")
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'parseprint' is not defined
> 
> actually running in bash subsystem of window
> there is no parseprint to see which attributes for craft the goal


in fact, i had myself only. i do not know others. of course dump my own window 
result.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to create this dependency table from ast?

2017-07-02 Thread Ho Yeung Lee
On Sunday, July 2, 2017 at 9:32:36 PM UTC+8, ad...@python.org wrote:
> ad...@python.org:
> > Hi, Ho!
> 
> 
> it is crucial that you dump that fucking Windows of yours and become
> real pythonic under Linux !

i do not understand what is difference in result if run in window and linux

goal is to create a table 

graph = {'A': ['C'], 
 'B': ['C'], 
 'C': ['D'], 
 'C': ['E']} 

from 

a = 1 
b = 1 
c = a + b 
d = c 
e = c 

Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> parseprint("a = 1")
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'parseprint' is not defined

actually running in bash subsystem of window
there is no parseprint to see which attributes for craft the goal
-- 
https://mail.python.org/mailman/listinfo/python-list


how to create this dependency table from ast?

2017-07-02 Thread Ho Yeung Lee
i find parseprint function not exist in python 2.7

goal is to create a table

graph = {'A': ['B', 'C'],
 'B': ['C', 'D'],
 'C': ['D'],
 'D': ['C'],
 'E': ['F'],
 'F': ['C']}

from

a = 1
b = 1
c = a + b
d = c
e = c

file = open(r"C:\Users\hello\Documents\testingsource.py", "r")
root = ast.parse(file.read())

a   b
 \ /
  c
 / \
d   e

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


Is there library to convert AST to DAG tree?

2017-07-01 Thread Ho Yeung Lee
Is there library to convert AST to DAG tree?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to make this situation return this result?

2017-07-01 Thread Ho Yeung Lee
just want to compare tuples like index (0,1), (0,2), (1,2) without duplicate
 such as (2,0), (1,0) etc


On Saturday, July 1, 2017 at 7:00:17 PM UTC+8, Peter Otten wrote:
> Ho Yeung Lee wrote:
> 
> > finally i searched dict.values()[index] solved this
> 
> That doesn't look like a good solution to anything -- including "this", 
> whatever it may be ;)
> 
> If you make an effort to better explain your problem in plain english rather 
> than with code examples you are likely tho get better answers.

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


Re: how to make this situation return this result?

2017-07-01 Thread Ho Yeung Lee
finally i searched dict.values()[index] solved this 


On Saturday, July 1, 2017 at 6:00:41 PM UTC+8, Peter Otten wrote:
> Ho Yeung Lee wrote:
> 
> > expect result as this first case
> > 
> > ii = 0
> > jj = 0
> > for ii in range(0,3):
> > for jj in range(0,3):
> > if ii < jj:
> > print (ii, jj)
> > 
> > 
> > but below is different
> > as sometimes the situation is not range(0,3), but it a a list of tuple
> > 
> >  = 0
> 
> > for ii in range(0,3):
>   # jj starts with 0 on invocation of the inner loop
>   # to get that with an independent counter you have to
>   #initialise it explicitly:
>    = 0
> > for jj in range(0,3):
> > if  < :
> > print (, )
> >  =  + 1
> >  =  + 1
> > 
> > how to make this situation return result like the first case?
> 
> Because you don't reset  the condition  <  is always True the 
> second and third time the inner loop is executed.

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


Re: how to make this situation return this result?

2017-07-01 Thread Ho Yeung Lee
i got an idea with below, but it can not compile

for ii, yy in range(2,5), range(0,3):
for jj, zz in range(2,5), range(0,3):
if yy < zz:
print (ii, jj)

real situation

groupkey
{(0, 1): [[0, 1], [0, 2], [0, 8]], (1, 2): [[1, 5], [1, 9], [2, 6], [2, 10], 
[8, 9], [8, 10]], (2, 3): [[5, 7], [6, 7], [6, 14], [10, 14]]}

ii = 0
jj = 0
for groupkeya in groupkey:
for groupkeyb in groupkey:
if ii < jj:
print "groupkeya"
print groupkeya, ii, jj
print "groupkeyb"
print groupkeyb, ii, jj
jj = jj + 1
ii = ii + 1


On Saturday, July 1, 2017 at 5:05:48 PM UTC+8, Ho Yeung Lee wrote:
> sorry for typo,
> 
>  = 0
>  = 0
> for ii in range(0,3):
> for jj in range(0,3):
> if  < :
> print (ii, jj)   <- correct here
>  =  + 1
>  = iiii + 1
> 
> 
> On Saturday, July 1, 2017 at 4:55:59 PM UTC+8, Ho Yeung Lee wrote:
> > expect result as this first case
> > 
> > ii = 0
> > jj = 0
> > for ii in range(0,3):
> > for jj in range(0,3):
> > if ii < jj:
> > print (ii, jj)
> > 
> > 
> > but below is different
> > as sometimes the situation is not range(0,3), but it a a list of tuple 
> > 
> >  = 0
> >  = 0
> > for ii in range(0,3):
> > for jj in range(0,3):
> > if  < :
> > print (, )
> >  =  + 1
> >  =  + 1
> > 
> > how to make this situation return result like the first case?

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


Re: how to make this situation return this result?

2017-07-01 Thread Ho Yeung Lee
sorry for typo,

 = 0
 = 0
for ii in range(0,3):
for jj in range(0,3):
if  < :
print (ii, jj)   <- correct here
 =  + 1
 =  + 1


On Saturday, July 1, 2017 at 4:55:59 PM UTC+8, Ho Yeung Lee wrote:
> expect result as this first case
> 
> ii = 0
> jj = 0
> for ii in range(0,3):
> for jj in range(0,3):
> if ii < jj:
> print (ii, jj)
> 
> 
> but below is different
> as sometimes the situation is not range(0,3), but it a a list of tuple 
> 
>  = 0
>  = 0
> for ii in range(0,3):
> for jj in range(0,3):
> if  < :
> print (, )
>  =  + 1
>  =  + 1
> 
> how to make this situation return result like the first case?

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


how to make this situation return this result?

2017-07-01 Thread Ho Yeung Lee
expect result as this first case

ii = 0
jj = 0
for ii in range(0,3):
for jj in range(0,3):
if ii < jj:
print (ii, jj)


but below is different
as sometimes the situation is not range(0,3), but it a a list of tuple 

 = 0
 = 0
for ii in range(0,3):
for jj in range(0,3):
if  < :
print (, )
 =  + 1
 =  + 1

how to make this situation return result like the first case?

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


how to add new tuple as key in dictionary?

2017-06-30 Thread Ho Yeung Lee
I find that list can not be key in dictionary
then find tuple can be as key

but when I add new tuple as key , got error in python 2.7

groupkey = {(0,0): []}
groupkey[tuple([0,3])] = groupkey[tuple([0,3])] + [[0,1]]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the html content and edit with scapy and see the edited result in browser?

2017-06-21 Thread Ho Yeung Lee

ƒ'αí╖k9½│%xLτì∙6╕<┬q║Q:αô«Fnⁿ╟å╜çÅÇ║5DÜJ£ε@╟▄╢Zn∞bò╩oy:Ü¡αúáâ7y) 
è*?w┴@T1Ω&╚╓╓÷¢éÉ╫ÄçmÑ╣,╕$╕Q»u±:"]²+*╩èä°ëÅΣΩ¬╘Φ≥╘├iG╝╘@ñç 
Dj▌ÉÜ=≤x0u≈$;±╩ I╫T╫bsvφ#Ñ╦av╘πJ
Begin emission:
..


from scapy.all import *
import os

# Interacts with a client by going through the three-way handshake.
# Shuts down the connection immediately after the connection has been 
established.
# Akaljed Dec 2010, http://www.akaljed.wordpress.com

# Wait for client to connect.
a=sniff(count=1,filter="tcp and host 192.168.3.245 and port 80")

# some variables for later use.
ValueOfPort=a[0].sport
SeqNr=a[0].seq
AckNr=a[0].seq+1

# Generating the IP layer:
ip=IP(src="192.168.3.245", dst="192.168.100.1")
# Generating TCP layer:
TCP_SYNACK=TCP(sport=80, dport=ValueOfPort, flags="SA", seq=SeqNr, ack=AckNr, 
options=[('MSS', 1460)])

#send SYNACK to remote host AND receive ACK.
ANSWER=sr1(ip/TCP_SYNACK)

# Capture next TCP packets with dport 80. (contains http GET request)
GEThttp = sniff(filter="tcp and port 80",count=1,prn=lambda 
x:x.sprintf("{IP:%IP.src%: %TCP.dport%}"))
AckNr=AckNr+len(GEThttp[0].load)
SeqNr=a[0].seq+1

# Print the GET request
# (Sanity check: size of data should be greater than 1.)
if len(GEThttp[0].load)>1: print GEThttp[0].load

# Generate custom http file content.
html1="HTTP/1.1 200 OK\x0d\x0aDate: Wed, 29 Sep 2010 20:19:05 
GMT\x0d\x0aServer: Testserver\x0d\x0aConnection: 
Keep-Alive\x0d\x0aContent-Type: text/html; charset=UTF-8\x0d\x0aContent-Length: 
291\x0d\x0a\x0d\x0aTestserver-Welcome to test 
server---"

# Generate TCP data
data1=TCP(sport=80, dport=ValueOfPort, flags="PA", seq=SeqNr, ack=AckNr, 
options=[('MSS', 1460)])

# Construct whole network packet, send it and fetch the returning ack.
ackdata1=sr1(ip/data1/html1)
# Store new sequence number.
SeqNr=ackdata1.ack

# Generate RST-ACK packet
Bye=TCP(sport=80, dport=ValueOfPort, flags="FA", seq=SeqNr, ack=AckNr, 
options=[('MSS', 1460)])

send(ip/Bye)


from scapy.all import *
import os

# Interacts with a client by going through the three-way handshake.
# Shuts down the connection immediately after the connection has been 
established.
# Akaljed Dec 2010, http://www.akaljed.wordpress.com

# Wait for client to connect.
a=sniff(count=1,filter="tcp and host 192.168.3.245 and port 80")

# some variables for later use.
ValueOfPort=a[0].sport
SeqNr=a[0].seq
AckNr=a[0].seq+1

# Generating the IP layer:
ip=IP(src="192.168.3.245", dst="192.168.100.1")
# Generating TCP layer:
TCP_SYNACK=TCP(sport=80, dport=ValueOfPort, flags="SA", seq=SeqNr, ack=AckNr, 
options=[('MSS', 1460)])

#send SYNACK to remote host AND receive ACK.
ANSWER=sr1(ip/TCP_SYNACK)

# Capture next TCP packets with dport 80. (contains http GET request)
GEThttp = sniff(filter="tcp and port 80",count=1,prn=lambda 
x:x.sprintf("{IP:%IP.src%: %TCP.dport%}"))
AckNr=AckNr+len(GEThttp[0].load)
SeqNr=a[0].seq+1

# Print the GET request
# (Sanity check: size of data should be greater than 1.)
if len(GEThttp[0].load)>1: print GEThttp[0].load

# Generate custom http file content.
html1="HTTP/1.1 200 OK\x0d\x0aDate: Wed, 29 Sep 2010 20:19:05 
GMT\x0d\x0aServer: Testserver\x0d\x0aConnection: 
Keep-Alive\x0d\x0aContent-Type: text/html; charset=UTF-8\x0d\x0aContent-Length: 
291\x0d\x0a\x0d\x0aTestserver-Welcome to test 
server---"

# Generate TCP data
data1=TCP(sport=80, dport=ValueOfPort, flags="PA", seq=SeqNr, ack=AckNr, 
options=[('MSS', 1460)])

# Construct whole network packet, send it and fetch the returning ack.
ackdata1=sr1(ip/data1/html1)
# Store new sequence number.
SeqNr=ackdata1.ack

# Generate RST-ACK packet
Bye=TCP(sport=80, dport=ValueOfPort, flags="FA", seq=SeqNr, ack=AckNr, 
options=[('MSS', 1460)])

send(ip/Bye)



On Tuesday, June 20, 2017 at 11:36:07 PM UTC+8, Ho Yeung Lee wrote:
> pkts = sniff(prn=lambda x:x.sprintf("{IP:%IP.src% -> 
> %IP.dst%\n}{Raw:%Raw.load%\n}"), filter="tcp port 80")
> 
> for i in range(1,len(pkts)):
> #if pkts[i][IP].sport == 80:
> i,pkts[i][TCP].payload
> 
> i find pkts[10] do not have html source code
> 
> (8, )
> (9, )
> (10, )
> (11, )
> 
> dir(pkts[10][TCP])

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


how to get the html content and edit with scapy and see the edited result in browser?

2017-06-20 Thread Ho Yeung Lee
pkts = sniff(prn=lambda x:x.sprintf("{IP:%IP.src% -> 
%IP.dst%\n}{Raw:%Raw.load%\n}"), filter="tcp port 80")

for i in range(1,len(pkts)):
#if pkts[i][IP].sport == 80:
i,pkts[i][TCP].payload

i find pkts[10] do not have html source code

(8, )
(9, )
(10, )
(11, )

dir(pkts[10][TCP])

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


how to decrypt encrypted text to a clear text

2017-06-06 Thread Ho Yeung Lee
i use wb to write pubic and private key 
and succeed to import private, but after decrypt, it return hex number 
not a clear text 
is there any more key needed?
or do wb influence the result?

from Crypto.PublicKey import RSA 
keypair = RSA.generate(2048) 
alice_privkey = keypair.exportKey('PEM', 'mysecret', pkcs=1) 
#alice_privkey = keypair.exportKey() 
alice_pubkey = keypair.publickey().exportKey() 

text_file = open("alice_pubkey.txt", "wb") 
text_file.write(alice_pubkey) 
text_file.close() 

keypair = RSA.generate(2048) 
bob_privkey = keypair.exportKey('PEM', 'mysecret2', pkcs=1) 
#bob_privkey = keypair.exportKey() 
bob_pubkey = keypair.publickey().exportKey() 

text_file = open("bob_pubkey.txt", "wb") 
text_file.write(bob_pubkey) 
text_file.close() 

text_file = open("alice_privkey.pem", "wb") 
text_file.write(alice_privkey) 
text_file.close() 
text_file = open("bob_privkey.pem", "wb") 
text_file.write(bob_privkey) 
text_file.close() 


#step 2 
#use alice public key to encrypt 
pubkey = RSA.importKey(alice_pubkey) 
alice_masterkey = pubkey.encrypt("i am Martin", None) 

text_file = open("alice_masterkey.txt", "w") 
text_file.write(bob_pubkey) 
text_file.close() 

quit() 
python 
text_file = open("alice_masterkey.txt", "r") 
alice_masterkey=text_file.read() 
text_file.close() 
text_file = open("alice_privkey.pem", "r") 
alice_privkey=text_file.read() 
text_file.close() 
text_file = open("alice_pubkey.txt", "r") 
alice_pubkey=text_file.read() 
text_file.close() 

from Crypto.PublicKey import RSA 
pubkey = RSA.importKey(alice_pubkey) 
privkey = RSA.importKey(alice_privkey,passphrase="mysecret") 
encryption_key = privkey.decrypt(alice_masterkey) 
encryption_key 

quit() 
python 
text_file = open("alice_masterkey.txt", "r") 
alice_masterkey=text_file.read() 
text_file.close() 
text_file = open("alice_privkey.pem", "r") 
alice_privkey=text_file.read() 
text_file.close() 
text_file = open("alice_pubkey.txt", "r") 
alice_pubkey=text_file.read() 
text_file.close() 

from Crypto.PublicKey import RSA 
pubkey = RSA.importKey(alice_pubkey) 
privkey = RSA.importKey(alice_privkey,passphrase="mysecret") 
encryption_key = privkey.decrypt(alice_masterkey) 
encryption_key 

>>> encryption_key 
'o\x94\xaeC\xe0S\x81\x05t\xd8\\A\x10?\xd2\xe5\x8c5\xc9\x1d\x14\xc7\xfd)Cs\x8b"cg\x16y\xe2\xf2L\xf1-\x08qHt\x99\xbc\xb5\xf6_\x17c\xd2\x0b\xc5t\t\xe0\x8b\x03G\x10\xce\xd6\xcd\x86\xfc!\xc9i\xa2\xab\x9d\x8a\x92\xfc7
 
g\xa5$\x91\x85\xa2L]I\xd6\xc6\xaez\xed\x01\x95\xee)8z\x18\xc9aag\x97\x97\xb0\\)\xec"\xe4\xbez\xd3\xa8\'k%\x12\x1d\xf9\xf0\x0e\x0c\xcb\xa8\xb1\xe7}\x90\xd3\xcfs@\xc2m\x1a^\x1b0\xa7\xdd\xcd\xea\x1f\xd5\x08\x13+y"]vu\xe3\x9e\xba\x97\x10\x90S\xea\xae1=r4Yp,\xe3\xa9\xc66H\xa7\x95[M|n\x91\x98\x9c,\xc4\xf5\x7f\x8cJ\x03\xba\x04Z0lV\xe1\xd6d\xeec@\xe1\xa0\xec\x81]\xef5\r\x12\x88\xbe/\xfc\xe01\xaacn,\x8a\xe1\x14\x8a\xf4\xd85\xd8\xabD\x137\xe7T\xc4\xc1\x84b.\xd9RZ\x0e\x03#\x1e\x8dl\xe8\xe4N^\r\xf0\x1d\x8c'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: error when import private key in python

2017-06-04 Thread Ho Yeung Lee
i use "wb" to write public and private key and succeed to import private key
but after decrypt, it is not clear text, it is hex number


from Crypto.PublicKey import RSA
keypair = RSA.generate(2048)
alice_privkey = keypair.exportKey('PEM', 'mysecret', pkcs=1)
#alice_privkey = keypair.exportKey()
alice_pubkey = keypair.publickey().exportKey()

text_file = open("alice_pubkey.txt", "wb")
text_file.write(alice_pubkey)
text_file.close()

keypair = RSA.generate(2048)
bob_privkey = keypair.exportKey('PEM', 'mysecret2', pkcs=1)
#bob_privkey = keypair.exportKey()
bob_pubkey = keypair.publickey().exportKey()

text_file = open("bob_pubkey.txt", "wb")
text_file.write(bob_pubkey)
text_file.close()

text_file = open("alice_privkey.pem", "wb")
text_file.write(alice_privkey)
text_file.close()
text_file = open("bob_privkey.pem", "wb")
text_file.write(bob_privkey)
text_file.close()


#step 2
#use alice public key to encrypt
pubkey = RSA.importKey(alice_pubkey)
alice_masterkey = pubkey.encrypt("i am Martin", None)

text_file = open("alice_masterkey.txt", "w")
text_file.write(bob_pubkey)
text_file.close()

#step 3
quit()
python
text_file = open("alice_masterkey.txt", "r")
alice_masterkey=text_file.read()
text_file.close()
text_file = open("alice_privkey.pem", "r")
alice_privkey=text_file.read()
text_file.close()
text_file = open("alice_pubkey.txt", "r")
alice_pubkey=text_file.read()
text_file.close()

from Crypto.PublicKey import RSA
pubkey = RSA.importKey(alice_pubkey)
privkey = RSA.importKey(alice_privkey,passphrase="mysecret")
encryption_key = privkey.decrypt(alice_masterkey)
encryption_key

hex number


On Sunday, June 4, 2017 at 7:29:07 PM UTC+8, Thomas Jollans wrote:
> On 04/06/17 13:22, Ho Yeung Lee wrote:
> > # [snip]
> > alice_privkey=text_file.read().replace('\n', '')
> 
> Why are you removing newlines? Does the documentation tell you to do this?
> 
> >>>> privkey = RSA.importKey(alice_privkey,passphrase="mysecret")
> > Traceback (most recent call last):
> >   File "", line 1, in 
> >   File "C:\Python27\lib\site-packages\Crypto\PublicKey\RSA.py", line 638, 
> > in importKey
> > if lines[1].startswith(b('Proc-Type:4,ENCRYPTED')):
> > IndexError: list index out of range
> > 
> 
> This is just a wild guess, but it looks like the package expects there
> to be multiple lines in the key.
> 
> 
> -- Thomas

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


Re: error when import private key in python

2017-06-04 Thread Ho Yeung Lee
i use wb to write pubic and private key
and succeed to import private, but after decrypt, it return hex number
not a clear text

from Crypto.PublicKey import RSA
keypair = RSA.generate(2048)
alice_privkey = keypair.exportKey('PEM', 'mysecret', pkcs=1)
#alice_privkey = keypair.exportKey()
alice_pubkey = keypair.publickey().exportKey()

text_file = open("alice_pubkey.txt", "wb")
text_file.write(alice_pubkey)
text_file.close()

keypair = RSA.generate(2048)
bob_privkey = keypair.exportKey('PEM', 'mysecret2', pkcs=1)
#bob_privkey = keypair.exportKey()
bob_pubkey = keypair.publickey().exportKey()

text_file = open("bob_pubkey.txt", "wb")
text_file.write(bob_pubkey)
text_file.close()

text_file = open("alice_privkey.pem", "wb")
text_file.write(alice_privkey)
text_file.close()
text_file = open("bob_privkey.pem", "wb")
text_file.write(bob_privkey)
text_file.close()


#step 2
#use alice public key to encrypt
pubkey = RSA.importKey(alice_pubkey)
alice_masterkey = pubkey.encrypt("i am Martin", None)

text_file = open("alice_masterkey.txt", "w")
text_file.write(bob_pubkey)
text_file.close()

quit()
python
text_file = open("alice_masterkey.txt", "r")
alice_masterkey=text_file.read()
text_file.close()
text_file = open("alice_privkey.pem", "r")
alice_privkey=text_file.read()
text_file.close()
text_file = open("alice_pubkey.txt", "r")
alice_pubkey=text_file.read()
text_file.close()

from Crypto.PublicKey import RSA
pubkey = RSA.importKey(alice_pubkey)
privkey = RSA.importKey(alice_privkey,passphrase="mysecret")
encryption_key = privkey.decrypt(alice_masterkey)
encryption_key

quit()
python
text_file = open("alice_masterkey.txt", "r")
alice_masterkey=text_file.read()
text_file.close()
text_file = open("alice_privkey.pem", "r")
alice_privkey=text_file.read()
text_file.close()
text_file = open("alice_pubkey.txt", "r")
alice_pubkey=text_file.read()
text_file.close()

from Crypto.PublicKey import RSA
pubkey = RSA.importKey(alice_pubkey)
privkey = RSA.importKey(alice_privkey,passphrase="mysecret")
encryption_key = privkey.decrypt(alice_masterkey)
encryption_key

>>> encryption_key
'o\x94\xaeC\xe0S\x81\x05t\xd8\\A\x10?\xd2\xe5\x8c5\xc9\x1d\x14\xc7\xfd)Cs\x8b"cg\x16y\xe2\xf2L\xf1-\x08qHt\x99\xbc\xb5\xf6_\x17c\xd2\x0b\xc5t\t\xe0\x8b\x03G\x10\xce\xd6\xcd\x86\xfc!\xc9i\xa2\xab\x9d\x8a\x92\xfc7
 
g\xa5$\x91\x85\xa2L]I\xd6\xc6\xaez\xed\x01\x95\xee)8z\x18\xc9aag\x97\x97\xb0\\)\xec"\xe4\xbez\xd3\xa8\'k%\x12\x1d\xf9\xf0\x0e\x0c\xcb\xa8\xb1\xe7}\x90\xd3\xcfs@\xc2m\x1a^\x1b0\xa7\xdd\xcd\xea\x1f\xd5\x08\x13+y"]vu\xe3\x9e\xba\x97\x10\x90S\xea\xae1=r4Yp,\xe3\xa9\xc66H\xa7\x95[M|n\x91\x98\x9c,\xc4\xf5\x7f\x8cJ\x03\xba\x04Z0lV\xe1\xd6d\xeec@\xe1\xa0\xec\x81]\xef5\r\x12\x88\xbe/\xfc\xe01\xaacn,\x8a\xe1\x14\x8a\xf4\xd85\xd8\xabD\x137\xe7T\xc4\xc1\x84b.\xd9RZ\x0e\x03#\x1e\x8dl\xe8\xe4N^\r\xf0\x1d\x8c'


On Sunday, June 4, 2017 at 7:29:07 PM UTC+8, Thomas Jollans wrote:
> On 04/06/17 13:22, Ho Yeung Lee wrote:
> > # [snip]
> > alice_privkey=text_file.read().replace('\n', '')
> 
> Why are you removing newlines? Does the documentation tell you to do this?
> 
> >>>> privkey = RSA.importKey(alice_privkey,passphrase="mysecret")
> > Traceback (most recent call last):
> >   File "", line 1, in 
> >   File "C:\Python27\lib\site-packages\Crypto\PublicKey\RSA.py", line 638, 
> > in importKey
> > if lines[1].startswith(b('Proc-Type:4,ENCRYPTED')):
> > IndexError: list index out of range
> > 
> 
> This is just a wild guess, but it looks like the package expects there
> to be multiple lines in the key.
> 
> 
> -- Thomas

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


error when import private key in python

2017-06-04 Thread Ho Yeung Lee
#step 1
from Crypto.PublicKey import RSA
keypair = RSA.generate(2048)
alice_privkey = keypair.exportKey('PEM', 'mysecret', pkcs=1)
#alice_privkey = keypair.exportKey()
alice_pubkey = keypair.publickey().exportKey()

text_file = open("alice_pubkey.txt", "w")
text_file.write(alice_pubkey)
text_file.close()

keypair = RSA.generate(2048)
bob_privkey = keypair.exportKey('PEM', 'mysecret2', pkcs=1)
#bob_privkey = keypair.exportKey()
bob_pubkey = keypair.publickey().exportKey()

text_file = open("bob_pubkey.txt", "w")
text_file.write(bob_pubkey)
text_file.close()

text_file = open("alice_privkey.pem", "w")
text_file.write(alice_privkey)
text_file.close()
text_file = open("bob_privkey.pem", "w")
text_file.write(bob_privkey)
text_file.close()

#step 2
#use alice public key to encrypt
pubkey = RSA.importKey(alice_pubkey)
alice_masterkey = pubkey.encrypt("i am Martin", None)

text_file = open("alice_masterkey.txt", "w")
text_file.write(bob_pubkey)
text_file.close()

#step 3
quit()
text_file = open("alice_masterkey.txt", "r")
alice_masterkey=text_file.read().replace('\n', '')
text_file.close()
text_file = open("alice_privkey.pem", "r")
alice_privkey=text_file.read().replace('\n', '')
text_file.close()

privkey = RSA.importKey(alice_privkey,passphrase="mysecret")
encryption_key = privkey.decrypt(alice_masterkey)

>>> privkey = RSA.importKey(alice_privkey,passphrase="mysecret")
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python27\lib\site-packages\Crypto\PublicKey\RSA.py", line 638, in 
importKey
if lines[1].startswith(b('Proc-Type:4,ENCRYPTED')):
IndexError: list index out of range
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to convert json to csv with python?

2017-06-03 Thread Ho Yeung Lee
after edit the file, 

Traceback (most recent call last):
  File "json2csv.py", line 148, in 
loader.load(args.json_file)
  File "json2csv.py", line 53, in load
self.process_each(json.load(json_file))
  File "C:\Python27\lib\json\__init__.py", line 291, in load
**kw)
  File "C:\Python27\lib\json\__init__.py", line 339, in loads
return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 38 column 1 (char 871)

got another error

Chris Warrick於 2017年6月3日星期六 UTC+8下午7時20分34秒寫道:
> On 3 June 2017 at 13:00, Ho Yeung Lee <davidbenny2...@gmail.com> wrote:
> > i use
> > https://github.com/evidens/json2csv
> >
> > Error:
> > Traceback (most recent call last):
> >   File "json2csv.py", line 148, in 
> > loader.load(args.json_file)
> >   File "json2csv.py", line 53, in load
> > self.process_each(json.load(json_file))
> >   File "C:\Python27\lib\json\__init__.py", line 291, in load
> > **kw)
> >   File "C:\Python27\lib\json\__init__.py", line 339, in loads
> > return _default_decoder.decode(s)
> >   File "C:\Python27\lib\json\decoder.py", line 367, in decode
> > raise ValueError(errmsg("Extra data", s, end, len(s)))
> > ValueError: Extra data: line 10 column 2 - line 50 column 2 (char 224 - 
> > 1179)
> >
> > sample file is
> > {
> >   "ip": "184.85.123.122",
> >   "hostname": "No Hostname",
> >   "city": "Cambridge",
> >   "region": "Massachusetts",
> >   "country": "US",
> >   "loc": "42.3626,-71.0843",
> >   "org": "AS20940 Akamai International B.V.",
> >   "postal": "02142"
> > },
> > {
> >   "ip": "203.185.0.32",
> >   "hostname": "20318532.ctinets.com",
> >   "city": "Central District",
> >   "region": "",
> >   "country": "HK",
> >   "loc": "22.2910,114.1500",
> >   "org": "AS9269 HKBN AS10103"
> > },
> > [snip]
> 
> This is invalid JSON. You need to wrap all your dicts in a JSON array,
> like this:
> 
> [
>   {
>  "ip": "…"
>   },
>   {
>  "ip": "…"
>   }
> ]
> 
> (just add [ and ] to the start and end of your file)
> 
> -- 
> Chris Warrick <https://chriswarrick.com/>
> PGP: 5EAAEA16

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


how to convert json to csv with python?

2017-06-03 Thread Ho Yeung Lee
i use
https://github.com/evidens/json2csv

Error:
Traceback (most recent call last):
  File "json2csv.py", line 148, in 
loader.load(args.json_file)
  File "json2csv.py", line 53, in load
self.process_each(json.load(json_file))
  File "C:\Python27\lib\json\__init__.py", line 291, in load
**kw)
  File "C:\Python27\lib\json\__init__.py", line 339, in loads
return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 367, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 10 column 2 - line 50 column 2 (char 224 - 1179)

sample file is
{
  "ip": "184.85.123.122",
  "hostname": "No Hostname",
  "city": "Cambridge",
  "region": "Massachusetts",
  "country": "US",
  "loc": "42.3626,-71.0843",
  "org": "AS20940 Akamai International B.V.",
  "postal": "02142"
},
{
  "ip": "203.185.0.32",
  "hostname": "20318532.ctinets.com",
  "city": "Central District",
  "region": "",
  "country": "HK",
  "loc": "22.2910,114.1500",
  "org": "AS9269 HKBN AS10103"
},
{
  "ip": "184.85.123.122",
  "hostname": "a184-85-123-122.deploy.static.akamaitechnologies.com",
  "city": "Cambridge",
  "region": "Massachusetts",
  "country": "US",
  "loc": "42.3626,-71.0843",
  "org": "AS20940 Akamai International B.V.",
  "postal": "02142"
},
{
  "ip": "203.185.0.32",
  "hostname": "No Hostname",
  "city": "Central District",
  "region": "",
  "country": "HK",
  "loc": "22.2910,114.1500",
  "org": "AS9269 HKBN AS10103",
},
{
  "ip": "184.85.123.122",
  "hostname": "a184-85-123-122.deploy.static.akamaitechnologies.com",
  "city": "Cambridge",
  "region": "Massachusetts",
  "country": "US",
  "loc": "42.3626,-71.0843",
  "org": "AS20940 Akamai International B.V.",
  "postal": "02142"
}

outline is
{
  "map": [["ip","ip"],["hostname", "hostname"],["city", "city"],["region", 
"region"],["country" ,"country"],["loc", "loc"],["org", "org"],["postal", 
"postal"]] }

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


how to change code in runtime after convert to executable file

2017-05-31 Thread Ho Yeung Lee
if keep module1.py code together with mainmodule1.exe, how to convert to 
executable file with py2exe in this case?

after tried, it can run executable file with source code module1.py
but the modified date can not be changed even if content of module1.py changed
then i delete the file and change in another directory, then copy to the 
directory with executable file, it still can not reload the module


mainmodule1.py

import module1
#from module1 import func1

while 1:
module1.func1()
reload(module1)

module1.py

def func1():
print("version 1")
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing.Process can not start a thread in py2exe

2017-05-21 Thread Ho Yeung Lee
On Sunday, May 21, 2017 at 2:47:26 PM UTC+8, Ho Yeung Lee wrote:
> On Sunday, May 21, 2017 at 2:40:49 PM UTC+8, top...@googlemail.com wrote:
> > Did you call freeze_support() function after script start?
> > https://docs.python.org/3/library/multiprocessing.html#multiprocessing.freeze_support
> 
> 
> no, i did not call freeze_support()

no matter i call it or not in the first line of main , it still can not run
the thread function
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing.Process can not start a thread in py2exe

2017-05-21 Thread Ho Yeung Lee
On Sunday, May 21, 2017 at 2:40:49 PM UTC+8, top...@googlemail.com wrote:
> Did you call freeze_support() function after script start?
> https://docs.python.org/3/library/multiprocessing.html#multiprocessing.freeze_support


no, i did not call freeze_support()
-- 
https://mail.python.org/mailman/listinfo/python-list


AttributeError: 'module' object has no attribute 'urlretrieve' in window subsystem ubuntu bash for tensorflow

2017-05-21 Thread Ho Yeung Lee
i use window subsystem ubuntu 
and install python 3 and tensorflow

then when try deep learning

https://www.tensorflow.org/tutorials/wide_and_deep

got error when urlretrieve local directory in ubuntu in window

tried urllib3 still have error

import tempfile
import pandas as pd
import urllib as urllib
import os

model_dir = tempfile.mkdtemp()
m = tf.contrib.learn.DNNLinearCombinedClassifier(
model_dir=model_dir,
linear_feature_columns=wide_columns,
dnn_feature_columns=deep_columns,
dnn_hidden_units=[100, 50])

...

urllib.urlretrieve(r"/mnt/c/Users/hello/Documents/data.csv", train_file.name)
urllib.urlretrieve(r"/mnt/c/Users/hello/Documents/dataTest.csv", test_file.name)


Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'urlretrieve'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing.Process can not start a thread in py2exe

2017-05-21 Thread Ho Yeung Lee
i mean executable file can not run the multiprocessing thread 
after convert to executable file with py2exe



On Sunday, May 21, 2017 at 2:09:04 PM UTC+8, Ho Yeung Lee wrote:
> p = multiprocessing.Process(target=helloconnect, args=(host,"",))
> 
> multiprocessing.Process can not start a thread in py2exe
> 
> it can compile and run without error
> but it can not run function helloconnect

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


multiprocessing.Process can not start a thread in py2exe

2017-05-21 Thread Ho Yeung Lee
p = multiprocessing.Process(target=helloconnect, args=(host,"",))

multiprocessing.Process can not start a thread in py2exe

it can compile and run without error
but it can not run function helloconnect
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: 'module' object has no attribute 'wrap_socket' when use ssl

2017-04-15 Thread Ho Yeung Lee
On Saturday, April 15, 2017 at 3:18:58 PM UTC+8, Peter Otten wrote:
> Ho Yeung Lee wrote:
> 
> > Python 2.7.6 (default, Jun 22 2015, 18:00:18)
> > [GCC 4.8.2] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> >>>> import ssl
> > Traceback (most recent call last):
> >   File "", line 1, in 
> >   File "/home/martin/Documents/ssl.py", line 13, in 
> > SSLError -- exception raised for I/O errors
> > AttributeError: 'module' object has no attribute 'wrap_socket'
> 
> Look at the traceback again -- the ssl.py you are importing is not the one 
> from the standard library, it's an arbitrary module, perhaps written by 
> "martin". Once you rename Martin's ssl.py to something else and also remove 
> the corresponding ssl.pyc you will be able to work with the desired ssl.py 
> from the stdlib.


thanks , it can run now,

but how to create a SSL server for testing it?
when I use google as server, it failed handshake

python crackssl.py
Traceback (most recent call last):
  File "crackssl.py", line 16, in 
wrappedSocket.connect((HOST, PORT))
  File "/usr/lib/python2.7/ssl.py", line 433, in connect
self._real_connect(addr, False)
  File "/usr/lib/python2.7/ssl.py", line 420, in _real_connect
socket.connect(self, addr)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused
martin@ubuntu:~/Documents$ python crackssl.py
Traceback (most recent call last):
  File "crackssl.py", line 16, in 
wrappedSocket.connect((HOST, PORT))
  File "/usr/lib/python2.7/ssl.py", line 433, in connect
self._real_connect(addr, False)
  File "/usr/lib/python2.7/ssl.py", line 423, in _real_connect
self.do_handshake()
  File "/usr/lib/python2.7/ssl.py", line 405, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [Errno 1] _ssl.c:510: error:14094410:SSL 
routines:SSL3_READ_BYTES:sslv3 alert handshake failure


import socket
import ssl

# SET VARIABLES
packet, reply = "SOME_DATA", ""
HOST, PORT = 'www.google.com.hk', 443

# CREATE SOCKET
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)

# WRAP SOCKET
wrappedSocket = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1, 
ciphers="ADH-AES256-SHA")

# CONNECT AND PRINT REPLY
wrappedSocket.connect((HOST, PORT))
wrappedSocket.send(packet)
print wrappedSocket.recv(1280)

# CLOSE SOCKET CONNECTION
wrappedSocket.close()
-- 
https://mail.python.org/mailman/listinfo/python-list


'module' object has no attribute 'wrap_socket' when use ssl

2017-04-15 Thread Ho Yeung Lee
Python 2.7.6 (default, Jun 22 2015, 18:00:18) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):
  File "", line 1, in 
  File "/home/martin/Documents/ssl.py", line 13, in 
SSLError -- exception raised for I/O errors
AttributeError: 'module' object has no attribute 'wrap_socket'
-- 
https://mail.python.org/mailman/listinfo/python-list


how to convert this container back to format build result?

2017-04-10 Thread Ho Yeung Lee

http://construct.readthedocs.io/en/latest/basics.html

format.build can build the hex string, 
but after edit attribute of the format parse result
it is a container, how to convert this container back to format build result?

#format.parse(format.build(dict(width=3,height=2,pixels=[7,8,9,11,12,13]))).signature
format = Struct(
"signature" / Const(b"a2MP"),
"width" / Int8ub,
"height" / Int8ub,
"pixels" / Array(3 * 2, Byte),
)

#protocolA = {"a1" : "a2"}
#protocolA["a2"] = "a3"
#protocolA["a3"] = "a1"
protocolA = {"a1MP" : 
format.build(dict(width=3,height=2,pixels=[7,8,9,11,12,13]))}

a2MP = 
format.parse(format.build(dict(width=3,height=2,pixels=[7,8,9,11,12,13])))
a2MP.signature = "a3MP"
protocolA["a2MP"] = a2MP.build({})
a1MP = 
format.parse(format.build(dict(width=3,height=2,pixels=[7,8,9,11,12,13])))
a1MP.signature = "a1MP"
protocolA["a3MP"] = format.build(a1MP)
-- 
https://mail.python.org/mailman/listinfo/python-list


how to group own any one common in the list?

2017-03-28 Thread Ho Yeung Lee
aaa = ["a","ab","c","b","bc"]

def similar(aa):
similarset = []
for ii in range(0,len(aa)):
for jj in range(ii,len(aa)):
if ii <> jj:
print("("+str(ii)+","+str(jj)+")")
if (aa[ii] in aa[jj]) or (aa[jj] in aa[ii]):
print(aa[ii] + " is similar with " + aa[jj])
similarset.append([aa[ii],aa[jj]])
return similarset


print similar(aaa)


do not know how to edit following code to group own any one common in the list?

import itertools
bb = similar(aaa)
from operator import itemgetter
[list(g) for _,g in itertools.groupby(sorted(bb),(itemgetter(0,1) and 
itemgetter(1,0)))]

Expected result:

group 1 [['a', 'ab'], [['ab', 'b']]
group 2 [['b', 'bc'], [['c', 'bc']]




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


Re: why and how to run forever and debug when error in for proc in psutil.process_iter()?

2017-03-26 Thread Ho Yeung Lee
On Sunday, March 26, 2017 at 7:40:20 PM UTC+8, Ho Yeung Lee wrote:
> On Sunday, March 26, 2017 at 7:32:12 PM UTC+8, Ho Yeung Lee wrote:
> > On Sunday, March 26, 2017 at 10:33:51 AM UTC+8, Deborah Swanson wrote:
> > > Someone here can probably help you, but they'll need your Python
> > > version, operating system, and full traceback. They get tired of saying
> > > so. 
> > > 
> > > In this case, the full traceback is needed to see what went wrong and
> > > when (after which statements).
> > > 
> > > 
> > > Ho Yeung Lee wrote, on Saturday, March 25, 2017 1:38 AM
> > > > 
> > > > expect below to run forever and keep running a fixed number 
> > > > of thread in python
> > > > 
> > > > would like to kill tasks when process connect internet except 
> > > > chrome and explorer.exe
> > > > 
> > > > i do this because MalwareBytes can not disconnect these 
> > > > existing trojan when my notebook connect internet
> > > > 
> > > > after run a few minutes, the program stopped, but i have 
> > > > already kept create process, why the whole program end?
> > > > 
> > > > why and how to debug when error in for proc in psutil.process_iter()?
> > > > 
> > > > 
> > > > import os
> > > > import psutil
> > > > import multiprocessing
> > > > import time
> > > > import sys
> > > > 
> > > > def cleantask():
> > > > p = os.popen("netstat -ano")
> > > > while 1:
> > > > line = p.readline()
> > > > if "TCP" in line or "UDP" in line:
> > > > linelist = line.split()
> > > > if len(linelist) > 4:
> > > > if "LISTEN" in str(linelist[3]):
> > > > for proc in psutil.process_iter():
> > > > try:
> > > > if "pop" not in str(proc.name).tolower():
> > > > os.system("taskkill /f /pid 
> > > > "+str(proc._pid))
> > > > except:
> > > > dummy = 1
> > > > #print "Unexpected error:", 
> > > > sys.exc_info()[0]
> > > > #print "Unexpected error:", 
> > > > sys.exc_info()[1]
> > > > if "ESTABLISHED" in str(linelist[3]):
> > > > if "127.0.0.1" not in str(linelist[2]):
> > > > for proc in psutil.process_iter():
> > > > try:
> > > > if str(linelist[4]) in 
> > > > str(proc._pid):
> > > > 
> > > > print(str(linelist[2])+","+str(linelist[4])+","+proc.name)
> > > > if "111.221" not in 
> > > > str(linelist[2]) and "explorer.exe" not in str(proc.name).tolower():
> > > > os.system("taskkill /f 
> > > > /pid "+str(proc._pid))
> > > > except:
> > > > dummy = 1
> > > > #print "Unexpected error:", 
> > > > sys.exc_info()[0]
> > > > #print "Unexpected error:", 
> > > > sys.exc_info()[1]
> > > > print(line)
> > > > if not line: break
> > > > 
> > > > if __name__ == '__main__':
> > > > print("main")
> > > > try:
> > > > numberofrunning = 0
> > > > plist = []
> > > > for ii in range(0,5):  
> > > > p = multiprocessing.Process(target=cleantask(), args=(0,))
> > > > p.start()
> > > > plist.append(p)
> > > > numberofrunning = numberofrunning + 1
> > > > time.sleep(1)
> > > > for pp in plist:
> > > > pp.join()
> > > > if pp.is_alive() == False:
> > > > numberofrunning = numberofrunning - 1
> >

Re: why and how to run forever and debug when error in for proc in psutil.process_iter()?

2017-03-26 Thread Ho Yeung Lee
On Sunday, March 26, 2017 at 7:32:12 PM UTC+8, Ho Yeung Lee wrote:
> On Sunday, March 26, 2017 at 10:33:51 AM UTC+8, Deborah Swanson wrote:
> > Someone here can probably help you, but they'll need your Python
> > version, operating system, and full traceback. They get tired of saying
> > so. 
> > 
> > In this case, the full traceback is needed to see what went wrong and
> > when (after which statements).
> > 
> > 
> > Ho Yeung Lee wrote, on Saturday, March 25, 2017 1:38 AM
> > > 
> > > expect below to run forever and keep running a fixed number 
> > > of thread in python
> > > 
> > > would like to kill tasks when process connect internet except 
> > > chrome and explorer.exe
> > > 
> > > i do this because MalwareBytes can not disconnect these 
> > > existing trojan when my notebook connect internet
> > > 
> > > after run a few minutes, the program stopped, but i have 
> > > already kept create process, why the whole program end?
> > > 
> > > why and how to debug when error in for proc in psutil.process_iter()?
> > > 
> > > 
> > > import os
> > > import psutil
> > > import multiprocessing
> > > import time
> > > import sys
> > > 
> > > def cleantask():
> > > p = os.popen("netstat -ano")
> > > while 1:
> > > line = p.readline()
> > > if "TCP" in line or "UDP" in line:
> > > linelist = line.split()
> > > if len(linelist) > 4:
> > > if "LISTEN" in str(linelist[3]):
> > > for proc in psutil.process_iter():
> > > try:
> > > if "pop" not in str(proc.name).tolower():
> > > os.system("taskkill /f /pid 
> > > "+str(proc._pid))
> > > except:
> > > dummy = 1
> > > #print "Unexpected error:", 
> > > sys.exc_info()[0]
> > > #print "Unexpected error:", 
> > > sys.exc_info()[1]
> > > if "ESTABLISHED" in str(linelist[3]):
> > > if "127.0.0.1" not in str(linelist[2]):
> > > for proc in psutil.process_iter():
> > > try:
> > > if str(linelist[4]) in 
> > > str(proc._pid):
> > > 
> > > print(str(linelist[2])+","+str(linelist[4])+","+proc.name)
> > > if "111.221" not in 
> > > str(linelist[2]) and "explorer.exe" not in str(proc.name).tolower():
> > > os.system("taskkill /f 
> > > /pid "+str(proc._pid))
> > > except:
> > > dummy = 1
> > > #print "Unexpected error:", 
> > > sys.exc_info()[0]
> > > #print "Unexpected error:", 
> > > sys.exc_info()[1]
> > > print(line)
> > > if not line: break
> > > 
> > > if __name__ == '__main__':
> > > print("main")
> > > try:
> > > numberofrunning = 0
> > > plist = []
> > > for ii in range(0,5):  
> > > p = multiprocessing.Process(target=cleantask(), args=(0,))
> > > p.start()
> > > plist.append(p)
> > > numberofrunning = numberofrunning + 1
> > > time.sleep(1)
> > > for pp in plist:
> > > pp.join()
> > >   if pp.is_alive() == False:
> > > numberofrunning = numberofrunning - 1
> > > plist.remove(pp)
> > > if numberofrunning > 10:
> > > print "more than 10 process"
> > > else:
> > > print("number of process = " + str(numberofrunning))
> > > if numberofrunning <= 5:
> > > p = 
> > > multiprocessing.Process(target=cleantask(), args=(0,))
> > > p.star

Re: why and how to run forever and debug when error in for proc in psutil.process_iter()?

2017-03-26 Thread Ho Yeung Lee
On Sunday, March 26, 2017 at 10:33:51 AM UTC+8, Deborah Swanson wrote:
> Someone here can probably help you, but they'll need your Python
> version, operating system, and full traceback. They get tired of saying
> so. 
> 
> In this case, the full traceback is needed to see what went wrong and
> when (after which statements).
> 
> 
> Ho Yeung Lee wrote, on Saturday, March 25, 2017 1:38 AM
> > 
> > expect below to run forever and keep running a fixed number 
> > of thread in python
> > 
> > would like to kill tasks when process connect internet except 
> > chrome and explorer.exe
> > 
> > i do this because MalwareBytes can not disconnect these 
> > existing trojan when my notebook connect internet
> > 
> > after run a few minutes, the program stopped, but i have 
> > already kept create process, why the whole program end?
> > 
> > why and how to debug when error in for proc in psutil.process_iter()?
> > 
> > 
> > import os
> > import psutil
> > import multiprocessing
> > import time
> > import sys
> > 
> > def cleantask():
> > p = os.popen("netstat -ano")
> > while 1:
> > line = p.readline()
> > if "TCP" in line or "UDP" in line:
> > linelist = line.split()
> > if len(linelist) > 4:
> > if "LISTEN" in str(linelist[3]):
> > for proc in psutil.process_iter():
> > try:
> > if "pop" not in str(proc.name).tolower():
> > os.system("taskkill /f /pid 
> > "+str(proc._pid))
> > except:
> > dummy = 1
> > #print "Unexpected error:", 
> > sys.exc_info()[0]
> > #print "Unexpected error:", 
> > sys.exc_info()[1]
> > if "ESTABLISHED" in str(linelist[3]):
> > if "127.0.0.1" not in str(linelist[2]):
> > for proc in psutil.process_iter():
> > try:
> > if str(linelist[4]) in 
> > str(proc._pid):
> > 
> > print(str(linelist[2])+","+str(linelist[4])+","+proc.name)
> > if "111.221" not in 
> > str(linelist[2]) and "explorer.exe" not in str(proc.name).tolower():
> > os.system("taskkill /f 
> > /pid "+str(proc._pid))
> > except:
> > dummy = 1
> > #print "Unexpected error:", 
> > sys.exc_info()[0]
> > #print "Unexpected error:", 
> > sys.exc_info()[1]
> > print(line)
> > if not line: break
> > 
> > if __name__ == '__main__':
> > print("main")
> > try:
> > numberofrunning = 0
> > plist = []
> > for ii in range(0,5):  
> > p = multiprocessing.Process(target=cleantask(), args=(0,))
> > p.start()
> > plist.append(p)
> > numberofrunning = numberofrunning + 1
> > time.sleep(1)
> > for pp in plist:
> > pp.join()
> > if pp.is_alive() == False:
> > numberofrunning = numberofrunning - 1
> > plist.remove(pp)
> > if numberofrunning > 10:
> > print "more than 10 process"
> > else:
> > print("number of process = " + str(numberofrunning))
> > if numberofrunning <= 5:
> > p = 
> > multiprocessing.Process(target=cleantask(), args=(0,))
> > p.start()
> > plist.append(p)
> > numberofrunning = numberofrunning + 1
> > time.sleep(1)
> > except:
> > print "Unexpected error:", sys.exc_info()[0]
> > print "Unexpected error:", sys.exc_info()[1]
> > -- 
> > https://mail.python.org/mailman/listinfo/python-list
> >

after window update error, I can not login window and reset system and
reinstall every thing

python 2.7.12

there is no error when run,

should i kill these two process with python?

2017-03-25 Thread Ho Yeung Lee
  TCP127.0.0.1:1663 127.0.0.1:28091ESTABLISHED 9900
  TCP127.0.0.1:28091127.0.0.1:1663 ESTABLISHED 9532

above two process connect to itself, named ismagent and updateui.exe

are they the malware software?


  TCP127.0.0.1:1663 127.0.0.1:28091ESTABLISHED 9900
  TCP127.0.0.1:7496 0.0.0.0:0  LISTENING   7496
  TCP127.0.0.1:270150.0.0.0:0  LISTENING   9968
  TCP127.0.0.1:280910.0.0.0:0  LISTENING   9532
  TCP127.0.0.1:28091127.0.0.1:1663 ESTABLISHED 9532
  TCP127.0.0.1:432270.0.0.0:0  LISTENING   3772
  TCP127.0.0.1:50.0.0.0:0  LISTENING   9532
  TCP192.168.1.102:1128 210.176.156.35:443 FIN_WAIT_2  5124
  TCP192.168.1.102:1509 64.233.188.102:443 ESTABLISHED 6700
  TCP192.168.1.102:1510 216.58.203.46:443  ESTABLISHED 6700
  TCP192.168.1.102:1511 216.58.203.46:443  ESTABLISHED 6700
  TCP192.168.1.102:1512 216.58.200.5:443   ESTABLISHED 6700
  TCP192.168.1.102:1513 172.217.26.195:443 ESTABLISHED 6700
  TCP192.168.1.102:1514 172.217.26.195:443 CLOSE_WAIT  6700
  TCP192.168.1.102:1898 111.221.29.156:443 ESTABLISHED 1544
-- 
https://mail.python.org/mailman/listinfo/python-list


why and how to run forever and debug when error in for proc in psutil.process_iter()?

2017-03-25 Thread Ho Yeung Lee
expect below to run forever and keep running a fixed number of thread in python

would like to kill tasks when process connect internet except chrome
and explorer.exe

i do this because MalwareBytes can not disconnect these existing trojan when my 
notebook connect internet

after run a few minutes, the program stopped, but i have already kept create 
process, why the whole program end?

why and how to debug when error in for proc in psutil.process_iter()?


import os
import psutil
import multiprocessing
import time
import sys

def cleantask():
p = os.popen("netstat -ano")
while 1:
line = p.readline()
if "TCP" in line or "UDP" in line:
linelist = line.split()
if len(linelist) > 4:
if "LISTEN" in str(linelist[3]):
for proc in psutil.process_iter():
try:
if "pop" not in str(proc.name).tolower():
os.system("taskkill /f /pid "+str(proc._pid))
except:
dummy = 1
#print "Unexpected error:", sys.exc_info()[0]
#print "Unexpected error:", sys.exc_info()[1]
if "ESTABLISHED" in str(linelist[3]):
if "127.0.0.1" not in str(linelist[2]):
for proc in psutil.process_iter():
try:
if str(linelist[4]) in str(proc._pid):  
  

print(str(linelist[2])+","+str(linelist[4])+","+proc.name)
if "111.221" not in str(linelist[2]) and 
"explorer.exe" not in str(proc.name).tolower():
os.system("taskkill /f /pid 
"+str(proc._pid))
except:
dummy = 1
#print "Unexpected error:", sys.exc_info()[0]
#print "Unexpected error:", sys.exc_info()[1]
print(line)
if not line: break

if __name__ == '__main__':
print("main")
try:
numberofrunning = 0
plist = []
for ii in range(0,5):  
p = multiprocessing.Process(target=cleantask(), args=(0,))
p.start()
plist.append(p)
numberofrunning = numberofrunning + 1
time.sleep(1)
for pp in plist:
pp.join()
if pp.is_alive() == False:
numberofrunning = numberofrunning - 1
plist.remove(pp)
if numberofrunning > 10:
print "more than 10 process"
else:
print("number of process = " + str(numberofrunning))
if numberofrunning <= 5:
p = multiprocessing.Process(target=cleantask(), args=(0,))
p.start()
plist.append(p)
numberofrunning = numberofrunning + 1
time.sleep(1)
except:
print "Unexpected error:", sys.exc_info()[0]
print "Unexpected error:", sys.exc_info()[1]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to embed non-tkinter VLC player into grid of tkinter with python?

2017-03-15 Thread Ho Yeung Lee
after several trial, still can not put the player into grid

import vlc
from Tkinter import *
import os
import sys
import ttk

import Tkinter as tk

class SampleApp(tk.Frame):
def __init__(self, parent, title=None):
#def __init__(self, *args, **kwargs):
#tk.Tk.__init__(self, *args, **kwargs)
tk.Frame.__init__(self, parent)
self.parent = parent
self.parent.title("video")
self.player = None
self.Instance = vlc.Instance('--no-audio')
self.player = self.Instance.media_player_new()
#self.player.set_xwindow(ttk.Frame(self.parent).winfo_id())
p=self.Instance.media_player_new()
m=self.Instance.media_new('file:///home/martin/Downloads/autoweb.mp4')
p.set_media(m)
p.play()
#self.videopanel = 
#p=vlc.MediaPlayer('file:///home/martin/Downloads/autoweb.mp4')
#p.play()
self.register(self, parent)
#self.player.grid(row=1,column=1)

#def GetHandle(self):
#return self.videopanel.winfo_id()

root = Tk()
for r in range(2):
for c in range(1):
termf = Frame(root, height=200, width=300)
termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()
#player = Player()
SampleApp(root, title="tkinter vlc").grid(row=r,column=c)
#os.system('xterm -into %d -geometry 40x20 -sb &' % wid)
#os.system('vlc --no-fullscreen 
"file:///home/martin/Downloads/autoweb.mp4" -into %d -geometry 100x200 -sb &' % 
wid)

root.mainloop()


On Thursday, March 16, 2017 at 1:16:41 PM UTC+8, Ho Yeung Lee wrote:
> martin@ubuntu:~/Downloads/python-vlc/examples$ xdg-screensaver: Window 
> 0x0900 does not exist
> 
> error when run after inherit a frame which put in a grid
> 
> import vlc
> from Tkinter import *
> import os
> 
> import Tkinter as tk
> 
> class SampleApp(tk.Frame):
> def __init__(self, parent, title=None):
> #def __init__(self, *args, **kwargs):
> #tk.Tk.__init__(self, *args, **kwargs)
> tk.Frame.__init__(self, parent)
> self.parent = parent
> self.parent.title("video")
> p=vlc.MediaPlayer('file:///home/martin/Downloads/autoweb.mp4')
> p.play()
> self.register()
> #self.grid(row=1,column=1)
> 
> root = Tk()
> for r in range(5):
> for c in range(5):
> termf = Frame(root, height=200, width=300)
> termf.pack(fill=BOTH, expand=YES)
> wid = termf.winfo_id()
> #player = Player()
> SampleApp(root, title="tkinter vlc").grid(row=r,column=c)
> #os.system('xterm -into %d -geometry 40x20 -sb &' % wid)
> #os.system('vlc --no-fullscreen 
> "file:///home/martin/Downloads/autoweb.mp4" -into %d -geometry 100x200 -sb &' 
> % wid)
> 
> root.mainloop()
> 
> 
> On Thursday, March 16, 2017 at 12:40:16 PM UTC+8, Ho Yeung Lee wrote:
> > we have many TV that would like to be monitored,
> > 
> > how to embed non-tkinter VLC player into grid of tkinter with python?
> > 
> > below code can embeded xterm but not for VLC player
> > 
> > 
> > import vlc
> > from Tkinter import *
> > import os
> > 
> > root = Tk()
> > for r in range(2):
> > for c in range(1):
> > termf = Frame(root, height=100, width=200)
> > termf.pack(fill=BOTH, expand=YES)
> > wid = termf.winfo_id()
> > termf.grid(row=r,column=c)
> > p=vlc.MediaPlayer('file:///home/martin/Downloads/autoweb.mp4')
> > p.get_tk_widget().grid(row=r,column=c)
> > p.play()
> > #os.system('xterm -into %d -geometry 40x20 -sb &' % wid)
> > os.system('vlc --no-fullscreen 
> > "file:///home/martin/Downloads/autoweb.mp4" -into %d -geometry 100x200 -sb 
> > &' % wid)
> > 
> > root.mainloop()

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


Re: how to embed non-tkinter VLC player into grid of tkinter with python?

2017-03-15 Thread Ho Yeung Lee
martin@ubuntu:~/Downloads/python-vlc/examples$ xdg-screensaver: Window 
0x0900 does not exist

error when run after inherit a frame which put in a grid

import vlc
from Tkinter import *
import os

import Tkinter as tk

class SampleApp(tk.Frame):
def __init__(self, parent, title=None):
#def __init__(self, *args, **kwargs):
#tk.Tk.__init__(self, *args, **kwargs)
tk.Frame.__init__(self, parent)
self.parent = parent
self.parent.title("video")
p=vlc.MediaPlayer('file:///home/martin/Downloads/autoweb.mp4')
p.play()
self.register()
#self.grid(row=1,column=1)

root = Tk()
for r in range(5):
for c in range(5):
termf = Frame(root, height=200, width=300)
termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()
#player = Player()
SampleApp(root, title="tkinter vlc").grid(row=r,column=c)
#os.system('xterm -into %d -geometry 40x20 -sb &' % wid)
#os.system('vlc --no-fullscreen 
"file:///home/martin/Downloads/autoweb.mp4" -into %d -geometry 100x200 -sb &' % 
wid)

root.mainloop()


On Thursday, March 16, 2017 at 12:40:16 PM UTC+8, Ho Yeung Lee wrote:
> we have many TV that would like to be monitored,
> 
> how to embed non-tkinter VLC player into grid of tkinter with python?
> 
> below code can embeded xterm but not for VLC player
> 
> 
> import vlc
> from Tkinter import *
> import os
> 
> root = Tk()
> for r in range(2):
> for c in range(1):
> termf = Frame(root, height=100, width=200)
> termf.pack(fill=BOTH, expand=YES)
> wid = termf.winfo_id()
> termf.grid(row=r,column=c)
> p=vlc.MediaPlayer('file:///home/martin/Downloads/autoweb.mp4')
> p.get_tk_widget().grid(row=r,column=c)
> p.play()
> #os.system('xterm -into %d -geometry 40x20 -sb &' % wid)
> os.system('vlc --no-fullscreen 
> "file:///home/martin/Downloads/autoweb.mp4" -into %d -geometry 100x200 -sb &' 
> % wid)
> 
> root.mainloop()

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


how to embed non-tkinter VLC player into grid of tkinter with python?

2017-03-15 Thread Ho Yeung Lee
we have many TV that would like to be monitored,

how to embed non-tkinter VLC player into grid of tkinter with python?

below code can embeded xterm but not for VLC player


import vlc
from Tkinter import *
import os

root = Tk()
for r in range(2):
for c in range(1):
termf = Frame(root, height=100, width=200)
termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()
termf.grid(row=r,column=c)
p=vlc.MediaPlayer('file:///home/martin/Downloads/autoweb.mp4')
p.get_tk_widget().grid(row=r,column=c)
p.play()
#os.system('xterm -into %d -geometry 40x20 -sb &' % wid)
os.system('vlc --no-fullscreen 
"file:///home/martin/Downloads/autoweb.mp4" -into %d -geometry 100x200 -sb &' % 
wid)

root.mainloop()
-- 
https://mail.python.org/mailman/listinfo/python-list


dictionary of pickle error when get it in multiprocessing and has name error

2017-02-26 Thread Ho Yeung Lee
before cloususerlogin
Unexpected error: 
after map pool


...
passwordlist = pickle.load( open( str(currentworkingdirectory) + 
"\\decryptedsecret.p", "rb" ) )

def processInput(host):
try:
decrypt_file(sys.argv[1], str(currentworkingdirectory) + 
"\\encryptedsecret.p", str(currentworkingdirectory) + "\\decryptedsecret.p", 
64*1024)
if os.path.isfile(str(currentworkingdirectory) + "\\decryptedsecret.p") 
== True:
print("exist file")
else:
print("not exist file")
print(str(currentworkingdirectory) + "\\decryptedsecret.p")
HOST = host 
print(host)
print("before cloususerlogin")
password = passwordlist["clouduserlogin"] 
<--- Name error
print(host)
text_file.write(host+"\n") 
try:
print("before recursiveconnect")
recursiveconnect(host,"")
print("after recursiveconnect")
except:
print "inner Unexpected error:", sys.exc_info()[0] 
except:
print "Unexpected error:", sys.exc_info()[0]
resultstringedge = ""



def easy_parallize():
pool = Pool(4)
print("after pool")
hostlist = [
"192.168.1.1"
]
#foo = Foo()
#results = pool.apply_async(processInput,args=(hostlist,))
results = pool.map(processInput, hostlist)
print("after map pool")
cleaned = [x for x in results if not x is None]
cleaned = np.asarray(cleaned)
pool.close()
pool.join()
print("cleaned")
return cleaned

if __name__ == '__main__':
currentworkingdirectory = str(os.getcwd()).replace("\\","")
text_file = open(newpath+"\\"+"errorstest.txt", "w")
easy_parallize()  
print("111")  
#for host in hostlist:
#if os.path.isfile(str(currentworkingdirectory) + "\\decryptedsecret.p") == 
True:
#try:
#os.remove(str(currentworkingdirectory) + "\\decryptedsecret.p")
#except:
#print("ignored error")
print("here close 1")
text_file.close()
print("here close 2")
text_file10.close()
-- 
https://mail.python.org/mailman/listinfo/python-list


Is "two different input map to one unique target called nonlinear case " or "one unique input map to two different target called nonlinear case"? Which learning method can train these two cases or no

2017-01-19 Thread Ho Yeung Lee
Is "two different input map to one unique target called nonlinear case " or 
"one unique input map to two different target called nonlinear case"? Which 
learning method can train these two cases or no method to train one of case?
-- 
https://mail.python.org/mailman/listinfo/python-list


Must target be only one bit one such as 0001,0010,0100,1000 In supervised neural learning f(w*p+b) with perceptron rule w = w + e for linear case?

2017-01-19 Thread Ho Yeung Lee
Must target be only one bit one such as 0001,0010,0100,1000 In supervised 
neural learning f(w*p+b) with perceptron rule w = w + e for linear case?

with neural network design

does it means that can not use two or more one as target such as 
0011,0110,1100,1010, 0111,1110,1101, etc when training weight?
-- 
https://mail.python.org/mailman/listinfo/python-list


how to override the solver function in sympy?

2016-12-03 Thread Ho Yeung Lee
how to override the solver function in sympy?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: compile error when using override

2016-12-02 Thread Ho Yeung Lee
from __future__ import division 
from sympy import * 
x, y, z, t = symbols('x y z t') 
k, m, n = symbols('k m n', integer=True) 
f, g, h = symbols('f g h', cls=Function) 

class AA(object): 
@staticmethod 
def __additionFunction__(a1, a2): 
return a1*a2 #Put what you want instead of this 
def __multiplyFunction__(a1, a2): 
return a1*a2+a1 #Put what you want instead of this 
def __divideFunction__(a1, a2): 
return a1*a1*a2 #Put what you want instead of this 
def __init__(self, value): 
self.value = value 
def __add__(self, other): 
return self.value*other.value 
def __mul__(self, other): 
return self.value*other.value + other.value 
def __div__(self, other): 
return self.value*other.value*other.value 

solve([AA(x)*AA(y) + AA(-1), AA(x) + AA(-2)], x, y) 

>>> solve([AA(x)*AA(y) + AA(-1), AA(x) + AA(-2)], x, y)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for +: 'Add' and 'AA'


still error, 

actually i invented 3 valued logic algebraic operation which is quintessential 
and would replace into it if this succeed


On Friday, December 2, 2016 at 1:02:19 PM UTC+8, Steve D'Aprano wrote:
> On Fri, 2 Dec 2016 01:35 pm, Ho Yeung Lee wrote:
> 
> > from __future__ import division
> > import ast
> > from sympy import *
> > x, y, z, t = symbols('x y z t')
> > k, m, n = symbols('k m n', integer=True)
> > f, g, h = symbols('f g h', cls=Function)
> > import inspect
> 
> Neither ast nor inspect is used. Why import them?
> 
> The only symbols you are using are x and y.
> 
> 
> > def op2(a,b):
> > return a*b+a
> 
> This doesn't seem to be used. Get rid of it.
> 
> 
> > class AA(object):
> > @staticmethod
> > def __additionFunction__(a1, a2):
> > return a1*a2 #Put what you want instead of this
> > def __multiplyFunction__(a1, a2):
> > return a1*a2+a1 #Put what you want instead of this
> > def __divideFunction__(a1, a2):
> > return a1*a1*a2 #Put what you want instead of this
> 
> None of those methods are used. Get rid of them.
> 
> > def __init__(self, value):
> > self.value = value
> > def __add__(self, other):
> > return self.value*other.value
> 
> Sorry, you want AA(5) + AA(2) to return 10?
> 
> > def __mul__(self, other):
> > return self.value*other.value + other.value
> > def __div__(self, other):
> > return self.value*other.value*other.value
> > 
> > solve([AA(x)*AA(y) + AA(-1), AA(x) + AA(-2)], x, y)
> 
> I don't understand what you are trying to do here. What result are you
> execting?
> 
> Maybe you just want this?
> 
> from sympy import solve, symbols
> x, y = symbols('x y')
> print( solve([x*y - 1, x - 2], x, y) )
> 
> which prints the result:
> [(2, 1/2)]
> 
> 
> Perhaps if you explain what you are trying to do, we can help better.
> 
> But please, cut down your code to only code that is being used!
> 
> 
> 
> 
> -- 
> 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: compile error when using override

2016-12-01 Thread Ho Yeung Lee
from __future__ import division 
import ast 
from sympy import * 
x, y, z, t = symbols('x y z t') 
k, m, n = symbols('k m n', integer=True) 
f, g, h = symbols('f g h', cls=Function) 
import inspect 
def op2(a,b): 
return a*b+a 

class AA(object):
@staticmethod
def __additionFunction__(a1, a2):
return a1*a2 #Put what you want instead of this
def __multiplyFunction__(a1, a2):
return a1*a2+a1 #Put what you want instead of this
def __divideFunction__(a1, a2):
return a1*a1*a2 #Put what you want instead of this
def __init__(self, value):
self.value = value
def __add__(self, other):
return self.value*other.value
def __mul__(self, other):
return self.value*other.value + other.value
def __div__(self, other):
return self.value*other.value*other.value

solve([AA(x)*AA(y) + AA(-1), AA(x) + AA(-2)], x, y)

>>> class AA(object):
... @staticmethod
... def __additionFunction__(a1, a2):
... return a1*a2 #Put what you want instead of this
... def __multiplyFunction__(a1, a2):
... return a1*a2+a1 #Put what you want instead of this
... def __divideFunction__(a1, a2):
... return a1*a1*a2 #Put what you want instead of this
... def __init__(self, value):
... self.value = value
... def __add__(self, other):
... return self.value*other.value
... def __mul__(self, other):
... return self.value*other.value + other.value
... def __div__(self, other):
... return self.value*other.value*other.value
...
>>> solve([AA(x)*AA(y) + AA(-1), AA(x) + AA(-2)], x, y)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for +: 'Add' and 'AA'


On Thursday, December 1, 2016 at 7:19:58 PM UTC+8, Steve D'Aprano wrote:
> On Thu, 1 Dec 2016 05:26 pm, Ho Yeung Lee wrote:
> 
> > import ast
> > from __future__ import division
> 
> That's not actually your code. That will be a SyntaxError.
> 
> Except in the interactive interpreter, "__future__" imports must be the very
> first line of code.
> 
> 
> > class A:
> >     @staticmethod
> >     def __additionFunction__(a1, a2):
> >         return a1*a2 #Put what you want instead of this
> 
> That cannot work in Python 2, because you are using a "classic"
> or "old-style" class. For staticmethod to work correctly, you need to
> inherit from object:
> 
> class A(object):
> ...
> 
> 
> Also, do not use double-underscore names for your own functions or methods.
> __NAME__ (two leading and two trailing underscores) are reserved for
> Python's internal use. You should not invent your own.
> 
> Why do you need this "additionFunction" method for? Why not put this in the
> __add__ method?
> 
> >   def __add__(self, other):
> >       return self.__class__.__additionFunction__(self.value, other.value)
> >   def __mul__(self, other):
> >       return self.__class__.__multiplyFunction__(self.value, other.value)
> 
> They should be:
> 
> def __add__(self, other):
> return self.additionFunction(self.value, other.value)
> 
> def __mul__(self, other):
> return self.multiplyFunction(self.value, other.value)
> 
> Or better:
> 
> def __add__(self, other):
> return self.value + other.value
> 
> def __mul__(self, other):
> return self.value * other.value
> 
> 
> 
> -- 
> 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


compile error when using override

2016-11-30 Thread Ho Yeung Lee
import ast 
from __future__ import division 
from sympy import * 
x, y, z, t = symbols('x y z t') 
k, m, n = symbols('k m n', integer=True) 
f, g, h = symbols('f g h', cls=Function) 
import inspect 

class A:
@staticmethod
def __additionFunction__(a1, a2):
return a1*a2 #Put what you want instead of this
def __multiplyFunction__(a1, a2):
return a1*a2+a1 #Put what you want instead of this
def __init__(self, value):
self.value = value
def __add__(self, other):
return self.__class__.__additionFunction__(self.value, other.value)
def __mul__(self, other):
return self.__class__.__multiplyFunction__(self.value, other.value)

solve([A(x)*A(y) + A(-1), A(x) + A(-2)], x, y)


>>> solve([A(x)*A(y) + A(-1), A(x) + A(-2)], x, y)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 12, in __mul__
TypeError: unbound method __multiplyFunction__() must be called with A instance
as first argument (got Symbol instance instead)


import ast 
from __future__ import division 
from sympy import * 
x, y, z, t = symbols('x y z t') 
k, m, n = symbols('k m n', integer=True) 
f, g, h = symbols('f g h', cls=Function) 
import inspect 

 class AA:
@staticmethod
def __additionFunction__(a1, a2):
return a1*a2 #Put what you want instead of this
def __multiplyFunction__(a1, a2):
return a1*a2+a1 #Put what you want instead of this
def __init__(self, value):
self.value = value
def __add__(self, other):
return self.__class__.__additionFunction__(self.value, other.value)
def __mul__(self, other):
return self.__class__.__multiplyFunction__(self.value, other.value)


ss = solve(AA)
ss([x*y + -1, x-2], x, y)

>>> ss([x*y + -1, x-2], x, y)
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: solve instance has no __call__ method

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


Is there a free graph library to show program flow by tranverse AST tree

2016-10-10 Thread Ho Yeung Lee
can this graph library handle recursive function call this symbol 
If it recursive call a function 3 times then in the inner loop call another 
function , can this graph library 
Show this relationship
I find video that viv can show this program flow.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to refactor nested for loop into smaller for loop assume each of them independent?

2016-10-10 Thread Ho Yeung Lee
I updated the code in msdn forum,

I calculated from the end of file 
Discover file 4550 takes a long time to run,

Assume it runs a whole day a file, 4550 days I guess need 12 years to finish 
full combination if only run at home.


Hope Python sympy can be faster than cmaple in Amazon instance
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to refactor nested for loop into smaller for loop assume each of them independent?

2016-10-10 Thread Ho Yeung Lee
Sorry my calculation is wrong, it should have around 14 billions of 
combinations after using program to count.
-- 
https://mail.python.org/mailman/listinfo/python-list


How to split a large nested for loop and distribute to hundreds of Amazon instance to run this kind of Python code?

2016-10-09 Thread Ho Yeung Lee
https://social.msdn.microsoft.com/Forums/vstudio/en-US/5f0a9a51-a256-4671-a5fc-e213949e7204/how-to-refactor-3-nested-for-loop-into-smaller-for-loop-assume-each-of-them-independent?forum=csharpgeneral

I wrote a algorithm to split for loop to generate maplesoft code for limited 
memory
Assume I convert this algorithm into Python script and use sympy to do dsolve
How to distribute to hundreds of Amazon instance to run each smaller for loop 
-- 
https://mail.python.org/mailman/listinfo/python-list


How to read maple .m file into Python?

2016-10-09 Thread Ho Yeung Lee
i saved a list of matrix of algebra into .m file in maple
How to read and import into Python for sympy to use?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: 'str' object has no attribute 'intersection' and unhashable set (Reposting On Python-List Prohibited)

2016-10-05 Thread Ho Yeung Lee
i do not understand, 

how to solve this frozonset ?


Lawrence D’Oliveiro於 2016年10月5日星期三 UTC+8下午2時24分13秒寫道:
> On Wednesday, October 5, 2016 at 2:35:25 PM UTC+13, meInvent bbird wrote:
> > it return unhashable type 'set'
> 
> This is what “frozenset” is for--it’s the immutable counterpart of “set”.
> 
> Data-typing supersymmetry, anybody?

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


Re: how to python to use virtual memory?

2016-06-25 Thread Ho Yeung Lee
what is the command or code to write to use virtual memory if i use extra 
20 GB from hard disk as memory, means from 70GB memory to 90GB memory
and left 10GB for file?

Michael Torrie於 2016年6月25日星期六 UTC+8上午11時00分36秒寫道:
> On 06/24/2016 08:44 PM, Dennis Lee Bieber wrote:
> > I don't know how Linux handles swap disk -- Windows normally sets the
> > swap space to ~2X physical memory (for small RAM -- my 12GB system has a
> > 12GB swap and suggests 18GB).
> 
> Linux typically uses a user-set swap partition.  The old rule of thumb
> was to make the swap partition 2x the size of RAM. Now, though, for most
> installations with lots of RAM, 1:1 is often used.
> 
> However, if the OP's program really requires 70 to 100 GB of space,
> relying on the virtual memory system to do this (64-bit only of course)
> is a mistake.  The system will simply thrash itself to death on any OS
> at that level of over-commit.  If he has that much data, he needs to
> employ techniques for working with the data directly on disk himself.  I
> highly doubt these big data sets that large companies work rely simply
> on the OS to manage it!

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


Re: which library has map reduce and how to use it for this case

2016-06-09 Thread Ho Yeung Lee
input are these six operators, output is finding full column of 27 elements add 
together is 54

i got memory error when searching this, 

i have difficulty in understanding map reduce and transforming my program into 
map reduce problem

M1 = {}
M2 = {}
M3 = {}
M4 = {}
M5 = {}
V6 = {}
M1['00']=0
M1['01']=2
M1['02']=1
M1['10']=1
M1['11']=1
M1['12']=1
M1['20']=1
M1['21']=1
M1['22']=2
M2['00']=0
M2['01']=1
M2['02']=1
M2['10']=1
M2['11']=1
M2['12']=1
M2['20']=1
M2['21']=1
M2['22']=1
M3['00']=2
M3['01']=2
M3['02']=2
M3['10']=0
M3['11']=2
M3['12']=1
M3['20']=0
M3['21']=1
M3['22']=2
M4['00']=1
M4['01']=2
M4['02']=1
M4['10']=2
M4['11']=2
M4['12']=2
M4['20']=0
M4['21']=1
M4['22']=2
M5['00']=0
M5['01']=1
M5['02']=1
M5['10']=0
M5['11']=2
M5['12']=1
M5['20']=0
M5['21']=1
M5['22']=1
V6['00']=1
V6['01']=1
V6['02']=2
V6['10']=1
V6['11']=2
V6['12']=1
V6['20']=1
V6['21']=2
V6['22']=2
MM = {}
MM[0] = M1
MM[1] = M2
MM[2] = M3
MM[3] = M4
MM[4] = M5
MM[5] = V6
m = 3
b = [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k in range(m)]
import itertools
deep = 3
final = []
mylist = [MM[i] for i in range(0,7-1)]
b = [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k in range(m)]
def DFS(b, deep, maxx, sourceoperators, path):
 initlist = []
 if deep > 0:
  print("deep=", deep)
  for aa,bb in itertools.combinations(sourceoperators, 2):
   print(aa,bb)
   if deep == maxx:
finalresult = []
op1xy = [aa[b[i][0:1]+b[i][1:2]] for i in range(len(b))]
op1yz = [aa[b[i][1:2]+b[i][2:3]] for i in range(len(b))]
op1xz = [aa[b[i][0:1]+b[i][2:3]] for i in range(len(b))]
op2xy = [bb[b[i][0:1]+b[i][1:2]] for i in range(len(b))]
op2yz = [bb[b[i][1:2]+b[i][2:3]] for i in range(len(b))]
op2xz = [bb[b[i][0:1]+b[i][2:3]] for i in range(len(b))]
if sum(op1xy) == 54:
  path.append([(deep, aa, "xy")])
if sum(op1yz) == 54:
  path.append([(deep, aa, "yz")])
if sum(op1xz) == 54:
  path.append([(deep, aa, "xz")])
if sum(op2xy) == 54:
  path.append([(deep, bb, "xy")])
if sum(op2yz) == 54:
  path.append([(deep, bb, "yz")])
if sum(op2xz) == 54:  
  path.append([(deep, bb, "xz")])
initlist.append(op1xy)
initlist.append(op1yz)
initlist.append(op1xz)
initlist.append(op2xy)
initlist.append(op2yz)
initlist.append(op2xz)
   else:
level = []
for j in range(len(b)):
 op1xy = [aa[b[j][i]] for i in range(len(b[j]))]
 op2xy = [bb[b[j][i]] for i in range(len(b[j]))]
 if sum(op1xy) == 54:
  path.append([(deep, aa, "xy")])
 if sum(op2xy) == 54:
  path.append([(deep, bb, "xy")])
 level.append(op1xy)
 level.append(op2xy)
 initlist.append(op1xy)
 initlist.append(op2xy)
 if deep == maxx:
  b = []
  #print(len(list(itertools.combinations(initlist, 2
  for aaa,bbb in itertools.combinations(initlist, 2): 
   b.append([str(i)+str(j) for i,j in zip(aaa, bbb)])
  path = DFS(b, deep-1, maxx, sourceoperators, path)
 else:
  #print(len(list(itertools.combinations(initlist, 2
  for aaa,bbb in itertools.combinations(initlist, 2):
   b.append([str(i)+str(j) for i,j in zip(aaa, bbb)])
  path = DFS(b, deep-1, maxx, sourceoperators, path)
 return path

path = []
mresult = DFS(b, 2, 2, mylist, path)




Michael Selik於 2016年6月10日星期五 UTC+8上午6時45分14秒寫道:
> I like using Yelp's mrjob module (https://github.com/Yelp/mrjob) to run
> Python on Hadoop.
> 
> On Thu, Jun 9, 2016 at 2:56 AM Ho Yeung Lee <davidbenny2...@gmail.com>
> wrote:
> 
> > [... a bunch of code ...]
> 
> 
> If you want to describe a map-reduce problem, start with the data. What
> does a record of your input data look like?
> 
> Then think about your mapper. What key-value pairs will you extract from
> each line of data?
> 
> Then think about your reducer. For a single key and its associated values,
> what will you calculate?

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


why it is like stop running after a 3 seconds

2016-06-09 Thread Ho Yeung Lee
i write a program, it is like forever loop
but i only restrict it to run 2 level recursively, 

why it is slow, where is the problem?


M1 = {}
M2 = {}
M3 = {}
M4 = {}
M5 = {}
V6 = {}
M1['00']=0
M1['01']=1
M1['02']=1
M1['10']=2
M1['11']=2
M1['12']=1
M1['20']=1
M1['21']=2
M1['22']=1
M2['00']=0
M2['01']=1
M2['02']=2
M2['10']=1
M2['11']=2
M2['12']=1
M2['20']=1
M2['21']=1
M2['22']=1
M3['00']=0
M3['01']=2
M3['02']=2
M3['10']=1
M3['11']=2
M3['12']=1
M3['20']=0
M3['21']=1
M3['22']=0
M4['00']=2
M4['01']=2
M4['02']=0
M4['10']=2
M4['11']=1
M4['12']=2
M4['20']=0
M4['21']=1
M4['22']=2
M5['00']=0
M5['01']=1
M5['02']=2
M5['10']=1
M5['11']=1
M5['12']=1
M5['20']=1
M5['21']=1
M5['22']=2
V6['00']=1
V6['01']=2
V6['02']=1
V6['10']=2
V6['11']=1
V6['12']=1
V6['20']=2
V6['21']=2
V6['22']=0
MM = {}
MM[0] = M1
MM[1] = M2
MM[2] = M3
MM[3] = M4
MM[4] = M5
MM[5] = V6
m = 3
b = [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k in range(m)]
import itertools
deep = 3
final = []
mylist = [MM[i] for i in range(0,7-1)]
b = [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k in range(m)]
def DFS(b, deep, maxx, sourceoperators, path):
 initlist = []
 if deep > 0:
  print("deep=", deep)
  for aa,bb in itertools.combinations(sourceoperators, 2):
   print(aa,bb)
   if deep == maxx:
finalresult = []
op1xy = [aa[b[i][0:1]+b[i][1:2]] for i in range(len(b))]
op1yz = [aa[b[i][1:2]+b[i][2:3]] for i in range(len(b))]
op1xz = [aa[b[i][0:1]+b[i][2:3]] for i in range(len(b))]
op2xy = [bb[b[i][0:1]+b[i][1:2]] for i in range(len(b))]
op2yz = [bb[b[i][1:2]+b[i][2:3]] for i in range(len(b))]
op2xz = [bb[b[i][0:1]+b[i][2:3]] for i in range(len(b))]
if sum(op1xy) == 54:
  path.append([(deep, aa, "xy")])
if sum(op1yz) == 54:
  path.append([(deep, aa, "yz")])
if sum(op1xz) == 54:
  path.append([(deep, aa, "xz")])
if sum(op2xy) == 54:
  path.append([(deep, bb, "xy")])
if sum(op2yz) == 54:
  path.append([(deep, bb, "yz")])
if sum(op2xz) == 54:  
  path.append([(deep, bb, "xz")])
initlist.append(op1xy)
initlist.append(op1yz)
initlist.append(op1xz)
initlist.append(op2xy)
initlist.append(op2yz)
initlist.append(op2xz)
   else:
level = []
for j in range(len(b)):
 op1xy = [aa[b[j][i]] for i in range(len(b[j]))]
 op2xy = [bb[b[j][i]] for i in range(len(b[j]))]
 if sum(op1xy) == 54:
  path.append([(deep, aa, "xy")])
 if sum(op2xy) == 54:
  path.append([(deep, bb, "xy")])
 level.append(op1xy)
 level.append(op2xy)
 initlist.append(op1xy)
 initlist.append(op2xy)
 if deep == maxx:
  b = []
  for aaa,bbb in itertools.combinations(initlist, 2): 
   b.append([str(i)+str(j) for i,j in zip(aaa, bbb)])
  path = DFS(b, deep-1, maxx, sourceoperators, path)
 else:
  for aaa,bbb in itertools.combinations(initlist, 2):
   b.append([str(i)+str(j) for i,j in zip(aaa, bbb)])
  path = DFS(b, deep-1, maxx, sourceoperators, path)
 return path

path = []
mresult = DFS(b, 2, 2, mylist, path)
-- 
https://mail.python.org/mailman/listinfo/python-list


which library has map reduce and how to use it for this case

2016-06-09 Thread Ho Yeung Lee
i got M1 to M5 and V6 operator

and would like to do a full combination and result will also act among each 
other, 

map reduce attract my application

how to use this in this example?

actually below is like vlookup
then example is op3(op2(op1(x->y, y->z)), x->z)

search which combinations will result in a column result is 1


M1 = {}
M2 = {}
M3 = {}
M4 = {}
M5 = {}
V6 = {}
M1['00']=0
M1['01']=1
M1['02']=1
M1['10']=1
M1['11']=1
M1['12']=1
M1['20']=1
M1['21']=1
M1['22']=1
M2['00']=0
M2['01']=0
M2['02']=1
M2['10']=0
M2['11']=1
M2['12']=1
M2['20']=1
M2['21']=1
M2['22']=1
M3['00']=0
M3['01']=0
M3['02']=0
M3['10']=0
M3['11']=1
M3['12']=1
M3['20']=0
M3['21']=1
M3['22']=1
M4['00']=0
M4['01']=1
M4['02']=0
M4['10']=1
M4['11']=1
M4['12']=1
M4['20']=0
M4['21']=1
M4['22']=1
M5['00']=0
M5['01']=0
M5['02']=0
M5['10']=0
M5['11']=1
M5['12']=1
M5['20']=0
M5['21']=1
M5['22']=1
V6['00']=1
V6['01']=1
V6['02']=1
V6['10']=1
V6['11']=1
V6['12']=1
V6['20']=0
V6['21']=1
V6['22']=1
MM = {}
MM[0] = M1
MM[1] = M2
MM[2] = M3
MM[3] = M4
MM[4] = M5
MM[5] = V6
m = 3
b = [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k in range(m)]
b[21][0:1]+b[21][1:2]
b[21][1:2]+b[21][2:3]
b[21][0:1]+b[21][2:3]

#def node(mmm, A):
 #H2 = [MM[mmm][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
 #return H2

#node(5, b[i][0:1]+b[i][1:2])


H2 = [MM[5][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
H3 = [MM[5][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
[str(k)+str(v) for k, v in zip(H2, H3)]

import itertools
deep = 3

finalresult = []
def DFS(H2, H3, b, deep, mresult)
 mylist = [i for i in range(0,7-1)]
 for aa,bb in itertools.combinations(mylist, 2):
  print(aa,bb)
  if deep == 3:
   #op0, op1
   op1xy = [MM[aa][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
   op1yz = [MM[aa][b[i][1:2]+b[i][2:3]] for i in range(len(b))]
   op1xz = [MM[aa][b[i][0:1]+b[i][2:3]] for i in range(len(b))]
   op2xy = [MM[bb][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
   op2yz = [MM[bb][b[i][1:2]+b[i][2:3]] for i in range(len(b))]
   op2xz = [MM[bb][b[i][0:1]+b[i][2:3]] for i in range(len(b))]
   mresult.append(op1xy)
   mresult.append(op1yz)
   mresult.append(op1xz)
   mresult.append(op2xy)
   mresult.append(op2yz)
   mresult.append(op2xz)
  else:
   H1 = H2
   H9 = H3
  ba = b
  b = [str(k)+str(v) for k, v in zip(H2, H3)]
  DFS(H1, H9, b)
  b = ba

DFS(0, 0, 0, 3, finalresult)
-- 
https://mail.python.org/mailman/listinfo/python-list


how to notify among the def and can network communication such as zeromq for python do this?

2016-02-23 Thread Ho Yeung Lee
in the following code, node 3 and node 4 running parallel

if there are 100 nodes running parallel, how can they notify each other

i find this post stackoverflow, 

http://stackoverflow.com/questions/29324346/how-do-i-connect-asyncio-coroutines-that-continually-produce-and-consume-data


if zeromq for python can not do notify among parallel nodes which means give 
handle to another def

my goal is to make each node can notify all other nodes if each node need

import asyncio
import time
from concurrent.futures import ProcessPoolExecutor

def f000():
 try:
  print "000"
  ex = ProcessPoolExecutor(2)
  yield from loop.run_in_executor(ex, time.sleep, 10)
 except:
  print "000 exception"

def f001():
 try:
  print "001"
  ex = ProcessPoolExecutor(2)
  yield from loop.run_in_executor(ex, time.sleep, 10)
 except:
  print "001 exception"

def f002():
 try:
  print "002"
  ex = ProcessPoolExecutor(2)
  yield from loop.run_in_executor(ex, time.sleep, 10)
 except:
  print "002 exception"

def f003():
 try:
  print "003"
  ex = ProcessPoolExecutor(2)
  yield from loop.run_in_executor(ex, time.sleep, 10)
 except:
  print "003 exception"

def f004():
 try:
  print "004"
  ex = ProcessPoolExecutor(2)
  yield from loop.run_in_executor(ex, time.sleep, 10)
 except:
  print "004 exception"

machine = {}
mappedfunc = {}
functionlist000 = []
functionlist001 = []
functionlist002 = []
functionlist003 = []
functionlist004 = []

functionlist000.append('002')
functionlist001.append('000')
functionlist002.append('003')
functionlist002.append('004')

functionlist003.append('001')
functionlist004.append('001')

machine000 = {'000': functionlist000}
machine001 = {'001': functionlist001}
machine002 = {'002': functionlist002}
machine003 = {'003': functionlist003}
machine004 = {'004': functionlist004}

machine.update(machine000)
machine.update(machine001)
machine.update(machine002)
machine.update(machine003)
machine.update(machine004)

functionkey000 = {'000': f000 }
functionkey001 = {'001': f001 }
functionkey002 = {'002': f002 }
functionkey002 = {'003': f003 }
functionkey002 = {'004': f004 }

mappedfunc.update(functionkey000)
mappedfunc.update(functionkey001)
mappedfunc.update(functionkey002)
mappedfunc.update(functionkey003)
mappedfunc.update(functionkey004)

def workingthreadpool(currentnumber1):
  i = 1
 #while i < 7:
  #i = i + 1
  functionlist0 = list(machine.get(currentnumber1).values())

  loop = asyncio.get_event_loop()
  tesks = []
  
  j = 1
  for functionlistelement in functionlist0
   tesks.append(asyncio.ensure_future(mappedfunc.get(functionlistelement)()))
   if j > 1:
workingthreadpool(functionlistelement)
   j = j + 1

  loop.run_until_complete(asyncio.wait_for(tesks, 1))
  loop.close()

currentnumber = "000"
workingthreadpool(currentnumber)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the list form dictionary's values

2016-02-21 Thread Ho Yeung Lee
Peter Otten於 2016年2月21日星期日 UTC+8下午10時07分18秒寫道:
> davidbenny2...@gmail.com wrote:
> 
> >   File "mainpy.py", line 81
> > for functionlistelement in functionlist0
> >^
> > SyntaxError: invalid syntax
> > 
> > 
> > import asyncio
> 
> [snip]
> 
> > mappedfunc = {}
> > functionlist000 = []
> > functionlist001 = []
> > functionlist002 = []
> > functionlist003 = []
> > functionlist004 = []
> 
> [snip many names with numeric suffix and repeated method calls]
> 
> I have no idea what you are trying to do; however, your code looks awfully 
> redundant. This is errorprone:
> 
> functionkey000 = {'000': f000 }
> functionkey001 = {'001': f001 }
> functionkey002 = {'002': f002 }
> functionkey002 = {'003': f003 }
> functionkey002 = {'004': f004 }
> 
> >   for functionlistelement in functionlist0
> 
> To answer what seems to be your actual question: a colon is missing at the 
> end of this line.


hi Peter,

i am running this task flow in this graph,

these code are not redundant, because this writing can clearly
show the work flow

https://drive.google.com/file/d/0B7fHc_dTzkY_OGMtTGI2UnR6ZEE/view?usp=sharing


Regards,

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


Re: how to get the list form dictionary's values

2016-02-21 Thread Ho Yeung Lee
Hi Chris,

0 ---> 2 --> 3--> 1 ---> 0
---> 4 /

i am practicing task flow in this graph situation

when current state is 2 , there are 3 and 4 to run parallel and wait list of 
tasks finish before running to 1 , 

however i feel that my code has been wrong because 3 and 4 can not combine to 
run task 1 , 

which is the correct way to combine to run task 1?

or is this graph situation an example impossible to do in computer science?

Regards,

Martin Lee

Chris Angelico於 2016年2月21日星期日 UTC+8下午10時04分22秒寫道:
> On Mon, Feb 22, 2016 at 12:34 AM,   wrote:
> >   File "mainpy.py", line 81
> > for functionlistelement in functionlist0
> >^
> > SyntaxError: invalid syntax
> 
> Your actual syntax problem here is simply a missing colon. That's
> easily fixed. But please, PLEASE, don't do this:
> 
> > def workingthreadpool(currentnumber1, machine1):
> >  try:
> >   functionlist0 = machine1.get(currentnumber1)
> >
> >   loop = asyncio.get_event_loop()
> >   tesks = []
> >
> >   j = 1
> >   for functionlistelement in functionlist0
> >
> > tesks.append(asyncio.ensure_future(mappedfunc.get(functionlistelement)()))
> >if j > 1:
> > workingthreadpool(functionlistelement)
> >j = j + 1
> >
> >   loop.run_until_complete(asyncio.wait(tesks))
> >   loop.close()
> >  except:
> >   print "workingthreadpool exception"
> 
> 1) Indenting by a single space makes it hard to skim and see what's at
> what indentation level. Indent by at least four spaces (I've seen two,
> and it's still very narrow), or one tab.
> 
> 2) Carrying a counter through a 'for' loop is much better done with
> enumerate(). But the counter's sole purpose is to ignore the first
> element, so you might want to find a completely different approach.
> 
> 3) Why does this recurse anyway? Your function appends one future to
> 'tesks' (should that be 'tasks'?), then appends another... and then
> calls itself recursively. What's that doing, exactly?
> 
> 4) Bare except clauses are almost always a bad idea. In fact, if a
> future version of Python raises a SyntaxWarning or even SyntaxError on
> the bare except, I would see it as no loss. As of Python 2.7, it's
> possible to raise an old-style class, which then won't be caught by
> "except Exception:" or even "except BaseException:", but I'm pretty
> sure there's nothing you can raise in Python 3 that isn't caught by
> "except BaseException:". Proper exception handling generally means
> either (a) catching a specific exception or list of exceptions, and
> handling them; or (b) catching everything, logging them in some way,
> and returning to some sort of main loop. Your code is simply reporting
> the presence of an exception, nothing more, and then bailing out. Let
> those exceptions bubble up to top level and be printed out!
> 
> 5) Asynchronous code is way better supported in Python 3.5 than in
> 2.7. I strongly recommend upgrading your Python.
> 
> These considerations are less simple than the syntax issue you were
> asking about, but they're also non-trivial problems. You might get
> your code to _run_, but can you get it to do the right thing, even
> after you tinker with it later on? Good code saves you time in the
> long run.
> 
> ChrisA

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