Core
(compound) STRing UTILitieS - A collection of (compound string utilities).
Used to grow on the fly. Aims to provide abstract, general purpose functionalities.
Split Operate Stitch. |
|
Split compound strings and permute its components. |
|
Split, variate and stitch compound strings. |
- strutils.core.sos_on(compound_string, using=<method 'capitalize' of 'str' objects>, split_at='_', stitch_with='_')[source]
Split Operate Stitch.
Split the
compound_string
atsplit_at
and operate on it withusing
on each component to stitch it back together into one string using :paramref:~sos_on.stitch_with.- Parameters
compound_string¶ (str) – Compound strings of which the components are to be split, operated and stitched.
using¶ (
Callable
) – Callable mapped to each component of the string.split_at¶ (str, default='_') – Each component of the inbound compound string will be identified by this.
stitch_with¶ (str, default='_') – The outbound compound string will be stitched together using this.
- Yields
Generator
– Generator object yielding the splitted, operated and stitched compound strings.
Examples
>>> import pprint >>> capitilzed = sos_on('variable_cost', using=str.capitalize) >>> print(type(capitilzed)) <class 'generator'> >>> pprint.pprint(list(capitilzed)) ['variable_cost', 'variable_Cost', 'Variable_cost', 'Variable_Cost']
- strutils.core.permute_splits(strings, stitch_with='_', split_at='_')[source]
Split compound strings and permute its components.
Split each compund string in
strings
atsplit_at
into its components. Create all possible permutations and stitch them back together into compound strings using :paramref:~permute_splits.stitch_with.- Parameters
strings¶ (
Iterable
, str) – String or iterable of compound strings of which the components are to be permutated. Must be of depth <= 1. i.e.:"My_String" or ["My-String1", "My-String2"]
split_at¶ (str, default='_') – Each component of the inbound compound string will be identified by this.
stitch_with¶ (str, default='_') – The outbound compound string will be stitched together using this.
- Yields
Generator
– Generator object yielding the permmuted compound strings.
Examples
>>> import pprint >>> capitilzed = sos_on('variable_cost', using=str.capitalize) >>> permuted = permute_splits(strings=capitilzed) >>> print(type(permuted)) <class 'generator'> >>> pprint.pprint(list(permuted)) ['variable_cost', 'cost_variable', 'variable_Cost', 'Cost_variable', 'Variable_cost', 'cost_Variable', 'Variable_Cost', 'Cost_Variable']
- strutils.core.variate_compounds(compound_string, using=<method 'capitalize' of 'str' objects>, permutate='True', split_at='_', stitch_with='_')[source]
Split, variate and stitch compound strings.
Convenience wrapper for
sos_on()
andpermute_splits()
.Designed to quickly generate variations of a compound string.
- Parameters
compound_string¶ (str) – Compound string of which the components are to be split, operated and stitched.
using¶ (
Callable
) – Callable mapped to each compound of the string. Used for creating variations. Seesos_on()
for details on the mutations.permutate¶ (bool, default=True) – If true, all possible permations of the variated compound strings will be generated. See
permute_splits()
for details on the permutation process.split_at¶ (str, default='_') – Each component of the inbound compound string will be identified by this.
stitch_with¶ (str, default='_') – The outbound compound string will be stitched together using this.
- Returns
variated – Generator object yielding the variated compound strings.
- Return type
Examples
Note how getting a list of generator can be a little inconvenient:
>>> import pprint >>> variated = variate_compounds('variable_cost') >>> pprint.pprint(list(variated)) ['variable_cost', 'cost_variable', 'variable_Cost', 'Cost_variable', 'Variable_cost', 'cost_Variable', 'Variable_Cost', 'Cost_Variable']
Use nested for loops and
itertools.chain()
to create complex mappings of variated compound strings:>>> import pprint >>> import collections >>> import itertools >>> import pandas >>> stitchers = ['_', ' '] >>> compounds = ['variable_cost', 'flow_costs'] >>> variations = collections.defaultdict(list) >>> for string in compounds: ... for stitcher in stitchers: ... variations[string] = list(itertools.chain( ... variations[string], ... variate_compounds(string, stitch_with=stitcher))) >>> print(pandas.DataFrame(variations)) variable_cost flow_costs 0 variable_cost flow_costs 1 cost_variable costs_flow 2 variable_Cost flow_Costs 3 Cost_variable Costs_flow 4 Variable_cost Flow_costs 5 cost_Variable costs_Flow 6 Variable_Cost Flow_Costs 7 Cost_Variable Costs_Flow 8 variable cost flow costs 9 cost variable costs flow 10 variable Cost flow Costs 11 Cost variable Costs flow 12 Variable cost Flow costs 13 cost Variable costs Flow 14 Variable Cost Flow Costs 15 Cost Variable Costs Flow