Advertisement
Guest User

Untitled

a guest
Jun 18th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Objects"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 22,
  13. "metadata": {
  14. "collapsed": false,
  15. "slideshow": {
  16. "slide_type": "-"
  17. }
  18. },
  19. "outputs": [],
  20. "source": [
  21. "class Author(object):\n",
  22. " MALE = 'mr'\n",
  23. " FEMALE = 'ms'\n",
  24. " \n",
  25. " def __init__(self, is_manning, name, email, gender):\n",
  26. " self.is_manning = is_manning # штатный автор\n",
  27. " self.name = name\n",
  28. " self.email = email\n",
  29. " self.gender = gender\n",
  30. "\n",
  31. " def __repr__(self):\n",
  32. " return \"{}.{} mailto: {} {}\".format(\n",
  33. " self.gender, \n",
  34. " self.name, \n",
  35. " self.email, \n",
  36. " 'S' if self.is_manning else 'NS')\n",
  37. "\n",
  38. "\n",
  39. "class Publisher(object):\n",
  40. " def __init__(self, name, index, email):\n",
  41. " self.name = name\n",
  42. " self.index = index\n",
  43. " self.email = email\n",
  44. "\n",
  45. " def __repr__(self):\n",
  46. " return \"{} ({}) mailto: {}\".format(self.name, self.index, self.email)\n",
  47. "\n",
  48. "\n",
  49. "class Book(object):\n",
  50. " def __init__(self, name, publisher, authors=None):\n",
  51. " self.name = name\n",
  52. " self.publisher = publisher\n",
  53. " self.authors = authors or []\n",
  54. "\n",
  55. " def __repr__(self):\n",
  56. " return \"{}, {}, {}\".format(\n",
  57. " self.name, self.publisher, \", \".join(x.name for x in self.authors))"
  58. ]
  59. },
  60. {
  61. "cell_type": "markdown",
  62. "metadata": {},
  63. "source": [
  64. "# Factories"
  65. ]
  66. },
  67. {
  68. "cell_type": "code",
  69. "execution_count": 23,
  70. "metadata": {
  71. "collapsed": false
  72. },
  73. "outputs": [],
  74. "source": [
  75. "import string\n",
  76. "import factory\n",
  77. "import datetime\n",
  78. "import faker\n",
  79. "from factory import fuzzy\n",
  80. "\n",
  81. "faker = faker.Factory.create()\n",
  82. "\n",
  83. "\n",
  84. "class AuthorFactory(factory.Factory):\n",
  85. " class Meta:\n",
  86. " model = Author\n",
  87. " inline_args = ('is_manning',)\n",
  88. "\n",
  89. " name = factory.LazyAttribute(lambda o: '{} {}'.format(faker.first_name(), faker.last_name()))\n",
  90. " # можно использовать декоратор @lazy_attribute\n",
  91. " email = factory.LazyAttribute(lambda o: '{}@authors.com'.format(o.name.lower().replace(' ', '_')))\n",
  92. " # можно использовать декоратор @factory.iterator\n",
  93. " gender = factory.Iterator([Author.MALE, Author.FEMALE])\n",
  94. " is_manning = False\n",
  95. " \n",
  96. " \n",
  97. "class FemaleAuthorFactory(AuthorFactory):\n",
  98. " \n",
  99. " gender = Author.FEMALE\n",
  100. "\n",
  101. "\n",
  102. "class PublisherFactory(factory.Factory):\n",
  103. " class Meta:\n",
  104. " model = Publisher\n",
  105. " # можно использовать декоратор @factory.sequence\n",
  106. " name = factory.Sequence(lambda n: 'Publisher_{}'.format(n))\n",
  107. " index = fuzzy.FuzzyText(length=6, chars=string.digits, prefix='i_')\n",
  108. " # можно использовать декоратор @factory.lazy_attribute_sequence\n",
  109. " email = factory.LazyAttributeSequence(\n",
  110. " lambda o, n: '{}@{}.publisher.com'.format(\n",
  111. " o.name.lower(),\n",
  112. " n\n",
  113. " ))\n",
  114. "\n",
  115. "\n",
  116. "class BookFactory(factory.Factory):\n",
  117. " class Meta:\n",
  118. " model = Book\n",
  119. " exclude = ('now',)\n",
  120. " \n",
  121. " # служебное поле\n",
  122. " now = factory.LazyAttribute(lambda o: datetime.datetime.now())\n",
  123. " name = factory.Sequence(lambda n: 'Book_{}'.format(n))\n",
  124. " publisher = factory.SubFactory(PublisherFactory)"
  125. ]
  126. },
  127. {
  128. "cell_type": "markdown",
  129. "metadata": {},
  130. "source": [
  131. "# Примеры"
  132. ]
  133. },
  134. {
  135. "cell_type": "code",
  136. "execution_count": 24,
  137. "metadata": {
  138. "collapsed": false
  139. },
  140. "outputs": [
  141. {
  142. "data": {
  143. "text/plain": [
  144. "mr.Derl Von mailto: derl_von@authors.com NS"
  145. ]
  146. },
  147. "execution_count": 24,
  148. "metadata": {},
  149. "output_type": "execute_result"
  150. }
  151. ],
  152. "source": [
  153. "AuthorFactory()"
  154. ]
  155. },
  156. {
  157. "cell_type": "code",
  158. "execution_count": 25,
  159. "metadata": {
  160. "collapsed": false
  161. },
  162. "outputs": [
  163. {
  164. "data": {
  165. "text/plain": [
  166. "ms.Tennessee Glover mailto: tennessee_glover@authors.com NS"
  167. ]
  168. },
  169. "execution_count": 25,
  170. "metadata": {},
  171. "output_type": "execute_result"
  172. }
  173. ],
  174. "source": [
  175. "FemaleAuthorFactory()"
  176. ]
  177. },
  178. {
  179. "cell_type": "code",
  180. "execution_count": 26,
  181. "metadata": {
  182. "collapsed": false
  183. },
  184. "outputs": [
  185. {
  186. "name": "stdout",
  187. "output_type": "stream",
  188. "text": [
  189. "Book_0, Publisher_0 (i_156333) mailto: publisher_0@0.publisher.com, \n",
  190. "False\n"
  191. ]
  192. }
  193. ],
  194. "source": [
  195. "a = BookFactory()\n",
  196. "print a\n",
  197. "print hasattr(a, 'now')"
  198. ]
  199. },
  200. {
  201. "cell_type": "code",
  202. "execution_count": 27,
  203. "metadata": {
  204. "collapsed": false
  205. },
  206. "outputs": [
  207. {
  208. "data": {
  209. "text/plain": [
  210. "Book_1, Publisher_1 (i_994439) mailto: publisher_1@1.publisher.com, "
  211. ]
  212. },
  213. "execution_count": 27,
  214. "metadata": {},
  215. "output_type": "execute_result"
  216. }
  217. ],
  218. "source": [
  219. "BookFactory.build()"
  220. ]
  221. },
  222. {
  223. "cell_type": "code",
  224. "execution_count": 28,
  225. "metadata": {
  226. "collapsed": false
  227. },
  228. "outputs": [
  229. {
  230. "data": {
  231. "text/plain": [
  232. "Book_2, Publisher_2 (i_117052) mailto: publisher_2@2.publisher.com, "
  233. ]
  234. },
  235. "execution_count": 28,
  236. "metadata": {},
  237. "output_type": "execute_result"
  238. }
  239. ],
  240. "source": [
  241. "BookFactory.create()"
  242. ]
  243. },
  244. {
  245. "cell_type": "code",
  246. "execution_count": 29,
  247. "metadata": {
  248. "collapsed": false
  249. },
  250. "outputs": [
  251. {
  252. "data": {
  253. "text/plain": [
  254. "{'name': 'Book_3',\n",
  255. " 'now': datetime.datetime(2015, 5, 16, 7, 36, 16, 394600),\n",
  256. " 'publisher': Publisher_3 (i_123236) mailto: publisher_3@3.publisher.com}"
  257. ]
  258. },
  259. "execution_count": 29,
  260. "metadata": {},
  261. "output_type": "execute_result"
  262. }
  263. ],
  264. "source": [
  265. "BookFactory.attributes()"
  266. ]
  267. },
  268. {
  269. "cell_type": "code",
  270. "execution_count": 30,
  271. "metadata": {
  272. "collapsed": false
  273. },
  274. "outputs": [],
  275. "source": [
  276. "BookFactory.reset_sequence()\n",
  277. "PublisherFactory.reset_sequence()\n",
  278. "AuthorFactory.reset_sequence()\n",
  279. "AuthorFactory.gender.reset()"
  280. ]
  281. },
  282. {
  283. "cell_type": "code",
  284. "execution_count": 31,
  285. "metadata": {
  286. "collapsed": false
  287. },
  288. "outputs": [
  289. {
  290. "name": "stdout",
  291. "output_type": "stream",
  292. "text": [
  293. "# Publisher #\n",
  294. "Publisher_0 (i_374114) mailto: publisher_0@0.publisher.com\n",
  295. "\n",
  296. "# Authors #\n",
  297. "[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",
  298. "\n",
  299. "# Book #\n",
  300. "Book_0, Publisher_0 (i_374114) mailto: publisher_0@0.publisher.com, Jayda Torphy, Octavio Kunze, Loren Stroman\n",
  301. "\n"
  302. ]
  303. }
  304. ],
  305. "source": [
  306. "publisher = PublisherFactory()\n",
  307. "authors = AuthorFactory.create_batch(3)\n",
  308. "print '# Publisher #'\n",
  309. "print publisher\n",
  310. "print ''\n",
  311. "print '# Authors #'\n",
  312. "print authors\n",
  313. "print ''\n",
  314. "print '# Book #'\n",
  315. "print BookFactory(publisher=publisher, authors=authors)\n",
  316. "print ''"
  317. ]
  318. },
  319. {
  320. "cell_type": "markdown",
  321. "metadata": {},
  322. "source": [
  323. "# Добавление провайдера в faker"
  324. ]
  325. },
  326. {
  327. "cell_type": "code",
  328. "execution_count": 32,
  329. "metadata": {
  330. "collapsed": false
  331. },
  332. "outputs": [
  333. {
  334. "data": {
  335. "text/plain": [
  336. "'bar'"
  337. ]
  338. },
  339. "execution_count": 32,
  340. "metadata": {},
  341. "output_type": "execute_result"
  342. }
  343. ],
  344. "source": [
  345. "from faker import Faker\n",
  346. "fake = Faker()\n",
  347. "# инициализируем генератор случайных чисел\n",
  348. "fake.seed(4321)\n",
  349. "\n",
  350. "from faker.providers import BaseProvider\n",
  351. "\n",
  352. "class MyProvider(BaseProvider):\n",
  353. " def foo(self):\n",
  354. " return 'bar'\n",
  355. "\n",
  356. "fake.add_provider(MyProvider)\n",
  357. "\n",
  358. "fake.foo()"
  359. ]
  360. }
  361. ],
  362. "metadata": {
  363. "kernelspec": {
  364. "display_name": "Python 2",
  365. "language": "python",
  366. "name": "python2"
  367. },
  368. "language_info": {
  369. "codemirror_mode": {
  370. "name": "ipython",
  371. "version": 2
  372. },
  373. "file_extension": ".py",
  374. "mimetype": "text/x-python",
  375. "name": "python",
  376. "nbconvert_exporter": "python",
  377. "pygments_lexer": "ipython2",
  378. "version": "2.7.6"
  379. }
  380. },
  381. "nbformat": 4,
  382. "nbformat_minor": 0
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement