Skip to content

Commit

Permalink
updated data store util class
Browse files Browse the repository at this point in the history
  • Loading branch information
rochi88 committed Feb 29, 2024
1 parent 82d4864 commit 55cdfbc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
26 changes: 13 additions & 13 deletions bdshare/util/store.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# -*- coding:utf-8 -*-

import pandas as pd
from pandas import compat
from datetime import datetime
import os


class Store(object):

def __init__(self, data=None, name=None, path=None):
Expand All @@ -15,20 +14,21 @@ def __init__(self, data=None, name=None, path=None):
self.name = name
self.path = path

def save_as(self, name, path, to='csv'):
if name is None:
name = self.name
if path is None:
path = self.path
def save(self, to='csv'):
if self.name is None:
self.name = f'{datetime.now().strftime("%Y%m%d-%H%M%S")}'

file_path = '%s%s%s.%s'
if isinstance(name, compat.string_types) and name is not '':
if (path is None) or (path == ''):
file_path = '.'.join([name, to])
if isinstance(self.name, str) and self.name is not '':
if (self.path is None) or (self.path == ''):
file_path = '.'.join([self.name, to])
self.data.to_csv(file_path, index=False)
else:
try:
if os.path.exists(path) is False:
os.mkdir(path)
file_path = file_path%(path, '/', name, to)
if os.path.exists(self.path) is False:
os.mkdir(self.path)
file_path = file_path%(self.path, '', self.name, to)
self.data.to_csv(file_path, index=False)
except:
pass

Expand Down
5 changes: 3 additions & 2 deletions tests/test_dsex.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
from bdshare import get_dsex_data, Store

df = get_dsex_data()
Store(data=df)
print(df.to_string())

print(df.to_string())
Store(data=df).save()

0 comments on commit 55cdfbc

Please sign in to comment.