Re: Python Dialogs

2024-05-06 Thread jak via Python-list

Loris Bennett ha scritto:

r...@zedat.fu-berlin.de (Stefan Ram) writes:


   Me (indented by 2) and the chatbot (flush left). Lines lengths > 72!


Is there a name for this kind of indentation, i.e. the stuff you are
writing not being flush left?  It is sort of contrary to
what I think of as "normal" indentation.  You seem to use it in all your
postings, too, which hurts my brain, but I guess that's my problem :-)

[snip (40 lines)]



It's not just your problem. When the texts are particularly long and
complex, I happen to use Google-Translator. It makes bad translations if
the lines are interrupted by the newline, so I wrote a tool dedicated to
Stefan and to all those who indent like him. This tool takes the text
from the clipboard, removes all the superfluous spaces, combines all the
lines in one and puts the result back into the clipboard. :-D

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


Re: Python Dialogs

2024-05-06 Thread jak via Python-list

Stefan Ram ha scritto:

r...@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:

translation services are gonna interpret line breaks as


   I just beefed up my posting program to replace "gonna".

   Now I won't come across like some street thug, but rather
   as a respectable member of human society!

# replace complete words
replacements =\
{ rb'kind of': b'kind of',
   rb'trying to': b'trying to',
   rb'got to': b'got to',
   rb'gonna': b'going to',
   }
lines =\
[ re.sub( rb"\b(" + b"|".join( replacements.keys() ) + rb")\b",
 lambda x: replacements[ x.group() ], line )
   if len( line )and line[ 0 ]not in b'>|' else line for line in lines ]



I present to you the brutality to which your posts are subjected:

   for(d = s = pCLipb; *s != TEXT('\0'); s++)
   {
  if(d == pCLipb)
  {
 if(*s != TEXT(' ') && *s != TEXT('\n') && *s != TEXT('\r'))
*d++ = *s;
  }
  else if(*s == TEXT(' ') || *s == TEXT('\n') || *s == TEXT('\r'))
  {
 if(d[-1] != TEXT(' '))
*d++ = TEXT(' ');
  }
  else
 *d++ = *s;
   }
   *d = TEXT('\0');

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


Re: UTF_16 question

2024-05-01 Thread jak via Python-list

Richard Damon ha scritto:

On Apr 29, 2024, at 12:23 PM, jak via Python-list  
wrote:

Hi everyone,
one thing that I do not understand is happening to me: I have some text
files with different characteristics, among these there are that they
have an UTF_32_le coding, utf_32be, utf_16_le, utf_16_be all of them
without BOM. With those utf_32_xx I have no problem but with the
UTF_16_xx I have. If I have an utf_16_le coded file and I read it with
encoding='utf_16_le' I have no problem I read it, with
encoding='utf_16_be' I can read it without any error even if the data I
receive have the inverted bytes. The same thing happens with the
utf_16_be codified file, I read it, both with encoding='utf_16_be' and
with 'utf_16_le' without errors but in the last case the bytes are
inverted. What did I not understand? What am I doing wrong?

thanks in advance

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


That is why the BOM was created. A lot of files can be “correctly” read as 
either UTF-16-LE or UTF-1-BE encoded, as most of the 16 bit codes are valid, so 
unless the wrong encoding happens to hit something that is invalid (most likely 
something looking like a Surrogage Pair without a match), there isn’t an error 
in reading the file. The BOM character was specifically designed to be an 
invalid code if read by the wrong encoding (if you ignore the possibility of 
the file having a NUL right after the BOM)

If you know the files likely contains a lot of “ASCII” characters, then you 
might be able to detect that you got it wrong, due to seeing a lot of 0xXX00 
characters and few 0x00XX characters, but that doesn’t create an “error” 
normally.



Thanks to you too for the reply. I was actually looking for a way to
distinguish "utf16le" texts from "utf16be" ones. Unfortunately, whoever
created this log file archive thought that the BOM was not important and
so omitted it. Now they want to switch to "utf8 " and also save the
previous. Fortunately I can be sure that the text of the log files
is in some European language, so after converting the file to "utf8" I
make sure that most of the bytes are less than the value 0x7F and if not
I reconvert them by replacing "utf16 " "le" with "be" or vice versa. The
strategy seems to be working. In the future, by writing files in "utf8"
they will no longer have problems like this.

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


Re: UTF_16 question

2024-04-29 Thread jak via Python-list

Stefan Ram ha scritto:

jak  wrote or quoted:

 I read it, both with encoding='utf_16_be' and
with 'utf_16_le' without errors but in the last case the bytes are
inverted.


   I think the order of the octets (bytes) is exactly the difference
   between these two encodings, so your observation isn't really
   surprising. The computer can't report an error here since it
   can't infer the correct encoding from the file data. It's like
   that koan: "A bit has the value 1. What does that mean?".



Understood. They are just 2 bytes and there is no difference between
them.

Thank you.


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


UTF_16 question

2024-04-29 Thread jak via Python-list

Hi everyone,
one thing that I do not understand is happening to me: I have some text
files with different characteristics, among these there are that they
have an UTF_32_le coding, utf_32be, utf_16_le, utf_16_be all of them
without BOM. With those utf_32_xx I have no problem but with the
UTF_16_xx I have. If I have an utf_16_le coded file and I read it with
encoding='utf_16_le' I have no problem I read it, with
encoding='utf_16_be' I can read it without any error even if the data I
receive have the inverted bytes. The same thing happens with the
utf_16_be codified file, I read it, both with encoding='utf_16_be' and
with 'utf_16_le' without errors but in the last case the bytes are
inverted. What did I not understand? What am I doing wrong?

thanks in advance

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


Re: help: pandas and 2d table

2024-04-16 Thread jak via Python-list

Stefan Ram ha scritto:

jak  wrote or quoted:

Stefan Ram ha scritto:

df = df.where( df == 'zz' ).stack().reset_index()
result ={ 'zz': list( zip( df.iloc[ :, 0 ], df.iloc[ :, 1 ]))}

Since I don't know Pandas, I will need a month at least to understand
these 2 lines of code. Thanks again.


   Here's a technique to better understand such code:

   Transform it into a program with small statements and small
   expressions with no more than one call per statement if possible.
   (After each litte change check that the output stays the same.)

import pandas as pd

# Warning! Will overwrite the file 'file_20240412201813_tmp_DML.csv'!
with open( 'file_20240412201813_tmp_DML.csv', 'w' )as out:
 print( '''obj,foo1,foo2,foo3,foo4,foo5,foo6
foo1,aa,ab,zz,ad,ae,af
foo2,ba,bb,bc,bd,zz,bf
foo3,ca,zz,cc,cd,ce,zz
foo4,da,db,dc,dd,de,df
foo5,ea,eb,ec,zz,ee,ef
foo6,fa,fb,fc,fd,fe,ff''', file=out )
# Note the "index_col=0" below, which is important here!
df = pd.read_csv( 'file_20240412201813_tmp_DML.csv', index_col=0 )

selection = df.where( df == 'zz' )
selection_stack = selection.stack()
df = selection_stack.reset_index()
df0 = df.iloc[ :, 0 ]
df1 = df.iloc[ :, 1 ]
z = zip( df0, df1 )
l = list( z )
result ={ 'zz': l }
print( result )

   I suggest to next insert print statements to print each intermediate
   value:

# Note the "index_col=0" below, which is important here!
df = pd.read_csv( 'file_20240412201813_tmp_DML.csv', index_col=0 )
print( 'df = \n', type( df ), ':\n"', df, '"\n' )

selection = df.where( df == 'zz' )
print( "result of where( df == 'zz' ) = \n", type( selection ), ':\n"',
   selection, '"\n' )

selection_stack = selection.stack()
print( 'result of stack() = \n', type( selection_stack ), ':\n"',
   selection_stack, '"\n' )

df = selection_stack.reset_index()
print( 'result of reset_index() = \n', type( df ), ':\n"', df, '"\n' )

df0 = df.iloc[ :, 0 ]
print( 'value of .iloc[ :, 0 ]= \n', type( df0 ), ':\n"', df0, '"\n' )

df1 = df.iloc[ :, 1 ]
print( 'value of .iloc[ :, 1 ] = \n', type( df1 ), ':\n"', df1, '"\n' )

z = zip( df0, df1 )
print( 'result of zip( df0, df1 )= \n', type( z ), ':\n"', z, '"\n' )

l = list( z )
print( 'result of list( z )= \n', type( l ), ':\n"', l, '"\n' )

result ={ 'zz': l }
print( "value of { 'zz': l }= \n", type( result ), ':\n"',
   result, '"\n' )

print( result )

   Now you can see what each single step does!

df =
   :
"  foo1 foo2 foo3 foo4 foo5 foo6
obj
foo1   aa   ab   zz   ad   ae   af
foo2   ba   bb   bc   bd   zz   bf
foo3   ca   zz   cc   cd   ce   zz
foo4   da   db   dc   dd   de   df
foo5   ea   eb   ec   zz   ee   ef
foo6   fa   fb   fc   fd   fe   ff "

result of where( df == 'zz' ) =
   :
"  foo1 foo2 foo3 foo4 foo5 foo6
obj
foo1  NaN  NaN   zz  NaN  NaN  NaN
foo2  NaN  NaN  NaN  NaN   zz  NaN
foo3  NaN   zz  NaN  NaN  NaN   zz
foo4  NaN  NaN  NaN  NaN  NaN  NaN
foo5  NaN  NaN  NaN   zz  NaN  NaN
foo6  NaN  NaN  NaN  NaN  NaN  NaN "

result of stack() =
   :
" obj
foo1  foo3zz
foo2  foo5zz
foo3  foo2zz
   foo6zz
foo5  foo4zz
dtype: object "

result of reset_index() =
   :
" obj level_1   0
0  foo1foo3  zz
1  foo2foo5  zz
2  foo3foo2  zz
3  foo3foo6  zz
4  foo5foo4  zz "

value of .iloc[ :, 0 ]=
   :
" 0foo1
1foo2
2foo3
3foo3
4foo5
Name: obj, dtype: object "

value of .iloc[ :, 1 ] =
   :
" 0foo3
1foo5
2foo2
3foo6
4foo4
Name: level_1, dtype: object "

result of zip( df0, df1 )=
   :
" "

result of list( z )=
   :
" [('foo1', 'foo3'), ('foo2', 'foo5'), ('foo3', 'foo2'), ('foo3', 'foo6'), ('foo5', 
'foo4')]"

value of { 'zz': l }=
   :
" {'zz': [('foo1', 'foo3'), ('foo2', 'foo5'), ('foo3', 'foo2'), ('foo3', 'foo6'), 
('foo5', 'foo4')]}"

{'zz': [('foo1', 'foo3'), ('foo2', 'foo5'), ('foo3', 'foo2'), ('foo3', 'foo6'), 
('foo5', 'foo4')]}

   The script reads a CSV file and stores the data in a Pandas
   DataFrame object named "df". The "index_col=0" parameter tells
   Pandas to use the first column as the index for the DataFrame,
   which is kinda like column headers.

   The "where" creates a new DataFrame selection that contains
   the same data as df, but with all values replaced by NaN (Not
   a Number) except for the values that are equal to 'zz'.

   "stack" returns a Series with a multi-level index created
   by pivoting the columns. Here it gives a Series with the
   row-col-addresses of a all the non-NaN values. The general
   meaning of "stack" might be the most complex operation of
   this script. It's explained in the pandas manual (see there).

   "reset_index" then just transforms this Series back into a
   DataFrame, and ".iloc[ :, 0 ]" and ".iloc[ :, 1 

Re: help: pandas and 2d table

2024-04-16 Thread jak via Python-list

Stefan Ram ha scritto:

df = df.where( df == 'zz' ).stack().reset_index()
result ={ 'zz': list( zip( df.iloc[ :, 0 ], df.iloc[ :, 1 ]))}


Since I don't know Pandas, I will need a month at least to understand
these 2 lines of code. Thanks again.
--
https://mail.python.org/mailman/listinfo/python-list


Re: help: pandas and 2d table

2024-04-13 Thread jak via Python-list

Stefan Ram ha scritto:

jak  wrote or quoted:

Would you show me the path, please?


   I was not able to read xls here, so I used csv instead; Warning:
   the script will overwrite file "file_20240412201813_tmp_DML.csv"!

import pandas as pd

with open( 'file_20240412201813_tmp_DML.csv', 'w' )as out:
 print( '''obj,foo1,foo2,foo3,foo4,foo5,foo6
foo1,aa,ab,zz,ad,ae,af
foo2,ba,bb,bc,bd,zz,bf
foo3,ca,zz,cc,cd,ce,zz
foo4,da,db,dc,dd,de,df
foo5,ea,eb,ec,zz,ee,ef
foo6,fa,fb,fc,fd,fe,ff''', file=out )

df = pd.read_csv( 'file_20240412201813_tmp_DML.csv' )

result = {}

for rownum, row in df.iterrows():
 iterator = row.items()
 _, rowname = next( iterator )
 for colname, value in iterator:
 if value not in result: result[ value ]= []
 result[ value ].append( ( rowname, colname ))

print( result )



In reality what I wanted to achieve was this:

what = 'zz'
result = {what: []}

for rownum, row in df.iterrows():
iterator = row.items()
_, rowname = next(iterator)
for colname, value in iterator:
if value == what:
result[what] += [(rowname, colname)]
print(result)

In any case, thank you again for pointing me in the right direction. I
had lost myself looking for a pandas method that would do this in a
single shot or almost.


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


help: pandas and 2d table

2024-04-12 Thread jak via Python-list

Hi everyone.
I state that I don't know anything about 'pandas' but I intuited that
it could do what I want. I get, through the "read_excel" method, a
table similar to this:

  obj| foo1 foo2 foo3 foo4 foo5 foo6
  ---
 foo1|   aa   ab   zz   ad   ae   af
 |
 foo2|   ba   bb   bc   bd   zz   bf
 |
 foo3|   ca   zz   cc   cd   ce   zz
 |
 foo4|   da   db   dc   dd   de   df
 |
 foo5|   ea   eb   ec   zz   ee   ef
 |
 foo6|   fa   fb   fc   fd   fe   ff


And I would like to get a result similar to this:

{
'zz':[('foo1','foo3'),
  ('foo2','foo5'),
  ('foo3','foo2'),
  ('foo3','foo6'),
  ('foo5','foo4')
 ]
}

Would you show me the path, please?
Thank you in advance.

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


Re: on writing a number as 2^s * q, where q is odd

2023-12-05 Thread jak via Python-list

Alan Bawden ha scritto:

If you like this sort of stuff, check out the book "Hacker's Delight" by
Henry Warren.  See.


Thank you for your suggestion. Really interesting. Just for fun I tried
to port the function to 64 bit:

def bit_count_64(n):
   lt = n >> 62
   n &= (~(0x3f << 62)) & ((1 << 63) - 1)
   n = (n - ((n >> 1) & 0o3)
  - ((n >> 2) & 0o1))
   n = (   (n & 0o307070707070707070707)
+ ((n & 0o070707070707070707070) >> 3))
   return (n % 63) + (0, 1, 1, 2)[lt]

n=0x
bit_count_64(n)
64

n=0x3ffe
bit_count_64(n)
61

bit_count_64(1 << 63)
1

...in C it would have been simpler :^)

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


Re: on writing a number as 2^s * q, where q is odd

2023-12-04 Thread jak via Python-list

Oscar Benjamin ha scritto:

On Sun, 3 Dec 2023 at 10:25, Julieta Shem via Python-list
 wrote:


Alan Bawden  writes:


def powers_of_2_in(n):
 bc = (n ^ (n - 1)).bit_count() - 1
 return bc, n >> bc


That's pretty fancy and likely the fastest.


It might be the fastest but it depends how big you expect n to be and
how many trailing zeros you expect. If n is a very large integer then
this does three large integer operations in (n^(n-1)).bit_count().
They are relatively fast operations but all linear in bit size. By
contrast a check like n & 1 is O(1) and half the time proves that no
further steps are necessary.

The mpmath library needs this exact operation and is generally
intended for the large n case so I checked how it is implemented
there:

https://github.com/mpmath/mpmath/blob/f13ea4dc925d522062ac734bd19a0a3cc23f9c04/mpmath/libmp/libmpf.py#L160-L177

That code is:

 # Strip trailing bits
 if not man & 1:
 t = trailtable[man & 255]
 if not t:
 while not man & 255:
 man >>= 8
 exp += 8
 bc -= 8
 t = trailtable[man & 255]
 man >>= t
 exp += t
 bc -= t

The trailtable variable is a pre-initialised list of shifts needed to
remove zeros from an 8-bit integer. The bc variable here is just
bc=man.bit_length() which is redundant but this code predates the
addition of the int.bit_length() method.

In principle this could use a large number of man>>=8 shifts which
would potentially be quadratic in the bit size of man. In practice the
probability of hitting the worst case is very low so the code is
instead optimised for the expected common case of large man with few
trailing zeros.

--
Oscar



HI,
I would turn the question to you: how big do you expect the value to
manage?
In a 'big numbers' context or when you need to convert a large amount of
values at the same time, your comment is absolutely valid, it is less
valid if the values can be represented with 64 bits.
I'll try to imagine the worst case in 64 bit:

b64Max=0x
i=1
while True:
i *= 2
if not i <= b64Max:
break
else:
n=i
n
9223372036854775808

If we now use the function being discussed:

powers_of_2_in(n)
(63, 1)

we can see that the bit_count() method had to do 63 iterations to count
the bits. It probably had to find the highest bit first but what if it
had a simple algorithm inside like this:

def h_bit(val):
tbits = 0
bits = 64 // 2
b64Max = 0xF
while bits > 0:
if (b64Max << bits) & val:
val >>= bits
tbits += bits
bits //= 2
return tbits

would only add 6 more iterations for values needing 64 bits (7
interactions for 128 bits, 3 if 8 bits)

If we use the library you suggest for a single value or a non-'big
numbers' value (e.g. 2048 bit) the initialization alone would be slower
than the function in question. The table you are talking about is
initialized in the following way:

trailtable = [trailing(n) for n in range(256)]

(it calls a function 256 times)

This is why I think that what you said is true but it depends a lot on
the context and to all this I would add that the best cases are much
greater than the worst cases.

e.g.:
powers_of_2_in(n + 1)
(0, 9223372036854775809)
powers_of_2_in(n - 1)
(0, 9223372036854775807)

(probably only 1 interaction)

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


Re: on writing a number as 2^s * q, where q is odd

2023-12-03 Thread jak via Python-list

Julieta Shem ha scritto:

jak  writes:

[...]


--8<---cut here---start->8---
def powers_of_2_in(n):
if remainder(n, 2) != 0:
  return 0, n
else:
  s, r = powers_of_2_in(n // 2)
  return 1 + s, r
--8<---cut here---end--->8---


for n = 0 your function get stack overflow


That's right.  Let's say ``assert n > 0'' before we say ``if''.



...or just:

...
if n == 0 or remainder(n, 2) != 0:
...

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


Re: on writing a number as 2^s * q, where q is odd

2023-12-03 Thread jak via Python-list

Julieta Shem ha scritto:

Alan Bawden  writes:


jak  writes:

Alan Bawden ha scritto:
> Julieta Shem  writes:
>
> How would you write this procedure?
> def powers_of_2_in(n):
> ...
>
> def powers_of_2_in(n):
>  return (n ^ (n - 1)).bit_count() - 1
>

Great solution, unfortunately the return value is not a tuple as in the
OP version. Maybe in this way?

def powers_of_2_inB(n):
bc = (n ^ (n - 1)).bit_count() - 1
return bc, int(n / (1 << bc))

Good point.  I overlooked that.  I should have written:

def powers_of_2_in(n):
 bc = (n ^ (n - 1)).bit_count() - 1
 return bc, n >> bc


That's pretty fancy and likely the fastest.

I was pretty happy with a recursive version.  If I'm not mistaken,
nobody has offered a recursive version so far.  It's my favorite
actually because it seems to be the clearest one.

--8<---cut here---start->8---
def powers_of_2_in(n):
   if remainder(n, 2) != 0:
 return 0, n
   else:
 s, r = powers_of_2_in(n // 2)
 return 1 + s, r
--8<---cut here---end--->8---



for n = 0 your function get stack overflow
--
https://mail.python.org/mailman/listinfo/python-list


Re: on writing a number as 2^s * q, where q is odd

2023-11-29 Thread jak via Python-list

Dom Grigonis ha scritto:

def powers_of_2_in(n):
 s = 0
 while n % 2 == 0:
 s += 1
 n = n // 2
 return s, n



Good solution, unfortunately if the input data is zero, the function
never ends.


On 30 Nov 2023, at 02:44, Julieta Shem via Python-list  
wrote:

How would you write this procedure?

--8<---cut here---start->8---
def powers_of_2_in(n):
  s = 0
  while "I still find factors of 2 in n...":
q, r = divmod(n, 2)
if r == 0:
  s = s + 1
  n = n // 2
else:
  return s, n
--8<---cut here---end--->8---
--
https://mail.python.org/mailman/listinfo/python-list




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


Re: on writing a number as 2^s * q, where q is odd

2023-11-29 Thread jak via Python-list

Alan Bawden ha scritto:

Julieta Shem  writes:

How would you write this procedure?
def powers_of_2_in(n):
...

def powers_of_2_in(n):
 return (n ^ (n - 1)).bit_count() - 1



Great solution, unfortunately the return value is not a tuple as in the
OP version. Maybe in this way?

def powers_of_2_inB(n):
bc = (n ^ (n - 1)).bit_count() - 1
return bc, int(n / (1 << bc))

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


Re: Code improvement question

2023-11-17 Thread jak via Python-list

MRAB ha scritto:

Bare excepts are a very bad idea.


I know, you're right but to test the CAS numbers were inside a string
(txt) and instead of the 'open(file)' there was 'io.StingIO(txt)' so the
risk was almost null. When I copied it here I didn't think about it.
Sorry.


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


Re: Code improvement question

2023-11-17 Thread jak via Python-list

Mike Dewhirst ha scritto:

On 15/11/2023 10:25 am, MRAB via Python-list wrote:

On 2023-11-14 23:14, Mike Dewhirst via Python-list wrote:

I'd like to improve the code below, which works. It feels clunky to me.

I need to clean up user-uploaded files the size of which I don't know in
advance.

After cleaning they might be as big as 1Mb but that would be super rare.
Perhaps only for testing.

I'm extracting CAS numbers and here is the pattern xx-xx-x up to
xxx-xx-x eg., 1012300-77-4

def remove_alpha(txt):

      """  r'[^0-9\- ]':

      [^...]: Match any character that is not in the specified set.

      0-9: Match any digit.

      \: Escape character.

      -: Match a hyphen.

      Space: Match a space.

      """

  cleaned_txt = re.sub(r'[^0-9\- ]', '', txt)

      bits = cleaned_txt.split()

      pieces = []

      for bit in bits:

      # minimum size of a CAS number is 7 so drop smaller clumps 
of digits


      pieces.append(bit if len(bit) > 6 else "")

      return " ".join(pieces)


Many thanks for any hints


Why don't you use re.findall?

re.findall(r'\b[0-9]{2,7}-[0-9]{2}-[0-9]{2}\b', txt)


I think I can see what you did there but it won't make sense to me - or 
whoever looks at the code - in future.


That answers your specific question. However, I am in awe of people who 
can just "do" regular expressions and I thank you very much for what 
would have been a monumental effort had I tried it.


That little re.sub() came from ChatGPT and I can understand it without 
too much effort because it came documented


I suppose ChatGPT is the answer to this thread. Or everything. Or will be.

Thanks

Mike


I respect your opinion but from the point of view of many usenet users
asking a question to chatgpt to solve your problem is truly an overkill.
The computer world overflows with people who know regex. If you had not
already had the answer with the use of 're' I would have sent you my
suggestion that as you can see it is practically identical. I am quite
sure that in this usenet the same solution came to the mind of many
people.

with open(file) as fp:
try: ret = re.findall(r'\b\d{2,7}\-\d{2}\-\d{1}\b', fp.read())
except: ret = []

The only difference is '\d' instead of '[0-9]' but they are equivalent.

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


Re: Beep on WIndows 11

2023-11-12 Thread jak via Python-list

Rob Cliffe ha scritto:

  Apologies if this is not a Python question.
I  recently moved from a WIndows 10 laptop to a Windows 11 one.
Although there is nothing wrong with the sound on the new machine (I can 
listen to podcasts and watch videos), I find that outputting "\a" to the 
console (aka stdout) no longer beeps (or makes any sound).  This is true 
whether I print "\a" from a python program, or "type 
".

I have found via Google workarounds such as
     os.system("rundll32 user32.dll,MessageBeep")
but it is a trifle annoying to have to modify all of my programs that beep.
Can anyone shed light on this, and perhaps give a simpler fix?
Best wishes
Rob Cliffe



HI,
I would first check the properties of the terminal, then the system
configuration relating to the system beep. It can be disabled.
You can find some tips here:


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


Re: Do subprocess.PIPE and subprocess.STDOUT sametime

2023-05-09 Thread jak

Horst Koiner ha scritto:

Hi @all,
i'm running a program which is still in development with subprocess.run (Python 
version 3.10), further i need to capture the output of the program in a python 
variable. The program itself runs about 2 minutes, but it can also freeze in 
case of new bugs.

For production i run the program with stdout=subprocess.PIPE and i can fetch 
than the output later. For just testing if the program works, i run with 
stdout=subprocess.STDOUT and I see all program output on the console, but my 
program afterwards crashes since there is nothing captured in the python 
variable. So I think I need to have the functionality of subprocess.PIPE and 
subprcess.STDOUT sametime.

What I tried until now:
1. Poll the the output and use Popen instead:

# Start the subprocess
process = subprocess.Popen(['./test.sh'], stdout=subprocess.PIPE, 
stderr=subprocess.PIPE)

captured_output = b''
process_running = True
while process_running:
 process_running = (process.poll() is not None)
 for pipe in [ process.stdout, process.stderr ]:
 while line := pipe.readline():
 print(line)
 captured_output += line

print(captured_output)
return_code = process.returncode

=> But this is discouraged by the python doc, since it says that polling this 
way is prone to deadlocks. Instead it proposes the use of the communicate() 
function.

2. Use communicate() with timeout.
=> This works not at all since when the timeout occurs an exception is thrown 
and communicate returns at all.

3. Use threading instead
=> For being that simple and universal like subprocess you will more or less 
reimplement subprocess with threading, like its done in subprocess.py. Just for a 
debug output the effort is much to high.

###
Do you have further ideas for implementing such a behavior?
Do you think that a feature request should be done of I'm omitting something 
obvious?

Thanks you in advance for your suggestions,
Horst.



I agree with @'thomas Passin' but I solved in a different way, I made 
the Readline() not blocking. even if I believe his idea better than

mine:

os.set_blocking(process.stdout.fileno(), False)
os.set_blocking(process.stderr.fileno(), False)

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


Re: What do these '=?utf-8?' sequences mean in python?

2023-05-08 Thread jak

Peter Pearson ha scritto:

On Sat, 6 May 2023 14:50:40 +0100, Chris Green  wrote:
[snip]

So, what do those =?utf-8? and ?= sequences mean?  Are they part of
the string or are they wrapped around the string on output as a way to
show that it's utf-8 encoded?


Yes, "=?utf-8?" signals "MIME header encoding".

I've only blundered about briefly in this area, but I think you
need to make sure that all header values you work with have been
converted to UTF-8 before proceeding.
Here's the code that seemed to work for me:

def mime_decode_single(pair):
 """Decode a single (bytestring, charset) pair.
 """
 b, charset = pair
 result = b if isinstance(b, str) else b.decode(
 charset if charset else "utf-8")
 return result

def mime_decode(s):
 """Decode a MIME-header-encoded character string.
 """
 decoded_pairs = email.header.decode_header(s)
 return "".join(mime_decode_single(d) for d in decoded_pairs)





HI,
You could also use make_header:

from email.header import decode_header, make_header

print(make_header(decode_header( subject )))

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


Re: Problem with accented characters in mailbox.Maildir()

2023-05-08 Thread jak

Chris Green ha scritto:

Chris Green  wrote:

A bit more information, msg.get("subject", "unknown") does return a
string, as follows:-

 Subject: 
=?utf-8?Q?aka_Marne_=C3=A0_la_Sa=C3=B4ne_(Waterways_Continental_Europe)?=

So it's the 'searchTxt in msg.get("subject", "unknown")' that's
failing. I.e. for some reason 'in' isn't working when the searched
string has utf-8 characters.

Surely there's a way to handle this.


... and of course I now see the issue!  The Subject: with utf-8
characters in it gets spaces changed to underscores.  So searching for
'(Waterways Continental Europe)' fails.

I'll either need to test for both versions of the string or I'll need
to change underscores to spaces in the Subject: returned by msg.get().
It's a long enough string that I'm searching for that I won't get any
false positives.


Sorry for the noise everyone, it's a typical case of explaining the
problem shows one how to fix it! :-)



This is probably what you need:

import email.header

raw_subj = 
'=?utf-8?Q?aka_Marne_=C3=A0_la_Sa=C3=B4ne_(Waterways_Continental_Europe)?='


subj = email.header.decode_header(raw_subj)[0]

subj[0].decode(subj[1])

'aka Marne à la Saône (Waterways Continental Europe)'




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


Re: What do these '=?utf-8?' sequences mean in python?

2023-05-08 Thread jak

Chris Green ha scritto:

Keith Thompson  wrote:

Chris Green  writes:

Chris Green  wrote:

I'm having a real hard time trying to do anything to a string (?)
returned by mailbox.MaildirMessage.get().


What a twit I am :-)

Strings are immutable, I have to do:-

 newstring = oldstring.replace("_", " ")

Job done!


Not necessarily.

The subject in the original article was:
=?utf-8?Q?aka_Marne_=C3=A0_la_Sa=C3=B4ne_(Waterways_Continental_Europe)?=

That's some kind of MIME encoding.  Just replacing underscores by spaces
won't necessarily give you anything meaningful.  (What if there are
actual underscores in the original subject line?)

You should probably apply some kind of MIME-specific decoding.  (I don't
have a specific suggestion for how to do that.)


Yes, OK, but my problem was that my filter looks for the string
"Waterways Continental Europe" in the message Subject: to route the
message to the appropriate mailbox.  When the Subject: has accents the
string becomes "Waterways_Continental_Europe" and thus the match
fails.  Simply changing all underscores back to spaces makes my test
for "Waterways Continental Europe" work.  The changed Subject: line
gets thrown away after the test so I don't care about anything else
getting changed.

(When there are no accented characters in the Subject: the string is
"Waterways Continental Europe" so I can't easily change the search
text. I guess I could use an RE.)



In reality you should also take into account the fact that if the header
contains a 'b' instead of a 'q' as a penultimate character, then the
rest of the package is converted on the basis64

"=?utf-8?Q?"  --> "=?utf-8?B?"


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


Re: Problem with accented characters in mailbox.Maildir()

2023-05-08 Thread jak

Chris Green ha scritto:

I have a custom mail filter in python that uses the mailbox package to
open a mail message and give me access to the headers.

So I have the following code to open each mail message:-

 #
 #
 # Read the message from standard input and make a message object from it
 #
 msg = mailbox.MaildirMessage(sys.stdin.buffer.read())

and then later I have (among many other bits and pieces):-

 #
 #
 # test for string in Subject:
 #
 if searchTxt in str(msg.get("subject", "unknown")):
 do
 various
 things


This works exactly as intended most of the time but occasionally a
message whose subject should match the test is missed.  I have just
realised when this happens, it's when the Subject: has accented
characters in it (this is from a mailing list about canals in France).

So, for example, the latest case of this happening has:-

 Subject: aka Marne à la Saône (Waterways Continental Europe)

where the searchTxt in the code above is "Waterways Continental Europe".


Is there any way I can work round this issue?  E.g. is there a way to
strip out all extended characters from a string?  Or maybe it's
msg.get() that isn't managing to handle the accented string correctly?

Yes, I know that accented characters probably aren't allowed in
Subject: but I'm not going to get that changed! :-)




Hi,
you could try extracting the "Content-Type:charset" and then using it
for subject conversion:

subj = str(raw_subj, encoding='...')

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


Re: How to 'ignore' an error in Python?

2023-04-29 Thread jak

Stefan Ram ha scritto:

jak  writes:

Maybe I only say this because it has happened to me too many times but
before ignoring the error in the 'except' branch, I would make sure that
if the name exists it is a folder and not a file.


   If the name exists and it is a file's name, this will be detected
   by "mkdir( exist_ok=True )", and an object will be raised. Such
   an object, indeed, usually should /not/ be ignored by the caller.
   But there is no reason to deviate from the EAFP path.





Maybe I expressed myself badly but I didn't mean to propose alternatives
to the EAFP way but just to evaluate the possibility that it is not a
folder.
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to 'ignore' an error in Python?

2023-04-29 Thread jak

Chris Angelico ha scritto:

Using mkdirs when you only want to make one is inviting problems of
being subtly wrong, where it creates too many levels of directory.
Personally, I would just do:



Maybe I only say this because it has happened to me too many times but
before ignoring the error in the 'except' branch, I would make sure that
if the name exists it is a folder and not a file.

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


Re: Incomplete sys.path with embeddable python (Windows)!?

2023-04-24 Thread jak

Ralf M. ha scritto:

Am 22.04.2023 um 03:27 schrieb Greg Ewing via Python-list:

How are you invoking your script? Presumably you have some code
in your embedding application that takes a script path and runs
it. Instead of putting the code to update sys.path into every
script, the embedding application could do it before running
the script.


In principle a good idea, but I don't know how to do that:
The script is currently invoked by a .cmd file, but that may change to a 
shortcut (.lnk). This is what the embeddable package documentation calls 
"Python Application - simple approach".

To update sys.path on start up I would need to do something like
   C:\path\to\python.exe --add-path C:\s-path C:\s-path\script.py
but I couldn't find an option like --add-path.



what about:

foo.py:
from sys import path
print('path:', path)
#end foo.py

from prompt:

$ python -c "import sys;sys.path=['/user/foopath']+sys.path;import foo;foo"

output:

path: ['/user/foopath', '', '/usr/lib/python39.zip', 
'/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', 
'/usr/lib/python3.9/site-packages']

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


Re: for a 'good python'

2023-04-13 Thread jak

Dennis Lee Bieber ha scritto:

On Thu, 13 Apr 2023 00:21:58 +0200, jak  declaimed the
following:



Thank you too. I had seen this library but I always try not to use
libraries outside the standard ones. Now I don't remember why I was
convinced that this wasn't part of it, perhaps because it was like that
at the time or because I got confused. Only now I realized that it is
not necessary to install it. Now I'm considering whether to use
'ipaddress' or 'socket'. What is certain is that this one you have
suggested is really comfortable. Thanks again for the report.


It is useful to skim the contents of the standard library documentation
every couple of releases. ipaddress came in with Python 3.3

https://docs.python.org/3.10/library/index.html (I dropped down to 3.10
just as that is the version I have installed; some 3rd party modules
weren't ready when I tried to install on 3.11)



Then it was my fault. The version was 3.8.6
--
https://mail.python.org/mailman/listinfo/python-list


Re: for a 'good python'

2023-04-12 Thread jak

Barry ha scritto:





On 12 Apr 2023, at 18:10, jak  wrote:
Hi everyone,
some time ago I wrote a class to determine if an ipv4 address belonged
to a subnet. Seldom using python I'm pretty sure it's not written in
'good python' nor too portable. Could you give me some advice to make it
better?

class calcip:
def __init__(self, psubnet: str):
ssubnet, scidr = psubnet.replace(' ', '').split('/')
subnet = int.from_bytes(tuple(
  map(lambda n: (int(n)), ssubnet.split('.'))),
'big')
cidr = int(scidr)
mask = ((2 ** cidr) - 1) << (32 - cidr)
self.__network = subnet & mask
self.__wildcard = ~mask & 0x
self.__broadcast = (subnet | self.__wildcard) & 0x
self.__tsubnet = tuple(subnet.to_bytes(4, 'big'))
self.__tnetwork = tuple(self.__network.to_bytes(4, 'big'))
self.__tbroadcast = tuple(self.__broadcast.to_bytes(4, 'big'))
self.__tmask = tuple(mask.to_bytes(4, 'big'))
self.__twildcard = tuple(self.__wildcard.to_bytes(4, 'big'))
self.__host_min = tuple((self.__network + 1).to_bytes(4, 'big'))
self.__host_max = tuple((self.__broadcast - 1).to_bytes(4, 'big'))

@staticmethod
def __to_str(val: tuple):
return '.'.join(str(v) for v in val)

@property
def subnet(self):
return self.__to_str(self.__tsubnet)

@property
def network(self):
return self.__to_str(self.__tnetwork)

@property
def broadcast(self):
return self.__to_str(self.__tbroadcast)

@property
def mask(self):
return self.__to_str(self.__tmask)

@property
def wildcard(self):
return self.__to_str(self.__twildcard)

@property
def host_min(self):
return self.__to_str(self.__host_min)

@property
def host_max(self):
return self.__to_str(self.__host_max)

@property
def hosts_num(self):
return self.__wildcard - 1

@property
def net_class(self):
tst = (self.__tnetwork[0] & 0xf0) >> 4
if (tst & 0x8) == 0:
clx = 'A'
elif (tst & 0xc) == 0x8:
clx = 'B'
elif (tst & 0xe) == 0xc:
clx = 'C'
elif (tst & 0xf) == 0xe:
clx = 'D'
elif (tst & 0xf) == 0xf:
clx = 'E'
return clx

def __contains__(self, item):
ret = True
row_hdr = None
try:
row_hdr = int.from_bytes(tuple(map(lambda n: (int(n)), 
item.split('.'))), 'big')
except:
ret = False
if ret:
if not self.__network < row_hdr < self.__broadcast:
ret = False
return ret


def main():
sn = calcip('10.0.0.0/26')

print(f"subnet: {sn.subnet}")
print(f"network: {sn.network}")
print(f"broadcast: {sn.broadcast}")
print(f"mask: {sn.mask}")
print(f"wildcard: {sn.wildcard}")
print(f"host_min: {sn.host_min}")
print(f"host_max: {sn.host_max}")
print(f"Avaible hosts: {sn.hosts_num}")
print(f"Class: {sn.net_class}")

tst_hdr = '10.0.0.31'
is_not = 'is '
if not tst_hdr in sn:
is_not = 'is NOT '
print("hdr %s %sin range %s - %s" %
  (tst_hdr, is_not, sn.host_min, sn.host_max))

if __name__ == '__main__':
main()


There is this https://docs.python.org/3/howto/ipaddress.html if you just want a 
solution.

Or are you after code review feedback?

Barry


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



Thank you too. I had seen this library but I always try not to use
libraries outside the standard ones. Now I don't remember why I was
convinced that this wasn't part of it, perhaps because it was like that
at the time or because I got confused. Only now I realized that it is
not necessary to install it. Now I'm considering whether to use
'ipaddress' or 'socket'. What is certain is that this one you have
suggested is really comfortable. Thanks again for the report.

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


Re: for a 'good python'

2023-04-12 Thread jak

Stefan Ram ha scritto:

jak  writes:

@property
def subnet(self):
return self.__to_str(self.__tsubnet)


   Maybe each of those attributes should be an object of a
   special class where your "__to_str" is "__str__"? E.g.,

# code in "calcip.__init__"
self.tsubnet = ip_address_class.from_int( subnet )

   where "ip_address_class" is as in:

import collections
import random

class ip_address_class( collections.UserList ):
 def __init__( self, bytes_address ):
 super().__init__( bytes_address )
 @classmethod
 def from_int( cls, int_address ):
 return cls( int_address.to_bytes( 4, 'big' ))
 def __str__( self ):
 return '.'.join( str( byte_ )for byte_ in self.data )

if __name__ == '__main__':
 ip_address = \
 ip_address_class.from_int( random.randint( 0, 4294967295 ))
 print( ip_address[ 0 ])
 print( ip_address )

   . Now the client can access each byte individually and also
   get a "nice" string representation.

   (You may add more "from_..." methods for all the various
   formats you create addresses from.)

   But you should also research the standard library to see
   if something like this doesn't already exist ready-made
   specifically for IP addresses in the standard library.



ok. thanks a lot. now i try to do that.


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


for a 'good python'

2023-04-12 Thread jak

Hi everyone,
some time ago I wrote a class to determine if an ipv4 address belonged
to a subnet. Seldom using python I'm pretty sure it's not written in
'good python' nor too portable. Could you give me some advice to make it
better?

class calcip:
def __init__(self, psubnet: str):
ssubnet, scidr = psubnet.replace(' ', '').split('/')
subnet = int.from_bytes(tuple(
  map(lambda n: (int(n)), 
ssubnet.split('.'))),

'big')
cidr = int(scidr)
mask = ((2 ** cidr) - 1) << (32 - cidr)
self.__network = subnet & mask
self.__wildcard = ~mask & 0x
self.__broadcast = (subnet | self.__wildcard) & 0x
self.__tsubnet = tuple(subnet.to_bytes(4, 'big'))
self.__tnetwork = tuple(self.__network.to_bytes(4, 'big'))
self.__tbroadcast = tuple(self.__broadcast.to_bytes(4, 'big'))
self.__tmask = tuple(mask.to_bytes(4, 'big'))
self.__twildcard = tuple(self.__wildcard.to_bytes(4, 'big'))
self.__host_min = tuple((self.__network + 1).to_bytes(4, 'big'))
self.__host_max = tuple((self.__broadcast - 1).to_bytes(4, 'big'))

@staticmethod
def __to_str(val: tuple):
return '.'.join(str(v) for v in val)

@property
def subnet(self):
return self.__to_str(self.__tsubnet)

@property
def network(self):
return self.__to_str(self.__tnetwork)

@property
def broadcast(self):
return self.__to_str(self.__tbroadcast)

@property
def mask(self):
return self.__to_str(self.__tmask)

@property
def wildcard(self):
return self.__to_str(self.__twildcard)

@property
def host_min(self):
return self.__to_str(self.__host_min)

@property
def host_max(self):
return self.__to_str(self.__host_max)

@property
def hosts_num(self):
return self.__wildcard - 1

@property
def net_class(self):
tst = (self.__tnetwork[0] & 0xf0) >> 4
if (tst & 0x8) == 0:
clx = 'A'
elif (tst & 0xc) == 0x8:
clx = 'B'
elif (tst & 0xe) == 0xc:
clx = 'C'
elif (tst & 0xf) == 0xe:
clx = 'D'
elif (tst & 0xf) == 0xf:
clx = 'E'
return clx

def __contains__(self, item):
ret = True
row_hdr = None
try:
row_hdr = int.from_bytes(tuple(map(lambda n: (int(n)), 
item.split('.'))), 'big')

except:
ret = False
if ret:
if not self.__network < row_hdr < self.__broadcast:
ret = False
return ret


def main():
sn = calcip('10.0.0.0/26')

print(f"subnet: {sn.subnet}")
print(f"network: {sn.network}")
print(f"broadcast: {sn.broadcast}")
print(f"mask: {sn.mask}")
print(f"wildcard: {sn.wildcard}")
print(f"host_min: {sn.host_min}")
print(f"host_max: {sn.host_max}")
print(f"Avaible hosts: {sn.hosts_num}")
print(f"Class: {sn.net_class}")

tst_hdr = '10.0.0.31'
is_not = 'is '
if not tst_hdr in sn:
is_not = 'is NOT '
print("hdr %s %sin range %s - %s" %
  (tst_hdr, is_not, sn.host_min, sn.host_max))

if __name__ == '__main__':
main()
--
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for package/library to extract MP4 metadata

2023-04-10 Thread jak

rbowman ha scritto:

On Sun, 9 Apr 2023 09:40:51 +0100, Chris Green wrote:


I'm looking for a Python (3) library to access (read only at present)
the metadata in MP4 video files, in particular I want to get at dates
and times.

What's available to do this?  Ideally something available in the Ubuntu
repositories but I can install with PIP if necessary.


https://mutagen.readthedocs.io/en/latest/



I thought it only dealt about audio.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for package/library to extract MP4 metadata

2023-04-10 Thread jak

Chris Green ha scritto:

jak  wrote:

rbowman ha scritto:

On Sun, 9 Apr 2023 09:40:51 +0100, Chris Green wrote:


I'm looking for a Python (3) library to access (read only at present)
the metadata in MP4 video files, in particular I want to get at dates
and times.

What's available to do this?  Ideally something available in the Ubuntu
repositories but I can install with PIP if necessary.


https://mutagen.readthedocs.io/en/latest/



I thought it only dealt about audio.


That's why I hadn't thought it would help me as I'm after getting
metadata from an MP4 video file but I guess the metadata format may be
the same regardless of whether it's video or audio.



Easiest way I found was run ffprobe command via popen. It can output the
information you need in json format which is easily readable with the
json library.

command:
ffprobe -v warning -i "input.mp4" -show_streams -of json
python:
Popen(command, stderr=STDOUT, stdout=PIPE, encoding='utf8')
json:
json.loads(''.join(p.stdout.readlines()))

It's easy to find a version of ffmpeg/ffprobe for every platform.
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to manage python shebang on mixed systems?

2022-11-08 Thread jak

Il 07/11/2022 20:41, Chris Green ha scritto:

Schachner, Joseph (US)  wrote:

Maybe you can't do this, but I would suggest only running on the Python
3 systems. Refuse to jump through hoops for the Python 2 system. It is
years out of date.


Exactly! That's why I (OP) have only one system where this is a
problem. The system is my hosting provider's cPanel platform which is
running a very old Linux and, as I've said has only python 2.

I can ask for python 3 on their system but I suspect that my voice is
a very tiny one and there are higher priority things to get done.



Perhaps this is because you speak to them about the problems of
compatibility, try to speak to them about power, speed and performance,
instead. ;-D

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


Re: How to manage python shebang on mixed systems?

2022-11-06 Thread jak

Il 06/11/2022 11:03, Chris Green ha scritto:

I have a number of python scripts that I run on a mix of systems.  I
have updated them all to run on python 3 but many will also run quite
happily with python 2.  They all have a #!/usr/bin/python3 shebang.

This works almost everywhere but there is one system where only
python 2 is available (at /usr/bin/python).

I don't have python 2 on any of the systems I manage myself now so a
#!/usr/bin/python shebang will fail.

Is there a neat way of handling this?  I could write a sort of wrapper
script to run via the shebang but that seems overkill to me.



hi,
If you can call Python from the shell prompt, then you could remove the
path from shebang:

#!python

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


Re: Find the path of a shell command

2022-10-12 Thread jak

Il 12/10/2022 20:19, MRAB ha scritto:

On 2022-10-12 06:11, jak wrote:

Il 12/10/2022 06:00, Paulo da Silva ha scritto:

Hi!

The simple question: How do I find the full path of a shell command 
(linux), i.e. how do I obtain the corresponding of, for example,

"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty 
well until it is invoked by cron! I suspect that for cron we need to 
specify the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
What about other commands?


Thanks for any comments/responses.
Paulo



I'm afraid you will have to look for the command in every path listed in
the PATH environment variable.


Isn't that what the "whereis" command does?


"whereis" does more than the OP wants. A suitable command could be
"which" but if the OP is in a context where he needs to know the path
of the command, then it will have the same problem with "whereis" or
"which". In any case they would be a few lines of Python:

import os

def which(c):
for p in os.getenv('PATH').split(os.path.pathsep):
if p and os.path.isfile(f := os.path.join(p, c)):
return f
return ''

cmd = 'explorer.exe'
cmdp = which(cmd)

if cmdp:
print("found:", cmdp)
else:
print("not found"

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


Re: Find the path of a shell command

2022-10-12 Thread jak

Il 12/10/2022 14:20, Chris Green ha scritto:

jak  wrote:

Il 12/10/2022 09:40, jkn ha scritto:

On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote:

Il 12/10/2022 06:00, Paulo da Silva ha scritto:

Hi!

The simple question: How do I find the full path of a shell command
(linux), i.e. how do I obtain the corresponding of, for example,
"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well
until it is invoked by cron! I suspect that for cron we need to specify
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
What about other commands?

Thanks for any comments/responses.
Paulo


I'm afraid you will have to look for the command in every path listed in
the PATH environment variable.


erm, or try 'which rm' ?


You might but if you don't know where the 'rm' command is, you will have
the same difficulty in using 'which' command. Do not you think?


 From a command prompt use the bash built-in 'command' :-

 command -v rm

... and rm will just about always be in /usr/bin.



ok but I didn't suggest a very complicated thing:

for p in os.getenv('PATH').split(os.path.pathsep):
if p:
if os.path.isfile(os.path.join(p, 'rm')):
print(f)

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


Re: Find the path of a shell command

2022-10-12 Thread jak

Il 12/10/2022 09:40, jkn ha scritto:

On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote:

Il 12/10/2022 06:00, Paulo da Silva ha scritto:

Hi!

The simple question: How do I find the full path of a shell command
(linux), i.e. how do I obtain the corresponding of, for example,
"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well
until it is invoked by cron! I suspect that for cron we need to specify
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin?
What about other commands?

Thanks for any comments/responses.
Paulo


I'm afraid you will have to look for the command in every path listed in
the PATH environment variable.


erm, or try 'which rm' ?


You might but if you don't know where the 'rm' command is, you will have
the same difficulty in using 'which' Command. Do not you think?

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


Re: Find the path of a shell command

2022-10-12 Thread jak

Il 12/10/2022 06:00, Paulo da Silva ha scritto:

Hi!

The simple question: How do I find the full path of a shell command 
(linux), i.e. how do I obtain the corresponding of, for example,

"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty well 
until it is invoked by cron! I suspect that for cron we need to specify 
the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
What about other commands?


Thanks for any comments/responses.
Paulo



I'm afraid you will have to look for the command in every path listed in
the PATH environment variable.

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


Re: open text file

2022-06-24 Thread jak

Il 24/06/2022 15:10, simone zambonardi ha scritto:

Good morning, I need to read a text file. How come when I open it (running the 
script) it says this? The text file is type RFT

{\rtf1\ansi\ansicpg1252\cocoartf2636
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0

\f0\fs24 \cf0

But even with in .txt it gives me this long string. I think it is a formatting 
problem .
thank you



I don't see the script you are talking about but it is normal to find
those strings inside a file of type '.rtf'. RTF documents are not simple
text documents (https://en.wikipedia.org/wiki/Rich_Text_Format) so if
you want to extract the text contained in these documents you will need
to use a library that can do this
(eg: https://pypi.org/project/striprtf/)
...or write a parser yourself.
--
https://mail.python.org/mailman/listinfo/python-list


Re: open text file

2022-06-24 Thread jak

Il 24/06/2022 15:44, jak ha scritto:

Il 24/06/2022 15:10, simone zambonardi ha scritto:
Good morning, I need to read a text file. How come when I open it 
(running the script) it says this? The text file is type RFT


{\rtf1\ansi\ansicpg1252\cocoartf2636
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 
Helvetica;}

{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0 

\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0 



\f0\fs24 \cf0

But even with in .txt it gives me this long string. I think it is a 
formatting problem .

thank you



I don't see the script you are talking about but it is normal to find
those strings inside a file of type '.rtf'. RTF documents are not simple
text documents (https://en.wikipedia.org/wiki/Rich_Text_Format) so if
you want to extract the text contained in these documents you will need
to use a library that can do this
(eg: https://pypi.org/project/striprtf/)
...or write a parser yourself.

P.S.
renaming the file extension does not change its content but it is useful
for the system to select the app that knows how to manage it

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


Re: "CPython"

2022-06-21 Thread jak

Il 21/06/2022 04:56, Paulo da Silva ha scritto:

Às 03:20 de 21/06/22, MRAB escreveu:

On 2022-06-21 02:33, Chris Angelico wrote:

On Tue, 21 Jun 2022 at 11:13, Paulo da Silva
 wrote:


Às 20:01 de 20/06/22, Paulo da Silva escreveu:
> Às 18:19 de 20/06/22, Stefan Ram escreveu:


[snip]


After all who cares in which language it is implemented?

Regards.
Paulo



Why are you asking this? The Facebook platform which is mainly developed
in Rust are converting it to C to make it faster and lighter. If as
often happens, many people complain about the speed of python, what
would be the purpose of translating python using a slower language?

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


Re: fill out bulletins

2022-06-14 Thread jak

Il 14/06/2022 15:28, Peter Pearson ha scritto:

On Tue, 14 Jun 2022 00:41:07 +0200, jak  wrote:
[snip]


If you are interested in seeing what I called "post office bulletin"
(English is not my language and I don't know the name, sorry), you can
find a sample pdf (fillable) but it works badly here:

https://www.guardiacostiera.gov.it/venezia/Documents/Bollettino%20MOD.%20TD123.pdf



Are these "post office bulletins" always PDFs?




Oh no, sadly no. these forms can be found almost exclusively in post
offices in paper form and you would need to fill them in by hand (only
with black or blue ballpoint pen). I struggled to find a pdf version of
them on the web that wasn't pre-compiled. I also tried to get a graphic
file by scanning (or photo) but I couldn't get a decent image, probably
because I don't have precise enough tools. Ask them for a copy of the
document from which they print them, I don't try because I think it's
wasted breath.
--
https://mail.python.org/mailman/listinfo/python-list


Re: fill out bulletins

2022-06-13 Thread jak

Il 13/06/2022 23:58, Mats Wichmann ha scritto:

On 6/13/22 11:11, Michael F. Stemper wrote:

On 13/06/2022 08.49, jak wrote:

Hello everyone,
I would like to write a tool to compile post office bulletins because
here, unfortunately, they are still the most convenient way to interface
the public administration. I don't want to create a UI to edit the
bulletin, I will take the data from some file or database but I'd like
to print or save the bulletin to the file with all the fields filled in.
To accomplish this, I thought about creating a basic graphic (jpg?) file
with the bulletin image,


Please don't create an image. Create something that preserves
text as text, so that your recipients can (if they so desire)
search on that text, or copy/paste from your bulletin.


Absolutely agree. We're having a flood of people post code on Twitter
and LinkedIn, which don't support code markup/markdown, so they post
images.  There are several tools that make really pretty pictures... and
they're completely useless as you can't pick the code out of them. Hate
this.


Somebody suggested TeX/LaTeX. Excellent idea.



Don't know what a "post office bulletin" is, but this sounds like a
template problem ("all the fields filled in"). There are a lot of
templating engines in the Python world from string handling in the
standard library
(https://docs.python.org/3/library/string.html#template-strings) to
tools like JInja and Cheetah and a host of others.  I think there are
many of these listed on the wiki - maybe something here would suit your
needs?

https://wiki.python.org/moin/Templating



If you are interested in seeing what I called "post office bulletin"
(English is not my language and I don't know the name, sorry), you can
find a sample pdf (fillable) but it works badly here:

https://www.guardiacostiera.gov.it/venezia/Documents/Bollettino%20MOD.%20TD123.pdf
--
https://mail.python.org/mailman/listinfo/python-list


Re: fill out bulletins

2022-06-13 Thread jak

Il 13/06/2022 19:11, Michael F. Stemper ha scritto:

On 13/06/2022 08.49, jak wrote:

Hello everyone,
I would like to write a tool to compile post office bulletins because
here, unfortunately, they are still the most convenient way to interface
the public administration. I don't want to create a UI to edit the
bulletin, I will take the data from some file or database but I'd like
to print or save the bulletin to the file with all the fields filled in.
To accomplish this, I thought about creating a basic graphic (jpg?) file
with the bulletin image,


Please don't create an image. Create something that preserves
text as text, so that your recipients can (if they so desire)
search on that text, or copy/paste from your bulletin.

Somebody suggested TeX/LaTeX. Excellent idea.



Part of the answer I gave to Stefan, moreover my goal is to get a piece
of paper to give to the post office and not a document to share (it
could contain personal data).

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


Re: fill out bulletins

2022-06-13 Thread jak

Il 13/06/2022 16:12, Stefan Ram ha scritto:

jak  writes:

Can you direct me to a better way?


   You could use a word processors with a mail-merge feature or
   generate TeX/troff code for the bulletins.
>Of course, for Python, the library PIL/Pillow is available,
   which can read or write JPG files and has a "text" method
   to write text to images.

   I you want to print the bulletins, you could have a first
   pass where the images are printed and then reinsert the same
   sheets again to add the text in a second pass.




I made a few attempts with libreoffice, unfortunately the bulletin is
too complicated to get a good result. I also took a look at Tex/LaTeX as
you advised me. Of these I just knew that they existed and their use but
I didn't want to learn a new script just for a tool and in any case I
think I would encounter the same problems I had with the word processor.
I think I will use the PIL/Pillow librariesalso because I wanted to
dynamically calculate the size of the characters so as not to go beyond
the limits of the fields to be filled in in the image. Thanks for the
tips.

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


fill out bulletins

2022-06-13 Thread jak

Hello everyone,
I would like to write a tool to compile post office bulletins because
here, unfortunately, they are still the most convenient way to interface
the public administration. I don't want to create a UI to edit the
bulletin, I will take the data from some file or database but I'd like
to print or save the bulletin to the file with all the fields filled in.
To accomplish this, I thought about creating a basic graphic (jpg?) file
with the bulletin image, filling in the fields and then saving it in a
new file.
Can you direct me to a better way? And also, direct me to a library that 
allows me to accomplish this easily?


Thank you in advance.
--
https://mail.python.org/mailman/listinfo/python-list


Re: installing

2022-06-11 Thread jak

Il 10/06/2022 10:51, Yusuf Özdemir ha scritto:

?

 


000?
  3f
  63
 077
001
--
https://mail.python.org/mailman/listinfo/python-list


Re: usb tv stick and python

2022-05-09 Thread jak

Il 09/05/2022 16:28, Dennis Lee Bieber ha scritto:

On Mon, 9 May 2022 08:47:50 +0200, jak  declaimed the
following:


Hello everybody,
I usually use vlc to watch tv and I use the w_scan program on linux to
create a file (.m3u) with the list of available channels. Unfortunately
I can't find an alternative to w_scan for Windows and I was wondering if
you could tell me some python library that allows me, easily, to
interface with the device and get the channel list.



UNTESTED... But if it works means no change to your procedures...

Presuming you are using W10 or later...

Activate the Windows Subsystem for Linux ([old style] Control Panel /
Programs / Programs and Features... Turn Windows Features On or Off...
Scroll down, it's the third from the bottom)

Windows "Microsoft Store"; Search "Debian"; Download/Install (set a
log-in account/password). (Unfortunately, it's not the most recent version
-- still on Buster...)

Open Debian instance console (to my knowledge, no graphical
applications are supported); do normal stuff to bring apt up-to-date.

-=-=-
wulfraed@ElusiveUnicorn:~$ apt search w_scan
Sorting... Done
Full Text Search... Done
w-scan/oldstable 20170107-2 amd64
   Channel scanning tool for DVB and ATSC channels

wulfraed@ElusiveUnicorn:~$
-=-=-

Install and test. Windows partitions are accessible as
/mnt/





First of all, thank you for your reply. Actually I already have a handy
work around to use w_scan because I have a VM with linux (ubuntu)
installed. I was just looking for a python package/library that would
allow me to write a wrapper around. I would also be satisfied with
finding documentation that describes the protocol to communicate with
the dvb interface and be able to write an app that produces the list of
available channels. Any advice, suggestion or pointing is welcome.

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


usb tv stick and python

2022-05-09 Thread jak

Hello everybody,
I usually use vlc to watch tv and I use the w_scan program on linux to
create a file (.m3u) with the list of available channels. Unfortunately
I can't find an alternative to w_scan for Windows and I was wondering if
you could tell me some python library that allows me, easily, to
interface with the device and get the channel list.

Thanks in advance for your attention.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Friday Finking: Contorted loops

2021-09-12 Thread jak

Il 11/09/2021 22:29, Avi Gross ha scritto:


Alan and others,

I think human languages used to make computer languages will often cause
confusion.

Some languages have an IF .. ELSE construct but also an EITHER ... OR and a
NEITHER ... NOR and other twists and turns like words that sometimes come
apart and you end up having to dangle a part that was in the front of a word
to later in the sentence and so on.

But I suspect many languages do NOT naturally have a construct like:

WHILE ... ELSE.

The might have a sentence like "While it is sunny you should use sunscreen
but when it rains use an umbrella." It probably is even a tad deceptive to
use WHILE in one part and not in the other. Perfectly valid sentences are
"When going outside if it is sunny use sunscreen but if it is rainy use an
umbrella" or skip the while and use a more standard if/else. The world
"while" just does not feel like a partner for "else".

So say you want to have a loop starting with WHILE and FOLLOWED by a single
ELSE clause. Arguably you could make WHILE as a construct return a status of
sorts if it runs at all or perhaps if it exits after at least one iteration
because the condition evaluates to FALSE. It would either return false if
you exit with a BREAK or by an error or perhaps not exit at all if you do a
return from within.

So if you made up a syntax like:

IF (WHILE condition {...})
ELSE {...}

Then what would that mean? Again, this is a make-believe construct. In the
above, if WHILE returned a True of some sort, the else is skipped.
Otherwise, no matter what has been done within the while loop, it is done.

But as noted we have odd choices here potentially. Could we differentiate
between a BREAK statement within and something like BREAK OK variant that
means the while is to be treated as succeeded and please do not do the
trailing ELSE? I can see many possible ways to design things and cannot
expect humans to automatically assume the specific nomenclature will be
meaningful to them.

There is an alternative that people who are not damn sure what the meaning
is can do. Create a variable that is set to False or True to represent
something before the WHILE is entered. Then make sure your code flips that
value in cased you want to make sure a trailing statement is run. Then
following the while, you place an IF statement that tests that variable and
does what the ELSE cluse would have done, or not.

Looking at other constructs, look at this code with a try:

i=0
while i<5:
 try:
 assert(i!=3) #Raises an AssertionError if i==3
 print("i={0}".format(i))
 except:
 continue
 finally:
 i+= 1; #Increment i

Now attach an ELSE clause to the WHILE, LOL!

At some point, van some humans decide just not to write the code this way?

What  about code that uses CONTINUE to the point where you enter the WHILE
statement and get a secondary IF or something that keeps triggering a
CONTINUE to start the next iteration. Arguably, this can effectively mean
the WHILE loop did nothing. An example would be evaluating the contents of a
data structure like a list and adding all numeric items together and
ignoring any that are character strings. Given all characters, no summation
is done. The first statement in the loop tests a list item and does a
CONTINUE. But by the rules as I see them, the loop was entered. Yet, a
similar loop written where the WHILE condition simply tests if ANY item is
numeric, might drop right through to an ELSE clause.

Bottom line is humans do not all think alike and language constructs that
are clear and logical to one may be confusing or mean the opposite to
others.

I can even imagine designing an interface like this:

WHILE (condition):
 ...
IF_NOT_RUN:
 ...
IF_EXITED_EARLY:
 ...
IF_ERROR_THROWN:
 ...
ON_PREMATURE_RETURN_DO_THIS:
 ...

I am not suggesting we need critters like that, simply that ELSE is a grab
bag case that can mean many things to many people.

But if the specific meaning is clearly documented, use it. Lots of people
who program in languages like Python do not necessarily even speak much
English and just memorize the keywords.

We can come up with ever more interesting or even bizarre constructs like
multiple WHILE in a row with each one being called only if the previous one
failed to process the data. An example might be if each tests the data type
and refuses to work on it so the next one in line is called. That could
perhaps be done by having multiple ELSE statements each with another WHILE.
But is that an ideal way to do this or perhaps instead use some variant of a
switch statement or a dictionary pointing to functions to invoke or
something.

Time to go do something lese of even minor usefulness!

-Original Message-
From: Python-list  On
Behalf Of Alan Gauld via Python-list
Sent: Saturday, September 11, 2021 3:59 AM
To: python-list@python.org
Subject: Re: Friday Finking: Contorted loops

On 10/09/2021 19:49, Stefan Ram wrote:

Alan 

Re: Friday Finking: Contorted loops

2021-09-12 Thread jak

-- snip --


An inconsistency that I have been able to notice is this:
someone suggests to remedy the absence of the do-while with:
while True:
  ...
  if condition:
  break
the problem arises if the while has an else of its own because the break
not only blocks the while loop but will also ignore the relative else.



I will try to make my doubt clearer:
if the only way to terminate a 'while True' loop is by using the 'break' 
statement, why is it allowed to add the 'else' statement which will only 
contain dead code?


while True:
break
else:
print('dead code')
--
https://mail.python.org/mailman/listinfo/python-list


Re: CPython / Decimal and bit length of value.

2021-09-06 Thread jak

Il 03/09/2021 22:09, Nacnud Nac ha scritto:

Hi,
Is there a quick way to get the number of bits required to store the value in a 
Decimal class?
What obvious thing am I missing? I'm working with really large integers, say, 
in the order of 5_000_000 of ASCII base 10 digits.
It seems the function mpd_sizeinbase would be a nice thing to be able to 
call.
It'd be nice to just be able to tell the "size of data", or something like that, that was 
stored in the *data? I note there is len "field" in the structure mpd_t
Sorry for the dumb question
Thanks,Duncan


to semplfy the example I'll use the value 100:

value="100"

exponent in base 10 is len(value) - 1 # (1 * 10^6)

now need change from base 10 to base 2: newexp = 6 / log(2) # 19 (more 
or less)


you will need newexp + 1 bits to represent the number: 2^20 = 1.048.576

hope helps


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


Re: on floating-point numbers

2021-09-04 Thread jak

Il 03/09/2021 14:45, Chris Angelico ha scritto:

I believe the definition of "accurate" here is that, if you take all
of the real numbers represented by those floats, add them all together
with mathematical accuracy, and then take the nearest representable
float, that will be the exact value that fsum will return. In other
words, its accuracy is exactly as good as the final result can be.


yup, I agree and this is the because of the link.
--
https://mail.python.org/mailman/listinfo/python-list


Re: on floating-point numbers

2021-09-03 Thread jak

Il 03/09/2021 09:07, Julio Di Egidio ha scritto:

On Friday, 3 September 2021 at 01:22:28 UTC+2, Chris Angelico wrote:

On Fri, Sep 3, 2021 at 8:15 AM Dennis Lee Bieber  wrote:

On Fri, 3 Sep 2021 04:43:02 +1000, Chris Angelico 
declaimed the following:


The naive summation algorithm used by sum() is compatible with a
variety of different data types - even lists, although it's documented
as being intended for numbers - but if you know for sure that you're
working with floats, there's a more accurate algorithm available to
you.


math.fsum([7.23, 8.41, 6.15, 2.31, 7.73, 7.77])

39.6

math.fsum([8.41, 6.15, 2.31, 7.73, 7.77, 7.23])

39.6

It seeks to minimize loss to repeated rounding and is, I believe,
independent of data order.


Most likely it sorts the data so the smallest values get summed first,
and works its way up to the larger values. That way it minimizes the losses
that occur when denormalizing a value (to set the exponent equal to that of
the next larger value).


I'm not sure, but that sounds familiar. It doesn't really matter
though - the docs just say that it is an "accurate floating point
sum", so the precise algorithm is an implementation detail.


The docs are quite misleading there, it is not accurate without further 
qualifications.




That said, fucking pathetic, when Dunning-Kruger is a compliment...

*Plonk*

Julio



https://en.wikipedia.org/wiki/IEEE_754
--
https://mail.python.org/mailman/listinfo/python-list


Re: urgent

2021-08-31 Thread jak

Il 31/08/2021 03:05, Python ha scritto:

Hari wrote:
i was download ur python software but it is like boring user interface 
for

me like young student to learn ,can u have any updates?


God, let me die please...





Oh no, please don't speak in that way ... evidently now that python has
reached its tenth version its prompt is a little boring. It may need to
be replaced. You could open a competition notice to vote on the new
prompt. I would vote for:

:^P>

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


Re: Decoding of EmailMessage text

2021-08-23 Thread jak

Il 23/08/2021 13:12, Loris Bennett ha scritto:

Jon Ribbens  writes:


On 2021-08-23, Loris Bennett  wrote:

If instead of

   mail.set_content(body)

I do

   mail.set_content(body, cte="quoted-printable")


Try print(mail.get_content()) rather than print(mail.as_string())


That did the trick - thanks!

Cheers,

Loris




If you also want to know about the text, then that is probably utf8
encoded and converted to base64:

from base64 import b64decode

coded=(b'RGVhciBEci4gQmVubmV0dCwKCkJsb3JwISBCbGVlcCEKCgotLQpNYWlsYm90IEl'
   b'uYy4KMDEwMTAxIEJvdCBCb3VsZXZhcmQKQm90aGFtIENpdHkKQsO2dGxhbmQK')

uncoded = b64decode(coded).decode('utf8')
print(uncoded)

output:

Dear Dr. Bennett,

Blorp! Bleep!


--
Mailbot Inc.
010101 Bot Boulevard
Botham City
Bötland
--
https://mail.python.org/mailman/listinfo/python-list


Re: Regarding inability of Python Module Winsound to produce beep in decimal frequency

2021-08-16 Thread jak

Il 13/08/2021 18:17, Chris Angelico ha scritto:

On Sat, Aug 14, 2021 at 2:11 AM Terry Reedy  wrote:


On 8/13/2021 6:53 AM, Umang Goswami wrote:

Hi There, Hope you find this mail in good health.

I am Umang Goswami, a Python developer and student working on a huge
project for automation of music instruments. I am producing the musical
notes using the Beep function of Winsound Module(
https://docs.python.org/3/library/winsound.html) by passing frequency as a
argument to the function.

Now whenever i provide frequency of any note in decimal(for example
277.1826 for C4 note) it shows following error:
Traceback (most recent call last):
File "C:\Users\Umang Goswami\Desktop\Umang  Goswami\test.py", line 2, in

  winsound.Beep(111.11,11)
TypeError: integer argument expected, got float

Now I have  to round up the frequencies. This is hurting the quality,
accuracy ,authenticity and future of the project. Almost all the notes have
the frequencies  in decimal parts. Rounding up means changing semitones and
quatertones thus whole note itself. This problem is technically making my
program useless.



Is it really? In my experience, no human ear can distinguish 277Hz
from 277.1826Hz when it's played on a one-bit PC speaker, which the
Beep function will be using.

ChrisA



Hi,
you could use a trick to get a better approximation:
277 * 4 = 1108
278 * 1 = 278
1108 + 278 = 1386
1386/5 = 277.2
now, respecting the duration of the tone, in accordance with its simple
rate, the buffer to be played will become:
277, 277, 277, 277, 278, 277,
277, 277, 277, 278, 277, 277,
... and so on.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Ask for help on using re

2021-08-07 Thread jak

Il 07/08/2021 11:18, jak ha scritto:

Il 07/08/2021 04:23, Jach Feng ha scritto:

jak 在 2021年8月6日 星期五下午4:10:05 [UTC+8] 的信中寫道:

Il 05/08/2021 11:40, Jach Feng ha scritto:

I want to distinguish between numbers with/without a dot attached:


text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
re.compile(r'ch \d{1,}[.]').findall(text)

['ch 1.', 'ch 23.']

re.compile(r'ch \d{1,}[^.]').findall(text)

['ch 23', 'ch 4 ', 'ch 56 ']

I can guess why the 'ch 23' appears in the second list. But how to 
get rid of it?


--Jach


import re
t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M)

res = r.findall(t)

dot = [x[1] for x in res if x[1] != '']
udot = [x[0] for x in res if x[0] != '']

print(f"dot: {dot}")
print(f"undot: {udot}")

out:

dot: ['ch 4', 'ch 56']
undot: ['ch 1.', 'ch 23.']

The result can be influenced by the order of re patterns?


import re
t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M).findall(t)

[('ch 1.', ''), ('ch 23.', ''), ('', 'ch 4'), ('', 'ch 56')]


re.compile(r'(ch +\d+)|(ch +\d+\.)', re.M).findall(t)

[('ch 1', ''), ('ch 23', ''), ('ch 4', ''), ('ch 56', '')]

--Jach


Yes, when the patterns intersect each other as in your case. the
difference between the 2 patterns is the "." in addition. The logical or
does not continue checking when the condition is satisfied, so it is a
good idea, in these cases, to search for the most complete patterns
before the others.



PS
... the behavior of the logical or that I have described is not typical
of regular expressions but it is common in all programming languages.

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


Re: Ask for help on using re

2021-08-07 Thread jak

Il 07/08/2021 04:23, Jach Feng ha scritto:

jak 在 2021年8月6日 星期五下午4:10:05 [UTC+8] 的信中寫道:

Il 05/08/2021 11:40, Jach Feng ha scritto:

I want to distinguish between numbers with/without a dot attached:


text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
re.compile(r'ch \d{1,}[.]').findall(text)

['ch 1.', 'ch 23.']

re.compile(r'ch \d{1,}[^.]').findall(text)

['ch 23', 'ch 4 ', 'ch 56 ']

I can guess why the 'ch 23' appears in the second list. But how to get rid of 
it?

--Jach


import re
t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M)

res = r.findall(t)

dot = [x[1] for x in res if x[1] != '']
udot = [x[0] for x in res if x[0] != '']

print(f"dot: {dot}")
print(f"undot: {udot}")

out:

dot: ['ch 4', 'ch 56']
undot: ['ch 1.', 'ch 23.']

The result can be influenced by the order of re patterns?


import re
t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M).findall(t)

[('ch 1.', ''), ('ch 23.', ''), ('', 'ch 4'), ('', 'ch 56')]


re.compile(r'(ch +\d+)|(ch +\d+\.)', re.M).findall(t)

[('ch 1', ''), ('ch 23', ''), ('ch 4', ''), ('ch 56', '')]

--Jach


Yes, when the patterns intersect each other as in your case. the
difference between the 2 patterns is the "." in addition. The logical or
does not continue checking when the condition is satisfied, so it is a
good idea, in these cases, to search for the most complete patterns
before the others.


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


Re: Ask for help on using re

2021-08-06 Thread jak

Il 06/08/2021 12:57, Jach Feng ha scritto:

jak 在 2021年8月6日 星期五下午4:10:05 [UTC+8] 的信中寫道:

Il 05/08/2021 11:40, Jach Feng ha scritto:

I want to distinguish between numbers with/without a dot attached:


text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
re.compile(r'ch \d{1,}[.]').findall(text)

['ch 1.', 'ch 23.']

re.compile(r'ch \d{1,}[^.]').findall(text)

['ch 23', 'ch 4 ', 'ch 56 ']

I can guess why the 'ch 23' appears in the second list. But how to get rid of 
it?

--Jach


import re
t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M)

res = r.findall(t)

dot = [x[1] for x in res if x[1] != '']
udot = [x[0] for x in res if x[0] != '']

print(f"dot: {dot}")
print(f"undot: {udot}")

out:

dot: ['ch 4', 'ch 56']
undot: ['ch 1.', 'ch 23.']
r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M)

That's an interest solution! Where the '|' operator in re.compile() was 
documented?

--Jach



I honestly can't tell you, I've been using it for over 30 years. In any
case you can find some traces of it in the "regular expressions quick
reference" on the site https://regex101.com (bottom right side).

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


Re: Ask for help on using re

2021-08-06 Thread jak

Il 06/08/2021 16:17, jak ha scritto:

Il 06/08/2021 12:57, Jach Feng ha scritto:

jak 在 2021年8月6日 星期五下午4:10:05 [UTC+8] 的信中寫道:

Il 05/08/2021 11:40, Jach Feng ha scritto:

I want to distinguish between numbers with/without a dot attached:


text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
re.compile(r'ch \d{1,}[.]').findall(text)

['ch 1.', 'ch 23.']

re.compile(r'ch \d{1,}[^.]').findall(text)

['ch 23', 'ch 4 ', 'ch 56 ']

I can guess why the 'ch 23' appears in the second list. But how to 
get rid of it?


--Jach


import re
t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M)

res = r.findall(t)

dot = [x[1] for x in res if x[1] != '']
udot = [x[0] for x in res if x[0] != '']

print(f"dot: {dot}")
print(f"undot: {udot}")

out:

dot: ['ch 4', 'ch 56']
undot: ['ch 1.', 'ch 23.']
r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M)
That's an interest solution! Where the '|' operator in re.compile() 
was documented?


--Jach



I honestly can't tell you, I've been using it for over 30 years. In any
case you can find some traces of it in the "regular expressions quick
reference" on the site https://regex101.com (bottom right side).


...if I'm not mistaken, the '|' it is part of normal regular
expressions, so it is not a specific extension of the python libraries.
Perhaps this is why you don't find any documentation on it.

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


Re: Ask for help on using re

2021-08-06 Thread jak

Il 05/08/2021 11:40, Jach Feng ha scritto:

I want to distinguish between numbers with/without a dot attached:


text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
re.compile(r'ch \d{1,}[.]').findall(text)

['ch 1.', 'ch 23.']

re.compile(r'ch \d{1,}[^.]').findall(text)

['ch 23', 'ch 4 ', 'ch 56 ']

I can guess why the 'ch 23' appears in the second list. But how to get rid of 
it?

--Jach



import re

t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M)

res = r.findall(t)

dot = [x[1] for x in res if x[1] != '']
udot = [x[0] for x in res if x[0] != '']

print(f"dot:   {dot}")
print(f"undot: {udot}")

out:

dot:   ['ch 4', 'ch 56']
undot: ['ch 1.', 'ch 23.']
--
https://mail.python.org/mailman/listinfo/python-list


Re: a clean exit

2021-07-29 Thread jak

Il 23/07/2021 10:55, jak ha scritto:

Hello everybody,
I wrote a bot for telegram which consists of some processes of which the
main ones are:
- the main process: a list of callback functions
- a second process: managed with a message queue
- a third process: started by the library I use (python-telegram-bot)
which is used for the event/error log ('logging' module).
The problem is that sometimes I get an error from the library (python-
telegram-bot) via logging that my bot has no way of intercepting. The
error is "connection reset by pear" after which my program is no longer
called and I need to restart it. Typically this happens when Telegram
runs an update.
In any case I can know this error because, to write a log in a telegram
channel, I inherit the 'emit' function of the 'logging.Handler' class.
Hoping to have explained clearly enough the context in which the program
receives information about the error (we are inside a process not
launched directly from the main program), my question is: do you have
advice on how I can close my program in the way as clean as possible in
a easy way?

Thanks in advance.


Thanks to both of you for the replies. I know the rules you described to
me for process synchronization but unfortunately, due to the structure
of the library I am using, I cannot apply them. I can't even use
try/except because the library, when the error occurs, displays it with
the loggin libraries and no longer calls any of the callback functions
my program is composed of. To understand when the error occurs, my only
possibility is to intercept the logging, read the error string and,
if it is not one of mine (mine all start with ":"), to 
close the program. Following some examples on the web I also configured

a function to handle the error:
'''
dispatcher.add_error_handler(callback=fallback_error,
 run_async=True)
'''
but unfortunately after getting that error the library seems to hang and 
doesn't call my callback function.
I was actually hoping to get some tricks to shut down all processes as a 
suggestion. Could I, for example, create a global list containing the 
ids of the processes I started and then kill each of them? Can it make 
sense?


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


a clean exit

2021-07-23 Thread jak

Hello everybody,
I wrote a bot for telegram which consists of some processes of which the
main ones are:
- the main process: a list of callback functions
- a second process: managed with a message queue
- a third process: started by the library I use (python-telegram-bot)
which is used for the event/error log ('logging' module).
The problem is that sometimes I get an error from the library (python-
telegram-bot) via logging that my bot has no way of intercepting. The
error is "connection reset by pear" after which my program is no longer
called and I need to restart it. Typically this happens when Telegram
runs an update.
In any case I can know this error because, to write a log in a telegram
channel, I inherit the 'emit' function of the 'logging.Handler' class.
Hoping to have explained clearly enough the context in which the program
receives information about the error (we are inside a process not
launched directly from the main program), my question is: do you have
advice on how I can close my program in the way as clean as possible in
a easy way?

Thanks in advance.
--
https://mail.python.org/mailman/listinfo/python-list


packages discoverer

2021-07-17 Thread jak

Sometimes, wandering around the web, I find some example of python code
that uses some class or function that I don't know and that I would like
to try. I copy the code locally and when I try to run it, I find that
the package containing the class/function is not installed on my pc, so
I go back to the web to search for it with the search engine. The
question is: is there a command or an easy way to find out the name of
the package that contains the class/function of my interest?

Thank you in advance.
--
https://mail.python.org/mailman/listinfo/python-list


Re: curses apps on MS Windows?

2021-06-14 Thread jak

Il 13/06/2021 19:44, Grant Edwards ha scritto:

There's been a surprising amount of discussion lately about using
curses libraries on Windows OS. I'm surprised by this, because I don't
think I've ever even seen a Windows curses application.

Are there examples of popular curses applications for Windows?

Does windows have a terminfo/termcap subsystem to deal with different
terminal types? Or do the apps only work with the built-in terminal
windows implemented by command.com/cmd.exe?

Can windows curses apps be used via real serial terminals? Is that
even "a thing" under Windows?

Is there such a thing as a Windows ssh server with some sort of shell
in which one can run console-mode applications?

--
Grant




https://winaero.com/enable-openssh-server-windows-10/
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to debug python + curses? [was: RE: Applying winpdb_reborn]

2021-06-01 Thread jak

Il 31/05/2021 02:36, Dennis Lee Bieber ha scritto:

On Mon, 31 May 2021 08:07:21 +1000, Cameron Simpson 
declaimed the following:



Open another terminal, note its terminal device with the "tty" command.
Start your programme like this:

python .. 2>/dev/tty-of-the-other-termina



The OP specified Win10, so the above is dead from the start.




OP can try this way on win10: write the debug information in a log file
and, from another console, open the same log file with an editor that
knows how to check the active status of the file (eg: notepad++), which
can automatically update the changes (you can remove the notification
for this action from the tab preferences).

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


Re: string storage [was: Re: imaplib: is this really so unwieldy?]

2021-05-28 Thread jak

Il 27/05/2021 05:54, Cameron Simpson ha scritto:

On 26May2021 12:11, Jon Ribbens  wrote:

On 2021-05-26, Alan Gauld  wrote:

I confess I had just assumed the unicode strings were stored
in native unicode UTF8 format.


If you do that then indexing and slicing strings becomes very slow.


True, but that isn't necessarily a show stopper. My impression, on
reflection, is that most slicing is close to the beginning or end of a
string, and that _most strings are small. (Alan has exceptions at least
to the latter.) In those circumstances, the cost of slicing a variable
width encoding is greatly mitigated.

Indexing is probably more general (in my subjective hand waving
guesstimation). But... how common is indexing into large strings?
Versus, say, iteration over a large string?

I was surprised when getting introduced to Golang a few years ago that
it stores all Strings as UTF8 byte sequences. And when writing Go code,
I found very few circumstances where that would actually bring
performance issues, which I attribute in part to my suggestions above
about when, in practical terms, we slice and index strings.

If the internal storage is UTF8, then in an ecosystem where all, or
most, text files are themselves UTF8 then reading a text file has zero
decoding cost - you can just read the bytes and store them! And to write
a String out to a UTF8 file, you just copy the bytes - zero encoding!




Also, UTF8 is a funny thing - it is deliberately designed so that you
can just jump into the middle of an arbitrary stream of UTF8 bytes and
find the character boundaries. That doesn't solve slicing/indexing in
general, but it does avoid any risk of producing mojibake just by
starting your decode at a random place.



Perhaps you are referring to what the python language does if you jump 
to an albiter position of an utf8 string. Otherwise, before you start 
decoding, you should align at the beginning of an utf8 character by 
discarding the bytes that meet the following test:


(byte & 0xc0) == 0x80 /* Clang */



Cheers,
Cameron Simpson 



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


Re: STARTUP FAILURE

2021-05-13 Thread jak

Il 13/05/2021 15:49, Sumukh chakkirala ha scritto:

Hello, I have been facing this " startup failure" for a while now, I can
open the IDLE from the programs that I have saved but I'm not able to open
the IDLE directly. Hope you can help me with this issue as soon as possible.


for Windows cmd:
C:\Python\pythonw.exe "C:\Python\Lib\idlelib\idle.pyw
--
https://mail.python.org/mailman/listinfo/python-list


Re: port to PDOS (especially mainframe)

2021-04-17 Thread jak

Il 17/04/2021 10:56, Paul Edwards ha scritto:

On Saturday, April 17, 2021 at 7:52:07 AM UTC+10, jak wrote:


one thing is not clear to me, do you absolutely need to use "asma"?

http://www.z390.org/


I forgot to mention that it also requires Java. So instead
of porting Python to the S/3X0 I would need to port
Java.

Note that Java (and Python for that matter) are available
for later versions of z/OS, but as far as I am aware, they
are not available for the free MVS that hobbyists use, ie
MVS 3.8J, and it's definitely not available for the
environment I'm actually interested in, which is PDOS/3X0.

BFN. Paul.



I looked at the "asma" folder and noticed that some files were touched 6 
years ago. I could deduce from this that the authors might have an older 
version, perhaps developed for an older version of python, probably for 
the 2.x versions. You could try contacting the authors to ask about 
this. Python 2.x porting would probably be easier.

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


Re: port to PDOS (especially mainframe)

2021-04-16 Thread jak

Il 23/03/2021 10:23, Paul Edwards ha scritto:

Hello. I have a new operating system called PDOS
which works on both PC and mainframe, which
can be found here:

http://pdos.sourceforge.net/

I know nothing about Python, my focus is on C90,
but I wish to run this mainframe assembler, asma,
which was written in Python, not C:

https://github.com/s390guy/SATK

It needs Python 3.3 as a minimum, so I wish to port
that version, first to the PC version of PDOS, then to
the mainframe version.

I don't wish to run configure, I want to hand-construct
the makefile. PDOS is a very simple system, so I only
want to produce a single executable, no DLLs etc. I
don't need extensions to work or anything, all I need
to be able to do is run asma.

I've started constructing a makefile (see below), which
I execute with pdmake, included in PDOS. I only have
a C90 runtime library, no more, no less, so I don't know
whether Python will actually build in my environment,
but so far things have been building successfully.

However I'm currently stuck on this unresolved
external:

python.a(abstract.o)(.text+0x668c):abstract.c: undefined reference to 
`PyExc_StopIteration'

What source file is meant to define that variable
(PyExc_StopIteration) as opposed to just declaring
it or using it? I tried looking at the OS/2 EMX for
hints, but wasn't able to figure it out.

Note that I am building a Win32 executable to start
with, but it is linking against my own C90 library
which will call either Microsoft's MSVCRT.DLL on
Windows, or my own MSVCRT.DLL when running
on PDOS/386 (aka PD-Windows).

I also don't know whether Python will survive being
transferred to an EBCDIC environment, as that is
where it will ultimately need to be compiled and
linked (MVS/380), or at least assembled, before it
can wind up on PDOS/3X0. Well, maybe it can all
be done on Windows. I need to see what asma
is capable of.

Thanks. Paul.



# Produce Windows executables
# links with PDPCLIB created by makefile.msv

CC=gccwin
CFLAGS=-O0
LD=ldwin
LDFLAGS=
AS=aswin
AR=arwin
STRIP=stripwin
COPTS=-S $(CFLAGS) -fno-common -ansi -I. -I../Include -I../../pdos/pdpclib 
-D__WIN32__

all: clean python.exe

python.exe: python.o ../Python/strdup.o ../Objects/object.o 
../Objects/abstract.o \
 ../Python/errors.o ../Objects/bytesobject.o
   rm -f python.a
   ar r python.a ../Python/strdup.o ../Objects/object.o ../Objects/abstract.o
   ar r python.a ../Python/errors.o ../Objects/bytesobject.o
   $(LD) $(LDFLAGS) -o python.exe ../../pdos/pdpclib/w32start.o python.o 
python.a ../../pdos/pdpclib/msvcrt.a ../../pdos/src/kernel32.a
   $(STRIP) python.exe

.c.o:
   $(CC) $(COPTS) -o $*.s $<
   $(AS) -o $@ $*.s
   rm -f $*.s

clean:
   rm -f *.o python.exe


Hi,
one thing is not clear to me, do you absolutely need to use "asma"?

http://www.z390.org/
--
https://mail.python.org/mailman/listinfo/python-list


Re: FileNotFoundError: [Errno 2] No such file or directory: ''

2021-04-14 Thread jak

Il 14/04/2021 18:13, Quentin Bock ha scritto:

I receive this error when I try to open a file
The file (what I'm trying to open) is in the same folder as the program I'm
trying to open it from; why is it saying no such file or directory?

this is the only part of the code that causes the error

file = open('Egils Saga 1-15.txt', "r")

file.close()



>>> file = open('Egils Saga 1-15.txt', "r")
>>> file.read()
'Hello from Egils Saga 1-15.txt'
>>> file.close()

Try to check your 'current working directory':

>>> import os
>>> os.getcwd()
'D:\\tmp'# where the file is
>>>
--
https://mail.python.org/mailman/listinfo/python-list


Re: Comparing text strings

2021-04-13 Thread jak

Il 13/04/2021 01:11, Rich Shepard ha scritto:

I'm running Slackware64-14.2 and keep a list of installed packages. When a
package is upgraded I want to remove the earlier version, and I've not
before written a script like this. Could there be a module or tool that
already exists to do this? If not, which string function would be best
suited to the task?

Here's an example:
atftp-0.7.2-x86_64-2_SBo.tgz
atftp-0.7.4-x86_64-1_SBo.tgz

and there are others like this. I want the python3 script to remove the
first one. Tools like like 'find' or 'sort -u' won't work because while the
file name is the same the version or build numbers differ.

All suggestions welcome.

Rich



If I understand your problem correctly, the problem would be dealing 
with numbers as such in file names. This is just a track but it might 
help you. This example splits filenames into strings and numbers into 
tuples, appends the tuple into a list, and then sorts the list:


files = ['atftp-0.7.4-x86_64-2_SBo.tgz', 'atftp-0.7.2-x86_64-1_SBo.tgz']
digit = None
da = ''
tfn = tuple()
ltafn = list()
for f in files:
for c in f:
if str(c).isdigit():
if not digit:
if len(da) > 0:
tfn += (da,)
da = ''
digit = True
da += c
else:
da += c
else:
if digit:
if len(da) > 0:
tfn += (int(da),)
da = ''
digit = False
da += c
else:
da += c
if len(da) > 0:
if da.isdigit():
tfn += (int(da),)
else:
tfn += (da,)
da = ''
ltafn += [tfn, ]
tfn = ()

ltafn.sort()

for t in files:
print(t)
print()
for t in ltafn:
nn = ''
for f in t:
nn += str(f)
print(nn)
--
https://mail.python.org/mailman/listinfo/python-list


Re: Horrible abuse of __init_subclass__, or elegant hack?

2021-04-04 Thread jak

Il 04/04/2021 18:18, jak ha scritto:

Il 04/04/2021 11:13, Chris Angelico ha scritto:

On Sun, Apr 4, 2021 at 6:26 PM jak  wrote:


Il 01/04/2021 01:14, Chris Angelico ha scritto:


copy / paste corrupted the tabs, sorry
--
https://mail.python.org/mailman/listinfo/python-list


Re: Horrible abuse of __init_subclass__, or elegant hack?

2021-04-04 Thread jak

Il 04/04/2021 11:13, Chris Angelico ha scritto:

On Sun, Apr 4, 2021 at 6:26 PM jak  wrote:


Il 01/04/2021 01:14, Chris Angelico ha scritto:

I think this code makes some sort of argument in the debate about
whether Python has too much flexibility or if it's the best
metaprogramming toolset in the world. I'm not sure which side of the
debate it falls on, though.

class Building:
  resource = None
  @classmethod
  def __init_subclass__(bldg):
  super().__init_subclass__()
  print("Building:", bldg.__name__)
  def make_recipe(recip):
  print(recip.__name__.replace("_", " "), "is made in a",
bldg.__name__.replace("_", " "))
  bldg.__init_subclass__ = classmethod(make_recipe)


class Extractor(Building): ...
class Refinery(Building): ...

class Crude(Extractor):
  resource = "Oil"
  time: 1
  Crude: 1

class Plastic(Refinery):
  Crude: 3
  time: 6
  Residue: 1
  Plastic: 2

class Rubber(Refinery):
  Crude: 3
  time: 6
  Residue: 2
  Rubber: 2

Full code is here if you want context:
https://github.com/Rosuav/shed/blob/master/satisfactory-production.py

Subclassing Building defines a class that is a building. (The ellipsis
body is a placeholder; I haven't implemented stuff where the buildings
know about their power consumptions and such. Eventually they'll have
other attributes.) But subclassing a building defines a recipe that is
produced in that building. Markers placed before the "time" are
ingredients, those after the "time" are products.

There are actually a lot of interesting wrinkles to trying to replace
__init_subclass__ on the fly. Things get quite entertaining if you
don't use the decorator, or if you define and decorate the function
outside of the class, or various other combinations.

On a scale of 1 to "submit this to The Daily WTF immediately", how bad
is this code? :)

ChrisA



Hi,
from https://github.com/Rosuav/shed/blob/master/satisfactory-production.py
I get this error:

cmd console Win10:
$> py -V
Python 3.8.6

$> py CrisAngelico.py
Building: Extractor
Building: Refinery
Building: Blender
Building: Packager
Building: Assembler
Crude is made in a Extractor
Water is made in a Extractor
Plastic is made in a Refinery
Rubber is made in a Refinery
Traceback (most recent call last):
File "CrisAngelico.py", line 123, in 
  class Rubber(Refinery):
File "CrisAngelico.py", line 72, in make_recipe
  if net <= alternate["makes"] and costs >= alternate["costs"]:
TypeError: '<=' not supported between instances of 'Counter' and 'Counter'



Huh. I forget sometimes which version something was introduced in.
Apparently the comparison operators on Counters came in with Python
3.10. Sorry about that. As an alternative, subtraction is very
approximately equivalent (if subtracting one counter from another
yields nothing, then the first one is smaller in total content than
the second), so it should be possible to adjust it.

ChrisA


I modified your code in the following way:

line 65-66:
from:

if (qty <= alternate["per_minute"]
	and (costs[Extractor], costs) > (alternate["costs"][Extractor], 
alternate["costs"])


to:
if (qty <= alternate["per_minute"]
and (costs[Extractor], set(costs)) > 
(alternate["costs"][Extractor], set(alternate["costs"]))


and line 72:
from:
if net <= alternate["makes"] and costs >= alternate["costs"]:

to:
if set(net) <= set(alternate["makes"]) and set(costs) >= 
set(alternate["costs"]):



The program runs now. Is the result correct?

Building: Extractor
Building: Refinery
Building: Blender
Building: Packager
Building: Assembler
Crude is made in a Extractor
Water is made in a Extractor
Plastic is made in a Refinery
Rubber is made in a Refinery
Fuel is made in a Refinery
Heavy Oil Residue is made in a Refinery
Polymer Resin is made in a Refinery
Residual Fuel is made in a Refinery
Diluted Fuel is made in a Blender
Canister is made in a Extractor
Package Water is made in a Packager
Diluted Packaged Fuel is made in a Refinery
Unpackage Fuel is made in a Packager
Petroleum Coke is made in a Refinery
Residual Plastic is made in a Refinery
Residual Rubber is made in a Refinery
Recycled Plastic is made in a Refinery
Recycled Rubber is made in a Refinery
Sulfur is made in a Extractor
Coal is made in a Extractor
Compacted is made in a Assembler
Turbofuel is made in a Refinery
Turbo Heavy Fuel is made in a Refinery
Turbo Blend Fuel is made in a Blender
Specify one or more target items
Exit code:  0

(forgive me your trouble)
cheers
--
https://mail.python.org/mailman/listinfo/python-list


Re: Horrible abuse of __init_subclass__, or elegant hack?

2021-04-04 Thread jak

Il 01/04/2021 01:14, Chris Angelico ha scritto:

I think this code makes some sort of argument in the debate about
whether Python has too much flexibility or if it's the best
metaprogramming toolset in the world. I'm not sure which side of the
debate it falls on, though.

class Building:
 resource = None
 @classmethod
 def __init_subclass__(bldg):
 super().__init_subclass__()
 print("Building:", bldg.__name__)
 def make_recipe(recip):
 print(recip.__name__.replace("_", " "), "is made in a",
bldg.__name__.replace("_", " "))
 bldg.__init_subclass__ = classmethod(make_recipe)


class Extractor(Building): ...
class Refinery(Building): ...

class Crude(Extractor):
 resource = "Oil"
 time: 1
 Crude: 1

class Plastic(Refinery):
 Crude: 3
 time: 6
 Residue: 1
 Plastic: 2

class Rubber(Refinery):
 Crude: 3
 time: 6
 Residue: 2
 Rubber: 2

Full code is here if you want context:
https://github.com/Rosuav/shed/blob/master/satisfactory-production.py

Subclassing Building defines a class that is a building. (The ellipsis
body is a placeholder; I haven't implemented stuff where the buildings
know about their power consumptions and such. Eventually they'll have
other attributes.) But subclassing a building defines a recipe that is
produced in that building. Markers placed before the "time" are
ingredients, those after the "time" are products.

There are actually a lot of interesting wrinkles to trying to replace
__init_subclass__ on the fly. Things get quite entertaining if you
don't use the decorator, or if you define and decorate the function
outside of the class, or various other combinations.

On a scale of 1 to "submit this to The Daily WTF immediately", how bad
is this code? :)

ChrisA



Hi,
from https://github.com/Rosuav/shed/blob/master/satisfactory-production.py
I get this error:

cmd console Win10:
$> py -V
Python 3.8.6

$> py CrisAngelico.py
Building: Extractor
Building: Refinery
Building: Blender
Building: Packager
Building: Assembler
Crude is made in a Extractor
Water is made in a Extractor
Plastic is made in a Refinery
Rubber is made in a Refinery
Traceback (most recent call last):
  File "CrisAngelico.py", line 123, in 
class Rubber(Refinery):
  File "CrisAngelico.py", line 72, in make_recipe
if net <= alternate["makes"] and costs >= alternate["costs"]:
TypeError: '<=' not supported between instances of 'Counter' and 'Counter'

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


Re: New Python curses book

2021-03-31 Thread jak

Il 30/03/2021 21:05, Brian Oney ha scritto:

Congratulations!

Indeed, I was wondering for a moment if this was a guide to al dente spaghetti 
code. With each curse being a funny way to mess with the colleagues performing 
the code review ;)


Indeed, I was wondering for a moment if this was a guide to "spaghetti 
al dente" code...


Or a list of funny Monty Python curses?

Or a set of programming problems that are cursed?



On March 30, 2021 6:35:20 PM GMT+02:00, Chris Angelico  wrote:

On Wed, Mar 31, 2021 at 3:21 AM Avi Gross via Python-list
 wrote:


Congratulations, Alan, on the book.

I continue to wonder if people will buy the book for the wrong reason or ban
it thinking you created an AI snake that creates and spews new CURSES never
heard before.


A reasonable misunderstanding. After all, profanity is the one
language that every computer programmer understands, regardless of
culture, tools, era, or anything else.

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


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


Re: Compare word to word files

2021-03-11 Thread jak

Il 11/03/2021 16:08, Dennis Lee Bieber ha scritto:

On Thu, 11 Mar 2021 08:07:40 +0100, jak  declaimed the
following:


Il 11/03/2021 05:28, CLYMATIC GAMING ha scritto:

Hello ,
I want to compare word to word files
please he me!


copy and paset this string onto
Google page:

how to find difference between 2 files in Python

...and press "Google Search" button.


Presuming the OP means plain text files and not M$ Word documents.




If the search engine results do not satisfy him, he will change the
search string. Optionally, you can prepare for him a list of search
strings for all cases, who knows, maybe he means word as two bytes at
time.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Compare word to word files

2021-03-10 Thread jak

Il 11/03/2021 05:28, CLYMATIC GAMING ha scritto:

Hello ,
I want to compare word to word files
please he me!


copy and paset this string onto
Google page:

how to find difference between 2 files in Python

...and press "Google Search" button.
--
https://mail.python.org/mailman/listinfo/python-list


Idle Python issue

2021-02-24 Thread jak

Hello everybody,
I encounter a problem using Idle Python in Windows when I use utf8 
characters longer than 2 bytes such as the character representing the 
smile emoticon:

:-)
that is this:

Try to write this in Idle:
"".encode('utf8')
b'\xf0\x9f\x98\x8a'
now try to write this:
"".encode('utf8')
now position the cursor between the double quotes and paste the smile 
character and try to add some text to the string (eg: 'the smile').

You may notice problems with editing.
Is there any way to prevent this from happening? It is an annoying problem.
Thanks for your attention.

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


Re: Is there a way to subtract 3 from every digit of a number?

2021-02-20 Thread jak

Il 20/02/2021 15:40, C W ha scritto:

Hello everyone,

I'm curious if there is a way take number and back each digit by 3 ?

2342 becomes 9019
8475 becomes 5142
5873 becomes 2540

The tricky part is that 2 becomes 9, not -1.

Here's my toy example and what I attempted,

test_series = pd.Series(list(['2342', '8475', '5873']))
test_series

02342
18475
25873
dtype: object


test_series.str.split('')

[, 2, 3, 4, 2, ]
[, 8, 4, 7, 5, ]
[, 5, 8, 7, 3, ]
dtype: object

What a good approach to this? Is there a method or function that should be
handling this?

Thanks so much!

Mike



>>> num='0123456789'
>>> n=8475
>>> sn = ''
>>> for x in str(n):
sn += num[(int(x) - 3) % 10]


>>> int(sn)
5142
>>>
--
https://mail.python.org/mailman/listinfo/python-list


Re: issue with seaborn

2021-02-20 Thread jak

Il 20/02/2021 01:56, Dino ha scritto:


trying to do some dayaviz with Italian Covid Open Data ( 
https://github.com/italia/covid19-opendata-vaccini/ )


here's how I pull my data:

import sys
import urllib.request
import pandas as pd
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

URL = 
"https://github.com/italia/covid19-opendata-vaccini/blob/master/dati/somministrazioni-vaccini-latest.csv?raw=true; 



with urllib.request.urlopen(URL) as url:
     df = pd.read_csv(url)


One of my diagrams came out screwed up today, and I am having a hard 
time understanding what went wrong:


https://imgur.com/a/XTd4akn

Any ideas?

Thanks



I don't think this is the cause of your problem and in addition I don't
know about pandas. In any case you send pandas some records that contain
the date in string format and this gives the alphabetic continuity but
if the data contained time holes, these would not be represented in your
graph. Maybe you should add an intermediate step and convert strings
dates to datetime format before you create the chart (but perhaps pandas
takes care of this. I don't know this).

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


Re: list() strange behaviour

2021-01-24 Thread jak

Il 20/12/2020 21:00, danilob ha scritto:

Hi,
I'm an absolute beginner in Python (and in English too ;-)
Running this code:


--
# Python 3.9.0

a = [[1, 2, 0, 3, 0],
  [0, 4, 5, 0, 6],
  [7, 0, 8, 0, 9],
  [2, 3, 0, 0, 1],
  [0, 0, 1, 8, 0]]


b = ((x[0] for x in a))

print(list(b))
print(list(b))
---


I get this output:

[1, 0, 7, 2, 0]
[]


I don't know why the second print() output shows an empty list.
Is it possible that the first print() call might have changed the value 
of "b"?


Thank you in advance.


You should see a generator as a container emptying while you are reading 
it, so you should recreate it every time:



# Python 3.9.0

a = [[1, 2, 0, 3, 0],
 [0, 4, 5, 0, 6],
 [7, 0, 8, 0, 9],
 [2, 3, 0, 0, 1],
 [0, 0, 1, 8, 0]]


b = lambda : (x[0] for x in a)

print(list(b()))
print(list(b()))

output:
[1, 0, 7, 2, 0]
[1, 0, 7, 2, 0]

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


Re: why sqrt is not a built-in function?

2021-01-14 Thread jak

Il 14/01/2021 18:44, Denys Contant ha scritto:

I don't understand why sqrt is not a built-in function.
Why do we have to first import the function from the math module?
I use it ALL THE TIME!

That felt good. Thank you.








>>> val=16
>>> exp=2
>>> val ** (1/exp)
4.0
>>>
--
https://mail.python.org/mailman/listinfo/python-list


Re: Control stript which is runing in background.

2021-01-02 Thread jak

PS:
difference starts after 64K block
--
https://mail.python.org/mailman/listinfo/python-list


Re: Control stript which is runing in background.

2021-01-02 Thread jak

Il 02/01/2021 01:07, Alan Bawden ha scritto:

jak  writes:

Il 01/01/2021 06:23, Alan Bawden ha scritto:
> jak  writes:
>
> Running the command:
>
> $ cat bible.txt > cmdpipe & cat bible.txt > cmdpipe & cat bible.txt > 
cmdpipe
>
> the three texts do not mix
>
> The three texts do not mix as long at the contents of bible.txt is short
> enough (and provided `cat' only calls `write' once).  In the POSIX
> specification, the manual page for the `write' system call describes
> writing to a pipe or FIFO as follows:
>
>Write requests of {PIPE_BUF} bytes or less shall not be interleaved
>with data from other processes doing writes on the same pipe.  Writes
>of greater than {PIPE_BUF} bytes may have data interleaved, on
>arbitrary boundaries, with writes by other processes, whether or not
>the O_NONBLOCK flag of the file status flags is set.
>
Ok. And...
 ...Running the command:

 $ cat bible.txt > cmdpipe & cat bible.txt > cmdpipe & cat bible.txt > 
cmdpipe

 the three texts do not mix

Saying it again doesn't make it any more true.  If bible.txt is large
enough, they most definitely DO mix!  Just to make sure I wasn't missing
something, I tested your exact command before I sent my previous reply.
They mixed.


You're right it's mixed. I tried this:
terminal 1:

$ python3 pmkfifo.py > output.txt

terminal 2:

cat bible.txt > mypipe & cat bible.txt > mypipe & cat bible.txt > mypipe

result:

-rw-rw-r-- 1 jak jak  4351187 gen  1 02:56 bible.txt
prw-rw-r-- 1 jak jak0 gen  2 11:40 mypipe
-rw-rw-r-- 1 jak jak 13053561 gen  2 11:40 output.txt
-rw-rw-r-- 1 jak jak  237 gen  1 02:46 pmkfifo.py

then I split ouput.txt:

$ split -n 3 -d output.txt result

result:

-rw-rw-r-- 1 jak jak  4351187 gen  1 02:56 bible.txt
prw-rw-r-- 1 jak jak0 gen  2 11:40 mypipe
-rw-rw-r-- 1 jak jak 13053561 gen  2 11:40 output.txt
-rw-rw-r-- 1 jak jak  237 gen  1 02:46 pmkfifo.py
-rw-rw-r-- 1 jak jak  4351187 gen  2 11:41 result00
-rw-rw-r-- 1 jak jak  4351187 gen  2 11:41 result01
-rw-rw-r-- 1 jak jak  4351187 gen  2 11:41 result02

but:

$ cmp result00 result01
result00 result01 differ: byte 1, line 1

$ cmp result01 result02
result01 result02 differ: byte 1, line 1

...but not happy I wanted to try sure that each process used a single 
write and I wrote this program in C:


int main()
{
FILE *fp;
long len, readed;
char *buffer;

if((fp = fopen("bible.txt", "rt")) == NULL)
return -1;
// get file size
if(!fseek(fp, 0, SEEK_END))
{
len = ftell(fp);
}
else
return -1;
// build buffer
   if((buffer = malloc(len)) == NULL)
return -1;
// reset position
if(fseek(fp, 0, SEEK_SET))
return -1;
if((readed = fread(buffer, len, 1, fp)) != 1)
return -1;
if((fwrite(buffer, len, 1, stdout)) < 1)
return -1;
fflush(stdout);
free(buffer);
fclose(fp);
return 0;
}

... and runned:

$ ./mycat > mypipe & ./mycat > mypipe & ./mycat > mypipe

I, however, got the same result.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Control stript which is runing in background.

2021-01-02 Thread jak

Il 02/01/2021 01:07, Alan Bawden ha scritto:

jak  writes:

Il 01/01/2021 06:23, Alan Bawden ha scritto:
> jak  writes:
>
> Running the command:
>
> $ cat bible.txt > cmdpipe & cat bible.txt > cmdpipe & cat bible.txt > 
cmdpipe
>
> the three texts do not mix
>
> The three texts do not mix as long at the contents of bible.txt is short
> enough (and provided `cat' only calls `write' once).  In the POSIX
> specification, the manual page for the `write' system call describes
> writing to a pipe or FIFO as follows:
>
>Write requests of {PIPE_BUF} bytes or less shall not be interleaved
>with data from other processes doing writes on the same pipe.  Writes
>of greater than {PIPE_BUF} bytes may have data interleaved, on
>arbitrary boundaries, with writes by other processes, whether or not
>the O_NONBLOCK flag of the file status flags is set.
>
Ok. And...
 ...Running the command:

 $ cat bible.txt > cmdpipe & cat bible.txt > cmdpipe & cat bible.txt > 
cmdpipe

 the three texts do not mix

Saying it again doesn't make it any more true.  If bible.txt is large
enough, they most definitely DO mix!  Just to make sure I wasn't missing
something, I tested your exact command before I sent my previous reply.
They mixed.



This is really strange. On which system did you test? unix, linux or a
surrogate (cygwin, msys)? I asked this because bible.txt is 4.25MB size
(https://github.com/mxw/grmr/blob/master/src/finaltests/bible.txt)...
and the OP needs to send only commands (I hope smaller than the bible).
--
https://mail.python.org/mailman/listinfo/python-list


Re: Control stript which is runing in background.

2021-01-01 Thread jak

Il 01/01/2021 14:49, 2qdxy4rzwzuui...@potatochowder.com ha scritto:

On 2021-01-01 at 11:11:47 +0100,
jak  wrote:


Il 01/01/2021 04:14, 2qdxy4rzwzuui...@potatochowder.com ha scritto:

On 2021-01-01 at 03:43:43 +0100,
jak  wrote:

I think you were clear enough before, but you may not have
considered things the OP did not specify.  One of the hardest parts
of software development is understanding and specifying the actual
problem to be solved.  I've never done that in a second language
(I'm nowhere near fluent in anything other than English); I can only
imagine the extra layers of complexity.


This is the OP's initial request:


Hi. I would like to make something like this:
A python script would run headlessly in the background.
I would like to control the script from the command line using other python 
scripts or from the python shell.
 From time to time I would ask the main script to create a popup window with an 
image or a plot.
What would be the proper way to approach it. How to make communication between 
two scripts?
Thank you.
Petro.


I have suggested an alternative way for his necessity.
It will be up to him to decide which one he should adopt. That way he
has a choice, at least.
I don't know where you work. I know, however, that in these zones, if
you do more than necessary wasting their time, they will kick you away.


So what is necessary?  Does "control the script" include reporting
success or failure of a request?  The OP also said "communication
between two scripts," which to me means two way traffic.  Only the OP
knows for sure, and at least one of us might get it wrong if we don't
ask, or at least acknowledge that we don't know.

If I start with a named pipe because it's good enough for Phase Zero, or
Version 0.0.1, or Release Experimental Alpha, then I'm going to end up
ripping that out or reinventing something that already existed on top of
that named pipe.

Suppose I look ahead just a little, and use a socket in the first place.
Yes, you could argue that it's more than what was necessary and kick me
away.  Will you kick me again when you can build on it (I can't; I'm
gone) instead of having to rebuild what already worked?

I'm retired now, but I at one employer, I worked on products with (a)
lifetimes measured in decades, (b) extremely expensive (money, time,
convenience, etc.) upgrade/maintenance cycles, and therefore (c) strict
interoperability requirements.¹  A little bit of foresight and extra
analysis up front went a long way towards saving a lot of headaches and
development resources (and customers!)  in the long term.
Communications protocols in particular had to be well thought out, and
often more complex than was immediately necessary, because backwards and
forwards compatibility were not negotiable.

Again, I'm not (and no one else is, either) saying that your solution is
wrong or won't work.  Your solution may even be the be the best solution
to the OP's problem.  All I'm saying is that we can't know until the OP
tells us more.  Yes, the OP has a choice to make.  Maybe we have helped,
not by making that choice, but by giving it to the OP with enough
supporting information to make it.

¹ Think about a system containing dozens of individually field
upgradeable components.  It had to work when a customer upgraded one
component at a time (over the course of weeks or months), or plugged in
an old component while they were waiting for a broken one to be
repaired, or added a brand new component to a system that hadn't been
changed or upgraded for years.  There was no such thing as telling the
customer to wait, or to make them take down the entire system to upgrade
everything at once.  *That* was what was necessary.


Sorry if I made you argumentative. It was not my intention. Let me
reiterate once again that my intention was to offer an alternative to
the OP and that I never claimed my way was better than the other
suggestions. Furthermore, the OP will have already made its choice by
now. Just one last comment: I understand your point of view but I
disagree because the difficulty in providing assistance as the customer
wishes depends on how you have designed the product and not on how you
develop the pieces that compose it. One more thing: you probably don't
care anymore, but people have taught me that if you give a customer
something, you can't sell it to him anymore.

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


Re: Control stript which is runing in background.

2021-01-01 Thread jak

Il 01/01/2021 04:14, 2qdxy4rzwzuui...@potatochowder.com ha scritto:

On 2021-01-01 at 03:43:43 +0100,
Regarding "Re: Control stript which is runing in background.,"
jak  wrote:


Il 01/01/2021 01:43, Cameron Simpson ha scritto:

On 01Jan2021 01:21, jak  wrote:

Il 01/01/2021 00:58, 2qdxy4rzwzuui...@potatochowder.com ha scritto:

Most of the time, I have several shells open, often with their own
background jobs running.  Limiting write permission on the pipe to "me"
wouldn't prevent concurrent access.


This is true but there would be no difference if this happened through
a socket.


Accessing a socket makes a distinct separate data connection - other
openers do not conflict with it. That's the critical difference between
a socket and a pipe in terms of functionality, and why sockets have a
connect/accept step.

Cheers,
Cameron Simpson 



Maybe the fact that I'm not English and I don't know the language well
doesn't allow me to express myself clearly. Try it one more time:
The OP would like to give some command to a script that is running. How?
With a script that sends commands to it. One of the ways, as mentioned,
is by using a mini socket server. Given the needs of the OP and the fact
that sockets are a limited resource in a system, I took the liberty of
proposing a simple alternative: using a named pipe, also because, IMO,
sockets, in this case, are an overkill. with a few lines of code in a
thread in the running script they can allow it to receive commands:
#-
import os, errno

fnpipe = 'cmdpipe'

try:
 os.mkfifo(fnpipe)
except OSError as e:
 if e.errno != errno.EEXIST:
 raise
while True:
 with open(fnpipe, 'rt', 1) as fifo:
 for line in fifo:
 print(line, ends='')
#-

Running the command:

$ cat bible.txt > cmdpipe & cat bible.txt > cmdpipe & cat bible.txt >
cmdpipe

the three texts do not mix. IMO, the OP should be enough. after that, I
know that a pipe is more like a queue than a soket and in this case a
socket, IMO, is wasted.


Only the OP knows for sure.  :-)   Does the server send data back to the
client that made the request?  Are the requests (and the responses, if
any) small enough to be sent/received atomically?  *If* the answers are
no and yes, then you're right, a named pipe would be good enough.  If
not, then a socket *might* be better.  Until the OP clarifies, we can't
tell.


greetings, hoping to have been clearer than before.


I think you were clear enough before, but you may not have considered
things the OP did not specify.  One of the hardest parts of software
development is understanding and specifying the actual problem to be
solved.  I've never done that in a second language (I'm nowhere near
fluent in anything other than English); I can only imagine the extra
layers of complexity.



This is the OP's initial request:

Hi. 
I would like to make something like this:

A python script would run headlessly in the background.
I would like to control the script from the command line using other python 
scripts or from the python shell.
From time to time I would ask the main script to create a popup window with an 
image or a plot.
What would be the proper way to approach it. How to make communication between 
two scripts?
Thank you.
Petro.


I have suggested an alternative way for his necessity.
It will be up to him to decide which one he should adopt. That way he
has a choice, at least.
I don't know where you work. I know, however, that in these zones, if
you do more than necessary wasting their time, they will kick you away.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Control stript which is runing in background.

2021-01-01 Thread jak

Il 01/01/2021 06:23, Alan Bawden ha scritto:

jak  writes:

Running the command:

$ cat bible.txt > cmdpipe & cat bible.txt > cmdpipe & cat bible.txt > 
cmdpipe

the three texts do not mix

The three texts do not mix as long at the contents of bible.txt is short
enough (and provided `cat' only calls `write' once).  In the POSIX
specification, the manual page for the `write' system call describes
writing to a pipe or FIFO as follows:

   Write requests of {PIPE_BUF} bytes or less shall not be interleaved
   with data from other processes doing writes on the same pipe.  Writes
   of greater than {PIPE_BUF} bytes may have data interleaved, on
   arbitrary boundaries, with writes by other processes, whether or not
   the O_NONBLOCK flag of the file status flags is set.


Ok. And...
 ...Running the command:

 $ cat bible.txt > cmdpipe & cat bible.txt > cmdpipe & cat 
bible.txt > cmdpipe


 the three texts do not mix
--
https://mail.python.org/mailman/listinfo/python-list


Re: Control stript which is runing in background.

2020-12-31 Thread jak

Il 01/01/2021 01:43, Cameron Simpson ha scritto:

On 01Jan2021 01:21, jak  wrote:

Il 01/01/2021 00:58, 2qdxy4rzwzuui...@potatochowder.com ha scritto:

Most of the time, I have several shells open, often with their own
background jobs running.  Limiting write permission on the pipe to "me"
wouldn't prevent concurrent access.


This is true but there would be no difference if this happened through
a socket.


Accessing a socket makes a distinct separate data connection - other
openers do not conflict with it. That's the critical difference between
a socket and a pipe in terms of functionality, and why sockets have a
connect/accept step.

Cheers,
Cameron Simpson 



Maybe the fact that I'm not English and I don't know the language well
doesn't allow me to express myself clearly. Try it one more time:
The OP would like to give some command to a script that is running. How?
With a script that sends commands to it. One of the ways, as mentioned,
is by using a mini socket server. Given the needs of the OP and the fact
that sockets are a limited resource in a system, I took the liberty of
proposing a simple alternative: using a named pipe, also because, IMO,
sockets, in this case, are an overkill. with a few lines of code in a
thread in the running script they can allow it to receive commands:
#-
import os, errno

fnpipe = 'cmdpipe'

try:
os.mkfifo(fnpipe)
except OSError as e:
if e.errno != errno.EEXIST:
raise
while True:
with open(fnpipe, 'rt', 1) as fifo:
for line in fifo:
print(line, ends='')
#-

Running the command:

$ cat bible.txt > cmdpipe & cat bible.txt > cmdpipe & cat bible.txt > 
cmdpipe


the three texts do not mix. IMO, the OP should be enough. after that, I
know that a pipe is more like a queue than a soket and in this case a
socket, IMO, is wasted.

greetings, hoping to have been clearer than before.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Control stript which is runing in background.

2020-12-31 Thread jak

Il 01/01/2021 00:58, 2qdxy4rzwzuui...@potatochowder.com ha scritto:

Most of the time, I have several shells open, often with their own
background jobs running.  Limiting write permission on the pipe to "me"
wouldn't prevent concurrent access.


This is true but there would be no difference if this happened through a 
socket.

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


Re: Control stript which is runing in background.

2020-12-31 Thread jak
... but this won't be the problem the OP may encounter if its intention 
is to create a script to command another one that is running. It will be 
sufficient to limit the write permissions to the pipe file to himself 
... perhaps protecting the pipe file inside a directory with stickibits set.

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


Re: Control stript which is runing in background.

2020-12-31 Thread jak

Il 31/12/2020 22:43, Cameron Simpson ha scritto:

On 31Dec2020 18:07, jak  wrote:

Il 31/12/2020 11:43, Petro ha scritto:

I would like to make something like this:
A python script would run headlessly in the background.
I would like to control the script from the command line using other python 
scripts or from the python shell.
 From time to time I would ask the main script to create a popup window with an 
image or a plot.
What would be the proper way to approach it. How to make communication between 
two scripts?


using named pipes would be an alternative. A small example that
produces an echo for windows.


A Windows named pipe seems to be more like a UNIX-side "UNIX domain
socket" than a UNIX side "named pipe".


For linux it's simpler using os.mkfifo:


Not really. For Linux (and of course other UNIXen) you really want a
UNIX domain socket, not a a name pipe (as from mkfifo).

The reason is that a socket (and a Windows pipe, from my limited
understanding) creates a new distinct connection when you open it. (The
other end has to accept that connection, at least in UNIX).


-

The problem with a UNIX pipe is that every client (your command line
control script) _share_ the same pipe - if two scripts un at once there
will be a failure. If you contrive some locking scheme then you can
share a named pipe in UNIX because only once client will use the pipe at
a time.



This is not completely true, in fact requests can be queued as they 
would be with sockets and it is their real job. The most important 
difference is that sockets are a limited resource on a system.



Just something to keep in mind.

Cheers,
Cameron Simpson 



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


Re: Control stript which is runing in background.

2020-12-31 Thread jak

Il 31/12/2020 11:43, Petro ha scritto:

Hi.
I would like to make something like this:
A python script would run headlessly in the background.
I would like to control the script from the command line using other python 
scripts or from the python shell.
 From time to time I would ask the main script to create a popup window with an 
image or a plot.
What would be the proper way to approach it. How to make communication between 
two scripts?
Thank you.
Petro.



using named pipes would be an alternative. A small example that produces 
an echo for windows. For linux it's simpler using os.mkfifo:


# -
import win32pipe as wp, win32file as wf

pfile = r'\\.\pipe\mypipe'
how = 0
data: bytes
while True:
pipe = wp.CreateNamedPipe(pfile, wp.PIPE_ACCESS_DUPLEX,
  wp.PIPE_TYPE_BYTE | wp.PIPE_READMODE_BYTE 
| wp.PIPE_WAIT,
  wp.PIPE_UNLIMITED_INSTANCES, 0x, 
0x, 0, None)

if pipe:
wp.ConnectNamedPipe(pipe, None)
while True:
try:
rv, data = wf.ReadFile(pipe, 1024)
except:
wf.CloseHandle(pipe)
break
else:
print(data.decode(), end='')
else:
break
# -

you can pass commands to it this way too:

$> echo "print log" > \\.\pipe\mypipe
--
https://mail.python.org/mailman/listinfo/python-list


Re: Which method to check if string index is queal to character.

2020-12-29 Thread jak

Il 29/12/2020 02:48, Bischoop ha scritto:

On 2020-12-28, Mats Wichmann  wrote:

On 12/28/20 10:46 AM, Marco Sulla wrote:

On Mon, 28 Dec 2020 at 17:37, Bischoop  wrote:


I'd like to check if there's "@" in a string and wondering if any method
is better/safer than others. I was told on one occasion that I should
use is than ==, so how would be on this example.

s = 't...@mail.is'


You could do simply

if "@" in s:

but probably what you really want is a regular expression.



Will add that Yes, you should always validate your inputs, but No, the
presence of an @ sign in a text string is not sufficient to know it's a
valid email address. Unfortunately validating that is hard.



Nah, by saying if is valid I meant exeactly if there's "@", I could add
yet if endswith() but at this point @ is enough.
Yes the only possible way for full validation would be just sending
email and waiting for reply.



you could try this way:

# ---
from dns import resolver as dns

emails=['john@fakeserver.bah',
'john@gmail.com']

for ue in emails:
try:
mxl = dns.resolve(ue.split('@')[1], 'MX')
except:
print(f'{ue}: bad mail server')
else:
if len(mxl) > 0:
print(f'{ue}: valid mail server')
# ---

... so, having verified the sever, you should only worry about the name.
--
https://mail.python.org/mailman/listinfo/python-list


Re: dict.get(key, default) evaluates default even if key exists

2020-12-17 Thread jak

Il 17/12/2020 13:33, Chris Angelico ha scritto:

On Thu, Dec 17, 2020 at 11:16 PM jak  wrote:


Il 17/12/2020 12:40, Peter J. Holzer ha scritto:

On 2020-12-17 12:16:29 +0100, jak wrote:

print(_ if d.get('a', None) is not None else get_default())


That doesn't work:


print(_ if d.get('a', None) is not None else get_default())

Traceback (most recent call last):
File "", line 1, in 
NameError: name '_' is not defined

But this works:


print(_ if (_ := d.get('a', None)) is not None else get_default())

1

(I would prefer ChrisA's solution, though.)

  hp


this one?

""""

D['a'] if 'a' in D else get_default()

ChrisA

""""

This solution search two times same key.


Yes, it does, but hash lookups are pretty fast. Unless your key is
some sort of custom object with a very expensive __hash__ function,
the double lookup isn't going to be too costly. But if that does
bother you, you can write it as a try/except instead.

ChrisA



try/except is a very expensive time. if we have a small dictionary in 
which you do little research, you definitely have reason. I would be 
curious to see the differences by taking times with 50 / 60K records by 
repeating the search 1 or 2 million times with and without try/except 
and also searching 1 or 2 times too.


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


Re: dict.get(key, default) evaluates default even if key exists

2020-12-17 Thread jak

Il 17/12/2020 12:40, Peter J. Holzer ha scritto:

On 2020-12-17 12:16:29 +0100, jak wrote:

print(_ if d.get('a', None) is not None else get_default())


That doesn't work:


print(_ if d.get('a', None) is not None else get_default())

Traceback (most recent call last):
   File "", line 1, in 
NameError: name '_' is not defined

But this works:


print(_ if (_ := d.get('a', None)) is not None else get_default())

1

(I would prefer ChrisA's solution, though.)

 hp



I had already corrected my oversight (jak 12:32):

> print((_ if d.get('a', None) is not None else get_default()))

while your fix works but not correctly because in that way does not 
collect the return code of the function get_default().

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


Re: dict.get(key, default) evaluates default even if key exists

2020-12-17 Thread jak

Il 17/12/2020 12:40, Peter J. Holzer ha scritto:

On 2020-12-17 12:16:29 +0100, jak wrote:

print(_ if d.get('a', None) is not None else get_default())


That doesn't work:


print(_ if d.get('a', None) is not None else get_default())

Traceback (most recent call last):
   File "", line 1, in 
NameError: name '_' is not defined

But this works:


print(_ if (_ := d.get('a', None)) is not None else get_default())

1

(I would prefer ChrisA's solution, though.)

 hp


this one?

""""

D['a'] if 'a' in D else get_default()

ChrisA

""""

This solution search two times same key.
--
https://mail.python.org/mailman/listinfo/python-list


Re: dict.get(key, default) evaluates default even if key exists

2020-12-17 Thread jak

Il 17/12/2020 12:16, jak ha scritto:

Il 15/12/2020 18:07, Mark Polesky ha scritto:

Hi.

# Running this script

D = {'a':1}
def get_default():
 print('Nobody expects this')
 return 0
print(D.get('a', get_default()))

# ...generates this output:

Nobody expects this
1

###

Since I'm brand new to this community, I thought I'd ask here first... 
Is this worthy of a bug report?  This behavior is definitely 
unexpected to me, and I accidentally coded an endless loop in a mutual 
recursion situation because of it.  Calling dict.get.__doc__ only 
gives this short sentence: Return the value for key if key is in the 
dictionary, else default.  Nothing in that docstring suggests that the 
default value is evaluated even if the key exists, and I can't think 
of any good reason to do so.


Am I missing something?

Thanks,
Mark



print(_ if d.get('a', None) is not None else get_default())




ops...

print((_ if d.get('a', None) is not None else get_default()))

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


  1   2   >