📜 ⬆️ ⬇️

Python guard wallet



Once I wandered to the site of a mobile operator in search of something interesting. Interesting was only the presence of a large number of different tariff plans for every taste and color. Here - a low tariff within the network, there - tariffing for the duration of the call, and here is the fixed charge for the call. In general, marketers on ZA worked for glory. On the sites of other operators the picture was similar. I was wondering how much my mobile spending would change depending on the selected tariff. But only the power of thought was not enough to analyze the statistics of calls in recent months and compare them with all tariffs. Having decided to postpone this matter until better times, I press Alt+Tab and get into - correctly, in the console with the Python 2.7.5+ and the inviting prompt >>>

Immediately, I’ll clarify that there are only 3 major mobile operators on the local market, so it’s not difficult to create a table of the most attractive tariff plans for me. Those turned out to be as much as 10:


The next step was to get the call history for the last 3 months. This feature was available directly from the site, thanks to my operator. Downloading PDF, turned it into a text file using PDFMiner (by the way, also written in python). The result was not bad, but a couple of errors in the text were noticed, so instead of dancing with a tambourine around this utility, the data was transferred to a text file using a banal copy-paste directly from a PDF file. This file was fed to our python:
')
 #!/usr/bin/python import MySQLdb as mysql con = mysql.connect('localhost', 'mobile', 'mobile', 'mobile') cur = con.cursor() with open('calls.log') as f: for line in f: if 'MOC' in line: tokens = line.split() if len(tokens) == 11: cur.execute("INSERT INTO calls (operator, amount) VALUES('" + tokens[1] + "','" + tokens[6] + "')") con.commit() 

Considering that I practically do not use SMS, the sample was made only by outgoing calls (mobile-originated call, MOC). As a result, the call log was saved in the MySQL database for further manipulations.

Further manipulations

The next step is quite predictable - to calculate how much hard-earned money by overworking money would be spent from each of the tariff plans. Data on calls and tariffs are already in the database. And again a little python code:

 cur.execute("SELECT operator,plan,call_init,first_min_int,first_min_ext,min_int,min_ext FROM tariffs") rows = cur.fetchall() for row in rows: operator, plan, call_init, first_min_int, first_min_ext, min_int, min_ext = row #   cur.execute("SELECT COUNT(*) FROM calls") total = call_init*cur.fetchone()[0] #     cur.execute("SELECT COALESCE(SUM(LEAST(amount,60)),0) FROM calls WHERE operator='" + operator + "'") total += cur.fetchone()[0]/60*first_min_int #  ,     cur.execute("SELECT COALESCE(SUM(amount-60),0) FROM calls WHERE operator='" + operator + "' AND amount > 60") total += cur.fetchone()[0]/60*min_int #     cur.execute("SELECT COALESCE(SUM(LEAST(amount,60)),0) FROM calls WHERE operator<>'" + operator + "'") total += cur.fetchone()[0]/60*first_min_ext #  ,     cur.execute("SELECT COALESCE(SUM(amount-60),0) FROM calls WHERE operator<>'" + operator + "' AND amount > 60") total += cur.fetchone()[0]/60*min_ext print plan + " : " + str(round(total/100, 2)) + " .." 

This is what we have at the exit:

 Magti Standard : 47.1 ye Magti I Alternative : 56.5 ye Bani Standard : 29.72 ye Bani Zero+ : 26.67 ye Geocell 000 : 49.94 ye Geocell 1-10 : 35.86 ye Geocell 12 : 35.66 ye Beeline 007 : 37.69 ye Beeline Non Stop : 40.7 ye Bani Universal : 39.21 ye 

The same data in the form of a diagram, for clarity:


findings

Thus, if we do not consider the extreme values, then the standard deviation turned out to be quite small, including among different telecom operators. However, even such a simple analysis can help save more than 2 times when choosing the most advantageous tariff plan compared to the least profitable, considering the specifics of a particular user.

Source: https://habr.com/ru/post/211490/


All Articles