Skip to content

Commit 724289f

Browse files
committed
Update make_chart function arguments
1 parent 7ff825a commit 724289f

File tree

3 files changed

+90
-81
lines changed

3 files changed

+90
-81
lines changed

README.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ print(f"Creation Time: {stats.creation_time}")
147147
### 📈 Generating Insightful Charts
148148

149149
```python
150-
plt = stats.make_chart(stats.browsers_analysis, chart_type="bar") # this returns an object of matplotlib
150+
plt = stats.make_chart(data="browsers_analysis", chart_type="bar") # this returns an object of matplotlib
151151
plt.show()
152152

153153
# ... and more (see below)
@@ -171,39 +171,39 @@ plt.show()
171171

172172
| Parameters | Description |
173173
|--------------------------|---------------------------------------------------------|
174-
| data | Type of data to visualize (e.g., stats.browsers_analysis, see below). |
174+
| data | Type of data to visualize (e.g., 'browsers_analysis', see below). |
175175
| chart_type | Type of chart to create (e.g., "bar", "pie", "line", see below). |
176176
| days | Number of days to consider for time-based analysis. (only for `last_n_days_analysis` and `last_n_days_unique_analysis`) |
177177

178178
#### Valid Data that can be passed to make the chart
179179

180-
- `stats.browsers_analysis`
181-
- `stats.platforms_analysis`
182-
- `stats.country_analysis`
183-
- `stats.referrers_analysis`
184-
- `stats.clicks_analysis`
185-
- `stats.unique_browsers_analysis`
186-
- `stats.unique_platforms_analysis`
187-
- `stats.unique_country_analysis`
188-
- `stats.unique_referrers_analysis`
189-
- `stats.unique_clicks_analysis`
190-
- `stats.last_n_days_analysis`
191-
- `stats.last_n_days_unique_analysis`
180+
- `'browsers_analysis'`
181+
- `'platforms_analysis'`
182+
- `'country_analysis'`
183+
- `'referrers_analysis'`
184+
- `'clicks_analysis'`
185+
- `'unique_browsers_analysis'`
186+
- `'unique_platforms_analysis'`
187+
- `'unique_country_analysis'`
188+
- `'unique_referrers_analysis'`
189+
- `'unique_clicks_analysis'`
190+
- `'last_n_days_analysis'`
191+
- `'last_n_days_unique_analysis'`
192192

193193
#### Valid Chart types
194194

195-
- bar
196-
- pie
197-
- line
198-
- scatter
199-
- hist
200-
- box
201-
- area
195+
- 'bar'
196+
- 'pie'
197+
- 'line'
198+
- 'scatter'
199+
- 'hist'
200+
- 'box'
201+
- 'area'
202202

203203
#### Usage Example
204204

205205
```python
206-
plt = stats.make_chart(stats.browsers_analysis, chart_type="bar")
206+
plt = stats.make_chart('browsers_analysis', chart_type="bar")
207207
plt.show()
208208
```
209209

py_spoo_url/statistics.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,38 @@ def get(self):
6666
else:
6767
raise Exception(f"Error {r.status_code}: {r.text}")
6868

69-
def validate_data(self, data):
70-
valid_data = [
71-
self.browsers_analysis,
72-
self.platforms_analysis,
73-
self.country_analysis,
74-
self.referrers_analysis,
75-
self.clicks_analysis,
76-
self.unique_browsers_analysis,
77-
self.unique_platforms_analysis,
78-
self.unique_country_analysis,
79-
self.unique_referrers_analysis,
80-
self.unique_clicks_analysis,
81-
self.last_n_days_analysis,
82-
self.last_n_days_unique_analysis,
83-
]
84-
85-
if data not in valid_data:
86-
raise Exception(
87-
"Invalid data type. Valid data types are: {}".format(valid_data)
88-
)
89-
90-
def make_chart(self, data, chart_type: str = "bar", days: int=7, **kwargs):
91-
92-
self.validate_data(data)
69+
def make_chart(self, data: Literal[
70+
'browsers_analysis',
71+
'platforms_analysis',
72+
'country_analysis',
73+
'referrers_analysis',
74+
'clicks_analysis',
75+
'unique_browsers_analysis',
76+
'unique_platforms_analysis',
77+
'unique_country_analysis',
78+
'unique_referrers_analysis',
79+
'unique_clicks_analysis',
80+
'last_n_days_analysis',
81+
'last_n_days_unique_analysis'
82+
], chart_type: Literal['bar', 'pie', 'line', 'scatter', 'hist', 'box', 'area']='bar', days:int=7, **kwargs):
83+
data_methods = {
84+
'browsers_analysis': self.browsers_analysis,
85+
'platforms_analysis': self.platforms_analysis,
86+
'country_analysis': self.country_analysis,
87+
'referrers_analysis': self.referrers_analysis,
88+
'clicks_analysis': self.clicks_analysis,
89+
'unique_browsers_analysis': self.unique_browsers_analysis,
90+
'unique_platforms_analysis': self.unique_platforms_analysis,
91+
'unique_country_analysis': self.unique_country_analysis,
92+
'unique_referrers_analysis': self.unique_referrers_analysis,
93+
'unique_clicks_analysis': self.unique_clicks_analysis,
94+
'last_n_days_analysis': self.last_n_days_analysis,
95+
'last_n_days_unique_analysis': self.last_n_days_unique_analysis
96+
}
97+
try:
98+
data = data_methods[data]
99+
except KeyError:
100+
raise ValueError("Invalid data type. Valid data types are: {}".format(list(data_methods.keys())))
93101

94102
if data == self.last_n_days_analysis or data == self.last_n_days_unique_analysis:
95103
data = data(days=days)
@@ -111,7 +119,7 @@ def make_chart(self, data, chart_type: str = "bar", days: int=7, **kwargs):
111119
elif chart_type == "area":
112120
plt.stackplot(data.keys(), data.values(), **kwargs)
113121
else:
114-
raise Exception("Invalid chart type. Valid chart types are: bar, pie")
122+
raise Exception("Invalid chart type. Valid chart types are: bar, pie, line, scatter, hist, box, area")
115123

116124
return plt
117125

@@ -227,7 +235,7 @@ def export_data(self, filename:str = "export.xlsx", filetype: Literal['csv', 'xl
227235
elif filetype == "csv":
228236
self.export_to_csv(filename)
229237
else:
230-
raise ValueError("Invalid file type. Choose either 'csv' or 'xlsx'.")
238+
raise ValueError("Invalid file type. Choose either 'csv', 'json' or 'xlsx'.")
231239

232240
def export_to_excel(self, filename:str = "export.xlsx"):
233241

setup.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
11
from setuptools import setup, find_packages
22

3-
with open('README.md', 'r', encoding='utf-8') as f:
3+
with open("README.md", "r", encoding="utf-8") as f:
44
long_description = f.read()
55

66
setup(
77
name="py_spoo_url",
8-
version="0.0.1",
8+
version="0.0.2",
99
description="Simple URL shortening with advanced analytics, emoji aliases, and more using spoo.me.",
1010
long_description=long_description,
11-
long_description_content_type='text/markdown',
12-
author='https://github.com/spoo-me',
13-
author_email='support@spoo.me',
14-
url='https://github.com/spoo-me/py_spoo_url',
15-
license='MIT',
11+
long_description_content_type="text/markdown",
12+
author="https://github.com/spoo-me",
13+
author_email="support@spoo.me",
14+
url="https://github.com/spoo-me/py_spoo_url",
15+
license="MIT",
1616
packages=find_packages(),
17-
install_requires=[
18-
"matplotlib",
19-
"requests",
20-
"geopandas",
21-
"pandas",
22-
"openpyxl"
23-
],
17+
install_requires=["matplotlib", "requests", "geopandas", "pandas", "openpyxl"],
2418
classifiers=[
25-
'Development Status :: 5 - Production/Stable',
26-
'Intended Audience :: Developers',
27-
'Intended Audience :: Customer Service',
28-
'License :: OSI Approved :: MIT License',
29-
'Natural Language :: English',
30-
'Programming Language :: Python :: 3',
31-
'Programming Language :: Python :: 3.6',
32-
'Programming Language :: Python :: 3.7',
33-
'Programming Language :: Python :: 3.8',
34-
'Programming Language :: Python :: 3.9',
35-
'Programming Language :: Python :: 3.10',
36-
'Programming Language :: Python :: 3.11',
37-
'Typing :: Typed'
19+
"Development Status :: 5 - Production/Stable",
20+
"Intended Audience :: Developers",
21+
"Intended Audience :: Customer Service",
22+
"License :: OSI Approved :: MIT License",
23+
"Natural Language :: English",
24+
"Programming Language :: Python :: 3",
25+
"Programming Language :: Python :: 3.6",
26+
"Programming Language :: Python :: 3.7",
27+
"Programming Language :: Python :: 3.8",
28+
"Programming Language :: Python :: 3.9",
29+
"Programming Language :: Python :: 3.10",
30+
"Programming Language :: Python :: 3.11",
31+
"Typing :: Typed",
3832
],
39-
project_urls={
40-
'spoo.me Service': 'https://spoo.me'
41-
},
33+
project_urls={"spoo.me Service": "https://spoo.me"},
4234
keywords=[
43-
'URL shortening', 'spoo.me', 'URL analytics', 'emoji aliases', 'Python API',
44-
'custom aliasing', 'password protection', 'click tracking', 'graph creation',
45-
'country heatmaps', 'URL management', 'Python package'
35+
"URL shortening",
36+
"spoo.me",
37+
"URL analytics",
38+
"emoji aliases",
39+
"Python API",
40+
"custom aliasing",
41+
"password protection",
42+
"click tracking",
43+
"graph creation",
44+
"country heatmaps",
45+
"URL management",
46+
"Python package",
4647
],
4748
)

0 commit comments

Comments
 (0)