Skip to main content

Simple File Configuration Parse is a simple library to import custom config/data files for your python program or script, and export any data to disk simply!

Project description

sfcparse

Simple File Configuration Parse is a simple library to import custom config/data files for your python program or script, and export any data to disk simply!

See "Updates" section on bottom for the latest version information

Functionality-QA-Tests

Tutorial Videos:

Quick Start Tutorial: YouTube Video

Full Training Series: YouTube Playlist

See Usage sections below for more detail

Install

pip install sfcparse

Intro

This library allows you to import custom config/data/save files for your python program or script on a plain text file with any file extension. It can be used to easily export any data to a file as well. Also contains a feature for easily formatting data types for clean multiline output when exporting data to files, and importing is more secure than importing your values from .py files.

Goal for the Project:

To provide an easy alternative to using config/data/save files in an attempt to customize importing python data and saving any data to files for your projects simple. This also gives you the universal freedom to use any file extension or any made up file type you want, and ensures importing from your file to retrieve values is more secure than importing your values from .py files.

Functionality:

Importing (python types only)

Imports stored variable names and their assigned values from any text file.

Returns a class with attributes from the file keeping python's natural recognition of data types, including comment lines being ignored.

Accepted Imported Data Types str, int, float, bool, list, dict, tuple, set, nonetype, bytes

Exporting/Appending

It simply sends string data to a file. It may be used for any string data file output.

Formatting

Simply formats any dict, list, set, or tuple to a clean multiline structure, and return it as str. It can be exported to a file neatly.


Usage: Importing

Imports Pythonic Data Types from your Text File

Example test file Python data inside a file called settings.config.

# Comment for Single Line Data
saved_str = 'John Doe'
saved_int = 1024
saved_float = 128.75
saved_bool = False
saved_list = [1, 2, 3]
saved_dict = {'key1': 1, 'key2': 2}
saved_tuple = (1,2,3)
saved_set = { 1, 2, 3 }
 
 
# Comment for Multiline Data Sets
saved_data_multiline_dict = {
    'key1' : 'value 1',
    'key2' : 'value 2',
    'key3' : 'value 3'
}
 
saved_data_multiline_tuple = (
    1,
    2,
    3
)

Importing the above file into your python project and accessing the data

import sfcparse
 
settings_file = sfcparse.importfile('settings.config')
 
# Access any values imported
settings_file.saved_str
settings_file.saved_list
settings_file.saved_data_multiline_dict['key1']
...

That's it!


Usage: Exporting - Single Line Values

Writes/Overwrites a New File

Exporting data to example file settings.config.

Note these are just examples to build your data to export.

import sfcparse
 
export_file = sfcparse.exportfile
 
# Single Line Values
string_to_save = 'John Doe'
number_to_save = 64
tuple_to_save = (1,2,3)
 
 
# Final Data to Export
data_to_save = f'''
# Comment Line
string_to_save = '{string_to_save}'
number_to_save = {number_to_save}
tuple_to_save = {tuple_to_save}
'''
 
export_file('settings.config', data_to_save)

This will be the expected output stored to the file

# Comment Line
string_to_save = 'John Doe'
number_to_save = 64
tuple_to_save = (1,2,3)

That's it!


Usage: Exporting - Multiline Values

Writes/Overwrites a New File

Exporting data to example file settings.config.

Note these are just examples to build your data to export.

import sfcparse
 
export_file = sfcparse.exportfile
 
string_to_save = 'John Doe'
number_to_save = 64
 
# Multiline Values
dict_to_save = """{
 
    'key1' : 'value1',
    'key2' : 'value2'    
}
"""
 
list_to_save = """[
 
    1,
    2,
    3
]
"""
 
# Dict with Vars - NOTE: Must escape Curly Braces if using variables via f-string
dict_to_save_vars = f"""{{
 
    'key1' : '{string_to_save}',
    'key2' : {number_to_save}
}}
"""
 
 
# Final Data to Export
data_to_save = f'''
# Comment Line
dict_to_save = {dict_to_save}
list_to_save = {list_to_save}
dict_to_save_vars = {dict_to_save_vars}
'''
 
export_file('settings.config', data_to_save)

This will be the expected output stored to the file

# Comment Line
dict_to_save = {
 
    'key1' : 'value1',
    'key2' : 'value2'    
}
 
list_to_save = [  
 
    1,
    2,
    3
]
 
dict_to_save_vars = {
 
    'key1' : 'John Doe',
    'key2' : 64
}

Clean Format: You can also use "cleanformat" to easily organize your multiline output

 
data_to_save_clean = {'key1':'value1', 'key2':'value2', 'key3':'value3'}
 
clean_output = sfcparse.cleanformat(data_to_save_clean)
 
export_file('settings.config', f"data_to_save_clean = {clean_output}")

This will be the clean expected output stored to the file

data_to_save_clean = {
    'key1' : 'value1',
    'key2' : 'value2',
    'key3' : 'value3'
}

That's it!


Usage: Appending:

Writes New Data to a File if it Exists

It is the same syntax as exporting, but here is an example using the same exported output from "Exporting - Single Line" settings.config.

Note these are just examples to build your data to append.

Also Single line appending may be more tedious than multiline, so it is recommended to build your data with multiple lines as shown!

import sfcparse
 
append_file = sfcparse.appendfile
 
data_to_append = ['item1', 'item2', 'item3']
data_to_append2 = [1,2,3]
 
# Single Line Appending
append_file('settings.config', f"data_to_append = {data_to_append}")
 
# Multiline Appending
final_save_data = f"""
data_to_append = {data_to_append}
data_to_append2 = {data_to_append2}
"""
 
append_file('settings.config', f"data_to_append = {final_save_data}")

This will be the expected output appended to the file if using the multiline append method

# Comment Line
string_to_save = 'John Doe'
number_to_save = 64
tuple_to_save = (1,2,3)
data_to_append = ['item1', 'item2', 'item3']
data_to_append2 = [1,2,3]

That's it!


Performance

Importing

Imported a file 1000 times with 100 lines of data in 0.6s.

Lab Test Conducted:
from sfcparse import importfile
import time
 
file_to_import = 'data.test'
num_of_tests = 1000
 
# Performance Test
start_time = time.perf_counter()
for i in range(num_of_tests):
    importfile(file_to_import)
end_time = time.perf_counter()
 
elapsed_time = (end_time - start_time)
 
print(elapsed_time)
System: Tested on a 4th-Gen Intel Core i7-4790 at 3.6GHz

Updates - Current v1.0.1

v1.0.0 - Official Production Release

sfcparse updates

  • Added new test env for QA
  • Officially supports Python version 3.6+
  • Officially Supports Latest Versions of Linux, Windows, and Mac OS
  • Cleaned up help text in doc strings and readme
  • Currently have plans to add support for json, yaml, ini, and removing files/dirs

###Update History:

0.8.0 - Release

API change:

  • NEW: v0.8.0 - Compatible only on Python version 3.6+
  • PREVIOUS: v0.7.7 - Compatible only on Python version 3.0+

Performance:

  • cleanformat:
    • code improvement has a 23% performance gain
  • importfile:
    • code improvement has a 10% performance gain

Feature Additions:

  • cleanformat:
    • Has a new indentation level feature to add custom indent levels for exporting to a file NOTE: formatting performance will gain 5% boost if you set indent level to 0 (Default 1). Performance will decrease with more levels of indentation added.
    • Now supports nested data sets, but will not apply indentation to sub-level data sets
    • Now does not parse commas in values or keys and retains its original representation
  • importfile:
    • Now supports importing nested data sets even when subsets are on multiple lines in the file
    • Comments can now be added after a value

General:

  • 0.8.3:
    • Fixed Importing and Appending error exceptions not raising correctly when checking if file exists
  • 0.8.4
    • Added Type Hinting and Return Hinting to improve readability
  • 0.8.5
    • Updated Type Hinting on importfile
    • Updated docstrings
  • 0.8.6
    • NEW: Added functionality test env
    • importfile: Fixed adding comments on post-start/end brackets
    • importfile: Fixed exception not being thrown when no end-bracket is found on multiline imports
    • Updated exceptions to reflect correct types
    • appendfile: Fixed data starts append on second line if file empty
    • Importing performance has dropped 7% due to these new changes. However there are plans to bring performance back up, but it's still really fast.
  • 0.8.7
    • Fixed a major multiline import bug preventing an import when a line preceding the first line of data was blank/there was no literal chars including a space char on that line
    • Added new test scenario for the newly fixed bug

Known Limitations

Importing

  • Does not support importing unpacked variables and values

Future Upgrades

importfile

  • Add support for unpacked variables and values

Future Features

  • Add Support for:
    • Simple YAML Importing/Exporting
    • Simple JSON Importing/Exporting
    • Simple XML Importing/Exporting
    • Simple INI Importing/Exporting

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sfcparse-1.0.1.tar.gz (7.8 kB view hashes)

Uploaded Source

Built Distribution

sfcparse-1.0.1-py3-none-any.whl (7.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page