Extract the “Matrix form” dataset from BCS website.

2022-12-22 Thread hongy...@gmail.com
I want to extract / scrape the “Matrix form” dataset from the BCS website [1], 
a.k.a., the data appeared in the 3rd column.

I tried with the following python code snippet, but still failed to figure out 
the trick:

import requests
from bs4 import BeautifulSoup
import re

proxies = {
'http': 'socks5h://127.0.0.1:1',
'https': 'socks5h://127.0.0.1:1'
}

requests.packages.urllib3.disable_warnings()
r = 
requests.get('https://www.cryst.ehu.es/cgi-bin/plane/programs/nph-plane_getgen?gnum=17=plane',
 proxies=proxies, verify=False)
soup = BeautifulSoup(r.content, features="lxml")

table = soup.find('table')
id = table.find_all('id')

My python environment is as follows:

werner@X10DAi:~$ pyenv shell datasci 
(datasci) werner@X10DAi:~$ python --version
Python 3.11.1

Any tips will be appreciated.

[1] 
https://www.cryst.ehu.es/cgi-bin/plane/programs/nph-plane_getgen?gnum=17=plane

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


Re: Obtain the query interface url of BCS server.

2022-09-14 Thread hongy...@gmail.com
On Wednesday, September 14, 2022 at 10:41:32 AM UTC+8, DFS wrote:
> On 9/13/2022 7:29 PM, hongy...@gmail.com wrote: 
> > On Tuesday, September 13, 2022 at 9:33:20 PM UTC+8, DFS wrote: 
> >> On 9/13/2022 3:46 AM, hongy...@gmail.com wrote: 
> >>> On Tuesday, September 13, 2022 at 4:20:12 AM UTC+8, DFS wrote: 
> >>>> On 9/12/2022 5:00 AM, hongy...@gmail.com wrote: 
> >>>>> I want to do the query from with in script based on the interface here 
> >>>>> [1]. For this purpose, the underlying posting URL must be obtained, 
> >>>>> say, the URL corresponding to "ITA Settings" button, so that I can make 
> >>>>> the corresponding query URL and issue the query from the script. 
> >>>>> 
> >>>>> However, I did not find the conversion rules from these buttons to the 
> >>>>> corresponding URL. Any hints for achieving this aim? 
> >>>>> 
> >>>>> [1] 
> >>>>> https://www.cryst.ehu.es/cgi-bin/cryst/programs/nph-getgen?list=new=gen=10
> >>>>>  
> >>>>> 
> >>>>> Regards, 
> >>>>> Zhao 
> >>>> You didn't say what you want to query. Are you trying to download 
> >>>> entire sections of the Bilbao Crystallographic Server? 
> >>> 
> >>> I am engaged in some related research and need some specific data used by 
> >>> BCS server. 
> >> What specific data? 
> > 
> > All the data corresponding to the total catalog here: 
> > https://www.cryst.ehu.es/cgi-bin/cryst/programs/nph-getgen 
> > 
> >> Is it available elsewhere? 
> > 
> > This is an internationally recognized authoritative data source in this 
> > field. Data from other places, even if there are readily available 
> > electronic versions, are basically taken from here and are not 
> > comprehensive. 
> > 
> >>>> Maybe the admins will give you access to the data. 
> >>> 
> >>> I don't think they will provide such convenience to researchers who have 
> >>> no cooperative relationship with them. 
> >> You can try. Tell the admins what data you want, and ask them for the 
> >> easiest way to get it. 
> >>>> * this link: https://www.cryst.ehu.es/cgi-bin/cryst/programs/nph-getgen 
> >>>> brings up the table of space group symbols. 
> >>>> 
> >>>> * choose say #7: Pc 
> >>>> 
> >>>> * now click ITA Settings, then choose the last entry "P c 1 1" and it 
> >>>> loads: 
> >>>> 
> >>>> https://www.cryst.ehu.es/cgi-bin/cryst/programs//nph-trgen?gnum=007=gp=b,-a-c,c=P%20c%201%201=ita
> >>>>  
> >>> 
> >>> Not only that, but I want to obtain all such URLs programmatically! 
> >>> 
> >>>> You might be able to fool around with that URL and substitute values and 
> >>>> get back the data you want (in HTML) via Python. Do you really want 
> >>>> HTML results? 
> >>>> 
> >>>> Hit Ctrl+U to see the source HTML of a webpage 
> >>>> 
> >>>> Right-click or hit Ctrl + Shift + C to inspect the individual elements 
> >>>> of the page 
> >>> 
> >>> For batch operations, all these manual methods are inefficient. 
> >> Yes, but I don't think you'll be able to retrieve the URLs 
> >> programmatically. The JavaScript code doesn't put them in the HTML 
> >> result, except for that one I showed you, which seems like a mistake on 
> >> their part. 
> >> 
> >> So you'll have to figure out the search fields, and your python program 
> >> will have to cycle through the search values: 
> >> 
> >> Sample from above 
> >> gnum = 007 
> >> what = gp 
> >> trmat = b,-a-c,c 
> >> unconv = P c 1 1 
> >> from = ita 
> > 
> > The problem is that I must first get all possible combinations of these 
> > variables.
> Shouldn't be too hard, but I've never done some of these things and have 
> no code for you: 
> 
> space group number = gnum = 1 to 230 
> 
> * use python to put each of those values, one at a time, into the group 
> number field on the webpage 
> 
> * use python to simulate a button click of the ITA Settings button 
> 
> * it should load the HTML of the list of ITA settings for that space group 

This is the trickiest part of the problem. For this purpose, Vladimir gave the 
following sug

Re: Obtain the query interface url of BCS server.

2022-09-13 Thread hongy...@gmail.com
On Tuesday, September 13, 2022 at 9:33:20 PM UTC+8, DFS wrote:
> On 9/13/2022 3:46 AM, hongy...@gmail.com wrote: 
> > On Tuesday, September 13, 2022 at 4:20:12 AM UTC+8, DFS wrote: 
> >> On 9/12/2022 5:00 AM, hongy...@gmail.com wrote: 
> >>> I want to do the query from with in script based on the interface here 
> >>> [1]. For this purpose, the underlying posting URL must be obtained, say, 
> >>> the URL corresponding to "ITA Settings" button, so that I can make the 
> >>> corresponding query URL and issue the query from the script. 
> >>> 
> >>> However, I did not find the conversion rules from these buttons to the 
> >>> corresponding URL. Any hints for achieving this aim? 
> >>> 
> >>> [1] 
> >>> https://www.cryst.ehu.es/cgi-bin/cryst/programs/nph-getgen?list=new=gen=10
> >>>  
> >>> 
> >>> Regards, 
> >>> Zhao 
> >> You didn't say what you want to query. Are you trying to download 
> >> entire sections of the Bilbao Crystallographic Server? 
> > 
> > I am engaged in some related research and need some specific data used by 
> > BCS server.
> What specific data? 

All the data corresponding to the total catalog here:
https://www.cryst.ehu.es/cgi-bin/cryst/programs/nph-getgen
 
> Is it available elsewhere?

This is an internationally recognized authoritative data source in this field. 
Data from other places, even if there are readily available electronic 
versions, are basically taken from here and are not comprehensive.

> >> Maybe the admins will give you access to the data. 
> > 
> > I don't think they will provide such convenience to researchers who have no 
> > cooperative relationship with them.
> You can try. Tell the admins what data you want, and ask them for the 
> easiest way to get it.
> >> * this link: https://www.cryst.ehu.es/cgi-bin/cryst/programs/nph-getgen 
> >> brings up the table of space group symbols. 
> >> 
> >> * choose say #7: Pc 
> >> 
> >> * now click ITA Settings, then choose the last entry "P c 1 1" and it 
> >> loads: 
> >> 
> >> https://www.cryst.ehu.es/cgi-bin/cryst/programs//nph-trgen?gnum=007=gp=b,-a-c,c=P%20c%201%201=ita
> >>  
> > 
> > Not only that, but I want to obtain all such URLs programmatically! 
> > 
> >> You might be able to fool around with that URL and substitute values and 
> >> get back the data you want (in HTML) via Python. Do you really want 
> >> HTML results? 
> >> 
> >> Hit Ctrl+U to see the source HTML of a webpage 
> >> 
> >> Right-click or hit Ctrl + Shift + C to inspect the individual elements 
> >> of the page 
> > 
> > For batch operations, all these manual methods are inefficient.
> Yes, but I don't think you'll be able to retrieve the URLs 
> programmatically. The JavaScript code doesn't put them in the HTML 
> result, except for that one I showed you, which seems like a mistake on 
> their part. 
> 
> So you'll have to figure out the search fields, and your python program 
> will have to cycle through the search values: 
> 
> Sample from above 
> gnum = 007 
> what = gp 
> trmat = b,-a-c,c 
> unconv = P c 1 1 
> from = ita 

The problem is that I must first get all possible combinations of these 
variables.
 
> wBase = "https://www.cryst.ehu.es/cgi-bin/cryst/programs//nph-trgen; 
> wGnum = "?gnum=" + findgnum 
> wWhat = "=" + findWhat 
> wTrmat = "=" + findTrmat 
> wUnconv = "=" + findUnconv 
> wFrom = "=" + findFrom 
> webpage = wBase + wGnum + wWhat + wTrmat + wUnconv + wFrom 
> 
> Then if that returns a hit, you'll have to parse the resulting HTML and 
> extract the exact data you want. 
> 
> 
> 
> I did something similar a while back using the requests and lxml libraries 
>  
> #build url 
> wBase = "http://www.usdirectory.com; 
> wForm = "/ypr.aspx?fromform=qsearch" 
> wKeyw = "=" + keyw 
> wCityZip = "=" + cityzip 
> wState = "=" + state 
> wDist = "=" + str(miles) 
> wSort = "=a2z" #sort alpha 
> wPage = "=" #used with the results page number 
> webpage = wBase + wForm + wKeyw + wCityZip + wState + wDist 
> 
> #open URL 
> page = requests.get(webpage) 
> tree = html.fromstring(page.content) 
> 
> #no matches 
> matches = tree.xpath('//strong/text()') 
> if passNbr == 1 and ("No results were found" in str(matches)): 
> print "No results found for that search" 
> exit(0) 
>  
> 
> 
> 
> 2.x code file: https://file.io/VdptORSKh5CN 
> 
> 
> 
> > Best Regards, 
> > Zhao
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Obtain the query interface url of BCS server.

2022-09-13 Thread hongy...@gmail.com
On Tuesday, September 13, 2022 at 4:20:12 AM UTC+8, DFS wrote:
> On 9/12/2022 5:00 AM, hongy...@gmail.com wrote: 
> > I want to do the query from with in script based on the interface here [1]. 
> > For this purpose, the underlying posting URL must be obtained, say, the URL 
> > corresponding to "ITA Settings" button, so that I can make the 
> > corresponding query URL and issue the query from the script. 
> > 
> > However, I did not find the conversion rules from these buttons to the 
> > corresponding URL. Any hints for achieving this aim? 
> > 
> > [1] 
> > https://www.cryst.ehu.es/cgi-bin/cryst/programs/nph-getgen?list=new=gen=10
> >  
> > 
> > Regards, 
> > Zhao
> You didn't say what you want to query. Are you trying to download 
> entire sections of the Bilbao Crystallographic Server? 

I am engaged in some related research and need some specific data used by BCS 
server.

> Maybe the admins will give you access to the data. 

I don't think they will provide such convenience to researchers who have no 
cooperative relationship with them. 

> * this link: https://www.cryst.ehu.es/cgi-bin/cryst/programs/nph-getgen 
> brings up the table of space group symbols. 
> 
> * choose say #7: Pc 
> 
> * now click ITA Settings, then choose the last entry "P c 1 1" and it 
> loads: 
> 
> https://www.cryst.ehu.es/cgi-bin/cryst/programs//nph-trgen?gnum=007=gp=b,-a-c,c=P%20c%201%201=ita
>  

Not only that, but I want to obtain all such URLs programmatically!
 
> You might be able to fool around with that URL and substitute values and 
> get back the data you want (in HTML) via Python. Do you really want 
> HTML results? 
> 
> Hit Ctrl+U to see the source HTML of a webpage 
> 
> Right-click or hit Ctrl + Shift + C to inspect the individual elements 
> of the page

For batch operations, all these manual methods are inefficient.

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


Re: Extract the space group generators from Bilbao Crystallographic Server.

2022-07-15 Thread hongy...@gmail.com
On Friday, July 15, 2022 at 11:08:55 AM UTC+8, avi.e...@gmail.com wrote:
> I guess Dan, that he may not be seeing what he is working on as a list of 
> lists of lists with each terminal sublist being of cardinality 4. Maybe 
> Zhao could look up what methods a list object has that allow you to place 
> additional items, such as a list of 4 numbers, at the beginning or end or in 
> middle and select a method that does what he wants. 
> 
> But I have to ask where exactly he wants to place this: "[0, 0, 0, 1]" 

Yes. This must be appended to each of the subsists to construct an affine 
transformation matrix, as described here [1-3].

[1] https://www.cryst.ehu.es/cgi-bin/cryst/programs/nph-doc-trmat
[2] https://www.gap-system.org/Manuals/pkg/crystcat/htm/CHAP001.htm#SECT002
[3] https://www.gap-system.org/Manuals/pkg/cryst/htm/CHAP002.htm

> Unfortunately, we got no before and after picture, just after. I will 
> explain what that means at the end but for now, I am making believe what you 
> show is the before and see what that would mean. 
> 
> The list seems to be a representation for a matrix that is 8 by 4 by 4. 
> Where do you place just the foursome above just once without breaking the 
> matrix? I mean you can extend it at the bottom by adding four of the above 
> as in 
> [ [0, 0, 0, 1], 
> [0, 0, 0, 1], 
> [0, 0, 0, 1], 
> [0, 0, 0, 1] ] 
> 
> Or you can take each internal four partner like the first one:
> [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0,1, 0], [0, 0, 0, 1]]
> And extend each one at the right end by adding a fifth: 
> 
> [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0,1, 0], [0, 0, 0, 1], [0, 0, 0, 1]] 
> 
> I can think of other transformations albeit I have no idea why this is being 
> done and what makes sense. 
> 
> So first he needs to be clear on where and what he is adding and then think 
> of a method. 
> 
> And BTW, this problem may also be looked at under a transformation. If 
> allowed to use some modules, it might make sense to feed the data structure 
> into something that returns a 3-D matrix directly and then use methods that 
> allow you to tack on parts of other matrices of various dimensions including 
> one. You can then flip it back into nested list format, if you wish. 
> 
> OK, as mentioned earlier, if this is the AFTER then I have to look and see 
> if it is obvious where the "[0, 0, 0, 1]" was placed to make this:
> [[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0,1, 0], [0, 0, 0, 1]], 
> [[-1, 0, 0, 1], [0,-1, 0, 1/2], [0, 0, 1, 1/2], [0, 0, 0, 1]], 
> [[-1, 0, 0, 1/2], [0, 1, 0, 1/2], [0, 0,-1, 1], [0, 0, 0, 1]], 
> [[0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]], 
> [[0, 1, 0, 3/4], [1, 0, 0, 1/4], [0, 0, -1, 3/4], [0, 0, 0, 1]], 
> [[-1, 0, 0, 1/4], [0, -1, 0, 1/4], [0, 0, -1, 1/4], [0, 0, 0, 1]], 
> [[1, 0, 0, 0], [0, 1, 0, 1/2], [0, 0, 1, 1/2], [0, 0, 0, 1 ]], 
> [[1, 0, 0, 1/2], [0, 1, 0, 0], [0, 0, 1, 1/2], [0, 0, 0, 1]]]
> It looks like you are adding not once copy but eight copies with one each at 
> the end of the lists within the main list. 
> 
> That makes this easy enough so think about what it means to deal with lists 
> and NOT matrices. 
> 
> Your top list contains what I will call an a,b,c,d,e,f,g,h so if you wrote 
> code like 
> 
> Mylist = [ "a", "b", ..., "g", "h"] 
> 
> Then you can use a loop to iterate over those or perhaps a construct like 
> this: 
> 
> [ item for item in Mylist ] 
> 
> Wil gather them together and place them back in a list, which is useless but 
> it could be something applied to item like changing to upper case. In your 
> case, each item will be something like this: 
> 
> [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0,1, 0]] 
> 
> And you want to extend it by attaching this [0, 0, 0, 1] 
> 
> So how do you attach something to a list? Whatever that method is, call it 
> append and look it up and consider a line of code like: 
> 
> [ item.append(something) for item in Mylist ] 
> 
> If done right, the above list comprehension loops over one dimension of your 
> list version of a matrix and adds [0, 0, 0, 1] to the end and then 
> eventually put the a/b/d/..h components back together to look like what I 
> THINK you are asking. 
> 
> You can make some of the other possible changes using other tricks and 
> gimmicks like a nested comprehension but at some point, if you work well 
> with matrices, you may be better off converting your nested list into a 
> numpy matrix and make your addition another matrix and then use 
> numpy.concatenate, numpy.vstack and numpy.hstack with proper care with 
> multiple dimensions to specify what axis they will combine on. 
> 
> But doing the full (home)work for you is ...

Thank you for your analysi

Extract the space group generators from Bilbao Crystallographic Server.

2022-07-14 Thread hongy...@gmail.com
I'm trying to extract the matrix data of "ITA-Setting F d -3 m [origin 1]" 
listed here [1], and then building an augmented matrix for each of them by 
adding the last row as "[0, 0, 0, 1]". In short, the following form is the 
ultimate-desired  result:

[[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0,1, 0], [0, 0, 0, 1]], 
   [[-1, 0, 0, 1], [0,-1, 0, 1/2], [0, 0, 1, 1/2], [0, 0, 0, 1]],
   [[-1, 0, 0, 1/2], [0, 1, 0, 1/2], [0, 0,-1, 1], [0, 0, 0, 1]],
   [[0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]],
   [[0, 1, 0, 3/4], [1, 0, 0, 1/4], [0, 0, -1, 3/4], [0, 0, 0, 1]],
   [[-1, 0, 0, 1/4], [0, -1, 0, 1/4], [0, 0, -1, 1/4], [0, 0, 0, 1]],
   [[1, 0, 0,  0], [0, 1, 0, 1/2], [0, 0, 1, 1/2], [0, 0, 0, 1 ]],
   [[1, 0, 0, 1/2], [0, 1, 0, 0], [0, 0, 1, 1/2], [0, 0, 0, 1]]]

Any hints/tips/tricks for achieving this aim will be appreciated.

[1] 
https://www.cryst.ehu.es/cgi-bin/cryst/programs//nph-trgen?gnum=227=gen=a-1/8,b-1/8,c-1/8=F%20d%20-3%20m%20:1=ita

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


Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form

2022-05-22 Thread hongy...@gmail.com
On Thursday, May 19, 2022 at 5:26:25 AM UTC+8, Cousin Stanley wrote:
> #!/usr/bin/env python3 
> 
> ''' 
> NewsGroup  comp.lang.python 
> 
> Subject .. Convert the decimal numbers
> expressed in a numpy.ndarray 
> into a matrix representing elements 
> in fractiona
> Date . 2022-05-16 
> 
> Post_By .. hongy... 
> 
> Edit_By .. Stanley C. Kitching 
> '''
> import numpy as np 
> 
> from fractions import Fraction
> b = [ 
> [ 0.0 , -1.0 , 0.0 , 0.25 ] , 
> [ 1.0 , 0.0 , 0.0 , 0.25 ] , 
> [ 0.0 , 0.0 , 1.0 , 0.25 ] , 
> [ 0.0 , 0.0 , 0.0 , 1.0 ] ] 
> 
> a = [ ] 
> 
> print( '\n b  \n' ) 
> 
> for row in b : 
> arow = [] 
> print( ' ' , row ) 
> 
> for dec_x in row : 
> frac_x = Fraction( dec_x ) 
> arow.append( frac_x ) 
> 
> a.append( arow ) 
> 
> 
> # using f-string format 
> 
> print( '\n a  \n' ) 
> 
> for row in a : 
> 
> for item in row : 
> 
> print( f' {item} ' , end = '' ) 
> 
> print() 
> 
> # -- 

This method doesn't work, as shown below:


 b  

  [0.0, -1.0, 0.0, 0.25]
  [1.0, 0.0, 0.0, 0.25]
  [0.0, 0.0, 1.0, 0.25]
  [0.0, 0.0, 0.0, 1.0]

 a  

 0  0  0  1 


 
> -- 
> Stanley C. Kitching 
> Human Being 
> Phoenix, Arizona
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form

2022-05-16 Thread hongy...@gmail.com
On Monday, May 16, 2022 at 11:27:58 PM UTC+8, Dennis Lee Bieber wrote:
> On Mon, 16 May 2022 02:03:26 -0700 (PDT), "hongy...@gmail.com" 
>  declaimed the following: 
> 
> 
> >print(lst) 
> 
> Printing higher level structures uses the repr() of the structure and 
> its contents -- theoretically a form that could be used within code as a 
> literal.

I tried with  the repr() method as follows, but it doesn't give any output:

```
import os,sys
import numpy as np
from fractions import Fraction
import re
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.core import Lattice, Structure, Molecule, IStructure

def filepath(file):
   script_dirname=os.path.dirname(os.path.realpath(__file__))
   return (script_dirname + '/' + file)

s=IStructure.from_file(filepath('EntryWithCollCode136212.cif'))
a = SpacegroupAnalyzer(s)
SymOp=a.get_symmetry_operations()
b=SymOp[1].affine_matrix.tolist()

def strmat(m):
   if(np.array([m]).ndim==1):
  return str(Fraction(m))
   else: return list(map(lambda L:strmat(L), np.array(m)))

lst=[]
for i in SymOp:
   lst.append(i.affine_matrix.tolist())
   
a=str(strmat(lst))
a=re.sub(r"'","",a)
repr(a)
```

> If you want human-readable str() you will need to write your own 
> output loop to do the formatting of the structure, and explicitly print 
> each item of the structure. 
 

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


Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form

2022-05-16 Thread hongy...@gmail.com
On Tuesday, May 17, 2022 at 7:11:24 AM UTC+8, hongy...@gmail.com wrote:
> On Monday, May 16, 2022 at 11:27:58 PM UTC+8, Dennis Lee Bieber wrote: 
> > On Mon, 16 May 2022 02:03:26 -0700 (PDT), "hongy...@gmail.com" 
> >  declaimed the following: 
> > 
> > 
> > >print(lst) 
> > 
> > Printing higher level structures uses the repr() of the structure and 
> > its contents -- theoretically a form that could be used within code as a 
> > literal. If you want human-readable str() you will need to write your own 
> > output loop to do the formatting of the structure, and explicitly print 
> > each item of the structure.
> Thank you for your explanation. I have come up with the following methods: 
> ``` 
> b=[[0.0, -1.0, 0.0, 0.25], [1.0, 0.0, 0.0, 0.25], [0.0, 0.0, 1.0, 0.25], 
> [0.0, 0.0, 0.0, 1.0]] 
> import numpy as np 
> from fractions import Fraction 
> import re 
> 
> def strmat(m): 
> if(np.array([m]).ndim==1): 
> return str(Fraction(m)) 
> else: return list(map(lambda L:strmat(L), np.array(m))) 
> 
> a=str(strmat(b)) 
> a=re.sub(r"'","",a) 
> repr(a) 
> print(repr(a)) 
> '[[0, -1, 0, 1/4], [1, 0, 0, 1/4], [0, 0, 1, 1/4], [0, 0, 0, 1]]' 
> ``` 
> Best, 
> HZ

See here [1] for the related discussion.

[1] 
https://discuss.python.org/t/convert-the-decimal-numbers-expressed-in-a-numpy-ndarray-into-a-matrix-representing-elements-in-fractional-form/15780
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form

2022-05-16 Thread hongy...@gmail.com
On Monday, May 16, 2022 at 11:27:58 PM UTC+8, Dennis Lee Bieber wrote:
> On Mon, 16 May 2022 02:03:26 -0700 (PDT), "hongy...@gmail.com" 
>  declaimed the following: 
> 
> 
> >print(lst) 
> 
> Printing higher level structures uses the repr() of the structure and 
> its contents -- theoretically a form that could be used within code as a 
> literal. If you want human-readable str() you will need to write your own 
> output loop to do the formatting of the structure, and explicitly print 
> each item of the structure.

Thank you for your explanation. I have come up with the following methods:
```
b=[[0.0, -1.0, 0.0, 0.25], [1.0, 0.0, 0.0, 0.25], [0.0, 0.0, 1.0, 0.25], [0.0, 
0.0, 0.0, 1.0]]
import numpy as np
from fractions import Fraction
import re

def strmat(m):
if(np.array([m]).ndim==1):
return str(Fraction(m))
else: return list(map(lambda L:strmat(L), np.array(m)))

a=str(strmat(b))
a=re.sub(r"'","",a)
repr(a)
print(repr(a))
'[[0, -1, 0, 1/4], [1, 0, 0, 1/4], [0, 0, 1, 1/4], [0, 0, 0, 1]]'
```
Best, 
HZ
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form

2022-05-16 Thread hongy...@gmail.com
On Tuesday, May 17, 2022 at 8:48:27 AM UTC+8, Dennis Lee Bieber wrote:
> On Mon, 16 May 2022 17:22:17 -0700 (PDT), "hongy...@gmail.com" 
>  declaimed the following: 
> 
> 
> >
> >I tried with the repr() method as follows, but it doesn't give any output:
> I have no idea what 50% of those libraries are supposed to do, and am 
> not going to install them just to try out your posted code. If you really 
> want such help, post the MINIMUM example code the produces your problem. 
> 
> >a=str(strmat(lst)) 
> >a=re.sub(r"'","",a) 
> 
> Explain what you believe this operation is doing, show us the input and 
> the output. 
> 
> The best I can make out of that is that it is looking for single quote 
> characters within whatever "a" is, and replacing them with nothing. 
> Something much more understandable, without invoking a regular expression 
> library (especially when neither the search nor the replacement terms are 
> regular expressions) with simple string operations... 
> 
> stripped = "".join(quoted.split("'")) 

Thank you for your above trick. I tried with the following code snippet:

```
from fractions import Fraction

def strmat(m):
   if(np.array([m]).ndim==1):
  return str(Fraction(m))
   else: return list(map(lambda L:strmat(L), np.array(m)))

# For test:
b=[[0.0, -1.0, 0.0, 0.25], [1.0, 0.0, 0.0, 0.25], [0.0, 0.0, 1.0, 0.25], [0.0, 
0.0, 0.0, 1.0]]  

a=str(strmat(b))
a1=stripped = "".join(a.split("'")) 
a=re.sub(r"'","",a)
#repr(a)
print("a1 = "+ a1)
print("a = "+ a)
```

As you can see, both methods give the same results:

```
a1 = [[0, -1, 0, 1/4], [1, 0, 0, 1/4], [0, 0, 1, 1/4], [0, 0, 0, 1]]
a = [[0, -1, 0, 1/4], [1, 0, 0, 1/4], [0, 0, 1, 1/4], [0, 0, 0, 1]]
```

> You also don't need to specify RAW format for the "'" -- Python is quite 
> happy mixing single and double quotes (that is: single quotes inside a 
> string using double quotes, double quotes inside a string using single 
> quotes, either inside strings using triply quoted delimiters) 
> 
> >>> "'" 
> "'" 
> >>> '"' 
> '"' 
> >>> """'"'""" 
> '\'"\'' 
> >>> '''"'"''' 
> '"\'"' 
> >>> 
> 
> (Note that the interactive console displays results using repr(), and hence 
> escapes ' that are internal to avoid conflict with the ones wrapping the 
> output) 
> 
> >>> repr('''"'"''') 
> '\'"\\\'"\'' 
> >>> str('''"'"''') 
> '"\'"' 
> >>> print('''"'"''') 
> "'" 
> >>> 
> 
> The print() operation does not wrap the output with extraneous quotes.

Thank your insightful explanation.

Regards,
HZ

> -- 
> Wulfraed Dennis Lee Bieber AF6VN 
> wlf...@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ModuleNotFoundError: No module named 'DistUtilsExtra'

2022-01-02 Thread hongy...@gmail.com
On Monday, January 3, 2022 at 6:15:53 AM UTC+8, Marco Sulla wrote:
> https://askubuntu.com/questions/584857/distutilsextra-problem

I found its Git repository using the following method:

$ for i in python-distutils-extra python3-distutils-extra; do apt showsrc $i |& 
grep git; done | sort -u
Vcs-Git: https://salsa.debian.org/python-team/modules/python-distutils-extra.git

Therefore, for developers, the more robust and reasonable pdfarranger 
installation steps should be as follows:

#https://github.com/pdfarranger/pdfarranger#install-from-source
$ sudo apt install gir1.2-gtk-3.0 gir1.2-poppler-0.18 
$ git clone https://github.com/pdfarranger/pdfarranger.git pdfarranger.git 
$ cd pdfarranger.git 
$ pyenv shell 3.8.3
$ pyenv virtualenv --system-site-packages pdfarranger
$ pyenv shell pdfarranger 
$ pip install -U pip
$ pip install 
git+https://salsa.debian.org/python-team/modules/python-distutils-extra.git
$ pip install img2pdf pygobject
$ pip install .

Another note: the python-distutils-extra or python3-distutils-extra is only a 
dependency in the installation process, once the package is installed, it will 
no longer be needed.

For a more detailed discussion, please see here [1].

[1] 
https://github.com/pdfarranger/pdfarranger/issues/604#issuecomment-1003801361

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


ModuleNotFoundError: No module named 'DistUtilsExtra'

2022-01-02 Thread hongy...@gmail.com
On Ubuntu 20.04.3 LTS, I try to install pdfarranger [1] as follows but failed:

$ sudo apt-get install python3-pip python3-distutils-extra \
  python3-wheel python3-gi python3-gi-cairo 
\
  gir1.2-gtk-3.0 gir1.2-poppler-0.18 
python3-setuptools
$ git clone https://github.com/pdfarranger/pdfarranger.git pdfarranger.git
$ cd pdfarranger.git
$ pyenv shell 3.8.3
$ pyenv virtualenv --system-site-packages pdfarranger
$ pyenv shell pdfarranger 
$ pip install -U pip
$ ./setup.py build
Traceback (most recent call last):
  File "./setup.py", line 24, in 
from DistUtilsExtra.command import (
ModuleNotFoundError: No module named 'DistUtilsExtra'


See the following for the package list installed in this virtualenv:

$ pip list 
PackageVersion
-- 
pip21.3.1
pyfiglet   0.8.post1
setuptools 41.2.0
vtk9.0.20200612

Any hints for fixing this problem? Also see here [2-3] for relevant discussions.

[1] https://github.com/pdfarranger/pdfarranger
[2] https://github.com/pdfarranger/pdfarranger/issues/604
[3] 
https://discuss.python.org/t/modulenotfounderror-no-module-named-distutilsextra/12834

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


Re: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-31 Thread hongy...@gmail.com
On Friday, December 31, 2021 at 4:18:28 PM UTC+8, Marco Sulla wrote:
> It was already done: https://pypi.org/project/tail-recursive/

A few days ago, I also noticed another similar project:
https://github.com/baruchel/tco

It seems that they are very similar, even identical. But I'm not sure, so I 
filed an issue here [1].

[1] https://github.com/0scarB/tail-recursive/issues/1
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-31 Thread hongy...@gmail.com
On Friday, December 31, 2021 at 7:17:18 PM UTC+8, hongy...@gmail.com wrote:
> On Friday, December 31, 2021 at 4:18:28 PM UTC+8, Marco Sulla wrote: 
> > It was already done: https://pypi.org/project/tail-recursive/
> A few days ago, I also noticed another similar project: 
> https://github.com/baruchel/tco 

And also see the following one:

https://pypi.org/project/tail-recursion/

 
> It seems that they are very similar, even identical. But I'm not sure, so I 
> filed an issue here [1]. 
> 
> [1] https://github.com/0scarB/tail-recursive/issues/1
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-30 Thread hongy...@gmail.com
On Friday, December 31, 2021 at 7:50:48 AM UTC+8, MRAB wrote:
> On 2021-12-30 23:27, hongy...@gmail.com wrote: 
> > On Friday, December 31, 2021 at 7:04:24 AM UTC+8, Chris Angelico wrote: 
> >> Neither of these wants to be recursive, and writing them recursively 
> >> pollutes the function signature with parameters that really exist just 
> >> to be local variables. Passing an accumulator down is a terrible way 
> >> to demonstrate the beauty of recursion - it instead shows off how you 
> >> can shoehorn anything into recursion and make it worse in the process. 
> > 
> > Then what cases/scenarios can demonstrate the beauty of recursion? 
> >
> Walking a tree.

There are many type of trees. Do you mean all of them?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-30 Thread hongy...@gmail.com
On Friday, December 31, 2021 at 7:04:24 AM UTC+8, Chris Angelico wrote:
> Neither of these wants to be recursive, and writing them recursively 
> pollutes the function signature with parameters that really exist just 
> to be local variables. Passing an accumulator down is a terrible way 
> to demonstrate the beauty of recursion - it instead shows off how you 
> can shoehorn anything into recursion and make it worse in the process. 

Then what cases/scenarios can demonstrate the beauty of recursion?

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


Re: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-30 Thread hongy...@gmail.com
On Thursday, December 30, 2021 at 11:23:35 PM UTC+8, Chris Angelico wrote:
> If it's an exception, it needs to subclass Exception or BaseException. 

I see. That is, the following:

class TailRecurseException(Exception):
  def __init__(self, args, kwargs):
self.args = args
self.kwargs = kwargs

 
> (Also, is this REALLY an optimization? Exception handling isn't the 
> fastest. Yes, it avoids some measure of recursion depth, but it looks 
> like a pretty inefficient way to do things. Python is not Lisp, and 
> there are very very few algorithms that actually benefit from tail 
> call optimization that wouldn't benefit far more from other ways of 
> doing the same thing.) 

Could you give some examples of the other methods you mentioned above?
 
> ChrisA

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


Re: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-30 Thread hongy...@gmail.com
On Thursday, December 30, 2021 at 11:24:20 PM UTC+8, Chris Angelico wrote:
> On Fri, Dec 31, 2021 at 2:03 AM hongy...@gmail.com 
>  wrote: 
> > See here [1] for the related discussion. 
> > 
> > [1] 
> > https://discuss.python.org/t/typeerror-catching-classes-that-do-not-inherit-from-baseexception-is-not-allowed/12800
> Why did you post in two places at once? Did you need more people to 
> tell you the same thing as the error message? 

Doing so may attract the attention of developers, such as increasing the 
content of traceback information to make troubleshooting easier, just as André 
Roberge says here [1].

[1] 
https://discuss.python.org/t/typeerror-catching-classes-that-do-not-inherit-from-baseexception-is-not-allowed/12800/6?u=hongyi-zhao

 > ChrisA

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


Re: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-30 Thread hongy...@gmail.com
On Thursday, December 30, 2021 at 9:04:36 PM UTC+8, hongy...@gmail.com wrote:
> I try to compute the factorial of a large number with tail-recursion 
> optimization decorator in Python3. The following code snippet is converted 
> from the code snippet given here [1] by the following steps: 
> 
> $ pyenv shell datasci 
> $ python --version 
> Python 3.9.1 
> $ pip install 2to3 
> $ 2to3 -w this-script.py 
> 
> ``` 
> # This program shows off a python decorator( 
> # which implements tail call optimization. It 
> # does this by throwing an exception if it is 
> # its own grandparent, and catching such 
> # exceptions to recall the stack. 
> 
> import sys 
> 
> class TailRecurseException: 
> def __init__(self, args, kwargs): 
> self.args = args 
> self.kwargs = kwargs 
> 
> def tail_call_optimized(g): 
> """ 
> This function decorates a function with tail call 
> optimization. It does this by throwing an exception 
> if it is its own grandparent, and catching such 
> exceptions to fake the tail call optimization. 
> 
> This function fails if the decorated 
> function recurses in a non-tail context. 
> """ 
> def func(*args, **kwargs): 
> f = sys._getframe() 
> if f.f_back and f.f_back.f_back \ 
> and f.f_back.f_back.f_code == f.f_code: 
> raise TailRecurseException(args, kwargs) 
> else: 
> while 1: 
> try: 
> return g(*args, **kwargs) 
> except TailRecurseException as e: 
> args = e.args 
> kwargs = e.kwargs 
> func.__doc__ = g.__doc__ 
> return func 
> 
> @tail_call_optimized 
> def factorial(n, acc=1): 
> "calculate a factorial" 
> if n == 0: 
> return acc 
> return factorial(n-1, n*acc) 
> 
> print(factorial(1)) 
> # prints a big, big number, 
> # but doesn't hit the recursion limit. 
> 
> @tail_call_optimized 
> def fib(i, current = 0, next = 1): 
> if i == 0: 
> return current 
> else: 
> return fib(i - 1, next, current + next) 
> 
> print(fib(1)) 
> # also prints a big number, 
> # but doesn't hit the recursion limit. 
> ``` 
> However, when I try to test the above script, the following error will be 
> triggered: 
> ``` 
> $ python this-script.py 
> Traceback (most recent call last): 
> File "/home/werner/this-script.py", line 32, in func 
> return g(*args, **kwargs) 
> File "/home/werner/this-script.py", line 44, in factorial 
> return factorial(n-1, n*acc) 
> File "/home/werner/this-script.py", line 28, in func 
> raise TailRecurseException(args, kwargs) 
> TypeError: exceptions must derive from BaseException 
> 
> During handling of the above exception, another exception occurred: 
> 
> Traceback (most recent call last): 
> File "/home/werner/this-script.py", line 46, in  
> print(factorial(1)) 
> File "/home/werner/this-script.py", line 33, in func 
> except TailRecurseException as e: 
> TypeError: catching classes that do not inherit from BaseException is not 
> allowed 
> ``` 
> 
> Any hints for fixing this problem will be highly appreciated. 

See here [1] for the related discussion. 

[1] 
https://discuss.python.org/t/typeerror-catching-classes-that-do-not-inherit-from-baseexception-is-not-allowed/12800

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


builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-30 Thread hongy...@gmail.com
I try to compute the factorial of a large number with tail-recursion 
optimization decorator in Python3. The following code snippet is converted from 
the code snippet given here [1] by the following steps:

$ pyenv shell datasci
$ python --version
Python 3.9.1
$ pip install 2to3
$ 2to3 -w this-script.py

```
# This program shows off a python decorator(
# which implements tail call optimization. It
# does this by throwing an exception if it is 
# its own grandparent, and catching such 
# exceptions to recall the stack.

import sys

class TailRecurseException:
  def __init__(self, args, kwargs):
self.args = args
self.kwargs = kwargs

def tail_call_optimized(g):
  """
  This function decorates a function with tail call
  optimization. It does this by throwing an exception
  if it is its own grandparent, and catching such
  exceptions to fake the tail call optimization.

  This function fails if the decorated
  function recurses in a non-tail context.
  """
  def func(*args, **kwargs):
f = sys._getframe()
if f.f_back and f.f_back.f_back \
and f.f_back.f_back.f_code == f.f_code:
  raise TailRecurseException(args, kwargs)
else:
  while 1:
try:
  return g(*args, **kwargs)
except TailRecurseException as e:
  args = e.args
  kwargs = e.kwargs
  func.__doc__ = g.__doc__
  return func

@tail_call_optimized
def factorial(n, acc=1):
  "calculate a factorial"
  if n == 0:
return acc
  return factorial(n-1, n*acc)

print(factorial(1))
# prints a big, big number,
# but doesn't hit the recursion limit.

@tail_call_optimized
def fib(i, current = 0, next = 1):
  if i == 0:
return current
  else:
return fib(i - 1, next, current + next)

print(fib(1))
# also prints a big number,
# but doesn't hit the recursion limit.
```
However, when I try to test the above script, the following error will be 
triggered:
```
$ python this-script.py 
Traceback (most recent call last):
  File "/home/werner/this-script.py", line 32, in func
return g(*args, **kwargs)
  File "/home/werner/this-script.py", line 44, in factorial
return factorial(n-1, n*acc)
  File "/home/werner/this-script.py", line 28, in func
raise TailRecurseException(args, kwargs)
TypeError: exceptions must derive from BaseException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/werner/this-script.py", line 46, in 
print(factorial(1))
  File "/home/werner/this-script.py", line 33, in func
except TailRecurseException as e:
TypeError: catching classes that do not inherit from BaseException is not 
allowed
```

Any hints for fixing this problem will be highly appreciated.

[1]  https://stackoverflow.com/q/27417874

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


Re: Get a function definition/implementation hint similar to the one shown in pycharm.

2021-10-19 Thread hongy...@gmail.com
On Tuesday, October 19, 2021 at 5:22:25 AM UTC+8, cameron...@gmail.com wrote:
> On 18Oct2021 01:43, Hongyi Zhao  wrote: 
> >I've written the following python code snippet in pycharm: 
> >```python 
> >import numpy as np 
> >from numpy import pi, sin 
> > 
> >a = np.array([1], dtype=bool) 
> >if np.in|vert(a) == ~a: 
> > print('ok') 
> >``` 
> >When putting the point/cursor in the above code snippet at the position 
> >denoted by `|`, I would like to see information similar to that provided by 
> >`pycharm`, as shown in the following screenshots: 
> > 
> >https://user-images.githubusercontent.com/11155854/137619512-674e0eda-7564-4e76-af86-04a194ebeb8e.png
> > 
> >https://user-images.githubusercontent.com/11155854/137619524-a0b584a3-1627-4612-ab1f-05ec1af67d55.png
> > 
> > 
> >But I wonder if there are any other python packages/tools that can help 
> >me achieve this goal?
> Broadly, you want the "inspect" module, which is part of the stdlib, 
> documented here: https://docs.python.org/3/library/inspect.html 
> 
> It has many functions for extracting information about things, and you 
> want the signature() function to get the parameters and type annotations 
> of a function. 
> 
> def f(a): 
> ... 
> 
> sig = signature(f) 
> 
> You also want the function docstring for the help text, which is 
> "f.__doc__" in the example above. This is what gets printed by help(): 
> 
> >>> import numpy as np 
> >>> help(np.invert) 

But the following doesn't work:

In [4]: help(~)
  File "", line 1
help(~)
  ^
SyntaxError: invalid syntax

 
> It may be that PyCharm has additional information about some libraries 
> allowing it to include a reference to the only documentation. 
> 
> Cheers, 
> Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Get a function definition/implementation hint similar to the one shown in pycharm.

2021-10-18 Thread hongy...@gmail.com
On Tuesday, October 19, 2021 at 5:22:25 AM UTC+8, cameron...@gmail.com wrote:
> On 18Oct2021 01:43, Hongyi Zhao  wrote: 
> >I've written the following python code snippet in pycharm: 
> >```python 
> >import numpy as np 
> >from numpy import pi, sin 
> > 
> >a = np.array([1], dtype=bool) 
> >if np.in|vert(a) == ~a: 
> > print('ok') 
> >``` 
> >When putting the point/cursor in the above code snippet at the position 
> >denoted by `|`, I would like to see information similar to that provided by 
> >`pycharm`, as shown in the following screenshots: 
> > 
> >https://user-images.githubusercontent.com/11155854/137619512-674e0eda-7564-4e76-af86-04a194ebeb8e.png
> > 
> >https://user-images.githubusercontent.com/11155854/137619524-a0b584a3-1627-4612-ab1f-05ec1af67d55.png
> > 
> > 
> >But I wonder if there are any other python packages/tools that can help 
> >me achieve this goal?
> Broadly, you want the "inspect" module, which is part of the stdlib, 
> documented here: https://docs.python.org/3/library/inspect.html 
> 
> It has many functions for extracting information about things, and you 
> want the signature() function to get the parameters and type annotations 
> of a function. 
> 
> def f(a): 
> ... 
> 
> sig = signature(f) 

The following ipython test failed:


In [1]: import numpy as np

In [2]: from inspect import  signature

In [3]: signature(np.invert)
---
ValueErrorTraceback (most recent call last)
 in 
> 1 signature(np.invert)

~/.pyenv/versions/3.9.1/lib/python3.9/inspect.py in signature(obj, 
follow_wrapped)
   3128 def signature(obj, *, follow_wrapped=True):
   3129 """Get a signature object for the passed callable."""
-> 3130 return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
   3131 
   3132 

~/.pyenv/versions/3.9.1/lib/python3.9/inspect.py in from_callable(cls, obj, 
follow_wrapped)
   2877 def from_callable(cls, obj, *, follow_wrapped=True):
   2878 """Constructs Signature for the given callable object."""
-> 2879 return _signature_from_callable(obj, sigcls=cls,
   2880 
follow_wrapper_chains=follow_wrapped)
   2881 

~/.pyenv/versions/3.9.1/lib/python3.9/inspect.py in 
_signature_from_callable(obj, follow_wrapper_chains, skip_bound_arg, sigcls)
   2441 raise ValueError(msg)
   2442 
-> 2443 raise ValueError('callable {!r} is not supported by 
signature'.format(obj))
   2444 
   2445 

ValueError: callable  is not supported by signature




 
> You also want the function docstring for the help text, which is 
> "f.__doc__" in the example above. This is what gets printed by help(): 
> 
> >>> import numpy as np 
> >>> help(np.invert) 
> 
> It may be that PyCharm has additional information about some libraries 
> allowing it to include a reference to the only documentation. 
> 
> Cheers, 
> Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


Get a function definition/implementation hint similar to the one shown in pycharm.

2021-10-18 Thread hongy...@gmail.com
I've written the following python code snippet in pycharm:
```python
import numpy as np
from numpy import pi, sin

a = np.array([1], dtype=bool)
if np.in|vert(a) == ~a:
print('ok')
```
When putting the point/cursor in the above code snippet at the position denoted 
by `|`, I would like to see information similar to that provided by `pycharm`, 
as shown in the following screenshots:

https://user-images.githubusercontent.com/11155854/137619512-674e0eda-7564-4e76-af86-04a194ebeb8e.png
https://user-images.githubusercontent.com/11155854/137619524-a0b584a3-1627-4612-ab1f-05ec1af67d55.png

But I wonder if there are any other python packages/tools that can help me 
achieve this goal?

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


Re: Understanding the working mechanis of python unary arithmetic operators.

2021-10-05 Thread hongy...@gmail.com
On Sunday, October 3, 2021 at 9:55:15 PM UTC+8, hongy...@gmail.com wrote:
> On Sunday, October 3, 2021 at 8:38:16 PM UTC+8, ju...@diegidio.name wrote: 
> > On Sunday, 3 October 2021 at 14:21:13 UTC+2, hongy...@gmail.com wrote: 
> > > On Sunday, October 3, 2021 at 6:31:05 PM UTC+8, ju...@diegidio.name 
> > > wrote: 
> > 
> > > > Then you can guess that numpy overrides it and gives you *logical* 
> > > > negation of boolean values, 
> > > 
> > > I try to dig through the numpy source code to pinning point the 
> > > overriding/monkey patching/decorating code snippets, as follows: 
> > And you keep missing the point: look up numpy's *documentation* for that, 
> > not any source code.
> I find the relevant explanation here [1]: 
> 
> numpy.invert 
> [...] 
> Compute bit-wise inversion, or bit-wise NOT, element-wise. 
> 
> Computes the bit-wise NOT of the underlying binary representation of the 
> integers in the input arrays. This ufunc implements the C/Python operator ~. 
> [...] 
> The ~ operator can be used as a shorthand for np.invert on ndarrays. 
> 
> x1 = np.array([True, False]) 
> 
> ~x1 
> array([False, True]) 
> 
> [1] 
> https://numpy.org/doc/stable/reference/generated/numpy.invert.html#numpy-invert
>  

Or use the following commands in IPython:

import numpy as np
np.invert?
np.info(np.invert)


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


Re: Understanding the working mechanis of python unary arithmetic operators.

2021-10-05 Thread hongy...@gmail.com
On Sunday, October 3, 2021 at 8:38:16 PM UTC+8, ju...@diegidio.name wrote:
> On Sunday, 3 October 2021 at 14:21:13 UTC+2, hongy...@gmail.com wrote: 
> > On Sunday, October 3, 2021 at 6:31:05 PM UTC+8, ju...@diegidio.name wrote: 
> 
> > > Then you can guess that numpy overrides it and gives you *logical* 
> > > negation of boolean values, 
> > 
> > I try to dig through the numpy source code to pinning point the 
> > overriding/monkey patching/decorating code snippets, as follows:
> And you keep missing the point: look up numpy's *documentation* for that, not 
> any source code. 

I find the relevant explanation here [1]:

numpy.invert
[...]
Compute bit-wise inversion, or bit-wise NOT, element-wise.

Computes the bit-wise NOT of the underlying binary representation of the 
integers in the input arrays. This ufunc implements the C/Python operator ~.
[...]
The ~ operator can be used as a shorthand for np.invert on ndarrays.

x1 = np.array([True, False])

~x1
array([False,  True])

[1] 
https://numpy.org/doc/stable/reference/generated/numpy.invert.html#numpy-invert

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


Re: Understanding the working mechanis of python unary arithmetic operators.

2021-10-05 Thread hongy...@gmail.com
On Sunday, October 3, 2021 at 6:31:05 PM UTC+8, ju...@diegidio.name wrote:
> On Sunday, 3 October 2021 at 11:24:58 UTC+2, hongy...@gmail.com wrote: 
> > On Sunday, October 3, 2021 at 2:18:17 PM UTC+8, hongy...@gmail.com wrote: 
> > > On Saturday, October 2, 2021 at 4:59:54 PM UTC+8, ju...@diegidio.name 
> > > wrote: 
> > > > On Saturday, 2 October 2021 at 10:34:27 UTC+2, hongy...@gmail.com 
> > > > wrote: 
> > > > > See the following testings: 
> > > > > 
> > > > > In [24]: a=3.1415926535897932384626433832795028841971 
> > > > > In [27]: -a 
> > > > > Out[27]: -3.141592653589793 
> > > > You've never heard of floating-point? Double precision has 53 
> > > > significant bits of mantissa, corresponding approximately to 16 decimal 
> > > > digits. 
> > > > <https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64>
> > > >  
> > > > > In [17]: ~-+1 
> > > > > Out[17]: 0 
> > > > << The unary ~ (invert) operator yields the bitwise inversion of its 
> > > > integer argument. The bitwise inversion of x is defined as -(x+1). It 
> > > > only applies to integral numbers or to custom objects that override the 
> > > > __invert__() special method. >> 
> > > > <https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations>
> > > >  
> > > A further inference based on the above description: 
> > > 
> > > Let us consider this equation: -(x+1) = x, the solution is -0.5, which is 
> > > not an integer. So we can safely come to a conclusion: 
> > > 
> > > If bool(a) == True, \forall a \in integer, then ~bool(a) == False; and 
> > > vice versa. 
> > > 
> > > This is exactly the theoretical basis to filter some specific columns in 
> > > pandas, just as the issue discussed here [1]. 
> > Sorry my not very precise description above. I should have wanted to 
> > express the fact that I observed below: 
> > In [3]: import numpy as np 
> > In [15]: ~np.array([True]) 
> > Out[15]: array([False]) 
> > 
> > In [16]: ~np.array([False]) 
> > Out[16]: array([ True]) 
> > 
> > But the normal `True' and `False' don't the good symmetric feature as shown 
> > above: 
> > 
> > In [21]: bool(~True) 
> > Out[21]: True 
> > 
> > In [22]: bool(~False) 
> > Out[22]: True
> You keep missing the point:
> << The unary ~ (invert) operator yields the bitwise inversion of its integer 
> argument. The bitwise inversion of x is defined as -(x+1). It only applies to 
> integral numbers or to custom objects that override the __invert__() special 
> method. >>
> Then you can guess that numpy overrides it and gives you *logical* negation 
> of boolean values,

I try to dig through the numpy source code to pinning point the 
overriding/monkey patching/decorating code snippets, as follows:

$ rg -A5 -uu 'def __invert__' .
./numpy/__init__.pyi
2022:def __invert__(self: NDArray[bool_]) -> NDArray[bool_]: ...
2023-@overload
2024:def __invert__(self: NDArray[_IntType]) -> NDArray[_IntType]: ...
2025-@overload
2026:def __invert__(self: NDArray[object_]) -> Any: ...
2027-
2028-@overload
2029-def __pos__(self: NDArray[_NumberType]) -> NDArray[_NumberType]: ...
2030-@overload
2031-def __pos__(self: NDArray[timedelta64]) -> NDArray[timedelta64]: ...
--
2885:def __invert__(self) -> bool_: ...
2886-__lshift__: _BoolBitOp[int8]
2887-__rlshift__: _BoolBitOp[int8]
2888-__rshift__: _BoolBitOp[int8]
2889-__rrshift__: _BoolBitOp[int8]
2890-__and__: _BoolBitOp[bool_]
--
2993:def __invert__(self: _IntType) -> _IntType: ...
2994-# Ensure that objects annotated as `integer` support bit-wise 
operations
2995-def __lshift__(self, other: _IntLike_co) -> integer: ...
2996-def __rlshift__(self, other: _IntLike_co) -> integer: ...
2997-def __rshift__(self, other: _IntLike_co) -> integer: ...
2998-def __rrshift__(self, other: _IntLike_co) -> integer: ...

./numpy/array_api/_array_object.py
510:def __invert__(self: Array, /) -> Array:
511-"""
512-Performs the operation __invert__.
513-"""
514-if self.dtype not in _integer_or_boolean_dtypes:
515-raise TypeError("Only integer or boolean dtypes are allowed in 
__invert__")

./numpy/lib/user_array.py
179:def __invert__(self):
180-return self._rc(invert(self.array))
181-
182-def _scalarfu

Re: Understanding the working mechanis of python unary arithmetic operators.

2021-10-05 Thread hongy...@gmail.com
On Saturday, October 2, 2021 at 4:59:54 PM UTC+8, ju...@diegidio.name wrote:
> On Saturday, 2 October 2021 at 10:34:27 UTC+2, hongy...@gmail.com wrote: 
> > See the following testings: 
> > 
> > In [24]: a=3.1415926535897932384626433832795028841971 
> > In [27]: -a 
> > Out[27]: -3.141592653589793
> You've never heard of floating-point? Double precision has 53 significant 
> bits of mantissa, corresponding approximately to 16 decimal digits. 
> <https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64>
> > In [17]: ~-+1 
> > Out[17]: 0
> << The unary ~ (invert) operator yields the bitwise inversion of its integer 
> argument. The bitwise inversion of x is defined as -(x+1). It only applies to 
> integral numbers or to custom objects that override the __invert__() special 
> method. >> 
> <https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations>

A further inference based on the above description:

Let us consider this equation:  -(x+1) = x, the solution is -0.5, which is not 
an integer. So we can safely come to a conclusion:

If bool(a) == True,  \forall a \in integer, then ~bool(a) == False; and vice 
versa.

This is exactly the theoretical basis to filter some specific columns in 
pandas, just as the issue discussed here [1].

[1] https://github.com/pandas-dev/pandas/issues/43832#issue-1013375587

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


Re: Understanding the working mechanis of python unary arithmetic operators.

2021-10-05 Thread hongy...@gmail.com
On Sunday, October 3, 2021 at 2:18:17 PM UTC+8, hongy...@gmail.com wrote:
> On Saturday, October 2, 2021 at 4:59:54 PM UTC+8, ju...@diegidio.name wrote:
> > On Saturday, 2 October 2021 at 10:34:27 UTC+2, hongy...@gmail.com wrote: 
> > > See the following testings: 
> > > 
> > > In [24]: a=3.1415926535897932384626433832795028841971 
> > > In [27]: -a 
> > > Out[27]: -3.141592653589793 
> > You've never heard of floating-point? Double precision has 53 significant 
> > bits of mantissa, corresponding approximately to 16 decimal digits. 
> > <https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64>
> >  
> > > In [17]: ~-+1 
> > > Out[17]: 0 
> > << The unary ~ (invert) operator yields the bitwise inversion of its 
> > integer argument. The bitwise inversion of x is defined as -(x+1). It only 
> > applies to integral numbers or to custom objects that override the 
> > __invert__() special method. >> 
> > <https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations>
> A further inference based on the above description: 
> 
> Let us consider this equation: -(x+1) = x, the solution is -0.5, which is not 
> an integer. So we can safely come to a conclusion: 
> 
> If bool(a) == True, \forall a \in integer, then ~bool(a) == False; and vice 
> versa. 
> 
> This is exactly the theoretical basis to filter some specific columns in 
> pandas, just as the issue discussed here [1]. 

Sorry my not very precise description above. I should have wanted to express 
the fact that I observed below:

In [3]: import numpy as np
In [15]: ~np.array([True])
Out[15]: array([False])

In [16]: ~np.array([False])
Out[16]: array([ True])

But the normal `True' and `False' don't the good symmetric feature as shown 
above:

In [21]: bool(~True)
Out[21]: True

In [22]: bool(~False)
Out[22]: True

 
> [1] https://github.com/pandas-dev/pandas/issues/43832#issue-1013375587 
> 
> HZ
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Understanding the working mechanis of python unary arithmetic operators.

2021-10-05 Thread hongy...@gmail.com
On Sunday, October 3, 2021 at 3:05:23 AM UTC+8, ju...@diegidio.name wrote:
> On Saturday, 2 October 2021 at 14:48:39 UTC+2, hongy...@gmail.com wrote: 
> > On Saturday, October 2, 2021 at 4:59:54 PM UTC+8, ju...@diegidio.name 
> > wrote: 
> > > On Saturday, 2 October 2021 at 10:34:27 UTC+2, hongy...@gmail.com wrote: 
> > > > See the following testings: 
> > > > 
> > > > In [24]: a=3.1415926535897932384626433832795028841971 
> > > > In [27]: -a 
> > > > Out[27]: -3.141592653589793 
> > > 
> > > You've never heard of floating-point? Double precision has 53 significant 
> > > bits of mantissa, corresponding approximately to 16 decimal digits. 
> > > <https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64>
> > >  
> > > 
> > > > In [17]: ~-+1 
> > > > Out[17]: 0 
> > > 
> > > << The unary ~ (invert) operator yields the bitwise inversion of its 
> > > integer argument. The bitwise inversion of x is defined as -(x+1). It 
> > > only applies to integral numbers or to custom objects that override the 
> > > __invert__() special method. >> 
> > > <https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations>
> > >  
> > > 
> > > > I'm very puzzled by these operators. Any hints will be highly 
> > > > appreciated. 
> > > 
> > > Try and read the proverbial manual: that's truly a fundamental skill... 
> > 
> > Thank you for your explanation. Then what about the following questions?: 
> > 
> > 1. Should `+' and `-' be classified as binary operators or unary operators?
> "Symbol overloading": a+b is binary *addition*, +a is unary *identity* (or 
> however you may like to call it). The meaning of a symbol or name depends on 
> context.

As I understand now, it can be used to identify/normalize the operand by the 
corresponding precision in the given context.

> > As we all know, `a + b', and `a - b' are the normal ways we do basic 
> > arithmetic operations.
> Nonsense. You yourself wrote ~(-(+1)) above, just without parentheses.

Thank you for pointing out my contradictory assertion.

> > 2. See the following testings:
> Read the bloody manual. 
> 
> Given that: 
> int(True) = 1 
> int(False) = 0 
> 
> and that: 
> bool(x) is True iff x <> 0 

In [8]: bool(None)
Out[8]: False

In [9]: bool('')
Out[9]: False

In [10]: bool(0)
Out[10]: False

So, bool(x) is True iff x <> 0 , None, and '', as shown here [1]:

In [3]: import numpy as np
In [11]: np.array([1, 0.5, 0, None, 'a', '', True, False], dtype=bool)
Out[11]: array([ True,  True, False, False,  True, False,  True, False])
 
[1] https://riptutorial.com/numpy/example/21181/creating-a-boolean-array#example
 
> and that: 
> ~~x = x for all x (integer) 
> 
> These: 
> ~1 = -(1+1) = -2 
> ~~1 = ~-2 = -(-2+1) = 1 
> ~~~1 = ~1 = -2 
> ... 
> all evaluate to True. 
> 
> And these: 
> ~0 = -(0+1) = -1 
> ~~0 = ~-1 = -(-1+1) = 0 
> ~~~0 = ~0 = -1 
> ... 
> evaluate to True and False alternatingly. 
> 
> In short, ~1=-2 (and ~-2=1) and 1 and -2 both convert to True, while ~0=1 but 
> 0 converts to False while 1 converts to True.
> > Why can’t/shouldn't we get something similar results for both `True' and 
> > `False' in the above testings?
> Because bitwise complement is not symmetric around 0. For comparison, try 
> with negation instead. 

Thanks again. I see, as follows:

In [1]: ~1
Out[1]: -2

In [2]: ~-1
Out[2]: 0

 
> But don't just guess, try and unpack those expressions while trying and 
> reading the docs: which is a necessary skill in itself. 

Thank you for the advice that showed me the way to truth.

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


Re: Understanding the working mechanis of python unary arithmetic operators.

2021-10-02 Thread hongy...@gmail.com
On Saturday, October 2, 2021 at 4:59:54 PM UTC+8, ju...@diegidio.name wrote:
> On Saturday, 2 October 2021 at 10:34:27 UTC+2, hongy...@gmail.com wrote: 
> > See the following testings: 
> > 
> > In [24]: a=3.1415926535897932384626433832795028841971 
> > In [27]: -a 
> > Out[27]: -3.141592653589793
> You've never heard of floating-point? Double precision has 53 significant 
> bits of mantissa, corresponding approximately to 16 decimal digits. 
> <https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64>
> > In [17]: ~-+1 
> > Out[17]: 0
> << The unary ~ (invert) operator yields the bitwise inversion of its integer 
> argument. The bitwise inversion of x is defined as -(x+1). It only applies to 
> integral numbers or to custom objects that override the __invert__() special 
> method. >> 
> <https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations>
> > I'm very puzzled by these operators. Any hints will be highly appreciated.
> Try and read the proverbial manual: that's truly a fundamental skill... 

Thank you for your explanation. Then what about the following questions?:

1. Should `+' and `-' be classified as binary operators or unary operators? As 
we all know, `a + b', and `a - b' are the normal ways we do basic arithmetic 
operations.

2. See the following testings:

In [20]: bool(int(True))
Out[20]: True

In [21]: bool(~int(True))
Out[21]: True

In [22]: bool(~~int(True))
Out[22]: True

In [23]: bool(~~~int(True))
Out[23]: True

In [24]: bool(int(False))
Out[24]: False

In [25]: bool(~int(False))
Out[25]: True

In [26]: bool(~~int(False))
Out[26]: False

In [27]: bool(~~~int(False))
Out[27]: True

Why can’t/shouldn't we get something similar results for both `True' and 
`False' in the above testings?

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


Understanding the working mechanis of python unary arithmetic operators.

2021-10-02 Thread hongy...@gmail.com
See the following testings:

In [24]: a=3.1415926535897932384626433832795028841971
In [27]: -a
Out[27]: -3.141592653589793

In [28]: +a
Out[28]: 3.141592653589793

In [17]: ~-+1
Out[17]: 0

In [18]: -~+1
Out[18]: 2

In [19]: -+~1
Out[19]: 2

In [20]: +~-1
Out[20]: 0

I'm very puzzled by these operators. Any hints will be highly appreciated.

Regards,
HZ

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


Re: XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\n\r\n\r\n\r\n'

2021-09-30 Thread hongy...@gmail.com
On Thursday, September 30, 2021 at 9:20:37 AM UTC+8, hongy...@gmail.com wrote:
> On Thursday, September 30, 2021 at 5:20:04 AM UTC+8, Peter J. Holzer wrote: 
> > On 2021-09-29 01:22:03 -0700, hongy...@gmail.com wrote: 
> > > I tried to convert a xls file into csv with the following command, but 
> > > failed: 
> > > 
> > > $ in2csv --sheet 'Sheet1' 2021-2022-1.xls 
> > > XLRDError: Unsupported format, or corrupt file: Expected BOF record; 
> > > found b'\r\n\r\n\r\n\r\n' 
> > > 
> > > The above testing file is located at here [1]. 
> > > 
> > > [1] https://github.com/hongyi-zhao/temp/blob/master/2021-2022-1.xls 
> > Why is that file name .xls when it's obviously an HTML file?
> Good catch! Thank you for pointing this out. This file is automatically 
> exported from my university's teaching management system, and it was assigned 
> the .xls extension by default. 

According to the above comment, after I change the extension to html, the 
following python code will do the trick:


import sys
import pandas as pd

if len(sys.argv) != 2:
print('Usage: ' + sys.argv[0] + ' input-file')
exit(1)

myhtml_pd = pd.read_html(sys.argv[1])
#In [25]: len(myhtml_pd)
#Out[25]: 3

for i in myhtml_pd[2].index:
if i > 0:
for j in myhtml_pd[2].columns:
if j >1 and not pd.isnull(myhtml_pd[2].loc[i][j]):
print(myhtml_pd[2].loc[i][j])

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


Re: XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\n\r\n\r\n\r\n'

2021-09-29 Thread hongy...@gmail.com
On Thursday, September 30, 2021 at 5:20:04 AM UTC+8, Peter J. Holzer wrote:
> On 2021-09-29 01:22:03 -0700, hongy...@gmail.com wrote: 
> > I tried to convert a xls file into csv with the following command, but 
> > failed: 
> > 
> > $ in2csv --sheet 'Sheet1' 2021-2022-1.xls 
> > XLRDError: Unsupported format, or corrupt file: Expected BOF record; found 
> > b'\r\n\r\n\r\n\r\n' 
> > 
> > The above testing file is located at here [1]. 
> > 
> > [1] https://github.com/hongyi-zhao/temp/blob/master/2021-2022-1.xls
> Why is that file name .xls when it's obviously an HTML file? 

Good catch! Thank you for pointing this out. This file is automatically 
exported from my university's teaching management system, and it was assigned 
the .xls extension by default.

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


Re: XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\n\r\n\r\n\r\n'

2021-09-29 Thread hongy...@gmail.com
On Wednesday, September 29, 2021 at 8:12:08 PM UTC+8, J.O. Aho wrote:
> On 29/09/2021 13.10, hongy...@gmail.com wrote: 
> > On Wednesday, September 29, 2021 at 5:40:58 PM UTC+8, J.O. Aho wrote: 
> >> On 29/09/2021 10.22, hongy...@gmail.com wrote: 
> >>> I tried to convert a xls file into csv with the following command, but 
> >>> failed: 
> >>> 
> >>> $ in2csv --sheet 'Sheet1' 2021-2022-1.xls 
> >>> XLRDError: Unsupported format, or corrupt file: Expected BOF record; 
> >>> found b'\r\n\r\n\r\n\r\n' 
> >>> 
> >>> The above testing file is located at here [1]. 
> >>> 
> >>> [1] https://github.com/hongyi-zhao/temp/blob/master/2021-2022-1.xls 
> >>> 
> >>> Any hints for fixing this problem? 
> >> You need to delete the 13 first lines in the file 
> > 
> > Yes. After deleting the top 3 lines, the problem has been fixed. 
> > 
> >> or you see to that your code does first trim the data before start xml 
> >> parse it. 
> > 
> > Yes. I really want to do this trick programmatically, but how do I do it 
> > without manually editing the file?
> You could do something like loading the XML into a string (myxmlstr)

How to do this operation? As you have seen, the file refused to be loaded at 
all.

> and then find the fist < in that string 
> 
> xmlstart = myxmlstr.find('<') 
> 
> xmlstr = myxmlstr[xmlstart:] 
> 
> then use the xmlstr in the xml parser, sure not as convenient as loading 
> the file directly to the xml parser. 
> 
> I don't say this is the best way of doing it, I'm sure some python wiz 
> here would have a smarter solution. 

Another very strange thing: I trimmed the first 3 lines in the original file 
and saved it into a new one named as  2021-2022-1-trimmed-top-3-lines.xls. [1]

Then I read the file with the following python script named as pandas-excel.py:

--
import pandas as pd

excel_file='2021-2022-1-trimmed-top-3-lines.xls'

#print(pd.ExcelFile(excel_file).sheet_names)

newpd=pd.read_excel(excel_file, sheet_name='Sheet1')

for i in newpd.index:
 if i >1:
 for j in newpd.columns:
 if int(j.split()[1]) > 2:
 if not pd.isnull(newpd.loc[i][j]):
 print(newpd.loc[i][j])
--

$ python pandas-excel.py | sort -u
汽车实用英语 [1-8]周 1-4节 38 汽车楼413基础电气实训室II 汽修1932 
汽车车载网络系统的检测与修复 [1-12]周 1-4节 38 汽车楼416安全、舒适系统实训室 汽修1932 

OTOH, I also tried to read the file with in2csv as follows:

$ in2csv --sheet Sheet1 2021-2022-1-trimmed-top-3-lines.xls 2>/dev/null |tr ',' 
'\n' | \
  sed -re '/^$/d' | sort -u  | awk '{print length($0),$0}' | sort -k1n | tail 
-3 | cut -d ' '  -f2-
汽车实用英语 [1-8]周 1-4节 38 汽车楼413基础电气实训室II 汽修1932 
智能网联汽车概论 [1-8]周 6-9节 45 汽车楼511汽车营销策划实训室 汽销1931 
汽车车载网络系统的检测与修复 [1-12]周 1-4节 38 汽车楼416安全、舒适系统实训室 汽修1932 

As you can see, the above two methods give different results. I'm very puzzled 
by this phenomenon. Any hints/tips/comments will be greatly appreciated.

[1] 
https://github.com/hongyi-zhao/temp/blob/master/2021-2022-1-trimmed-top-3-lines.xls

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


Re: XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\n\r\n\r\n\r\n'

2021-09-29 Thread hongy...@gmail.com
On Wednesday, September 29, 2021 at 5:40:58 PM UTC+8, J.O. Aho wrote:
> On 29/09/2021 10.22, hongy...@gmail.com wrote: 
> > I tried to convert a xls file into csv with the following command, but 
> > failed: 
> > 
> > $ in2csv --sheet 'Sheet1' 2021-2022-1.xls 
> > XLRDError: Unsupported format, or corrupt file: Expected BOF record; found 
> > b'\r\n\r\n\r\n\r\n' 
> > 
> > The above testing file is located at here [1]. 
> > 
> > [1] https://github.com/hongyi-zhao/temp/blob/master/2021-2022-1.xls 
> > 
> > Any hints for fixing this problem?
> You need to delete the 13 first lines in the file 

Yes. After deleting the top 3 lines, the problem has been fixed. 

> or you see to that your code does first trim the data before start xml parse 
> it. 

Yes. I really want to do this trick programmatically, but how do I do it 
without manually editing the file?

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


XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\n\r\n\r\n\r\n'

2021-09-29 Thread hongy...@gmail.com
I tried to convert a xls file into csv with the following command, but failed:

$ in2csv --sheet 'Sheet1'  2021-2022-1.xls
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found 
b'\r\n\r\n\r\n\r\n'

The above testing file is located at here [1].

[1] https://github.com/hongyi-zhao/temp/blob/master/2021-2022-1.xls

Any hints for fixing this problem?

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


Re: Change the display style of the text on the STACKLINE.

2021-09-10 Thread hongy...@gmail.com
On Saturday, September 11, 2021 at 7:43:44 AM UTC+8, hongy...@gmail.com wrote:
> On Friday, September 10, 2021 at 2:12:31 PM UTC+8, Roland Mueller wrote: 
> > pe 10. syysk. 2021 klo 8.53 hongy...@gmail.com (hongy...@gmail.com) 
> > kirjoitti: 
> > > On Thursday, September 9, 2021 at 8:57:37 PM UTC+8, Roland Mueller wrote: 
> > > > Hello 
> > > > 
> > > > to 9. syysk. 2021 klo 6.53 hongy...@gmail.com (hongy...@gmail.com) 
> > > > kirjoitti: 
> > > > > I'm using the following code in my forked project [1]: 
> > > > > 
> > > > > percol.view.STACKLINE = 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s 
> > > Dir:M-d 
> > > > > Dircmd:M-b' 
> > > > > 
> > > > > I would like to change the display style of the text mentioned above, 
> > > for 
> > > > > example, to underline some characters in it, as shown below: 
> > > > > 
> > > > > _D_ir:M-d 
> > > > > 
> > > > > 
> > > > You can use e.g. str.replace() or re.sub() 
> > > > 
> > > > >>> percol.view.STACKLINE = percol.view.STACKLINE.replace('D', '_D_') 
> > > > Result: 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s _D_ir:M-d 
> > > _D_ircmd:M-b' 
> > > > 
> > > > >>> import re 
> > > > 
> > > > Replace D with _D_ 
> > > > >>> percol.view.STACKLINE = re.sub(r'([D])', 
> > > > r'_\1_', percol.view.STACKLINE) 
> > > > Result: 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s _D_ir:M-d 
> > > _D_ircmd:M-b' 
> > > > 
> > > > Replace D and M with _D_, _M_ 
> > > > >>> percol.view.STACKLINE = re.sub(r'([DM])', r'_\1_', 
> > > > percol.view.STACKLINE) 
> > > > 'Fold:F1,F2,F3 Push:C-p Pop:_M_-p Script:_M_-s _D_ir:_M_-d 
> > > _D_ircmd:_M_-b' 
> > > > 
> > > > Regards, 
> > > > Roland 
> > > 
> > > I tried with the following, but failed to achieve the expected effect: 
> > > 
> > > class Term: 
> > > HEADER = '\033[95m' 
> > > OKBLUE = '\033[94m' 
> > > OKGREEN = '\033[92m' 
> > > WARNING = '\033[93m' 
> > > FAIL = '\033[91m' 
> > > ENDC = '\033[0m' 
> > > LIGHTCYAN = '\033[1;36m' 
> > > LIGHTGRAY = '\033[0;37m' 
> > > YELLOW = '\033[0;33m' 
> > > BOLD = '\033[1m' 
> > > UNDERLINE = '\033[4m' 
> > > 
> > > [...] 
> > > 
> > > percol.view.STACKLINE = percol.view.STACKLINE.replace('D', Term.UNDERLINE 
> > > + 'D' + Term.ENDC) 
> > > 
> > > The result will look like this: 
> > > 
> > > Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s ?[4mD?[0mir:M-d 
> > > ?[4mD?[0mircmd:M-b 
> > > 
> > > I cannot repeat that. Are you sure that the '?' shown in your output are 
> > not due to your terminal settings that influence how strings printed by 
> > Python or inside used terminal are shown? 
> > 
> > Python 3.9.6 (default, Jul 16 2021, 00:00:00) 
> > [GCC 11.1.1 20210531 (Red Hat 11.1.1-3)] on linux 
> > Type "help", "copyright", "credits" or "license" for more information. 
> > >>> UL = '\033[4m' 
> > >>> UL 
> > '\x1b[4m' 
> > >>> ENDC = '\033[0m' 
> > >>> ENDC 
> > '\x1b[0m' 
> > 
> > >>> s = UL + 'D' + ENDC 
> > >>> s 
> > '\x1b[4mD\x1b[0m' 
> > 
> > >>> s = 'ABCDE' 
> > >>> s = s.replace('D', UL + 'D' + ENDC) 
> > >>> s 
> > 'ABC\x1b[4mD\x1b[0mE' 
> > 
> > When I call print(s) it even shows ABCD and D is underscored.
> If I test the code snippet above with ipython/ptpython/python, I got the same 
> result as you described. But the problem I reported here is triggered by 
> running the command line wrapper of the project by `Ctrl-r`, which is worked 
> with curses library. And I also noticed the following file [1] used by the 
> project, which may be pertinent to the problem discussed here. 
> But till now I still can't think of a solution. 
> 
> [1] https://github.com/hongyi-zhao/ariadne/blob/master/percol/ansi.py

And refer to the following file used in the project:

https://github.com/hongyi-zhao/ariadne/blob/master/percol/display.py

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


Re: Change the display style of the text on the STACKLINE.

2021-09-10 Thread hongy...@gmail.com
On Friday, September 10, 2021 at 2:12:31 PM UTC+8, Roland Mueller wrote:
> pe 10. syysk. 2021 klo 8.53 hongy...@gmail.com (hongy...@gmail.com) 
> kirjoitti:
> > On Thursday, September 9, 2021 at 8:57:37 PM UTC+8, Roland Mueller wrote: 
> > > Hello 
> > > 
> > > to 9. syysk. 2021 klo 6.53 hongy...@gmail.com (hongy...@gmail.com) 
> > > kirjoitti: 
> > > > I'm using the following code in my forked project [1]: 
> > > > 
> > > > percol.view.STACKLINE = 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s 
> > Dir:M-d 
> > > > Dircmd:M-b' 
> > > > 
> > > > I would like to change the display style of the text mentioned above, 
> > for 
> > > > example, to underline some characters in it, as shown below: 
> > > > 
> > > > _D_ir:M-d 
> > > > 
> > > > 
> > > You can use e.g. str.replace() or re.sub() 
> > > 
> > > >>> percol.view.STACKLINE = percol.view.STACKLINE.replace('D', '_D_') 
> > > Result: 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s _D_ir:M-d 
> > _D_ircmd:M-b' 
> > > 
> > > >>> import re 
> > > 
> > > Replace D with _D_ 
> > > >>> percol.view.STACKLINE = re.sub(r'([D])', 
> > > r'_\1_', percol.view.STACKLINE) 
> > > Result: 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s _D_ir:M-d 
> > _D_ircmd:M-b' 
> > > 
> > > Replace D and M with _D_, _M_ 
> > > >>> percol.view.STACKLINE = re.sub(r'([DM])', r'_\1_', 
> > > percol.view.STACKLINE) 
> > > 'Fold:F1,F2,F3 Push:C-p Pop:_M_-p Script:_M_-s _D_ir:_M_-d 
> > _D_ircmd:_M_-b' 
> > > 
> > > Regards, 
> > > Roland 
> > 
> > I tried with the following, but failed to achieve the expected effect: 
> > 
> > class Term: 
> > HEADER = '\033[95m' 
> > OKBLUE = '\033[94m' 
> > OKGREEN = '\033[92m' 
> > WARNING = '\033[93m' 
> > FAIL = '\033[91m' 
> > ENDC = '\033[0m' 
> > LIGHTCYAN = '\033[1;36m' 
> > LIGHTGRAY = '\033[0;37m' 
> > YELLOW = '\033[0;33m' 
> > BOLD = '\033[1m' 
> > UNDERLINE = '\033[4m' 
> > 
> > [...] 
> > 
> > percol.view.STACKLINE = percol.view.STACKLINE.replace('D', Term.UNDERLINE 
> > + 'D' + Term.ENDC) 
> > 
> > The result will look like this: 
> > 
> > Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s ?[4mD?[0mir:M-d 
> > ?[4mD?[0mircmd:M-b 
> >
> > I cannot repeat that. Are you sure that the '?' shown in your output are 
> not due to your terminal settings that influence how strings printed by 
> Python or inside used terminal are shown? 
> 
> Python 3.9.6 (default, Jul 16 2021, 00:00:00) 
> [GCC 11.1.1 20210531 (Red Hat 11.1.1-3)] on linux 
> Type "help", "copyright", "credits" or "license" for more information. 
> >>> UL = '\033[4m' 
> >>> UL 
> '\x1b[4m' 
> >>> ENDC = '\033[0m' 
> >>> ENDC 
> '\x1b[0m' 
> 
> >>> s = UL + 'D' + ENDC 
> >>> s 
> '\x1b[4mD\x1b[0m' 
> 
> >>> s = 'ABCDE' 
> >>> s = s.replace('D', UL + 'D' + ENDC) 
> >>> s 
> 'ABC\x1b[4mD\x1b[0mE' 
> 
> When I call print(s) it even shows ABCD and D is underscored.

If I test the code snippet above with ipython/ptpython/python, I got the same 
result as you described. But the problem I reported here is triggered by 
running the command line wrapper of the project by `Ctrl-r`, which is worked 
with curses library. And I also noticed the following file [1] used by the 
project, which may be pertinent to the problem discussed here.
But till now I still can't think of a solution.

[1] https://github.com/hongyi-zhao/ariadne/blob/master/percol/ansi.py

> But copying 
> the output to mail looses the underscore ... 
> [image: image.png] 
> 
> BR, 
> Roland
> > Regards, 
> > HY 
> > 
> > 
> 
> 
> 
> 
> > > > How to achieve this purpose? 
> > > > 
> > > > [1] 
> > > > 
> > https://github.com/hongyi-zhao/ariadne/blob/838179bb4275ac85f5342d9e7d086d6ade3be1de/rc.py#L55
> >  
> > > > 
> > > > Regards, 
> > > > HY 
> > > > -- 
> > > > https://mail.python.org/mailman/listinfo/python-list 
> > > >
> > -- 
> > https://mail.python.org/mailman/listinfo/python-list 
> >
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Change the display style of the text on the STACKLINE.

2021-09-09 Thread hongy...@gmail.com
On Thursday, September 9, 2021 at 8:57:37 PM UTC+8, Roland Mueller wrote:
> Hello 
> 
> to 9. syysk. 2021 klo 6.53 hongy...@gmail.com (hongy...@gmail.com) 
> kirjoitti:
> > I'm using the following code in my forked project [1]: 
> > 
> > percol.view.STACKLINE = 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s Dir:M-d 
> > Dircmd:M-b' 
> > 
> > I would like to change the display style of the text mentioned above, for 
> > example, to underline some characters in it, as shown below: 
> > 
> > _D_ir:M-d 
> > 
> >
> You can use e.g. str.replace() or re.sub() 
> 
> >>> percol.view.STACKLINE = percol.view.STACKLINE.replace('D', '_D_') 
> Result: 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s _D_ir:M-d _D_ircmd:M-b' 
> 
> >>> import re 
> 
> Replace D with _D_ 
> >>> percol.view.STACKLINE = re.sub(r'([D])', 
> r'_\1_', percol.view.STACKLINE) 
> Result: 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s _D_ir:M-d _D_ircmd:M-b' 
> 
> Replace D and M with _D_, _M_ 
> >>> percol.view.STACKLINE = re.sub(r'([DM])', r'_\1_', 
> percol.view.STACKLINE) 
> 'Fold:F1,F2,F3 Push:C-p Pop:_M_-p Script:_M_-s _D_ir:_M_-d _D_ircmd:_M_-b' 
> 
> Regards, 
> Roland

I tried with the following, but failed to achieve the expected effect:

class Term:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
LIGHTCYAN = '\033[1;36m'
LIGHTGRAY = '\033[0;37m'
YELLOW = '\033[0;33m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

[...]

percol.view.STACKLINE = percol.view.STACKLINE.replace('D', Term.UNDERLINE + 'D' 
+ Term.ENDC)

The result will look like this:

Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s ?[4mD?[0mir:M-d ?[4mD?[0mircmd:M-b

Regards,
HY

> > How to achieve this purpose? 
> > 
> > [1] 
> > https://github.com/hongyi-zhao/ariadne/blob/838179bb4275ac85f5342d9e7d086d6ade3be1de/rc.py#L55
> >  
> > 
> > Regards, 
> > HY
> > -- 
> > https://mail.python.org/mailman/listinfo/python-list 
> >
-- 
https://mail.python.org/mailman/listinfo/python-list


Change the display style of the text on the STACKLINE.

2021-09-08 Thread hongy...@gmail.com
I'm using the following code in my forked project [1]:

percol.view.STACKLINE = 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s Dir:M-d 
Dircmd:M-b'

I would like to change the display style of the text mentioned above, for 
example, to underline some characters in it, as shown below:

_D_ir:M-d

How to achieve this purpose? 

[1] 
https://github.com/hongyi-zhao/ariadne/blob/838179bb4275ac85f5342d9e7d086d6ade3be1de/rc.py#L55

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


Re: combine multiple xlsx files which include dropdown columns in them.

2021-09-08 Thread hongy...@gmail.com
On Thursday, September 9, 2021 at 9:15:23 AM UTC+8, Kushal Kumaran wrote:
> On Tue, Sep 07 2021 at 10:05:58 PM, "hongy...@gmail.com"  
> wrote: 
> > I've some xlsx files which include dropdown columns in them. I want to 
> > know whether I can combine all the lines into one xlsx file. Any hints 
> > for doing this job with python programmatically will be highly 
> > appreciated. 
> >
> The dropdown is *probably* implemented using Excel's data validation 
> mechanism. openpyxl's documentation says it can read[1]/write Excel 
> files and also mentions data validation[2]. You might be able to use it 
> to read the existing files, combine the rows and write to a different 
> file. 
> 
> [1] 
> https://openpyxl.readthedocs.io/en/stable/usage.html#read-an-existing-workbook
>  
> [2] https://openpyxl.readthedocs.io/en/stable/validation.html 

Thank you for your comments. For a related discussion on this topic, please 
refer to [1].

[1] https://groups.google.com/g/openpyxl-users/c/k2xnPZS2kbo/m/m2YmEO9ZBgAJ

Regards,
HY

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


combine multiple xlsx files which include dropdown columns in them.

2021-09-08 Thread hongy...@gmail.com
I've some xlsx files which include dropdown columns in them. I want to know 
whether I can combine all the lines into one xlsx file. Any hints for doing 
this job with python programmatically will be highly appreciated.

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


Re-design the position of the RPROMPT string.

2021-09-06 Thread hongy...@gmail.com
I forked and made some improvements to the ariadne package [1]. I noticed that 
the current RPROMPT line is composed by percol.view.PROMPT [2] and 
percol.view.__class__.RPROMPT [3], as shown below:

X10DAi-00 (M-h/M-n)> M-m:string Path:C-d Local:C-l Unique:M-r Exit0:M-t 
Fold:F1,F2,F3

But this leaves very little room for the search string used to filter the 
history. So, I want to split the RPROMPT line into two lines as follows:

M-m:string Path:C-d Local:C-l Unique:M-r Exit0:M-t Fold:F1,F2,F3
X10DAi-00 (M-h/M-n)>

But I've tried a lot and still don't know how to do it. Any hints will be 
highly appreciated.

Regareds,
HY

[1] https://github.com/hongyi-zhao/ariadne
[2] 
https://github.com/hongyi-zhao/ariadne/blob/ec864434b8a947b801f019e84c827c3a96dcf7e4/rc.py#L56
[3] 
https://github.com/hongyi-zhao/ariadne/blob/ec864434b8a947b801f019e84c827c3a96dcf7e4/rc.py#L73
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Mutually exclusive options with argparse.

2021-09-02 Thread hongy...@gmail.com
On Thursday, September 2, 2021 at 10:16:09 AM UTC+8, hongy...@gmail.com wrote:
> On Wednesday, September 1, 2021 at 11:20:21 PM UTC+8, Lee Congdon wrote: 
> > Does a mutually exclusive group, as described in "Mutual exclusion" at 
> > https://docs.python.org/3/library/argparse.html meet your needs?
> Thanks for letting me know about this feature. I'll give it a try.

The exact document URL is located at here [1].

[1] https://docs.python.org/3/library/argparse.html#mutual-exclusion

> > 
> > On Wed, Sep 1, 2021 at 9:48 AM hongy...@gmail.com  
> > wrote: 
> > > See the following code snippets [1] for implementation of the exclusive 
> > > options with argparse: 
> > > 
> > > def query_builder(args): 
> > > if args.r and args.s: 
> > > sys.exit(Term.FAIL + 'Only one of -re and -sql should be set' + 
> > > Term.ENDC) 
> > > sum_status = sum(1 for x in [args.failure, args.code != -1] if x) 
> > > if sum_status > 1: 
> > > sys.exit(Term.FAIL + ('Only one of --failure and --code has to be 
> > > set') + Term.ENDC) 
> > > 
> > > [1] 
> > > https://github.com/hongyi-zhao/recent2/blob/5486afbd56a6b06bb149a3ea969fb33d9d8b288f/recent2.py#L391
> > >  
> > > 
> > > It seems that the above method is awkward, but I'm out of idea to work 
> > > out 
> > > more graceful solutions. Any comment/suggestion/enhancement will be 
> > > highly 
> > > appreciated. 
> > > 
> > > Regards, 
> > > HY 
> > > -- 
> > > https://mail.python.org/mailman/listinfo/python-list 
> > > 
> > 
> > 
> > -- 
> > +1-202-507-9867, Twitter @lcongdon
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Mutually exclusive options with argparse.

2021-09-02 Thread hongy...@gmail.com
On Wednesday, September 1, 2021 at 11:20:21 PM UTC+8, Lee Congdon wrote:
> Does a mutually exclusive group, as described in "Mutual exclusion" at 
> https://docs.python.org/3/library/argparse.html meet your needs? 

Thanks for letting me know about this feature. I'll give it a try.

> 
> On Wed, Sep 1, 2021 at 9:48 AM hongy...@gmail.com  
> wrote:
> > See the following code snippets [1] for implementation of the exclusive 
> > options with argparse: 
> > 
> > def query_builder(args): 
> > if args.r and args.s: 
> > sys.exit(Term.FAIL + 'Only one of -re and -sql should be set' + 
> > Term.ENDC) 
> > sum_status = sum(1 for x in [args.failure, args.code != -1] if x) 
> > if sum_status > 1: 
> > sys.exit(Term.FAIL + ('Only one of --failure and --code has to be 
> > set') + Term.ENDC) 
> > 
> > [1] 
> > https://github.com/hongyi-zhao/recent2/blob/5486afbd56a6b06bb149a3ea969fb33d9d8b288f/recent2.py#L391
> >  
> > 
> > It seems that the above method is awkward, but I'm out of idea to work out 
> > more graceful solutions. Any comment/suggestion/enhancement will be highly 
> > appreciated. 
> > 
> > Regards, 
> > HY
> > -- 
> > https://mail.python.org/mailman/listinfo/python-list 
> > 
> 
> 
> -- 
> +1-202-507-9867, Twitter @lcongdon
-- 
https://mail.python.org/mailman/listinfo/python-list


Mutually exclusive options with argparse.

2021-09-01 Thread hongy...@gmail.com
See the following code snippets [1] for implementation of the exclusive options 
with argparse:

def query_builder(args):
if args.r and args.s:
sys.exit(Term.FAIL + 'Only one of -re and -sql should be set' + 
Term.ENDC)
sum_status = sum(1 for x in [args.failure, args.code != -1] if x)
if sum_status > 1:
sys.exit(Term.FAIL + ('Only one of --failure and --code has to be set') 
+ Term.ENDC)

[1] 
https://github.com/hongyi-zhao/recent2/blob/5486afbd56a6b06bb149a3ea969fb33d9d8b288f/recent2.py#L391

It seems that the above method is awkward, but I'm out of idea to work out more 
graceful solutions. Any comment/suggestion/enhancement will be highly 
appreciated.

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


Re: Create a real-time interactive TUI using Python.

2021-08-31 Thread hongy...@gmail.com
On Wednesday, September 1, 2021 at 12:49:51 AM UTC+8, Chris Angelico wrote:
> On Wed, Sep 1, 2021 at 1:59 AM hongy...@gmail.com  wrote: 
> > 
> > I want to know whether python can be used to create real-time interactive 
> > TUI, as hstr [1] does. 
> > 
> > [1] https://github.com/dvorka/hstr 
> >
> Yes. 

The following are some python TUI framework libraries/projects I have 
discovered so far:

https://github.com/pfalcon/picotui
https://github.com/peterbrittain/asciimatics
https://github.com/bczsalba/pytermgui
https://github.com/GeorgeFilipkin/pulsemixer
https://github.com/jwlodek/py_cui
https://github.com/saulpw/visidata
https://github.com/willmcgugan/textual
https://github.com/urwid/urwid

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


Create a real-time interactive TUI using Python.

2021-08-31 Thread hongy...@gmail.com
I want to know whether python can be used to create real-time interactive TUI, 
as hstr [1] does.

[1] https://github.com/dvorka/hstr

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


Re: The sqlite3 timestamp conversion between unixepoch and localtime can't be done according to the timezone setting on the machine automatically.

2021-08-31 Thread hongy...@gmail.com
On Tuesday, August 31, 2021 at 7:55:51 AM UTC+8, Dennis Lee Bieber wrote:
> On Sun, 29 Aug 2021 19:49:19 -0700 (PDT), "hongy...@gmail.com" 
>  declaimed the following:
> >On Ubuntu 20.04.2 LTS, I use the 
> >[recent2](https://github.com/dotslash/recent2/blob/master/recent2.py) to log 
> >and query my bash history, which uses a sqlite3 database to store the 
> >history information. The `datetime` of running a command is inserted in the 
> >sqlite3 database with [a `unixepoch` 
> >format](https://github.com/dotslash/recent2/blob/f1018aee228c710cc7d1b8b93bc0228791a54563/recent2.py#L45),
> > and it will be converted into `localtime` accordingly when retrieved and 
> >displayed later. 
> >
> As I read it, it is interpreting whatever value was provided as 
> UNIXEPOCH when storing the value -- but stores it as an ISO date/time 
> string! https://www.sqlite.org/lang_datefunc.html 
> 
> sqlite> select datetime(1092941466, 'unixepoch'); 
> 2004-08-19 18:51:06 
> sqlite> 
> sqlite> select datetime('now'); 
> 2021-08-30 22:28:58 
> sqlite> 
> 
> I can't see anything in that code listing that explicitly manipulates 
> the date/time when fetched for output. Nor do I see the connection 
> specifying Python adapter usage: 
> https://docs.python.org/3/library/sqlite3.html#default-adapters-and-converters
>  
> so I'd expect the output to be in ISO UTC format; the native result of 
> using SQLite3's datetime().. 
> 
> """ 
> if not args.hide_time: 
> cmd_time = row_dict["command_dt"] 

Thank you very much. Based on your above comments and the discussion on 
<https://stackoverflow.com/questions/4770297/convert-utc-datetime-string-to-local-datetime>,
 I fixed this problem by the following method:

# Install and import some necessary packages: 
from datetime import datetime
# pip install python-dateutil
from dateutil import tz

 
Then use the following codes to do the trick:

from_zone = tz.tzutc()
to_zone = tz.tzlocal()
cmd_time = row_dict["command_dt"]
cmd_time = datetime.strptime(cmd_time, '%Y-%m-%d 
%H:%M:%S').replace(tzinfo=from_zone).astimezone(to_zone).strftime("%Y-%m-%d 
%H:%M:%S")


Best, Hongyi

> if args.time_first: 
> print(f'{Term.YELLOW}{cmd_time}{Term.ENDC} 
> {colored_cmd}') 
> else: 
> padded_cmd = pad(raw_text=row_dict['command'], 
> print_text=colored_cmd) 
> print(f'{padded_cmd} # rtime@ 
> {Term.YELLOW}{cmd_time}{Term.ENDC}')
> """ 
> >But I found that it did not perform the correct conversion according to the 
> >time zone setting on the machine, as shown below: 
> >```shell 
> >werner@X10DAi-00:~$ rm 22 
> >rm: cannot remove '22': No such file or directory 
> >werner@X10DAi-00:~$ recent -fo -w . 2 
> >rm 22 # rtime@ 2021-08-29 10:57:13 
> >werner@X10DAi-00:~$ date 
> >Sun 29 Aug 2021 06:57:22 PM CST 
> >```
> Might have helped to mention you were in China... To me, CST is North 
> America Central Standard Time (and I'd have expected this time of year to 
> see CDT - Central Daylight Time)... That led me on a weird meaningless side 
> track... 
> 
> What documentation do you have that says it will display the date/time 
> in local timezone? (The README appears to be incorrect -- the utility logs 
> unix epoch [UTC seconds since 1970] AS ISO UTC string). 
> 
> sqlite> select datetime(1092941466, 'unixepoch'); 
> 2004-08-19 18:51:06 
> sqlite> select datetime(1092941466, 'unixepoch', 'localtime'); 
> 2004-08-19 14:51:06 
> sqlite> 
> sqlite> select datetime('now', 'localtime'); 
> 2021-08-30 18:50:19 
> sqlite> select datetime('now'); 
> 2021-08-30 22:50:32 
> sqlite> 
> 
> I'm in EDT (Eastern Daylight Time) -- so 4 hours behind UTC. 
> 
> 
> -- 
> Wulfraed Dennis Lee Bieber AF6VN 
> wlf...@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


The sqlite3 timestamp conversion between unixepoch and localtime can't be done according to the timezone setting on the machine automatically.

2021-08-30 Thread hongy...@gmail.com
On Ubuntu 20.04.2 LTS, I use the 
[recent2](https://github.com/dotslash/recent2/blob/master/recent2.py) to log 
and query my bash history, which uses a sqlite3 database to store the history 
information. The `datetime` of running a command is inserted in the sqlite3 
database with [a `unixepoch` 
format](https://github.com/dotslash/recent2/blob/f1018aee228c710cc7d1b8b93bc0228791a54563/recent2.py#L45),
 and it will be converted into `localtime` accordingly when retrieved and 
displayed later.

But I found that it did not perform the correct conversion according to the 
time zone setting on the machine, as shown below:
```shell
werner@X10DAi-00:~$ rm 22
rm: cannot remove '22': No such file or directory
werner@X10DAi-00:~$ recent -fo -w . 2
rm 22  # rtime@ 2021-08-29 10:57:13
werner@X10DAi-00:~$ date 
Sun 29 Aug 2021 06:57:22 PM CST
```
I also filed an issue 
[here](https://discuss.python.org/t/python-sqlite3-datetime-verconsion-between-unixepoch-and-localtime/10382).
 Any hints for this problem?

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


Re: Generate a Google Translate API token through the Socks5 proxy using gtoken.py

2021-07-28 Thread hongy...@gmail.com
On Wednesday, July 28, 2021 at 3:05:11 PM UTC+8, Peter Otten wrote:
> On 28/07/2021 07:32, Cameron Simpson wrote: 
> > On 27Jul2021 19:24, Hongyi Zhao  wrote:
> >> On Wednesday, July 28, 2021 at 7:25:27 AM UTC+8, cameron...@gmail.com 
> >> wrote: 
> >>> Just to follow on a bit to Dennis: 
> >> 
> >> But I can't any reply from Dennis in this issue. 
> >
> > Odd, because I replied to his reply to you :-)
> Perhaps it has something to do with the X-No-Archive flag set by Dennis?

I'm not sure. I use the Google Groups from Firefox for reading from/posting on 
this group.

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


Re: Generate a Google Translate API token through the Socks5 proxy using gtoken.py

2021-07-27 Thread hongy...@gmail.com
On Wednesday, July 28, 2021 at 7:25:27 AM UTC+8, cameron...@gmail.com wrote:
> Just to follow on a bit to Dennis: 

But I can't any reply from Dennis in this issue.
 
> C:\Users\Wulfraed\Documents\_Hg-Repositories\Python Progs>python 
> googletrans_test.py 
> Traceback (most recent call last): 
> File "googletrans_test.py", line 4, in  
> tk = acquirer.do(text) 
> File "C:\Python38\lib\site-packages\googletrans\gtoken.py", line 194, in do 
> self._update() 
> File "C:\Python38\lib\site-packages\googletrans\gtoken.py", line 62, in 
> _update
> code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
> AttributeError: 'NoneType' object has no attribute 'group'
> The implication here is that: 
> 
> self.RE_TKK.search(r.text).group(1) 
> 
> generates the error because: 
> 
> self.RE_TKK.search(r.text) 
> 
> returned None instead of a regular expression match object. That means 
> that "r.text" way not what was expected. 
> 
> Like Dennis, i note the remark "for internal use only", suggesting this 
> was some internal Google code for doing something fiddly. It may not be 
> the best thing for what you're trying to do. 
> 
> WRT to a socks proxy, Dennis showed that the issue occurs even without a 
> proxy. I would advocate doing all your debugging without trying to use 
> socks, then get to going through a socks proxy _after_ the main stuff is 
> working. 
> 
> Cheers, 
> Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


Generate a Google Translate API token through the Socks5 proxy using gtoken.py

2021-07-27 Thread hongy...@gmail.com
I want to use 
[gtoken.py](https://github.com/ssut/py-googletrans/blob/master/googletrans/gtoken.py)
 through a socks5 proxy. Based on the comment 
[here](https://github.com/encode/httpx/issues/203#issuecomment-611914974) and 
the example usage in 
[gtoken.py](https://github.com/ssut/py-googletrans/blob/d15c94f176463b2ce6199a42a1c517690366977f/googletrans/gtoken.py#L29),
 I tried with the following method, but failed:
```python
(datasci) werner@X10DAi:~$ ipython
Python 3.9.1 (default, Feb 10 2021, 15:30:33) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.23.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import socks

In [2]: import socket

In [3]: socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 18889)
   ...: socket.socket = socks.socksocket

In [4]: from googletrans.gtoken import TokenAcquirer

In [5]: acquirer=TokenAcquirer()

In [6]: text = 'test'

In [7]: tk= acquirer.do(text)
---
AttributeErrorTraceback (most recent call last)
 in 
> 1 tk= acquirer.do(text)

~/.pyenv/versions/3.9.1/envs/datasci/lib/python3.9/site-packages/googletrans/gtoken.py
 in do(self, text)
192 
193 def do(self, text):
--> 194 self._update()
195 tk = self.acquire(text)
196 return tk

~/.pyenv/versions/3.9.1/envs/datasci/lib/python3.9/site-packages/googletrans/gtoken.py
 in _update(self)
 60 
 61 # this will be the same as python code after stripping out a 
reserved word 'var'
---> 62 code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
 63 # unescape special ascii characters such like a \x3d(=)
 64 code = code.encode().decode('unicode-escape')

AttributeError: 'NoneType' object has no attribute 'group'
```
Any hints for this problem will be highly appreciated.

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