Skip to content

Commit

Permalink
to_type() renamed to str_to_type()
Browse files Browse the repository at this point in the history
  • Loading branch information
mezantrop committed Jul 3, 2024
1 parent 14088d0 commit 49122ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ Controls what arguments are available to `eval()` function

### Standalone functions

| Name | Status | Description |
|--------------|---------|------------------------------------------------------------|
| `open_file` | ☑ | Open a file |
| `close_file` | ☑ | Close a file |
| `read_json` | ☑ | Read `JSON` file |
| `read_csv` | ☑ | Read `CSV` file |
| `read_xml` | ☐ | Read `XML`. NB: Do we need XML support? |
| `to_type` | ☑ | Convert a `str` to a proper type: `int`, `float` or `bool` |
| Name | Status | Description |
|---------------|---------|------------------------------------------------------------|
| `open_file` | ☑ | Open a file |
| `close_file` | ☑ | Close a file |
| `read_json` | ☑ | Read `JSON` file |
| `read_csv` | ☑ | Read `CSV` file |
| `read_xml` | ☐ | Read `XML`. NB: Do we need XML support? |
| `str_to_type` | ☑ | Convert a `str` to a proper type: `int`, `float` or `bool` |

#### WARNING

Expand Down
10 changes: 6 additions & 4 deletions tsqlike/tsqlike.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ def close_file(file):
if file and file is not sys.stdout and file is not sys.stdin:
file.close()


# ------------------------------------------------------------------------------------------------ #
def to_type(s):
def str_to_type(s):
""" Convert string s to a proper type: int, float or boolean """

if s in ('True', 'true', 'False', 'false'): # to boolean
Expand All @@ -104,6 +105,7 @@ def to_type(s):
except (ValueError, TypeError):
return s # no conversion possible -> string


# ------------------------------------------------------------------------------------------------ #
def read_csv(in_file=None, encoding=None, newline='', name='', detect_types=False,
dialect='excel', **fmtparams):
Expand Down Expand Up @@ -278,7 +280,7 @@ def import_list_dicts(self, data, name=None, detect_types=False):
if TNAME_COLUMN_DELIMITER not in str(f) else f for f in (data[0].keys())]

self.table = [list(r.values()) for r in data] if not detect_types else [
[to_type(v) for v in r.values()] for r in data]
[str_to_type(v) for v in r.values()] for r in data]

else:
raise ValueError('[email protected]_list_dicts: Unexpected data format')
Expand Down Expand Up @@ -307,7 +309,7 @@ def import_dict_lists(self, data, name=None, detect_types=False):

for c, f in enumerate(data.keys()):
for r, v in enumerate(data[f]):
self.table[r][c] = v if not detect_types else to_type(v)
self.table[r][c] = v if not detect_types else str_to_type(v)
self._redimension()
else:
raise ValueError('[email protected]_dict_lists: Unexpected data format')
Expand Down Expand Up @@ -336,7 +338,7 @@ def import_list_lists(self, data, header=True, name=None, detect_types=False):
if not detect_types:
self.table = data[1:] if header else data
else:
self.table = [[to_type(v) for v in r] for r in data[1:]]
self.table = [[str_to_type(v) for v in r] for r in data[1:]]

self._redimension()

Expand Down

0 comments on commit 49122ea

Please sign in to comment.