Пример использования python с django и другими библиотеками и с его помощью нужно ответить на следующие вопросы
Я совсем новичок в python и в задании мне попался этот университетский пример использования. Мне нужна помощь. Ваша помощь будет высоко оценена заранее. Пожалуйста, помогите мне с этой проблемой.
import os
import django
import datetime
from pytz import timezone
from xwing import settings
from gentella.api_access.emarsys.api import EmarsysApi
from collections import namedtuple
from typing import List
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xwing.settings")
django.setup()
from gentella.emarsys_models.models import ContactSegment
brand_shop_segment = namedtuple('brand_shop_segment', ['id', 'name', 'count', 'brand'])
def get_segment_info_as_df(brand: str) -> List[brand_shop_segment]:
assert brand in settings.EMARSYS_LOGINS.keys(), 'passed \'brand\' is not in EMARSYS
LOGINS'
etc_timezone = timezone('Europe/Amsterdam')
api = EmarsysApi(username=settings.EMARSYS_LOGINS[brand]['username'],
secret_token=settings.EMARSYS_LOGINS[brand]['secret'], tzinfo_obj=etc_timezone)
segment_data = api.get_segments()
segments = []
for segment in segment_data:
segment_count = api.get_segment_contact_count(segment_tuple=segment)
brand_segment_combo = brand_shop_segment(id=segment_count.id, name=segment_count.name,
count=segment_count.count, brand=brand)
segments.append(brand_segment_combo)
return segments
def save_segments(segment_info: List[brand_shop_segment], brand: str) -> None:
today = datetime.datetime.today().date()
ContactSegment.objects.filter(dt=today, brand=brand).delete()
data_list = []
for segment in segment_info:
data = ContactSegment(
id=segment.id,
name=segment.name,
count=segment.count,
brand=segment.brand,
dt=today
)
data_list.append(data)
ContactSegment.objects.bulk_create(data_list)
def main() -> None:
for brand in settings.EMARSYS_LOGINS.keys():
print(brand)
segment_list = get_segment_info_as_df(brand=brand)
save_segments(segment_list, brand=brand)
if _name_ == '_main_':
main()
============================================================================================== Вопросы:
- What type of process is the code supporting?
- Kindly walk us through the code – main functions, objects, general functionality.
- What is exactly the purpose of line 19? Could the code function without it? What would be a drawback of omitting that line?
- In which line(s) is an API request sent?
- What information are we saving from the data source? Please name the exact variables.
- In which line(s) do we send the retrieved data to the database? Are we writing the same variables that we scraped? If not, which is (are) added and based on your experience, why?
- Could you please explain the syntax on line 33? What are the differences between the line 33 and line 18?
- BONUS: Why do you think we need a process like that? What value does it provide? Think bigger, not a technical question. Hint: Automation & MI is part of the Marketing department.