Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Objects"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {
- "collapsed": false,
- "slideshow": {
- "slide_type": "-"
- }
- },
- "outputs": [],
- "source": [
- "class Author(object):\n",
- " MALE = 'mr'\n",
- " FEMALE = 'ms'\n",
- " \n",
- " def __init__(self, is_manning, name, email, gender):\n",
- " self.is_manning = is_manning # штатный автор\n",
- " self.name = name\n",
- " self.email = email\n",
- " self.gender = gender\n",
- "\n",
- " def __repr__(self):\n",
- " return \"{}.{} mailto: {} {}\".format(\n",
- " self.gender, \n",
- " self.name, \n",
- " self.email, \n",
- " 'S' if self.is_manning else 'NS')\n",
- "\n",
- "\n",
- "class Publisher(object):\n",
- " def __init__(self, name, index, email):\n",
- " self.name = name\n",
- " self.index = index\n",
- " self.email = email\n",
- "\n",
- " def __repr__(self):\n",
- " return \"{} ({}) mailto: {}\".format(self.name, self.index, self.email)\n",
- "\n",
- "\n",
- "class Book(object):\n",
- " def __init__(self, name, publisher, authors=None):\n",
- " self.name = name\n",
- " self.publisher = publisher\n",
- " self.authors = authors or []\n",
- "\n",
- " def __repr__(self):\n",
- " return \"{}, {}, {}\".format(\n",
- " self.name, self.publisher, \", \".join(x.name for x in self.authors))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Factories"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "import string\n",
- "import factory\n",
- "import datetime\n",
- "import faker\n",
- "from factory import fuzzy\n",
- "\n",
- "faker = faker.Factory.create()\n",
- "\n",
- "\n",
- "class AuthorFactory(factory.Factory):\n",
- " class Meta:\n",
- " model = Author\n",
- " inline_args = ('is_manning',)\n",
- "\n",
- " name = factory.LazyAttribute(lambda o: '{} {}'.format(faker.first_name(), faker.last_name()))\n",
- " # можно использовать декоратор @lazy_attribute\n",
- " email = factory.LazyAttribute(lambda o: '{}@authors.com'.format(o.name.lower().replace(' ', '_')))\n",
- " # можно использовать декоратор @factory.iterator\n",
- " gender = factory.Iterator([Author.MALE, Author.FEMALE])\n",
- " is_manning = False\n",
- " \n",
- " \n",
- "class FemaleAuthorFactory(AuthorFactory):\n",
- " \n",
- " gender = Author.FEMALE\n",
- "\n",
- "\n",
- "class PublisherFactory(factory.Factory):\n",
- " class Meta:\n",
- " model = Publisher\n",
- " # можно использовать декоратор @factory.sequence\n",
- " name = factory.Sequence(lambda n: 'Publisher_{}'.format(n))\n",
- " index = fuzzy.FuzzyText(length=6, chars=string.digits, prefix='i_')\n",
- " # можно использовать декоратор @factory.lazy_attribute_sequence\n",
- " email = factory.LazyAttributeSequence(\n",
- " lambda o, n: '{}@{}.publisher.com'.format(\n",
- " o.name.lower(),\n",
- " n\n",
- " ))\n",
- "\n",
- "\n",
- "class BookFactory(factory.Factory):\n",
- " class Meta:\n",
- " model = Book\n",
- " exclude = ('now',)\n",
- " \n",
- " # служебное поле\n",
- " now = factory.LazyAttribute(lambda o: datetime.datetime.now())\n",
- " name = factory.Sequence(lambda n: 'Book_{}'.format(n))\n",
- " publisher = factory.SubFactory(PublisherFactory)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Примеры"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "mr.Derl Von mailto: derl_von@authors.com NS"
- ]
- },
- "execution_count": 24,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "AuthorFactory()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "ms.Tennessee Glover mailto: tennessee_glover@authors.com NS"
- ]
- },
- "execution_count": 25,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "FemaleAuthorFactory()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Book_0, Publisher_0 (i_156333) mailto: publisher_0@0.publisher.com, \n",
- "False\n"
- ]
- }
- ],
- "source": [
- "a = BookFactory()\n",
- "print a\n",
- "print hasattr(a, 'now')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "Book_1, Publisher_1 (i_994439) mailto: publisher_1@1.publisher.com, "
- ]
- },
- "execution_count": 27,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "BookFactory.build()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "Book_2, Publisher_2 (i_117052) mailto: publisher_2@2.publisher.com, "
- ]
- },
- "execution_count": 28,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "BookFactory.create()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'name': 'Book_3',\n",
- " 'now': datetime.datetime(2015, 5, 16, 7, 36, 16, 394600),\n",
- " 'publisher': Publisher_3 (i_123236) mailto: publisher_3@3.publisher.com}"
- ]
- },
- "execution_count": 29,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "BookFactory.attributes()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "BookFactory.reset_sequence()\n",
- "PublisherFactory.reset_sequence()\n",
- "AuthorFactory.reset_sequence()\n",
- "AuthorFactory.gender.reset()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "# Publisher #\n",
- "Publisher_0 (i_374114) mailto: publisher_0@0.publisher.com\n",
- "\n",
- "# Authors #\n",
- "[mr.Jayda Torphy mailto: jayda_torphy@authors.com NS, ms.Octavio Kunze mailto: octavio_kunze@authors.com NS, mr.Loren Stroman mailto: loren_stroman@authors.com NS]\n",
- "\n",
- "# Book #\n",
- "Book_0, Publisher_0 (i_374114) mailto: publisher_0@0.publisher.com, Jayda Torphy, Octavio Kunze, Loren Stroman\n",
- "\n"
- ]
- }
- ],
- "source": [
- "publisher = PublisherFactory()\n",
- "authors = AuthorFactory.create_batch(3)\n",
- "print '# Publisher #'\n",
- "print publisher\n",
- "print ''\n",
- "print '# Authors #'\n",
- "print authors\n",
- "print ''\n",
- "print '# Book #'\n",
- "print BookFactory(publisher=publisher, authors=authors)\n",
- "print ''"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Добавление провайдера в faker"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'bar'"
- ]
- },
- "execution_count": 32,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "from faker import Faker\n",
- "fake = Faker()\n",
- "# инициализируем генератор случайных чисел\n",
- "fake.seed(4321)\n",
- "\n",
- "from faker.providers import BaseProvider\n",
- "\n",
- "class MyProvider(BaseProvider):\n",
- " def foo(self):\n",
- " return 'bar'\n",
- "\n",
- "fake.add_provider(MyProvider)\n",
- "\n",
- "fake.foo()"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 2",
- "language": "python",
- "name": "python2"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 2
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.6"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement