Giter Club home page Giter Club logo

pyrec's Introduction

pyrec (Record.py) let's you:

* Easily build immutable data structures
* Use more declerative-style programming
* Never have to write __init__ (for records, at least)
* Avoid concurrency bugs
* Reduce memory consumption when dealing with lots of objects

All in once easy-to-include source file.  Just "from Record import Record"

see examples.py

But be careful, once you get hooked on Record, you'll use it
everywhere.  Python will never be the same to you.



UPDATE: Some have asked: "how is Record.py different from
collection.namedtuple?".  You could view this as an improvement on
collection.namedtuple.  Record.py actually existed before namedtuple,
and I've learned a few things in the years I've been using it.  Here
are some advantages to Record.py that have been important to me the
last few years:


1. It has a nicer interface. I prefer new(val1, val2) to _make([val1,
val2]), alter to _update, and class Person(Record("name", "age")) to
Person = namedtuple("Person", "name, age")

2. I added the set_field methods. That's what I use 90% of the
time. Only about 10% of the time do I use alter. set_field is a lot
more convenient.

3. With pyrec, you can safely override __iter__ and __getitem__. For
example, in Record.py, you'll see the implementation of a
LinkedList. I tried doing that with namedtuple, but the overidden
__getitem__ clobers the name lookup and __iter__ clobers tuple
unpacking.  

4. pyrec has .namedValues for ordered (field, value) pairs, unlike
_asdict() which throws out the order. Minor thing, but it's nice.

5. You can improve it! Have looked at the code for namedtuple?
Ugly. This is pretty clean, so you can improve it very easily if you
need additional functionality which will work with all of your
records.  In particular, the version of pyrec I use has some custom
serialization stuff added to it, which is very handy.  Good luck
adding something like that to namedtuple.


If you don't think that's compelling, go ahead and used namedtuple.
It's still immutable, usees less memory, doesn't require __init__, and
avoids concurrency bugs.  pyrec is just easier to use.

If you want most of the good stuff but still want to use namedtuples,
I added NamedTuple.py which works almost exactly like Record but is a
subclass of namedtuple.  You still can't override __getitem__, though.

pyrec's People

Contributors

pthatcher avatar

Stargazers

Timothy Chon avatar

Watchers

Timothy Chon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.