Skip to main content

Gabi's syntactic sugar & other utils

Project description

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 of map()
    • timap: Tuple version of map()
    • izip: List version of zip()
    • ifilter: List version of filter()
    • dmap: Apply function to dictionary values
    • dfilter: Filter dictionary keys based on values
    • flatten: Flatten nested lists
    • unique: 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 access
  • Obj: 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

MIT License

Documentation

Documentation generated with assistance from Claude (Anthropic).

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

gabiteodoru_sugar-0.1.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gabiteodoru_sugar-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file gabiteodoru_sugar-0.1.0.tar.gz.

File metadata

  • Download URL: gabiteodoru_sugar-0.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for gabiteodoru_sugar-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3307978c3fd61cbd28ab31a5842c77ad13625cc3b5cdb1c7a614d950c176c995
MD5 daa8d2b4a5a9f7e5c6f87f97d94c8a7e
BLAKE2b-256 ea86763b203ce83fcac1dccff6a102452f83ac5ac006ad42ed478c2e35e9f812

See more details on using hashes here.

File details

Details for the file gabiteodoru_sugar-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gabiteodoru_sugar-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bcfa55120c50050a4591e1b4e6d3fd981d216e2915caee553b9e940c83cb1e9f
MD5 f860f6ec902b923ba4c4011905832c7e
BLAKE2b-256 da5b440aca6307c513b95092674c7c88a75f82702bb34b01a087ef9d67b2e8b8

See more details on using hashes here.

Supported by

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