Skip to main content

README

Build Status

λamla is a performant functional programming library for python which supports mixing async and regular functions.

Installation: pip install gamla

API reference: https://gamla.readthedocs.io/

Basic example

gamla can help you turn this:

import dataclasses

@dataclasses.dataclass
class Person:
    age: int
    name: str

    def is_eligible(self):
        return self.age > 9


def get_names_eligible_for_vaccine(people):
    result = []
    for person in people:
        if person.is_eligible():
            result.append(person.name)
    return result

into this:

import dataclasses
from gamla import attrgetter, greater_than, compose_left, filter, map

@dataclasses.dataclass(frozen=True)
class Person:
    age: int
    name: str

is_eligible = compose_left(attrgetter("age"), greater_than(9))
get_names_eligible_for_vaccine = compose_left(filter(is_eligible), map(attrgetter("name")), list)

Is this a good thing? that's for you to decide.

The upside:

Functional programming is mainly about how to split your code into composable parts. Composability means that things are easy to move, replace or combine together like lego. It helps you identify recurring patterns (e.g. filter), factor them out and reuse them. If your generalizations are good, they free your mind to focus on the new logic. Concretely it saves a lot of code and helps a reader understand what a piece of code is doing. For example, if you are familiar with what filter is, you don't have to squint and realize that an if and a for actually do a filtering pattern.

The downside:

Programming in this style in python means some tools won't be so useful (e.g. your debugger, static analysis tools).

Debugging anonymous compositions

gamla.debug

Classic breakpoints are less useful when working with compositions, as there isn't always a line of code to place the breakpoint on. Instead one can use gamla.debug and gamla.debug_exception.

gamla.debug can be used within pipelines and will provide a pdb breakpoint prompt where the value at this position can be referenced by x.

def increment(x):
    return x + 1

increment_twice = gamla.compose_left(increment, gamla.debug, increment)

increment_twice(1)

The above code will break with x being 2.

When you have a long pipeline and want to debug at each step of the way, you can use gamla.debug_compose and gamla.debug_compose_left.

gamla.debug_exception

In some cases tracking down an exception involves inspecting code that runs many times. Consider the following example:

def increment(x):
    return x + 1

def sometimes_has_a_bug(x):
    if x == 666:
        raise Exception
    return x

increment_with_bug = gamla.map(gamla.compose_left(increment, sometimes_has_a_bug))

tuple(increment_with_bug(range(1000)))

Adding a gamla.debug here can be quite tedious, because the code will break many times.

Instead we can use gamla.debug_exception to break only in the case the inner function raises, at which case we would get a breakpoint prompt, and be able to inspect the value causing the exception, use the name x. This would like this:

increment_with_bug = gamla.map(gamla.compose_left(increment, gamla.debug_exception(sometimes_has_a_bug)))

One can also use gamla.debug_exception using a decorator.

@gamla.debug_exception
def sometimes_has_a_bug(x):
    if x == 666:
        raise Exception
    return x

Mixing asynchronous and synchronous code

Most functions in this lib will work seamlessly with async and regular functions, and allow the developer to focus on the logic instead of deciding where to place an await.

For example:

import asyncio

import gamla


def increment(i):
    return i + 1


async def increment_async(i):
    await asyncio.sleep(1)
    return i + 1


async def run():
    mixed_composition = gamla.compose_left(increment, increment_async, increment)
    return await mixed_composition(0)  # returns 3!

Releasing a new version

Increment the version in master, and pypi will automatically update.

Updating documentation after change in README.md

While in gamla directory:

  1. Install md-to-rst converter: pip install m2r
  2. Convert README.md to README.rst: m2r README.md
  3. Move README.rst to docs/source folder instead of existing one: mv README.rst docs/source

Download files

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

Source Distribution

gamla-164.tar.gz (50.7 kB view details)

Uploaded Source

File details

Details for the file gamla-164.tar.gz.

File metadata

  • Download URL: gamla-164.tar.gz
  • Upload date:
  • Size: 50.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.0

File hashes

Hashes for gamla-164.tar.gz
Algorithm Hash digest
SHA256 8d44548f6c4d711f2ad90b906ab91a250cad2be216b295fd746786dc16e5097c
MD5 a43242449519dc48717c354e770d0a3b
BLAKE2b-256 4180551d66cc54c199afb0ef1eab42abdc82bb6355b792a74c87e7ae2a6c2ff5

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

164 This release

1 file

163

1 file

162

1 file

161

1 file

160

1 file

159

1 file

158

1 file

157

1 file

156

1 file

155

1 file

154

1 file

153

1 file

152

1 file

151

1 file

150

1 file

149

1 file

148

1 file

147

1 file

146

1 file

145

1 file

144

1 file

143

1 file

142

1 file

141

1 file

140

1 file

139

1 file

138

1 file

137

1 file

136

1 file

135

1 file

134

1 file

133

1 file

132

1 file

131

1 file

130

1 file

129

1 file

128

1 file

127

1 file

126

1 file

125

1 file

124

1 file

123

1 file

122

1 file

121

1 file

120

1 file

119

1 file

118

1 file

117

1 file

116

1 file

115

1 file

114

1 file

113

1 file

112

1 file

111

1 file

110

1 file

109

1 file

108

1 file

107

1 file

106

1 file

105

1 file

104

1 file

103

1 file

102

1 file

101

1 file

100

1 file

99

1 file

98

1 file

97

1 file

96

1 file

95

1 file

94

1 file

93

1 file

92

1 file

91

1 file

90

1 file

89

1 file

88

1 file

87

1 file

86

1 file

85

1 file

84

1 file

83

1 file

82

1 file

81

1 file

80

1 file

79

1 file

78

1 file

77

1 file

76

1 file

75

1 file

74

1 file

73

1 file

72

2 files

71

1 file

70

1 file

69

1 file

68

2 files

67

2 files

66

2 files

65

1 file

64

1 file

63

2 files

62

2 files

61

2 files

60

2 files

59

1 file

58

2 files

57

2 files

56

2 files

55

2 files

54

1 file

53

1 file

52

1 file

51

2 files

50

2 files

49

1 file

48

2 files

47

2 files

46

1 file

45

1 file

44

2 files

43

2 files

42

2 files

41

2 files

40

2 files

39

2 files

38

2 files

37

2 files

36

2 files

35

2 files

34

2 files

33

2 files

32

2 files

31

2 files

30

2 files

29

2 files

28

2 files

27

2 files

26

2 files

25

2 files

24

2 files

23

2 files

22

2 files

21

2 files

20

2 files

19

2 files

18

2 files

17

2 files

16

2 files

15

2 files

14

2 files

13

2 files

12

2 files

11

2 files

10

2 files

9

2 files

8

2 files

7

2 files

6

2 files

5

2 files

4

2 files

3

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1

2 files

0.0.78

2 files

0.0.77

2 files

0.0.76

2 files

0.0.75

2 files

0.0.74

2 files

0.0.73

2 files

0.0.72

2 files

0.0.71

2 files

0.0.70

2 files

0.0.69

2 files

0.0.68

2 files

0.0.67

2 files

0.0.66

2 files

0.0.65

2 files

0.0.63

2 files

0.0.62

2 files

0.0.61

2 files

0.0.60

2 files

0.0.59

2 files

0.0.58

2 files

0.0.57

2 files

0.0.56

2 files

0.0.55

2 files

0.0.54

2 files

0.0.53

1 file

0.0.52

2 files

0.0.51

2 files

0.0.50

2 files

0.0.49

2 files

0.0.48

2 files

0.0.47

2 files

0.0.46

2 files

0.0.45

2 files

0.0.44

2 files

0.0.43

2 files

0.0.42

2 files

0.0.41

2 files

0.0.40

2 files

0.0.39

2 files

0.0.38

2 files

0.0.37

2 files

0.0.36

2 files

0.0.35

2 files

0.0.34

2 files

0.0.33

2 files

0.0.32

2 files

0.0.31

2 files

0.0.30

2 files

0.0.29

2 files

0.0.28

2 files

0.0.27

2 files

0.0.26

2 files

0.0.25

2 files

0.0.24

2 files

0.0.23

2 files

0.0.22

2 files

0.0.21

2 files

0.0.20

2 files

0.0.19

2 files

0.0.18

2 files

0.0.17

2 files

0.0.16

2 files

0.0.15

2 files

0.0.14

2 files

0.0.13

2 files

0.0.12

2 files

0.0.11

2 files

0.0.10

1 file

0.0.9

1 file

0.0.8

1 file

0.0.7

1 file

0.0.6

1 file

0.0.5

1 file

0.0.4

1 file

0.0.3

1 file

0.0.2

1 file

0.0.1

1 file

Supported by

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