Python: with-statement, generators

385 views

Published on

Published in: Software, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
385
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python: with-statement, generators

  1. 1. Cleaning up
  2. 2. with open(filename) as file: #do something
  3. 3. with tmpTable() as table: #do something
  4. 4. class tmpTable(object): ! def __init__(self, hive, create_query): self.hive = hive self.create_query = create_query self.table_name = self._get_tmp_table_name() ! def __enter__(self): query = self.create_query % self.table_name self.hive.run_query(self.create_query) return self.table_name ! def __exit__(self, type, value, traceback): self.hive.drop(self.table_name) ! ! ! ! with tmpTable(hive, create_query) as table_name: # do something
  5. 5. „The data does not fit into my pandas dataframe.“
  6. 6. with open(filename) as file: for line in file: #do something
  7. 7. for x in xrange(0, 100): print x * x
  8. 8. class xrange(object): ! def __init__(self, start, stop): self.start = start self.stop = stop ! def __iter__(self): return self ! def next(self): if self.start > self.stop: raise StopIteration self.start += 1 return self.start
  9. 9. def xrange(start, stop): while start <= stop start += 1 yield start ! for x in xrange(0, 100): print x * x
  10. 10. squares = (x * x for x in xrange(0, 100)) ! for square in squares: print square
  11. 11. sum_of_squares = sum(x * x for x in xrange(0, 100))
  12. 12. squares = (x * x for x in itertools.count(1)) ! for square in squares: print square
  13. 13. def parse_event(line): timestamp, user, eventtype = line.split() return timestamp, eventtype ! ! counts = collections.defaultdict( lambda: collections.defaultdict(int)) ! with open(filename) as file: events = itertools.imap(parse, file) for timestamp, eventtype in events: hour = to_hour(timestamp) counts[eventtype][hour] += 1 histogram
  14. 14. def parse_event(line): timestamp, user, eventtype = line.split() return timestamp, eventtype ! ! with open(filename) as file: events = itertools.imap(parse, file) earliest_100 = heapq.nsmallest(parsed_lines, 100) print earliest_100 subset
  15. 15. def parse_event(line): timestamp, user, eventtype = line.split() return timestamp, eventtype ! def add_random_key(elem): return random.random(), elem ! with open(filename) as file: events = itertools.imap(parse, file) with_random_keys = (add_random_key(elem) for elem in parsed_lines) sample_with_keys = heapq.nlargest(100, sample_size, with_random_keys) ! sample
  16. 16. with open(filename) as file: events = itertools.imap(parse, file) earliest_100 = heapq.nsmallest(parsed_lines, 100) print earliest_100 ! largest_100 = heapq.nlargest(parsed_lines, 100) print largest_100 common pitfalls
  17. 17. with open(filename) as file: events = itertools.imap(parse, file) earliest_100 = heapq.nsmallest(parsed_lines, 100) print earliest_100 ! ! with open(filename) as file: events = itertools.imap(parse, file) ! largest_100 = heapq.nlargest(parsed_lines, 100) print largest_100 common pitfalls

×