Skip to main content

Making Immutable and stateless data structures

Project description

Changeless is a set of functions and objects to help convert your data into a set stateless or immutable data objects.

Types

from changeless.types import FancyHash, FancyModel, ImmutableHash, ImmutableModel

Fancy Types take a dict object and replaces the dereferencing operator ([]) with the dot operator. So the object

an_obj= FancyHash({"name":"me"})

will allow us to retrieve the attribute ‘name’ with

print "the object's name: " + an_obj.name
>the object's name: matt

Model Types take a Django ORM QueueSet and converts it to a comparable api. This uses the Fancy type as a base after converting the QuerySet to a dict. Foreign keys and many to many relationships are converted to nested dicts and lists of dicts(respectively) to aid in the objects behaving as similarly as possible.

Note that Model Types retrieve all of data at once which includes by default relationships directly adjacent. This might incur more queries than expected. Choose your data carefully and scale back when necessary.

  • FancyHash(a_dictonary)

  • FancyModel(a_model, depth=1)

  • ImmutableHash(a_dictonary)

  • ImmutableModel(a_model, depth=1)

Just pass the correct object into the type constructor to convert your data.

Decorators

from changeless.decorators import fancy_list

@fancy_list
def get_books():
    return Book.objects.all()

Place the following decorators before functions that return a Django ORM QueueSet to convert it to the correct changeless object. Decorators are the preferred way to use the changeless library. Using the decorators promote readability by keeping the conversion away from the ORM call, as well as providing an easy to way to turn the changeless conversion on and off. Notice that the _gen decorators will return a generator that will lazily convert each object in the list. Generators may be more efficient for long lists.

The following generators are available.
* fancy_list * fancy_gen * immutable_list * immutable_gen

Functions

I’ve found the following functions useful. ###fuzzyEquals### from changeless.compare import fuzzyEquals

i_obj = FancyHash({"name":'test name',
                   'sub_dict':{'name':'sub name', 'attrib':'sub attr value'}
                  })
second_i_obj = FancyHash({"name":'test name',
                          'sub_dict':{'name':'sub name', 'attrib':'sub attr value' }})
self.assertTrue( fuzzyEquals(
     i_obj,
     second_i_obj ))

fuzzyEquals will find attributes that the changeless objects have in common and compare only that union. This also inspects nested relationships for shared attributes. ###to_dict### from changeless.methods import to_dict to_dict is the reverse conversion from a base fancy_object to its dictionary representation.

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

changeless-0.1.4.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

changeless-0.1.4.macosx-10.5-x86_64.exe (75.1 kB view details)

Uploaded Source

File details

Details for the file changeless-0.1.4.tar.gz.

File metadata

  • Download URL: changeless-0.1.4.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for changeless-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d2b77f3ac659a803a6b616b99402c6576e902394bb5af7f787b622f82c436461
MD5 01d7b643b29aee27053fd90e00d7035f
BLAKE2b-256 0b105a888cbcf52a5b13f19a5886b633b04a8a0dd3143fb5c6c20191e5f5bb81

See more details on using hashes here.

File details

Details for the file changeless-0.1.4.macosx-10.5-x86_64.exe.

File metadata

File hashes

Hashes for changeless-0.1.4.macosx-10.5-x86_64.exe
Algorithm Hash digest
SHA256 45d943dce39d60dd0af0642ad14c941d681a900cf14c8399e70b714783301a16
MD5 3ee0f69a65550388be19d9ba4831bdfd
BLAKE2b-256 eea57efe6c8c9d2f0392a638e21f202263544d9c5581574f986e4c159d9734ac

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