Sugar: Python Syntactic Sugar
A lightweight utility package that provides syntactic sugar and data manipulation tools to make Python code more concise and expressive.
Installation
pip install git+https://github.com/gabiteodoru/sugar.git
Dependencies:
- pandas
- more_itertools
Features
Core Functions
- Mapping Functions:
imap: List version ofmap()timap: Tuple version ofmap()izip: List version ofzip()ifilter: List version offilter()dmap: Apply function to dictionary valuesdfilter: Filter dictionary keys based on valuesflatten: Flatten nested listsunique: Return unique values from a list
- Function Wrappers: Operations using subtraction syntax
- Smart String Splitting: Handle nested brackets, quotes and special characters
- Data Structure Manipulation: Functions for dictionaries, lists and pandas DataFrames
- Concise Data Access: Simplified syntax for common operations
Examples
from sugar import *
# List operations
imap(lambda x: x+2, [2, 3, 4]) # [4, 5, 6]
timap(lambda x: x+2, [2, 3, 4]) # (4, 5, 6)
izip([1, 2], ['a', 'b']) # [(1, 'a'), (2, 'b')]
ifilter(lambda x: x > 2, [1, 2, 3, 4]) # [3, 4]
flatten([[1, 2], [3, 4]]) # [1, 2, 3, 4]
unique([1, 2, 2, 3, 3]) # [1, 2, 3]
# Dictionary operations
dmap(lambda x: x*2, {'a': 1, 'b': 2}) # {'a': 2, 'b': 4}
dfilter(lambda x: x > 1, {'a': 1, 'b': 2, 'c': 3}) # ['b', 'c']
# String splitting
'a, b, c'-spl # ['a', 'b', 'c']
'a b c'-spls # ['a', 'b', 'c']
'a>b>c'-splon('>') # ['a', 'b', 'c']
'a b c'-spls-first # 'a'
'a b c'-spls-last # 'c'
# Dictionary operations
'a:x, b:y, c:z'-spld # {'a': 'x', 'b': 'y', 'c': 'z'}
'a:x, b:y, c:z'-spld-flipDict # {'x': 'a', 'y': 'b', 'z': 'c'}
# Condition building
'a==1, b==2'-spland # '(a==1)&(b==2)'
# DataFrame operations
df = pd.DataFrame({'x':'a, a, b'-spl, 'y':'c, d, d'-spl, 'z':[1, 2, 3]})
isUnique(df) # True
qselect(df,'y, colX:x') # DataFrame with columns 'y' and 'colX' (renamed from 'x')
frontCols(df, 'y, z') # DataFrame with columns reordered as ['y', 'z', 'x']
Advanced Features
Script Execution
Run Python scripts in the current namespace:
# Execute a script in the current namespace
eval(runpy)('script.py')
Function Application Syntax
Replace verbose function definitions with concise string expressions:
# Traditional approach
def f(x):
d = {}
d['v'] = x.v.sum()
d['dv'] = (x.v*x.c).sum()
return pd.Series(d, index=['v', 'dv'])
rawData.groupby('s').apply(f)
# With sugar
rawData.groupby('s').apply('v: x.v.sum(), dv: (x.v*x.c).sum()'-fapply)
Data Classes
FriendlyDataClass: Base class with dictionary-like accessObj: Enhanced dictionary with attribute access
Nested Structure Creation
createNestedStruct('a, b, c', 'x, y', init=[1, 2, 3])
# Creates nested dictionary structure with leaf nodes [1, 2, 3]
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Documentation
Documentation generated with assistance from Claude (Anthropic).
Metadata
Release files for gabiteodoru-sugar 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| gabiteodoru_sugar-0.1.0.tar.gz | 7.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| gabiteodoru_sugar-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.9 kB
Release files / gabiteodoru_sugar-0.1.0.tar.gz
| Download URL | gabiteodoru_sugar-0.1.0.tar.gz |
|---|---|
| Size | 7.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3307978c3fd61cbd28ab31a5842c77ad13625cc3b5cdb1c7a614d950c176c995
|
|
BLAKE2b-256 checksum How to use checksums |
ea86763b203ce83fcac1dccff6a102452f83ac5ac006ad42ed478c2e35e9f812
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Release files / gabiteodoru_sugar-0.1.0-py3-none-any.whl
| Download URL | gabiteodoru_sugar-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bcfa55120c50050a4591e1b4e6d3fd981d216e2915caee553b9e940c83cb1e9f
|
|
BLAKE2b-256 checksum How to use checksums |
da5b440aca6307c513b95092674c7c88a75f82702bb34b01a087ef9d67b2e8b8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|