{ "info": { "author": "Josh Marshall", "author_email": "catchjosh@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5" ], "description": "Mogo\n====\nThis library is a simple \"schema-less\" object wrapper around the\npymongo library (http://github.com/mongodb/mongo-python-driver).\nMogo provides helpers to use PyMongo in an MVC environment \n(things like dot-attribute syntax, model methods, \nreference fields, etc.)\n\nWhile pymongo is straightforward to use and really flexible, it\ndoesn't help with MVC because you are working with plain dicts\nand can't attach model logic anywhere.\n\nMogo is licensed under the Apache License, Version 2.0\n(http://www.apache.org/licenses/LICENSE-2.0.html).\n\nFeatures\n--------\n* Put classes / structure around pymongo results \n* Models are dicts, so dot-attribute or key access is valid. Dot attribute\n gives \"smart\" values, key access gives \"raw\" pymongo values.\n* Support for specifiying Field() attributes without requiring\n them or enforcing types.\n* Simple ReferenceField implementation.\n\nRequirements\n------------\n* PyMongo - http://github.com/mongodb/mongo-python-driver\n\nInstallation\n------------\nYou can install it from PyPI with:\n\n```sh\npip install mogo\n```\n\nAlternatively you should be able to grab it via git and run the following\ncommand:\n\n```sh\npython setup.py install\n```\n\nTests\n-----\nTo run the tests, make sure you have a MongoDB instance running\non your local machine. It will write and delete entries to\nmogotest db, so if by some bizarre coincidence you have / need that,\nyou might want to alter the DBNAME constant in the mogo/tests.py\nfile.\n\nAfter installation, or from the root project directory, run:\n\n```sh\nnosettest tests/\n```\n\nIf you don't have nose, it's available with:\n\n```sh\npip install nose\n```\n\nImporting\n---------\n\nAll the major classes and functions are available under the top level\nmogo module:\n\n```python\nimport mogo\n# or\nfrom mogo import Model, Field, connect, ReferenceField\n```\n\nConnecting\n----------\n\nMogo uses a single global connection, so that once you connect, you can\njust start accessing your model class methods. Connecting looks like:\n\n```python\nfrom mogo import connect\n\nconnect(\"my_database\") # connects to a local mongodb server with default port\nconnect(\"foobar\", host=\"127.0.0.1\", port=28088)\nconnect(uri=\"mongodb://user:pass@192.168.0.5/awesome\") # for heroku, etc.\n```\n\nIf you need to use an alternate connection for a chunk of code, without\nlosing your main connection, you can use the following style:\n\n```python\nfrom mogo import connect, session\n\nconnect(\"my_awesome_database\")\n# do normal stuff\nwith mogo.session(\"my_alternate_database\"):\n # do stuff with other database\n```\n\nModels\n------\nModels are subclasses of dicts with some predefined class and instance\nmethods. They are designed so that you should be able to use them\nwith an existing MongoDB project (DATA BE WARNED: THIS IS ALPHA!)\nAll you need is a class with the proper collection name, and connect\nto the DB before you access it.\n\nYes, this means the most basic example is just a class with nothing:\n\n```python\nclass Hero(Model):\n pass\n```\n\nYou can now do things like:\n\n```python\nhero = Hero.find({\"name\": \"Malcolm Reynolds\"}).first()\n```\n\nBy default, it will use the lowercase name of the model as the\ncollection name. So, in the above example, the equivalent pymongo\ncall would be:\n\n```python\ndb.hero.find({\"name\": \"Malcolm Reynolds\"})[0]\n```\n\nOf course, Models are much more useful with methods:\n\n```python\nclass Hero(Model):\n def swashbuckle(self):\n print \"%s is swashbuckling!\" % self[\"name\"]\n\nmal = Hero.find({\"name\": \"Mal\"}).first()\nhero.swashbuckle()\n# prints \"Mal is swashbuckling!\"\n```\n\nSince Models just subclass dictionaries, you can use (most) of the\nnormal dictionary methods (see `update` later on):\n\n```python\nhero = Hero.find_one({\"name\": \"Book\"})\nhero.get(\"powers\", [\"big darn hero\"]) # returns [\"big darn hero\"]\nhero_dict = hero.copy()\nfor key, value in hero.iteritems():\n print key, value\n```\n\nTo save or update values in the database, you use either `save` or\n`update`. (Imagine that.) If it is a new object, you have to `save`\nit first:\n\n```python\nmal = Hero(name=\"Malcom Reynolds\")\nmal.save()\n```\n\n`save` will always overwrite the entire entry in the database.\nThis is the same behavior that PyMongo uses, and it is helpful for\nsimpler list and dictionary usage:\n\n```python\nzoe = Hero(name=\"Zoe\", powers=[\"warrior woman\"])\nzoe.save()\nzoe[\"powers\"].append(\"big darn hero\")\nzoe.save()\n```\n\n...however, this can ultimately be inefficient, not to\nmention produce race conditions and have people saving over each\nother's changes.\n\nThis is where `update` comes in. Note that the `update` method does\nNOT function like the dictionary method. It has two roles, \ndepending on whether it is called from a class or from an instance.\n\nIf it is called from a class, it just passes everything on to PyMongo\nlike you might expect:\n\n```python\nHero.update({\"name\": \"Malcolm Reynolds\"},\n {\"$set\":{\"name\": \"Capt. Tightpants\"}}, safe=True)\n# equals the following in PyMongo\ndb.hero.update({\"name\": \"Malcolm Reynolds\"},\n {\"$set\":{\"name\": \"Capt. Tightpants\"}}, safe=True)\n```\n\nIf it is called from an instance, it uses keyword arguments to set\nattributes, and then sends off a PyMongo \"$set\" update:\n\n```python\nhero = Hero.find_one({\"name\": \"River Tam\"})\nhero.update(powers=[\"telepathy\", \"mystic weirdness\"])\n# equals the following in PyMongo\nhero = db.hero.find_one({\"name\": \"River Tam\"})\ndb.hero.update({\"_id\": hero[\"_id\"]},\n {\"$set\": {\"powers\": [\"telepathy\", \"mystic weirdness\"]}})\n```\n\n(BETA) If you call it from a cursor, it will use the query you\noriginally provided to the cursor. This does not currently respect\nadditional filtering like `where()`, does not check types when\nsetting values, and has not been exhaustively tested. (So beware.)\n\n```python\nhero_cursor = Hero.find({\"name\": {\"$in\": [\"River\", \"Simon\"]}})\nhero_cursor.update({\"$push\": {\"powers\": \"siblingness\"}})\n# or, for you keyword-liking people...\nhero_cursor.change(powers=\"siblingness\")\n```\n\nFields\n------\nUsing a Field is (usually) necessary for a number of reasons. While\nyou can remain completely schemaless in Mongo, you will probably go\na little nutty if you don't document the standard top level fields\nyou are using.\n\nFields just go on the model like so:\n\n```python\nclass Hero(Model):\n name = Field()\n```\n\n...and enable dot-attribute access, as well as some other goodies.\nFields take several optional arguments -- the first argument is a\ntype, and if used the field will validate any value passed as an\ninstance of that (sub)class. For example:\n\n```python\nclass Hero(Model):\n name = Field(unicode)\n\n# the following will raise a ValueError exception...\nwash = Hero(name=\"Wash\")\n# but this is fine\nwash = Hero(name=u\"Wash\")\n```\n\nIf you don't want this validation, just don't pass in any type. If you\nwant to customize getting and setting, you can pass in `set\\_callback`\nand `get\\_callback` functions to the Field constructor:\n\n```python\nclass Ship(Model):\n type = Field(set_callback=lambda x: \"Firefly\")\n\nship = Ship(type=\"firefly\")\nprint ship.type #prints \"Firefly\"\nship.type = \"NCC 1701\"\nprint ship.type #prints \"Firefly\"\n# overwriting the \"real\" stored value\nship[\"type\"] = \"Millenium Falcon\"\nprint ship.type # prints \"Millenium Falcon\"\n```\n\nYou can also pass an optional default=VALUE, where VALUE is either a\nstatic value like \"foo\" or 42, or it is a callable that returns a static\nvalue like time.time() or datetime.now(). (Thanks @nod!)\n\n```python\nclass Ship(Model):\n name = Field(unicode, default=u\"Dormunder\")\n```\n\nReferenceField\n--------------\nThe ReferenceField class allows (simple) model references to be used.\nThe \"search\" class method lets you pass in model instances and compare.\n\nSo most real world models will look more this:\n\n```python\nclass Ship(Model):\n name = Field(unicode, required=True)\n age = Field(int, default=10)\n type = Field(unicode, default=\"Firefly\")\n\n @classmethod\n def new(cls, name):\n \"\"\" Creating a strict interface for new models \"\"\"\n\n @property\n def crew(self):\n return Crew.search(ship=self)\n\nclass Crew(Model):\n name = Field(unicode, required=True)\n joined = Field(float, default=datetime.now, required=True)\n ship = ReferenceField(Ship)\n```\n\n...and simple usage would look like this:\n\n```python\nserenity = Ship.new(u\"Serenity\")\nserenity.save()\nmal = Crew(name=u\"Malcom Reynolds\", ship=None)\nmal.sav()\nmal.ship = serenity\nmal.save()\n\nprint [person.name for person in serenity.crew]\n# results in [u\"Malcom Reynolds\",]\nprint mal.joined\n# prints out the datetime that the instance was created\n```\n\nNote -- only use a ReferenceField with legacy data if you have been\nstoring DBRef's as the values. If you've just been storing ObjectIds or\nsomething, it may be easier for existing data to just use a Field() with\na `(set|get)\\_callback` do the retrieval logic yourself.\n\n\nPolyModels\n----------\nMongoDB lets you store any fields in any collection -- this means it is\nparticularly well suited for storing and querying across inheritance \nrelationships. I've recently added a new model type of `PolyModel` that\nlets you define this in a (hopefully) simple way.\n\n```python\nclass Person(PolyModel):\n \"\"\" The 'base' person model \"\"\"\n name = Field(unicode, required=True)\n role = Field(unicode, default=u\"person\")\n\n # custom method\n def is_good(self):\n \"\"\" All people are innately good. :) \"\"\"\n return True\n\n # required to determine what `type` something is\n def get_model_key(self):\n return \"role\"\n```\n\nAs you can see, we use the \"role\" field to determine what type a person\nis -- by default, they are all just \"person\" and therefore should return\na Person instance. We need to register some new people types:\n\n```python\n@Person.register\nclass Villain(Person):\n role = Field(unicode, default=u\"villain\")\n\n # Overwriting method\n def is_good(self):\n \"\"\" All villains are not good \"\"\"\n return False\n\n@Person.register(\"questionable\")\nclass FlipFlopper(Person):\n role = Field(unicode, default=u\"questionable\")\n alliance = Field(unicode, default=u\"good\")\n\n def is_good(self):\n return self.alliance == \"good\"\n\n def trade_alliance(self):\n if self.alliance == \"good\":\n self.alliance = \"bad\"\n else:\n self.alliance = \"good\"\n self.save()\n```\n\nThe PolyModel.register decorator takes an optional value argument, which\nis what is used to compare to the field specified by `get_model_key` in\nthe base model. It works with the following pseudo-logic:\n\n* Create a new Person instance (either from the DB or __init__)\n* key = Person.get_model_key() # in this case, it's \"role\"\n* Get current value of \"role\" (or use the default)\n* Check the registered models, find one that matches the role value\n* If a registered model class is found, use that.\n* Otherwise, use the base class (Person in this case)\n\nUsing the above classes that we created / registered, here's a usage example:\n\n```python\nsimon = Person(name=\"Simon Tam\")\nsimon.save()\nsimon.is_good() # True\nbadger = Villain(name=\"Badger\")\nbadger.save()\nbadger.is_good() # False\njayne = FlipFlopper(name=\"Jayne\")\njayne.save()\n\nPerson.find().count() # should be 3\njayne = Person.find(name=\"Jayne\")\nisinstance(jayne, FlipFlopper) # True\njayne.is_good() # True\njayne.trade_alliance()\njayne.is_good() # False\n\nVillain.find().count() # should be 1\n```\n\nContact\n-------\n* Mailing List Web: http://groups.google.com/group/mogo-python\n* Mailing List Address: mogo-python@googlegroups.com\n\nIf you play with this in any way, I'd love to hear about it.\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/joshmarshall/mogo/", "keywords": "", "license": "http://www.apache.org/licenses/LICENSE-2.0", "maintainer": "", "maintainer_email": "", "name": "mogo", "package_url": "https://pypi.org/project/mogo/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/mogo/", "project_urls": { "Homepage": "http://github.com/joshmarshall/mogo/" }, "release_url": "https://pypi.org/project/mogo/0.4.0/", "requires_dist": null, "requires_python": "", "summary": "Simple PyMongo \"schema-less\" object wrapper", "version": "0.4.0" }, "last_serial": 2016029, "releases": { "0.1.1": [ { "comment_text": "", "digests": { "md5": "4612574862c972ab5a64c598d65def7f", "sha256": "1c9cefeb9adfa35f824eaa4d27453b198487bf47cfdd77a739d7e7d282daae28" }, "downloads": -1, "filename": "mogo-0.1.1.tar.gz", "has_sig": false, "md5_digest": "4612574862c972ab5a64c598d65def7f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8631, "upload_time": "2011-03-18T01:15:47", "url": "https://files.pythonhosted.org/packages/36/bd/657dfe62c3847440b190fbb09b92570b4ad7e3cc4bea00e9463744ed606c/mogo-0.1.1.tar.gz" } ], "0.1.2": [ { "comment_text": "", "digests": { "md5": "010c31180a7d43a4d1cd9efc6dab6f4e", "sha256": "ce6ea9bf3c6f9ad51e5f05d90b35bac8abc2f7e7bc95a02af80fc04dd4bade21" }, "downloads": -1, "filename": "mogo-0.1.2.tar.gz", "has_sig": false, "md5_digest": "010c31180a7d43a4d1cd9efc6dab6f4e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7365, "upload_time": "2011-09-26T00:10:10", "url": "https://files.pythonhosted.org/packages/53/0f/3cc148ffd70500cefe9c95a3f2264642e58c7f690d0c85a39cb81ddf915c/mogo-0.1.2.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "e36e52c3c7399228008326762f7fad41", "sha256": "e2368513621befaf964cd0349c6bbf73cd272cba0546c593d826a0b888a92a02" }, "downloads": -1, "filename": "mogo-0.2.0.tar.gz", "has_sig": false, "md5_digest": "e36e52c3c7399228008326762f7fad41", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14415, "upload_time": "2012-04-14T05:56:32", "url": "https://files.pythonhosted.org/packages/58/f6/5c8b1562bd143d31a7829ba029f80c0a1e2f8f866012c7d63c3527e78af5/mogo-0.2.0.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "df734c884ed1c779856003f40383b55f", "sha256": "0067cb84ca9d56d75af771cf19678c2bb68d0507b31d9f5b1d4499c7f673d708" }, "downloads": -1, "filename": "mogo-0.2.1.tar.gz", "has_sig": false, "md5_digest": "df734c884ed1c779856003f40383b55f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14341, "upload_time": "2012-08-30T03:06:27", "url": "https://files.pythonhosted.org/packages/f0/8f/7f0934e3c340d1d9c9e6eb8c41d9416f829e782fc2ea665a95434347c273/mogo-0.2.1.tar.gz" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "17a7414575aa5798439e659375be37aa", "sha256": "97c09cd46ab912e4b6e3aa60c3154729b9fa50e82bf05eefe541af4d780bdb0c" }, "downloads": -1, "filename": "mogo-0.2.2.tar.gz", "has_sig": false, "md5_digest": "17a7414575aa5798439e659375be37aa", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14758, "upload_time": "2013-03-06T00:26:10", "url": "https://files.pythonhosted.org/packages/f1/60/11a7f35abe0d25a4c712a244f570dc6df611bf236dc1c532faf6838e25ad/mogo-0.2.2.tar.gz" } ], "0.2.3": [ { "comment_text": "", "digests": { "md5": "ef0ad61b5ee526d09d625427fb6cbe6f", "sha256": "a0746c9595d58d7129794206e27c0b22146f91c1109f5f7d14399574dcc53488" }, "downloads": -1, "filename": "mogo-0.2.3.tar.gz", "has_sig": false, "md5_digest": "ef0ad61b5ee526d09d625427fb6cbe6f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14699, "upload_time": "2013-11-03T21:12:19", "url": "https://files.pythonhosted.org/packages/c6/c7/b689dec0ae7cfbd5698304ac59689adfad4535b62fb2af1e879e71cf167b/mogo-0.2.3.tar.gz" } ], "0.2.4": [ { "comment_text": "", "digests": { "md5": "84525d3f94a028bd6c77cced31aeb1b3", "sha256": "90f7f9a7c339678c2d040eaecfb96cc259a8b962fabf97eec034af06385f204d" }, "downloads": -1, "filename": "mogo-0.2.4.tar.gz", "has_sig": false, "md5_digest": "84525d3f94a028bd6c77cced31aeb1b3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14801, "upload_time": "2013-12-29T03:01:25", "url": "https://files.pythonhosted.org/packages/04/2e/e33693dace8e5bd437830339262d898a74752cc155ede3478ca91630e23e/mogo-0.2.4.tar.gz" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "1c33725ea543fb30f3f663ae75c1176f", "sha256": "fcfdb638ebeea9c9299be2112059920400e00ba226a2b812c28874364f8d3f9a" }, "downloads": -1, "filename": "mogo-0.3.0.tar.gz", "has_sig": false, "md5_digest": "1c33725ea543fb30f3f663ae75c1176f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13479, "upload_time": "2015-07-11T03:04:05", "url": "https://files.pythonhosted.org/packages/66/cf/998105edaf309b501a42a0ae14db612ddeb8721688e6352ef4fb08de45d0/mogo-0.3.0.tar.gz" } ], "0.3.0a": [ { "comment_text": "", "digests": { "md5": "7ca7a50c2020004feb0ffa47595221d6", "sha256": "ac1b0cd89ccca723c2282c68a621beffabefaea6ea35a6a6353ee65a9bc66c15" }, "downloads": -1, "filename": "mogo-0.3.0a.tar.gz", "has_sig": false, "md5_digest": "7ca7a50c2020004feb0ffa47595221d6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14691, "upload_time": "2014-07-25T17:25:51", "url": "https://files.pythonhosted.org/packages/00/0a/1476af03fa5632dab693a7f5c2796c67c49b5cc0ac16e1cec2f7713662ba/mogo-0.3.0a.tar.gz" } ], "0.3.1": [ { "comment_text": "", "digests": { "md5": "62f4c5faf7c0f427b262cae948f88e99", "sha256": "cf8250655dcbca811085b12b3c7e728028573ec92596c9d95c5a0a849ce1e412" }, "downloads": -1, "filename": "mogo-0.3.1.tar.gz", "has_sig": false, "md5_digest": "62f4c5faf7c0f427b262cae948f88e99", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13469, "upload_time": "2015-11-18T09:07:07", "url": "https://files.pythonhosted.org/packages/eb/c3/295f4be609014a9b953c0044b40e60d52458b3b43ba5ccb131def7c5f334/mogo-0.3.1.tar.gz" } ], "0.3.2": [ { "comment_text": "", "digests": { "md5": "90d066a385f6bcb5842247398a295491", "sha256": "b7c00f74e6934a42bc6d8f474a44728725f309ba607428a0c75f2b715cacb214" }, "downloads": -1, "filename": "mogo-0.3.2.tar.gz", "has_sig": false, "md5_digest": "90d066a385f6bcb5842247398a295491", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13439, "upload_time": "2016-03-14T23:59:06", "url": "https://files.pythonhosted.org/packages/16/e2/62c823f73cc131b0c54cf35374c2d35a55e9ef4eaa463d66c69b6836ac06/mogo-0.3.2.tar.gz" } ], "0.4.0": [ { "comment_text": "", "digests": { "md5": "3b23a7e2bf61579d50aac1f1c993bbc8", "sha256": "569031756c4236a355d7bac7c6704d7552fe7207db9a8cb4668e72920b66871c" }, "downloads": -1, "filename": "mogo-0.4.0.tar.gz", "has_sig": false, "md5_digest": "3b23a7e2bf61579d50aac1f1c993bbc8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13591, "upload_time": "2016-03-19T16:56:38", "url": "https://files.pythonhosted.org/packages/2a/99/93207c093f1a42d85670ce7c9efbdb8b2b6b572a9d3c135151913d43424a/mogo-0.4.0.tar.gz" } ], "0.4.0a": [ { "comment_text": "", "digests": { "md5": "5af2d1398f2a70eec75ae2be5f23cc70", "sha256": "b7c14bd5368c296f0c7d35e1e0aaaf64b8a0a0d6a1a44d23ed8c6d02d814d8c8" }, "downloads": -1, "filename": "mogo-0.4.0a.tar.gz", "has_sig": false, "md5_digest": "5af2d1398f2a70eec75ae2be5f23cc70", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13635, "upload_time": "2015-07-11T08:26:24", "url": "https://files.pythonhosted.org/packages/33/96/e4602a5d2d2c0802784901412cf9ce00751fe92540641978029cb8456b31/mogo-0.4.0a.tar.gz" } ], "0.4.0a1": [ { "comment_text": "", "digests": { "md5": "3b3d7bcef067a5a322e008962e6b55f7", "sha256": "bf27527bf0347a8a58a22a3ee89c15e0368c19a901bf765b273a63575a2c5ff8" }, "downloads": -1, "filename": "mogo-0.4.0a1.tar.gz", "has_sig": false, "md5_digest": "3b3d7bcef067a5a322e008962e6b55f7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13694, "upload_time": "2015-11-21T02:53:53", "url": "https://files.pythonhosted.org/packages/99/db/9673eaa749795c6f50a7f448049a99171ecbe54b4a3cbe67f186ee85f877/mogo-0.4.0a1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "3b23a7e2bf61579d50aac1f1c993bbc8", "sha256": "569031756c4236a355d7bac7c6704d7552fe7207db9a8cb4668e72920b66871c" }, "downloads": -1, "filename": "mogo-0.4.0.tar.gz", "has_sig": false, "md5_digest": "3b23a7e2bf61579d50aac1f1c993bbc8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13591, "upload_time": "2016-03-19T16:56:38", "url": "https://files.pythonhosted.org/packages/2a/99/93207c093f1a42d85670ce7c9efbdb8b2b6b572a9d3c135151913d43424a/mogo-0.4.0.tar.gz" } ] }