This class encapsulates logic to convert a string input from one string format into another string format, or into a dictionary.
Why not just use the CSV standard library?
xlate could borrow at least one feature from CSV's dialects: incorporating quotes. Otherwise, xlate focuses on string translation no matter how the strings are sourced-- not just from files. In addition, xlate is intended to have built in translations which do not strictly adhere to the same translations possible in CSV.
That said: for translation of one read file's delimited format to another write file's delimited format, CSV is the one to use.
from xlate import xlate_string_formats
input_del = ' '
input_format = ['lname', 'fname', 'bdate', 'language_of_choice', 'hometown']
output_format = '{fname} {lname}, born on {bdate}, ' \
'lives in {hometown} and prefers {language_of_choice}'
input_strings = [
'pohl erik 9/2/72 python arlington',
'pynchon thomas 5/5/45 muted_posthorn manhattan'
]
If you don't specify the input format with keywords, it converts the output format into a positional format
with xlate_string_formats(
xlate_input_delimiter=input_del,
xlate_output_format=output_format
) as f:
[print(f(input_string)) for input_string in input_strings]
with xlate_string_formats(
xlate_input_delimiter=input_del,
xlate_input_format=input_format,
xlate_output_format=output_format
) as f:
[print(f(input_string)) for input_string in input_strings]
from xlate import xlate
input_string = 'pohl erik 9/2/72 python arlington'
input_del = ' '
input_format = ['lname', 'fname', 'bdate', 'language_of_choice', 'hometown']
output_format = '{fname} {lname}, born on {bdate}, ' \
'lives in {hometown} and prefers {language_of_choice}'
demo_usage = xlate(
xlate_input_delimiter=input_del,
xlate_input_format=input_format,
xlate_output_format=output_format
)
Output an input string in the format out the output format using the input format keywords as a string then a dictionary
print(demo_usage.to_string_using_keyword_format(input_string))
print(demo_usage.to_dictionary(input_string))
output_format = '{0} {1}, born on {2}, lives in {3} and prefers {4}'
demo_usage = xlate(
xlate_input_delimiter=input_del,
xlate_output_format=output_format
)
print(demo_usage.to_string_using_keyword_format(input_string))
print(demo_usage.to_dictionary(input_string))
output_format = '{fname} {lname}, born on {bdate}, ' \
'lives in {hometown} and prefers {language_of_choice}'
demo_usage = xlate(
xlate_input_delimiter=input_del,
xlate_output_format=output_format
)
print(demo_usage.to_string_forcing_positional(input_string))
The code here does not represent work I'd submit for production code-review. Standards differ, and I have worked within many different sets, helping to establish and build on them.
Here are some elements I expect to be able to provide, if needed:
- A complete regression test suite.
- Meaningful exceptions and exception-handling coverage.
- Adequate output to permit users to understand the results, assisting in the self-documenting nature of the code.
- Actual docstring comments at all levels of the code.
- Requirements documents, user-facing documents and presentations, and other documents consistent with Agile User Stories to add value.
Download these files to corresponding folders under your Python src path.
I'll provide prereqs here.
I will provide installation steps here.
I will explain how to test the system here using the automated tests.
For now, I'd be excited to receive pull requests. I don't have rules for contributing right now.
- Erik Pohl - Initial work -
Also see the list of github contributors.
This project is licensed under the MIT License - see the LICENSE.md file for details
- Thanks to everyone who has motivated me to learn more.