Skip to content

Commit 8a3e28b

Browse files
authored
Merge pull request #21 from PostHog/code-style
Black and isort all the things
2 parents e6f71e4 + 1aa0d63 commit 8a3e28b

21 files changed

+662
-613
lines changed

.github/workflows/ci.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: CI
2+
3+
on:
4+
- pull_request
5+
6+
jobs:
7+
code-quality:
8+
name: Code quality checks
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v2
13+
with:
14+
fetch-depth: 1
15+
16+
- name: Set up Python 3.8
17+
uses: actions/setup-python@v2
18+
with:
19+
python-version: 3.8
20+
21+
- uses: actions/cache@v1
22+
with:
23+
path: ~/.cache/pip
24+
key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }}
25+
restore-keys: |
26+
${{ runner.os }}-pip-
27+
28+
- name: Install dev dependencies
29+
run: |
30+
python -m pip install -e .[dev]
31+
if: steps.cache.outputs.cache-hit != 'true'
32+
33+
- name: Check formatting with black
34+
run: |
35+
black --check .
36+
37+
- name: Check import order with isort
38+
run: |
39+
isort --check-only .
40+
41+
tests:
42+
name: Python tests
43+
runs-on: ubuntu-latest
44+
45+
steps:
46+
- uses: actions/checkout@v1
47+
with:
48+
fetch-depth: 1
49+
50+
- name: Set up Python 3.7
51+
uses: actions/setup-python@v1
52+
with:
53+
python-version: 3.7
54+
55+
- name: Install requirements.txt dependencies with pip
56+
run: |
57+
python -m pip install -e .
58+
59+
- name: Run posthog tests
60+
run: |
61+
python setup.py test

.github/workflows/tests.yml

Lines changed: 0 additions & 27 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
repos:
2+
- repo: https://github.com/psf/black
3+
rev: stable
4+
hooks:
5+
- id: black
6+
- repo: https://github.com/pycqa/isort
7+
rev: 5.7.0
8+
hooks:
9+
- id: isort

example.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
# PostHog Python library example
22

33
# Import the library
4-
import posthog
54
import time
65

6+
import posthog
7+
78
# You can find this key on the /setup page in PostHog
8-
posthog.api_key = ''
9-
posthog.personal_api_key = ''
9+
posthog.api_key = ""
10+
posthog.personal_api_key = ""
1011

1112
# Where you host PostHog, with no trailing /.
1213
# You can remove this line if you're using posthog.com
13-
posthog.host = 'http://127.0.0.1:8000'
14+
posthog.host = "http://127.0.0.1:8000"
1415

1516
# Capture an event
16-
posthog.capture('distinct_id', 'event', {'property1': 'value', 'property2': 'value'})
17+
posthog.capture("distinct_id", "event", {"property1": "value", "property2": "value"})
1718

18-
print(posthog.feature_enabled('beta-feature', 'distinct_id'))
19+
print(posthog.feature_enabled("beta-feature", "distinct_id"))
1920

20-
print('sleeping')
21+
print("sleeping")
2122
time.sleep(45)
2223

23-
print(posthog.feature_enabled('beta-feature', 'distinct_id'))
24+
print(posthog.feature_enabled("beta-feature", "distinct_id"))
2425

2526
# # Alias a previous distinct id with a new one
26-
posthog.alias('distinct_id', 'new_distinct_id')
27+
posthog.alias("distinct_id", "new_distinct_id")
2728

2829
# # Add properties to the person
29-
posthog.identify('distinct_id', {'email': '[email protected]'})
30+
posthog.identify("distinct_id", {"email": "[email protected]"})

posthog/__init__.py

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1+
from typing import Callable, Dict, Optional
12

2-
from posthog.version import VERSION
33
from posthog.client import Client
4-
from typing import Optional, Dict, Callable
4+
from posthog.version import VERSION
55

66
__version__ = VERSION
77

88
"""Settings."""
9-
api_key = None # type: str
10-
host = None # type: str
11-
on_error = None # type: Callable
12-
debug = False # type: bool
13-
send = True # type: bool
14-
sync_mode = False # type: bool
15-
disabled = False # type: bool
16-
personal_api_key = None # type: str
9+
api_key = None # type: str
10+
host = None # type: str
11+
on_error = None # type: Callable
12+
debug = False # type: bool
13+
send = True # type: bool
14+
sync_mode = False # type: bool
15+
disabled = False # type: bool
16+
personal_api_key = None # type: str
1717

1818
default_client = None
1919

2020

2121
def capture(
22-
distinct_id, # type: str,
23-
event, # type: str,
24-
properties=None, # type: Optional[Dict]
25-
context=None, # type: Optional[Dict]
26-
timestamp=None, # type: Optional[datetime.datetime]
27-
message_id=None, # type: Optional[str]
28-
):
22+
distinct_id, # type: str,
23+
event, # type: str,
24+
properties=None, # type: Optional[Dict]
25+
context=None, # type: Optional[Dict]
26+
timestamp=None, # type: Optional[datetime.datetime]
27+
message_id=None, # type: Optional[str]
28+
):
2929
# type: (...) -> None
3030
"""
3131
Capture allows you to capture anything a user does within your system, which you can later use in PostHog to find patterns in usage, work out which features to improve or where people are giving up.
3232
3333
A `capture` call requires
3434
- `distinct id` which uniquely identifies your user
35-
- `event name` to make sure
35+
- `event name` to make sure
3636
- We recommend using [verb] [noun], like `movie played` or `movie updated` to easily identify what your events mean later on.
3737
3838
Optionally you can submit
@@ -43,22 +43,31 @@ def capture(
4343
posthog.capture('distinct id', 'movie played', {'movie_id': '123', 'category': 'romcom'})
4444
```
4545
"""
46-
_proxy('capture', distinct_id=distinct_id, event=event, properties=properties, context=context, timestamp=timestamp, message_id=message_id)
46+
_proxy(
47+
"capture",
48+
distinct_id=distinct_id,
49+
event=event,
50+
properties=properties,
51+
context=context,
52+
timestamp=timestamp,
53+
message_id=message_id,
54+
)
55+
4756

4857
def identify(
49-
distinct_id, # type: str,
50-
properties=None, # type: Optional[Dict]
51-
context=None, # type: Optional[Dict]
52-
timestamp=None, # type: Optional[datetime.datetime]
53-
message_id=None, # type: Optional[str]
54-
):
58+
distinct_id, # type: str,
59+
properties=None, # type: Optional[Dict]
60+
context=None, # type: Optional[Dict]
61+
timestamp=None, # type: Optional[datetime.datetime]
62+
message_id=None, # type: Optional[str]
63+
):
5564
# type: (...) -> None
5665
"""
5766
Identify lets you add metadata on your users so you can more easily identify who they are in PostHog, and even do things like segment users by these properties.
5867
5968
An `identify` call requires
6069
- `distinct id` which uniquely identifies your user
61-
- `properties` with a dict with any key: value pairs
70+
- `properties` with a dict with any key: value pairs
6271
6372
For example:
6473
```python
@@ -68,20 +77,28 @@ def identify(
6877
})
6978
```
7079
"""
71-
_proxy('identify', distinct_id=distinct_id, properties=properties, context=context, timestamp=timestamp, message_id=message_id)
80+
_proxy(
81+
"identify",
82+
distinct_id=distinct_id,
83+
properties=properties,
84+
context=context,
85+
timestamp=timestamp,
86+
message_id=message_id,
87+
)
88+
7289

7390
def group(*args, **kwargs):
7491
"""Send a group call."""
75-
_proxy('group', *args, **kwargs)
92+
_proxy("group", *args, **kwargs)
7693

7794

7895
def alias(
79-
previous_id, # type: str,
80-
distinct_id, # type: str,
81-
context=None, # type: Optional[Dict]
82-
timestamp=None, # type: Optional[datetime.datetime]
83-
message_id=None, # type: Optional[str]
84-
):
96+
previous_id, # type: str,
97+
distinct_id, # type: str,
98+
context=None, # type: Optional[Dict]
99+
timestamp=None, # type: Optional[datetime.datetime]
100+
message_id=None, # type: Optional[str]
101+
):
85102
# type: (...) -> None
86103
"""
87104
To marry up whatever a user does before they sign up or log in with what they do after you need to make an alias call. This will allow you to answer questions like "Which marketing channels leads to users churning after a month?" or "What do users do on our website before signing up?"
@@ -99,13 +116,21 @@ def alias(
99116
posthog.alias('anonymous session id', 'distinct id')
100117
```
101118
"""
102-
_proxy('alias', previous_id=previous_id, distinct_id=distinct_id, context=context, timestamp=timestamp, message_id=message_id)
119+
_proxy(
120+
"alias",
121+
previous_id=previous_id,
122+
distinct_id=distinct_id,
123+
context=context,
124+
timestamp=timestamp,
125+
message_id=message_id,
126+
)
127+
103128

104129
def feature_enabled(
105-
key, # type: str,
106-
distinct_id, # type: str,
107-
default=False, # type: bool
108-
):
130+
key, # type: str,
131+
distinct_id, # type: str,
132+
default=False, # type: bool
133+
):
109134
# type: (...) -> bool
110135
"""
111136
Use feature flags to enable or disable features for users.
@@ -118,33 +143,33 @@ def feature_enabled(
118143
119144
You can call `posthog.load_feature_flags()` before to make sure you're not doing unexpected requests.
120145
"""
121-
return _proxy('feature_enabled', key=key, distinct_id=distinct_id, default=default)
146+
return _proxy("feature_enabled", key=key, distinct_id=distinct_id, default=default)
122147

123148

124149
def page(*args, **kwargs):
125150
"""Send a page call."""
126-
_proxy('page', *args, **kwargs)
151+
_proxy("page", *args, **kwargs)
127152

128153

129154
def screen(*args, **kwargs):
130155
"""Send a screen call."""
131-
_proxy('screen', *args, **kwargs)
156+
_proxy("screen", *args, **kwargs)
132157

133158

134159
def flush():
135160
"""Tell the client to flush."""
136-
_proxy('flush')
161+
_proxy("flush")
137162

138163

139164
def join():
140165
"""Block program until the client clears the queue"""
141-
_proxy('join')
166+
_proxy("join")
142167

143168

144169
def shutdown():
145170
"""Flush all messages and cleanly shutdown the client"""
146-
_proxy('flush')
147-
_proxy('join')
171+
_proxy("flush")
172+
_proxy("join")
148173

149174

150175
def _proxy(method, *args, **kwargs):
@@ -153,9 +178,15 @@ def _proxy(method, *args, **kwargs):
153178
if disabled:
154179
return None
155180
if not default_client:
156-
default_client = Client(api_key, host=host, debug=debug,
157-
on_error=on_error, send=send,
158-
sync_mode=sync_mode, personal_api_key=personal_api_key)
181+
default_client = Client(
182+
api_key,
183+
host=host,
184+
debug=debug,
185+
on_error=on_error,
186+
send=send,
187+
sync_mode=sync_mode,
188+
personal_api_key=personal_api_key,
189+
)
159190

160191
fn = getattr(default_client, method)
161192
return fn(*args, **kwargs)

0 commit comments

Comments
 (0)