Re: In code, list.clear doesn't throw error - it's just ignored

2022-11-25 Thread Dan Stromberg
On Sun, Nov 13, 2022 at 4:45 PM DFS  wrote:

> In code, list.clear is just ignored.
> At the terminal, list.clear shows
> 
>
>
> in code:
> x = [1,2,3]
> x.clear
> print(len(x))
> 3
>
> at terminal:
> x = [1,2,3]
> x.clear
> 
> print(len(x))
> 3
>
>
> Caused me an hour of frustration before I noticed list.clear() was what
> I needed.
>
> x = [1,2,3]
> x.clear()
> print(len(x))
> 0
>
> --
> https://mail.python.org/mailman/listinfo/python-list



I'm not 100% sanguine about properties, but the fact is they are part of
the language:

$ cat p
below cmd output started 2022 Fri Nov 25 07:54:42 AM PST
#!/usr/bin/env python3

class P:
def __init__(self):
self.count = 0

@property
def increment(self):
self.count += 1

def result(self):
return self.count


p = P()
p.increment
p.increment
print(p.result())
above cmd output done2022 Fri Nov 25 07:54:42 AM PST
dstromberg@tp-mini-c:~/src/experiments/property x86_64-pc-linux-gnu 2670

$ ./p
below cmd output started 2022 Fri Nov 25 07:54:44 AM PST
2

As you can see, if the interpreter refused to do something with p.increment
because it has no parens, the meaning of this code would change
significantly.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Preprocessing not quite fixed-width file before parsing

2022-11-25 Thread Loris Bennett
Thomas Passin  writes:

> On 11/24/2022 9:06 AM, Loris Bennett wrote:
>> Thomas Passin  writes:
>> 
>>> On 11/23/2022 11:00 AM, Loris Bennett wrote:
 Hi,
 I am using pandas to parse a file with the following structure:
 Name   filesettype KB  quota  limit
 in_doubtgrace |files   quotalimit in_doubtgrace
 shortname  sharedhome USR14097664  524288000  545259520  0 
 none |   107110   000 none
 gracedays  sharedhome USR   774858944  524288000  775946240  0 
   5 days |  1115717   000 none
 nametoolong sharedhome USR27418496  524288000  545259520  
 0 none |11581   000 none
 I was initially able to use
 df = pandas.read_csv(file_name, delimiter=r"\s+")
 because all the values for 'grace' were 'none'.  Now, however,
 non-"none" values have appeared and this fails.
 I can't use
 pandas.read_fwf
 even with an explicit colspec, because the names in the first column
 which are too long for the column will displace the rest of the data to
 the right.
 The report which produces the file could in fact also generate a
 properly delimited CSV file, but I have a lot of historical data in the
 readable but poorly parsable format above that I need to deal with.
 If I were doing something similar in the shell, I would just pipe
 the
 file through sed or something to replace '5 days' with, say '5_days'.
 How could I achieve a similar sort of preprocessing in Python, ideally
 without having to generate a lot of temporary files?
>>>
>>> This is really annoying, isn't it?  A space-separated line with spaces
>>> in data entries.   If the example you give is typical, I don't think
>>> there is a general solution.  If you know there are only certain
>>> values like that, then you could do a search-and-replace for them in
>>> Python just like the example you gave for "5 days".
>>>
>>> If you know that the field that might contain entries with spaces is
>>> the same one, e.g., the one just before the "|" marker, you could make
>>> use of that. But it could be tricky.
>>>
>>> I don't know how many files like this you will need to process, nor
>>> how many rows they might contain. If I were to do tackle this job, I
>>> would probably do some quality checking first.  Using this example
>>> file, figure out how many fields there are supposed to be.  First,
>>> split the file into lines:
>>>
>>> with open("filename") as f:
>>>  lines = f.readlines()
>>>
>>> # Check space-separated fields defined in first row:
>>> fields = lines[0].split()
>>> num_fields = len(fields)
>>> print(num_fields)   # e.g., 100)
>>>
>>> # Find lines that have the wrong number of fields
>>> bad_lines = []
>>> for line in lines:
>>> fields = line.split()
>>> if len(fields) != num_fields:
>>>   bad_lines.append(line)
>>>
>>> print(len(bad_lines))
>>>
>>> # Inspect a sample
>>> for line in bad_lines[:10]:
>>>  print(line)
>>>
>>> This will give you an idea of how many problems lines there are, and
>>> if they can all be fixed by a simple replacement.  If they can and
>>> this is the only file you need to handle, just fix it up and run it.
>>> I would replace the spaces with tabs or commas.  Splitting a line on
>>> spaces (split()) takes care of the issue of having a variable number
>>> of spaces, so that's easy enough.
>>>
>>> If you will need to handle many files, and you can automate the fixes
>>> - possibly with a regular expression - then you should preprocess each
>>> file before giving it to pandas.  Something like this:
>>>
>>> def fix_line(line):
>>> """Test line for field errors and fix errors if any."""
>>> # 
>>> return fixed
>>>
>>> # For each file
>>> with open("filename") as f:
>>>  lines = f.readlines()
>>>
>>> fixed_lines = []
>>> for line in lines:
>>>  fixed = fix_line(line)
>>>  fields = fixed.split()
>>>  tabified = '\t'.join(fields) # Could be done by fix_line()
>>>  fixed_lines.append(tabified)
>>>
>>> # Now use an IOString to feed the file to pandas
>>> # From memory, some details may not be right
>>> f = IOString()
>>> f.writelines(fixed_lines)
>>>
>>> # Give f to pandas as if it were an external file
>>> # ...
>>>
>> Thanks to both Gerard and Thomas for the pointer to IOString.  I
>> ended up
>> just reading the file line-by-line, using a regex to replace
>>'  |'
>> with
>>' |'
>> and writing the new lines to an IOString, which I then passed to
>> pandas.read_csv.
>> The wrapper approach looks interesting, but it looks like I need to
>> read
>> up more on contexts before adding that to my own code, otherwise I may
>> not understand it in a month's time.
>
> Glad that IOString works for you here. I seem to remember that after
> writing to the IOString, you have to seek to 0 before reading from
> it

Re: is mypy failing here

2022-11-25 Thread Robin Becker

On 24/11/2022 14:10, Thomas Passin wrote:
.


C:\temp\python>py -V
Python 3.10.4

C:\temp\python>py tdc.py
DC(a=, b='B')

C:\temp\python>mypy tdc.py
tdc.py:10: error: Argument 1 to "DC" has incompatible type "Type[DC]"; expected 
"str"  [arg-type]
Found 1 error in 1 file (checked 1 source file)


Forgot the mypy version:

C:\Users\tom>mypy --version
mypy 0.910

interesting; I'm on archlinux and neither the system python 3.10.8 / mypy 0.982 gives an error. I did try running in my 
self build 3.10.8 with latest mypy 0.991 and mypy 0.910 and I still don't get an error.


I'll break out the windows 10 laptop and see what happens there.

You ran with the py runner. I wonder if that does something special.
--
Robin Becker
--
https://mail.python.org/mailman/listinfo/python-list


Re: is mypy failing here

2022-11-25 Thread Robin Becker

On 24/11/2022 13:50, Kirill Ratkin via Python-list wrote:

mypy --strict gives you detail info.

Thanks Kirill,

it seems --strict does find the errors. One of those is that on line 9 I have 
to add a return type ie

def main() -> None:
.

if that is added then mypy without --strict also finds the real typing error.

So it seems the tool fails in the simplest cases if you forget some typing.

Interesting that it works in windows without --strict though.
--
Robin Becker
--
https://mail.python.org/mailman/listinfo/python-list


Re: is mypy failing here

2022-11-25 Thread Thomas Passin

On 11/25/2022 12:00 PM, Robin Becker wrote:

On 24/11/2022 14:10, Thomas Passin wrote:
.


C:\temp\python>py -V
Python 3.10.4

C:\temp\python>py tdc.py
DC(a=, b='B')

C:\temp\python>mypy tdc.py
tdc.py:10: error: Argument 1 to "DC" has incompatible type 
"Type[DC]"; expected "str"  [arg-type]

Found 1 error in 1 file (checked 1 source file)


Forgot the mypy version:

C:\Users\tom>mypy --version
mypy 0.910

interesting; I'm on archlinux and neither the system python 3.10.8 / 
mypy 0.982 gives an error. I did try running in my self build 3.10.8 
with latest mypy 0.991 and mypy 0.910 and I still don't get an error.


I'll break out the windows 10 laptop and see what happens there.

You ran with the py runner. I wonder if that does something special.
--
Robin Becker


I've never noticed any difference running anything else with "py" 
instead of "python3" or whatever would have to be typed to get another 
version.


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