Skip to content

Commit

Permalink
Add timedelta2string
Browse files Browse the repository at this point in the history
  • Loading branch information
penut85420 committed Jan 6, 2021
1 parent 9e45551 commit 5b00bd3
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 348 deletions.
42 changes: 21 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
MIT License

Copyright (c) 2020 The Penut Python Package

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT License
Copyright (c) 2020 The Penut Python Package
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
77 changes: 34 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
# Penut

## Introduction
+ [GitHub](https://github.com/penut85420/penut)
[![GitHub release](https://img.shields.io/github/v/release/penut85420/penut.svg)](https://github.com/penut85420/penut/releases)
[![GitHub release date](https://img.shields.io/github/release-date/penut85420/penut.svg)](https://github.com/penut85420/penut/releases)
[![GitHub issues](https://img.shields.io/github/issues/penut85420/penut.svg)](https://github.com/penut85420/penut/issues)
+ PyPI
[![PyPI version](https://img.shields.io/pypi/v/penut.svg?maxAge=3600)](https://pypi.org/project/penut)
[![PyPI license](https://img.shields.io/pypi/l/penut.svg?maxAge=3600)](https://github.com/ckiplab/penut/blob/master/LICENSE)
[![PyPI python](https://img.shields.io/pypi/pyversions/penut.svg?maxAge=3600)](https://pypi.org/project/penut)
+ This package is a collection of my useful functions.
+ Including useful IO operation and NLP utils.

## Installation
+ You can install this package through pip:
```
pip install penut
```

## Usage
+ You can easily load or dump and json/pkl/csv/npy file:
```python
import penut.io as pio

data = pio.load('./data.json')
data = pio.load('./data.pkl')
data = pio.load('./data.csv')
data = pio.load('./data.npy')

pio.dump(data, './data.json')
pio.dump(data, './data.pkl')
pio.dump(data, './data.csv')
pio.dump(data, './data.npy')
```
+ You can easily measure the execute time of code:
```python
from penut.uitls import TimeCost

with TimeCost('Sleep Time'):
import time
time.sleep(1)
# Output: Sleep Time: 1.000262s
# Penut

## Introduction
+ This package is a collection of my useful functions, including some useful IO operations.

## Installation
+ You can install this package through pip:
```
pip install penut
```

## Usage
+ You can easily load or dump and json/pkl/csv/npy file:
```python
import penut.io as pio

data = pio.load('./data.json')
data = pio.load('./data.pkl')
data = pio.load('./data.csv')
data = pio.load('./data.npy')

pio.dump(data, './data.json')
pio.dump(data, './data.pkl')
pio.dump(data, './data.csv')
pio.dump(data, './data.npy')
```
+ You can easily measure the execution time of code:
```python
from penut.uitls import TimeCost

with TimeCost('Sleep Time'):
import time
time.sleep(1)
# Output: Sleep Time: 1.000262s
```
13 changes: 13 additions & 0 deletions mk_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Upload to PyPI
# 1. Delete all the file in dist if exists.
# 2. Build dist by `python setup.py sdist bdist_wheel`.
# 3. Upload by `twine upload dist/*`.

python -m pip install -U setuptools wheel twine
rm -rf build dist penut_utils.egg-info
python setup.py sdist bdist_wheel
python -m twine upload --repository testpypi dist/*
# python -m twine upload dist/*
# python -m pip install -i https://test.pypi.org/simple/ penut_utils==0.0.1
1 change: 1 addition & 0 deletions penut/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .utils import TimeCost, walk_dir, td2s, timedelta2string
147 changes: 66 additions & 81 deletions penut/io.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,66 @@
import os
import json
import numpy as np
import pandas as pd
import pickle as pk

def dump_json(obj, fpath, encoding='UTF-8', is_ascii=False, indent=4):
with open(fpath, 'w', encoding=encoding) as f:
json.dump(obj, f, ensure_ascii=is_ascii, indent=indent)

def load_json(fpath, encoding='UTF-8'):
with open(fpath, 'r', encoding=encoding) as f:
return json.load(f)

def dump_pkl(obj, fpath):
with open(fpath, 'wb') as f:
pk.dump(obj, f)

def load_pkl(fpath):
with open(fpath, 'rb') as f:
return pk.load(f)

def dump_npy(obj, fpath):
np.save(fpath, obj)

def load_npy(fpath):
return np.load(fpath)

def dump_csv(obj, fpath, header=False, index=False):
pd.DataFrame(obj).to_csv(fpath, header=header, index=index)

def load_csv(fpath, header=None):
pd.read_csv(fpath, header=header)

def load(fpath, **kwargs):
load_dict = {
'.json': load_json,
'.pkl': load_pkl,
'.npy': load_npy,
'.csv': load_csv
}
_, ext = os.path.splitext(fpath)
return load_dict[ext](fpath)

def dump(obj, fpath, **kwargs):
dump_dict = {
'.json': dump_json,
'.pkl': dump_pkl,
'.npy': dump_npy,
'.csv': dump_csv
}
_, ext = os.path.splitext(fpath)
return dump_dict[ext](obj, fpath, **kwargs)

def mkdir(dir_path):
os.makedirs(dir_path, exist_ok=True)

if __name__ == "__main__":
d = {'Name': 'Testing', 'Arr': [1, 2, 3], '測試': '項目'}
dump(d, 'a.json', is_ascii=True)
# dump_json(d, './a.json')
# dd = load_json('./a.json')
# assert d == dd

# dump_pkl(d, './a.pkl')
# dd = load_pkl('./a.pkl')
# assert d == dd

# dump(d, 'a.json')
# dump(d, 'a.pkl')
# dump(d, 'a.csv')
# a = load('a.json')
# b = load('a.pkl')
# c = load('a.csv')
# assert a == b

# print('Success!')

os.remove('a.json')
# os.remove('a.pkl')
# os.remove('a.csv')
import os
import json

def dump_json(obj, fpath, encoding='UTF-8', is_ascii=False, indent=4):
with open(fpath, 'w', encoding=encoding) as f:
json.dump(obj, f, ensure_ascii=is_ascii, indent=indent)

def load_json(fpath, encoding='UTF-8'):
with open(fpath, 'r', encoding=encoding) as f:
return json.load(f)

def dump_pkl(obj, fpath):
import pickle as pk
with open(fpath, 'wb') as f:
pk.dump(obj, f)

def load_pkl(fpath):
import pickle as pk
with open(fpath, 'rb') as f:
return pk.load(f)

def dump_npy(obj, fpath):
import numpy as np
np.save(fpath, obj)

def load_npy(fpath):
import numpy as np
return np.load(fpath)

def dump_csv(obj, fpath, header=False, index=False):
pd = load_pandas()
pd.DataFrame(obj).to_csv(fpath, header=header, index=index)

def load_csv(fpath, header=None):
pd = load_pandas()
return pd.read_csv(fpath, header=header)

def load_pandas():
try:
import pandas as pd
return pd
except ModuleNotFoundError:
raise RuntimeError('Pandas is not installed!')

def load(fpath, **kwargs):
load_dict = {
'.json': load_json,
'.pkl': load_pkl,
'.npy': load_npy,
'.csv': load_csv
}
_, ext = os.path.splitext(fpath)
return load_dict[ext](fpath, **kwargs)

def dump(obj, fpath, **kwargs):
dump_dict = {
'.json': dump_json,
'.pkl': dump_pkl,
'.npy': dump_npy,
'.csv': dump_csv
}
_, ext = os.path.splitext(fpath)
return dump_dict[ext](obj, fpath, **kwargs)

def mkdir(dir_path):
os.makedirs(dir_path, exist_ok=True)
90 changes: 0 additions & 90 deletions penut/nlp/corpus.py

This file was deleted.

13 changes: 0 additions & 13 deletions penut/nlp/segmentation.py

This file was deleted.

Loading

0 comments on commit 5b00bd3

Please sign in to comment.