Newline (NuBe Question)

2023-11-14 Thread Grizzy Adams via Python-list
Hi & thanks for patience with what could be simple to you

Have this (from an online "classes" tutorial) 

--- Start Code Snippit  ---

students = []
grades = []
for s in geographyClass:
students.append(geographyStudent(s))
for s in students:
grades.append(s.school)
grades.append(s.name)
grades.append(s.finalGrade())
if s.finalGrade()>82:
grades.append("Pass")
else:
grades.append("Fail")
print(grades)

--- End Code Snippit  ---

I have extended (from tutorial) it a bit, I would really like to have a newline 

at end of each record, I have searched (and tested) but cant get "\n" to give a 

newline, I get "Mydata\n"

Do I need to replace "append" with "print", or is there a way to get the 
newline in as I append to list?

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


Re: Code improvement question

2023-11-14 Thread MRAB via Python-list

On 2023-11-15 03:41, Mike Dewhirst via Python-list wrote:

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.


\b  Word boundary
[0-9]{2,7}  2..7 digits
-   "-"
[0-9]{2}2 digits
-   "-"
[0-9]{2}2 digits
\b  Word boundary

The "word boundary" thing is to stop it matching where there are letters 
or digits right next to the digits.


For example, if the text contained, say, "123456789-12-1234", you 
wouldn't want it to match because there are more than 7 digits at the 
start and more than 2 digits at the end.


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


Re: Code improvement question

2023-11-14 Thread Mike Dewhirst via Python-list

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
--
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-14 Thread Peter J. Holzer via Python-list
On 2023-11-14 00:11:30 +0200, Dom Grigonis via Python-list wrote:
> Benchmarks:
> test1 = [False] * 100 + [True] * 2
> test2 = [True] * 100 + [False] * 2
> 
> TIMER.repeat([
> lambda: xor(test1), # 0.0168
> lambda: xor(test2), # 0.0172
> lambda: xor_ss(test1),  # 0.1392
> lambda: xor_ss(test2),  # 0.0084
> lambda: xor_new(test1), # 0.0116
> lambda: xor_new(test2), # 0.0074
> lambda: all(test1), # 0.0016
> lambda: all(test2)  # 0.0046
> ])
> Your first function is fairly slow.
> Second one deals with short-circuiting, but is super slow on full search.
> 
> `xor_new` is the best what I could achieve using python builtins.
> 
> But builtin `all` has the best performance.

One question worth asking is if a list of bool is the best data
structure for the job. This is essentially a bitmap, and a bitmap is
equivalent to a set of integers. len(s) == 1 is also a fairly quick
operation if s is small. On my system, len(test1s) == 1 (where test1s is
{100, 101}) is about as fast as all(test1) and len(test2s) == 1 (where
test2s is set(range(100))) is about twice as fast as all(test2).

If you are willing to stray from the standard library, you could e.g.
use pyroaring instead of sets: This is about as fast as all(test1)
whether there are two bits set or a hundred.

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Code improvement question

2023-11-14 Thread MRAB via Python-list

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)

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


Code improvement question

2023-11-14 Thread Mike Dewhirst via Python-list

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

Cheers

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


Re: No current way to just compile flet code into truly native packages for smart phones, etc.?

2023-11-14 Thread Jacob Kruger via Python-list

Yup.


Also checked out beeware - which also offers cross-platform compilation 
of same code - but, one minor issue there first time tried it out was 
working with additional/external modules, and, packaging resources.



Plus, while it includes it's own form of LBC GUI interface - toga - the 
one included here in flet seemed 'nicer':


https://beeware.org/project/projects/libraries/toga/


But, haven't taken time to really take it much further than just a bit 
of playing around so far.



Jacob Kruger
+2782 413 4791
"Resistance is futile!...Acceptance is versatile..."


On 2023/11/13 19:47, Barry wrote:



On 13 Nov 2023, at 17:21, Jacob Kruger via Python-list  
wrote:

Had a look at the following bit of introduction to using python and flet to 
build cross-platform flutter-based apps using same python code, and, while it 
seems to work alright if tell it to run as under GUI here on windows desktop, 
and, while can get it to fire up PWA version as well, that's not really 
stand-alone since will still require code to be running in background, and, in 
terms of, for example, android, it seems like it will need to then be running 
via the native android flet interpreter for it to work as such?


https://flet.dev/docs/


Flet PWA deployment 


flet looks interesting. It seems from the road map there is lots missing today.
Also the people that know flet are on discord. You may find someone here.

Barry


--

Jacob Kruger
+2782 413 4791
"Resistance is futile!...Acceptance is versatile..."

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


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