Wednesday, March 17, 2021

Python Bytes: #225 SELECT Pydantic from MongoDB

<p>Sponsored by <strong>Linode!</strong> <a href="https://pythonbytes.fm/linode"><strong>pythonbytes.fm/linode</strong></a> Special guest: <a href="https://twitter.com/SebaWitowski"><strong>Sebastian Witowski</strong></a></p> <a href='https://www.youtube.com/watch?v=Omdzzl6XHDE' style='font-weight: bold;'>Watch on YouTube</a><br> <br> <p><strong>Brian #1:</strong> <strong>Raspberry Pi Pico</strong></p> <ul> <li><a href="https://www.raspberrypi.org/blog/raspberry-pi-silicon-pico-now-on-sale/">Release Announcement</a></li> <li><a href="https://www.tomshardware.com/reviews/raspberry-pi-pico-review">A review</a></li> <li>$4 microcontroller</li> <li>Small</li> <li>Extremely low power needs.</li> <li>Built on RP2040, a brand-new chip developed by Raspberry Pi</li> <li>Related: <a href="https://codewith.mu/"><strong>Mu : codewith.mu</strong></a><strong>,</strong> <a href="https://mu.readthedocs.io/en/latest/changes.html">1.1.0-beta.2</a> <ul> <li>Mu is “a simple Python editor for beginner programmers.”</li> <li>1.1.0 support new boards, including Pico, Lego Spike, plus lots of new fixes.</li> </ul></li> </ul> <p><strong>Michael #2:</strong> <a href="https://dev.to/romanright/announcing-beanie-mongodb-odm-56e"><strong>New MongoDB ODM: Beanie</strong></a> </p> <ul> <li>via PyCoders</li> <li>Beanie - is an asynchronous ODM for MongoDB, based on <a href="https://motor.readthedocs.io/en/stable/">Motor</a> and <a href="https://pydantic-docs.helpmanual.io/">Pydantic</a>.</li> <li>Very new but also very exciting.</li> <li>Main component of Beanie is <a href="https://pydantic-docs.helpmanual.io/">Pydantic</a>. It helps to implement the main feature - data structuring. </li> <li>Beanie <code>Document</code> - is an abstraction over the Pydantic <code>BaseModel</code> that allows working with Python objects at the application level and JSON objects at the database level.</li> <li>Example, classes:</li> </ul> <pre><code> class TagColors(str, Enum): RED = "RED" BLUE = "BLUE" GREEN = "GREEN" class Tag(BaseModel): name: str color: TagColors = TagColors.BLUE class Note(Document): # This is the document structure title: str text: Optional[str] tag_list: List[Tag] = [] </code></pre> <p><strong>Sebastian #3:</strong> <a href="https://sourcery.ai/"><strong>Sourcery</strong></a> </p> <ul> <li>No, not the Terry Pratchett novel (although this one is pretty cool too!)</li> <li>Gives you refactoring recommendations in your code editor</li> <li>Integrates with PyCharm and VS Code</li> <li>Super easy to use - you get suggestions as you type and with one click you can apply them</li> <li>Free to use in the code editor (you will need a personal token) and paid plans with analytics, CI integration, etc.</li> <li>It keeps finding errors in my code (well, maybe I'm just a bad programmer 😉)</li> </ul> <p><strong>Michael #4:</strong> <a href="https://neomodel.readthedocs.io/en/latest/"><strong>Neomodel</strong></a></p> <ul> <li>An Object Graph Mapper (OGM) for the <a href="https://www.neo4j.org">neo4j</a> graph database, built on the awesome <a href="https://github.com/neo4j/neo4j-python-driver">neo4j_driver</a></li> <li>Features:</li> <li>Familiar Django model style definitions.</li> <li>Powerful query API.</li> <li>Enforce your schema through cardinality restrictions.</li> <li>Full transaction support.</li> <li>Thread safe.</li> <li>pre/post save/delete hooks.</li> <li>Django integration via <a href="https://github.com/neo4j-contrib/django-neomodel">django_neomodel</a></li> <li>Example of classes</li> </ul> <pre><code> from neomodel import (config, StructuredNode, StringProperty, IntegerProperty, UniqueIdProperty, RelationshipTo) config.DATABASE_URL = 'bolt://neo4j:password@localhost:7687' class Country(StructuredNode): code = StringProperty(unique_index=True, required=True) class Person(StructuredNode): uid = UniqueIdProperty() name = StringProperty(unique_index=True) age = IntegerProperty(index=True, default=0) # traverse outgoing IS_FROM relations, inflate to Country objects country = RelationshipTo(Country, 'IS_FROM') </code></pre> <ul> <li>Relationships</li> </ul> <pre><code> germany = Country.nodes.filter(code='DE) jim = Person.nodes.get(name='Jim') jim.country.connect(germany) if jim.country.is_connected(germany): print("Jim's from Germany") for p in germany.inhabitant.all(): print(p.name) # Jim, ... len(germany.inhabitant) # N: int # Find people called 'Jim' in germany germany.inhabitant.search(name='Jim') # Find all the people called in germany except 'Jim' germany.inhabitant.exclude(name='Jim') </code></pre> <p><strong>Brian 5#:</strong> <a href="https://blog.thea.codes/my-python-testing-style-guide/#a-mock-must-always-have-a-spec"><strong>A mock must always have a spec</strong></a></p> <ul> <li>From Stargirl Flowers “<a href="https://blog.thea.codes/my-python-testing-style-guide/">My Python testing style guide</a>”</li> <li>Great guide altogether, but this bit about mock specs is awesome.</li> <li>Some mocking guidance: <ul> <li>Use real objects for collaborators whenever possible</li> <li>A mock must always have a spec with <code>mock.create_autospec()</code> or <code>mock.patch(..., autospec=True)</code>. <ul> <li>“This ensures there's some connection between your mock and the real collaborator's interface. If you change the collaborator's interface in a way that breaks downstream targets, those targets tests will rightfully fail.”</li> </ul></li> <li>Consider using a stub or fake (with examples)</li> <li>Consider a spy (real object + mock wrapper lets you assert called and such)</li> <li>Don't give mock/stubs/fakes special names</li> <li>Use factory helpers to create complex collaborators</li> </ul></li> <li>And then some random weird advice: <ul> <li>“Use fixtures sparingly” - Now them’s fighting words. :)</li> </ul></li> </ul> <p><strong>Sebastian #6:</strong> <a href="https://pypi.org/project/conference-radar/"><strong>Conference radar</strong></a></p> <ul> <li>The PyCon 2021 Call for Proposal acceptance emails will be sent soon, so let's talk about conferences.</li> <li>It's 2021, and just like the last year, most conferences are moving to an online format.</li> <li>Which is great, because it's so much easier to attend them. Not only the tickets got cheaper or even free, but you also don't have to pay for the accommodation, plane tickets and you don't have to actually fly anywhere.</li> <li>But how do you find what are the upcoming conferences? There is a list of conferences at python.org, but it doesn't have the smaller, local events, and you don't immediately see when each conference is taking place.</li> <li>I've found a tool called "conference radar" - a Python package that gives you a CLI tool to check for upcoming Python conferences!</li> <li>It prints a nice ASCII table with the dates of each conference. There are even some options that you can pass, for example, to see which conferences have an open Call for Proposal, in case you want to submit something. </li> <li>The main downside is that plenty of conferences are not included there, but I hope that the list of sources will be expanded in the future. The CFP flag is also not working very well, I guess, because it's hard to parse the data sources and extract this information automatically.</li> <li>So far, my best way to stay on top of the open CFPs is to follow my friend Miroslav on Twitter.</li> <li>MK: Heads up on installation. They say you can <code>pip install conrad</code> but the actual command is <code>pip install conference-radar</code></li> </ul> <p><strong>Extras</strong></p> <p>Michael</p> <ul> <li>Announcing Modern Python Projects course: <a href="https://talkpython.fm/modern-python-projects"><strong>talkpython.fm/modern-python-projects</strong></a></li> <li>Now highlighting <strong>live</strong> livestreams on Python Bytes: <a href="https://pythonbytes.fm/stream/live"><strong>pythonbytes.fm/stream/live</strong></a></li> <li>Mars again. Yes, <strong>Python IS on Mars</strong>. See <a href="https://twitter.com/tjmcgrew/status/1370009196167626752">tweet</a>.</li> <li>Signups for the Python Language Summit at PyCon (online only) <a href="https://twitter.com/gvanrossum/status/1371563816786358274?cn=ZmxleGlibGVfcmVjcw%3D%3D&amp;refsrc=email"><strong>are now open</strong></a>.</li> </ul> <p>Sebastian</p> <ul> <li>I've started using VS Code in the browser (a new project for a new client), and it's surprisingly good! I would never try it myself (I love to have all my tools installed locally on my computer). The worst part? Learning to not click "Ctrl+W" when I want to close a tab in VS Code (as it closes the whole tab in my browser). I'm curious to see if it will ever become a standard in programming. It's definitely a great way to set up a standardized development platform for the whole team.</li> </ul> <p><strong>Joke</strong>: <strong><a href="https://trello-attachments.s3.amazonaws.com/6041d3e66056524cad8fd110/460x936/02ad871763c0031a1fab0ef4c57be88f/Screen_Shot_2021-03-04_at_10.35.54_PM.png">He has WiFi</a></strong></p>

from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...