Re: ast.parse, ast.dump, but with comment preservation?

2021-12-16 Thread Barry


> On 16 Dec 2021, at 03:49, samue...@gmail.com  wrote:
> 
> I wrote a little open-source tool to expose internal constructs in OpenAPI. 
> Along the way, I added related functionality to:
> - Generate/update a function prototype to/from a class
> - JSON schema
> - Automatically add type annotations to all function arguments, class 
> attributes, declarations, and assignments
> 
> alongside a bunch of other features. All implemented using just the builtin 
> modules (plus astor on Python < 3.9; and optionally black).
> 
> Now I'm almost at the point where I can run it—without issue—against, e.g., 
> the entire TensorFlow codebase. Unfortunately this is causing huge `diff`s 
> because the comments aren't preserved (and there are some whitespace issues… 
> but I should be able to resolve the latter).
> 
> Is the only viable solution available to rewrite around redbaron | libcst? - 
> I don't need to parse the comments just dump them out unedited whence they're 
> found…
> 
> Thanks for any suggestions

Have a look at the code that is used by https://github.com/asottile/pyupgrade
There are a couple of libraries that it uses that does what I think you want to 
do.

Barry

> 
> PS: Library is https://github.com/SamuelMarks/cdd-python (might relicense 
> with CC0… anyway too early for others to use; wait for the 0.1.0 release ;])
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ast.parse, ast.dump, but with comment preservation?

2021-12-16 Thread lucas

Hi !

Maybe RedBaron may help you ?

https://github.com/PyCQA/redbaron

IIRC, it aims to conserve the exact same representation of the source 
code, including comments and empty lines.


--lucas


On 16/12/2021 04:37, samue...@gmail.com wrote:

I wrote a little open-source tool to expose internal constructs in OpenAPI. 
Along the way, I added related functionality to:
- Generate/update a function prototype to/from a class
- JSON schema
- Automatically add type annotations to all function arguments, class 
attributes, declarations, and assignments

alongside a bunch of other features. All implemented using just the builtin 
modules (plus astor on Python < 3.9; and optionally black).

Now I'm almost at the point where I can run it—without issue—against, e.g., the 
entire TensorFlow codebase. Unfortunately this is causing huge `diff`s because 
the comments aren't preserved (and there are some whitespace issues… but I 
should be able to resolve the latter).

Is the only viable solution available to rewrite around redbaron | libcst? - I 
don't need to parse the comments just dump them out unedited whence they're 
found…

Thanks for any suggestions

PS: Library is https://github.com/SamuelMarks/cdd-python (might relicense with 
CC0… anyway too early for others to use; wait for the 0.1.0 release ;])

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


Update a specific element in all a list of N lists

2021-12-16 Thread hanan lamaazi
Dear All,

I really need your assistance,

I have a dataset with 1005000 rows and 25 columns,

The main column that I repeatedly use are Time, ID, and Reputation

First I sliced the data based on the time, and I append the sliced data in
a list called "df_list". So I get 201 lists with 25 columns

The main code is starting for here:

for elem in df_list:

{do something.}

{Here I'm trying to calculate the outliers}

Out.append(outliers)

Now my problem is that I need to locate those outliers in the df_list and
then update another column with is the "Reputation"

Note that the there is a duplicated IDs but at different time slot

example is ID = 1 is outliers, I need to select all ID = 1 in the list and
update their reputation column

I tried those solutions:
1)

grp = data11.groupby(['ID'])
for i in GlobalNotOutliers.ID:
data11.loc[grp.get_group(i).index, 'Reput'] += 1

for j in GlobalOutliers.ID:
data11.loc[grp.get_group(j).index, 'Reput'] -= 1


It works for a dataframe but not for a list

2)

for elem in df_list:

elem.loc[elem['ID'].isin(Outlier['ID'])]


It doesn't select the right IDs, it gives the whole values in elem

3) Here I set the index using IDs:

for i in Outlier.index:
for elem in df_list:
print(elem.Reput)
if i in elem.index:
# elem.loc[elem[i] , 'Reput'] += 1
m = elem.iloc[i, :]
print(m)


It gives this error:

IndexError: single positional indexer is out-of-bounds


I'm greatly thankful to anyone who can help me,
-- 
https://mail.python.org/mailman/listinfo/python-list