Skip to content

Commit 5346e3c

Browse files
committed
Use context manager consistently in examples
1 parent 6ece7a0 commit 5346e3c

File tree

1 file changed

+40
-52
lines changed

1 file changed

+40
-52
lines changed

README.rst

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -176,39 +176,37 @@ City Database
176176
>>>
177177
>>> # This creates a Reader object. You should use the same object
178178
>>> # across multiple requests as creation of it is expensive.
179-
>>> reader = geoip2.database.Reader('/path/to/GeoLite2-City.mmdb')
179+
>>> with geoip2.database.Reader('/path/to/GeoLite2-City.mmdb') as reader:
180180
>>>
181-
>>> # Replace "city" with the method corresponding to the database
182-
>>> # that you are using, e.g., "country".
183-
>>> response = reader.city('128.101.101.101')
181+
>>> # Replace "city" with the method corresponding to the database
182+
>>> # that you are using, e.g., "country".
183+
>>> response = reader.city('128.101.101.101')
184184
>>>
185-
>>> response.country.iso_code
185+
>>> response.country.iso_code
186186
'US'
187-
>>> response.country.name
187+
>>> response.country.name
188188
'United States'
189-
>>> response.country.names['zh-CN']
189+
>>> response.country.names['zh-CN']
190190
u'美国'
191191
>>>
192-
>>> response.subdivisions.most_specific.name
192+
>>> response.subdivisions.most_specific.name
193193
'Minnesota'
194-
>>> response.subdivisions.most_specific.iso_code
194+
>>> response.subdivisions.most_specific.iso_code
195195
'MN'
196196
>>>
197-
>>> response.city.name
197+
>>> response.city.name
198198
'Minneapolis'
199199
>>>
200-
>>> response.postal.code
200+
>>> response.postal.code
201201
'55455'
202202
>>>
203-
>>> response.location.latitude
203+
>>> response.location.latitude
204204
44.9733
205-
>>> response.location.longitude
205+
>>> response.location.longitude
206206
-93.2323
207207
>>>
208-
>>> response.traits.network
208+
>>> response.traits.network
209209
IPv4Network('128.101.101.0/24')
210-
>>>
211-
>>> reader.close()
212210
213211
Anonymous IP Database
214212
^^^^^^^^^^^^^^^^^^^^^
@@ -219,25 +217,24 @@ Anonymous IP Database
219217
>>>
220218
>>> # This creates a Reader object. You should use the same object
221219
>>> # across multiple requests as creation of it is expensive.
222-
>>> reader = geoip2.database.Reader('/path/to/GeoIP2-Anonymous-IP.mmdb')
220+
>>> with geoip2.database.Reader('/path/to/GeoIP2-Anonymous-IP.mmdb') as reader:
223221
>>>
224-
>>> response = reader.anonymous_ip('85.25.43.84')
222+
>>> response = reader.anonymous_ip('85.25.43.84')
225223
>>>
226-
>>> response.is_anonymous
224+
>>> response.is_anonymous
227225
True
228-
>>> response.is_anonymous_vpn
226+
>>> response.is_anonymous_vpn
229227
False
230-
>>> response.is_hosting_provider
228+
>>> response.is_hosting_provider
231229
False
232-
>>> response.is_public_proxy
230+
>>> response.is_public_proxy
233231
False
234-
>>> response.is_tor_exit_node
232+
>>> response.is_tor_exit_node
235233
True
236-
>>> response.ip_address
234+
>>> response.ip_address
237235
'85.25.43.84'
238-
>>> response.network
236+
>>> response.network
239237
IPv4Network('85.25.43.0/24')
240-
>>> reader.close()
241238
242239
ASN Database
243240
^^^^^^^^^^^^
@@ -264,17 +261,14 @@ Connection-Type Database
264261
>>>
265262
>>> # This creates a Reader object. You should use the same object
266263
>>> # across multiple requests as creation of it is expensive.
267-
>>> reader = geoip2.database.Reader('/path/to/GeoIP2-Connection-Type.mmdb')
268-
>>>
269-
>>> response = reader.connection_type('128.101.101.101')
270-
>>>
271-
>>> response.connection_type
264+
>>> with geoip2.database.Reader('/path/to/GeoIP2-Connection-Type.mmdb') as reader:
265+
>>> response = reader.connection_type('128.101.101.101')
266+
>>> response.connection_type
272267
'Corporate'
273-
>>> response.ip_address
268+
>>> response.ip_address
274269
'128.101.101.101'
275-
>>> response.network
270+
>>> response.network
276271
IPv4Network('128.101.101.101/24')
277-
>>> reader.close()
278272
279273
280274
Domain Database
@@ -286,15 +280,12 @@ Domain Database
286280
>>>
287281
>>> # This creates a Reader object. You should use the same object
288282
>>> # across multiple requests as creation of it is expensive.
289-
>>> reader = geoip2.database.Reader('/path/to/GeoIP2-Domain.mmdb')
290-
>>>
291-
>>> response = reader.domain('128.101.101.101')
292-
>>>
293-
>>> response.domain
283+
>>> with geoip2.database.Reader('/path/to/GeoIP2-Domain.mmdb') as reader:
284+
>>> response = reader.domain('128.101.101.101')
285+
>>> response.domain
294286
'umn.edu'
295-
>>> response.ip_address
287+
>>> response.ip_address
296288
'128.101.101.101'
297-
>>> reader.close()
298289
299290
Enterprise Database
300291
^^^^^^^^^^^^^^^^^^^
@@ -354,23 +345,20 @@ ISP Database
354345
>>>
355346
>>> # This creates a Reader object. You should use the same object
356347
>>> # across multiple requests as creation of it is expensive.
357-
>>> reader = geoip2.database.Reader('/path/to/GeoIP2-ISP.mmdb')
358-
>>>
359-
>>> response = reader.isp('1.128.0.0')
360-
>>>
361-
>>> response.autonomous_system_number
348+
>>> with geoip2.database.Reader('/path/to/GeoIP2-ISP.mmdb') as reader:
349+
>>> response = reader.isp('1.128.0.0')
350+
>>> response.autonomous_system_number
362351
1221
363-
>>> response.autonomous_system_organization
352+
>>> response.autonomous_system_organization
364353
'Telstra Pty Ltd'
365-
>>> response.isp
354+
>>> response.isp
366355
'Telstra Internet'
367-
>>> response.organization
356+
>>> response.organization
368357
'Telstra Internet'
369-
>>> response.ip_address
358+
>>> response.ip_address
370359
'1.128.0.0'
371-
>>> response.network
360+
>>> response.network
372361
IPv4Network('1.128.0.0/16')
373-
>>> reader.close()
374362
375363
Database Reader Exceptions
376364
--------------------------

0 commit comments

Comments
 (0)