paddlenlp

Форк
0
74 строки · 2.4 Кб
1
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

15
import csv
16
import os
17
import tarfile
18
import zipfile
19
from typing import Callable, Iterable
20

21
from ppfleetx.distributed.apis import env
22

23

24
@env.work_at_local_rank0
25
def unzip(zip_path, mode="r", out_dir=None, delete=False):
26
    with zipfile.ZipFile(zip_path, mode) as zip_ref:
27
        zip_ref.extractall(out_dir)
28

29
    if delete:
30
        os.remove(zip_path)
31

32

33
@env.work_at_local_rank0
34
def untar(tar_path, mode="r:gz", out_dir=None, delete=False):
35
    try:
36
        with tarfile.open(tar_path, "r:gz") as f:
37
            f.extractall(out_dir)
38
    finally:
39
        if delete:
40
            os.remove(tar_path)
41

42

43
def parse_csv(
44
    path, skip_lines=0, delimiter=" ", quotechar="|", quoting=csv.QUOTE_NONE, map_funcs=None, filter_funcs=None
45
):
46

47
    with open(path, newline="") as csvfile:
48
        data = []
49
        spamreader = csv.reader(csvfile, delimiter=delimiter, quotechar=quotechar, quoting=quoting)
50
        for idx, row in enumerate(spamreader):
51
            if idx < skip_lines:
52
                continue
53
            filter_flag = True
54
            if filter_funcs is not None:
55
                if isinstance(filter_funcs, Iterable):
56
                    for func in filter_funcs:
57
                        filter_flag = func(row)
58
                        if filter_flag is False:
59
                            break
60
                else:
61
                    assert isinstance(filter_funcs, Callable)
62
                    filter_flag = filter_funcs(row)
63
            if filter_flag is False:
64
                continue
65

66
            if map_funcs is not None:
67
                if isinstance(map_funcs, Iterable):
68
                    for func in map_funcs:
69
                        row = func(row)
70
                else:
71
                    assert isinstance(map_funcs, Callable)
72
                    row = map_funcs(row)
73
            data.append(row)
74
        return data
75

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.