Skip to content

Instantly share code, notes, and snippets.

@DeoEsor
Created September 19, 2022 08:51
Show Gist options
  • Save DeoEsor/5d568c2004750bb51a87228f651cb8a6 to your computer and use it in GitHub Desktop.
Save DeoEsor/5d568c2004750bb51a87228f651cb8a6 to your computer and use it in GitHub Desktop.
address_generator.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/DeoEsor/5d568c2004750bb51a87228f651cb8a6/address_generator.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "oTIpgCAumzwS",
"outputId": "0a817288-19ab-489e-f324-4aeca2d380cb"
},
"source": [
"'''\n",
"Подключаю гугл диск и инициализирую путь к нему \n",
"'''\n",
"\n",
"from google.colab import drive\n",
"drive.mount('/content/drive')\n",
"sub_path = '/content/drive/MyDrive/'"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "LMguOvVgd2k2",
"outputId": "9c56f71b-c0a9-4c35-8216-13beb9a7e18d"
},
"source": [
"'''\n",
" Устанавливаю пакет для генерации фейковых адресов\n",
"'''\n",
"\n",
"!pip install Faker"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Requirement already satisfied: Faker in /usr/local/lib/python3.7/dist-packages (8.12.1)\n",
"Requirement already satisfied: python-dateutil>=2.4 in /usr/local/lib/python3.7/dist-packages (from Faker) (2.8.2)\n",
"Requirement already satisfied: text-unidecode==1.3 in /usr/local/lib/python3.7/dist-packages (from Faker) (1.3)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.4->Faker) (1.15.0)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "6tNMG-bSgWE3"
},
"source": [
"'''\n",
" Импортирую необходимые для дальнейшей работы пакеты\n",
"'''\n",
"\n",
"from faker import Faker\n",
"import json\n",
"import pathlib"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "DaUkYy8kj6Ck"
},
"source": [
"'''\n",
" Инициализирую данные для генерации адресов\n",
"'''\n",
"\n",
"#Локация адресов(можно указать несколько)\n",
"locale = ['en_US']\n",
"\n",
"#Количество адресов\n",
"count = 10\n",
"\n",
"#Путь - по которому будет сохранён json файл со сгенерированными адресами\n",
"path = \"ColabNotebooks/data/\"\n",
"\n",
"#Имя json файла\n",
"name = \"addresses.json\""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "hVySV0QWgu1M"
},
"source": [
"'''\n",
" Генерирую адреса по ранее заданным параметрам\n",
"'''\n",
"\n",
"#создаю объект генератора\n",
"fake = Faker(locale)\n",
"\n",
"#Декларирую массив адресов\n",
"addresses_arr = []\n",
"\n",
"#Генерирую адреса и заполняю ими массив\n",
"for _ in range(count):\n",
" addresses_arr.append(fake.address())"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "vVzO3A2hpTiA"
},
"source": [
"'''\n",
" Сохраняю результат\n",
"'''\n",
"\n",
"#Декларирую словарь для сериализации \n",
"json_output = {}\n",
"\n",
"#Инициализирую словарь\n",
"json_output[\"addresses\"] = addresses_arr\n",
"\n",
"#Создаю папку, если её не существует\n",
"pathlib.Path(sub_path + path).mkdir(parents=True, exist_ok=True) \n",
"\n",
"#Сериализую словарь в json и сохраняю в указаном файле\n",
"with open(sub_path + path + name, 'w', encoding='utf-8') as f:\n",
" json.dump(json_output, f, ensure_ascii=False, indent=4)"
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment