SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
Cleaning up
with open(filename) as file:
#do something
with tmpTable() as table:
#do something
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
„The data does not fit into
my pandas dataframe.“
with open(filename) as file:
for line in file:
#do something
for x in xrange(0, 100):
print x * x
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
def xrange(start, stop):
while start <= stop
start += 1
yield start
!
for x in xrange(0, 100):
print x * x
squares = (x * x for x in xrange(0, 100))
!
for square in squares:
print square
sum_of_squares = sum(x * x for x in xrange(0, 100))
squares = (x * x for x in itertools.count(1))
!
for square in squares:
print square
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
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
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
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

Php 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodPhp 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodJeremy Kendall
 
Drupal module development
Drupal module developmentDrupal module development
Drupal module developmentDamjan Cvetan
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP ArraysAhmed Swilam
 
GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2Heather Rock
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPJeremy Kendall
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPJeremy Kendall
 
Uncovering Iterators
Uncovering IteratorsUncovering Iterators
Uncovering Iteratorssdevalk
 
6 things about perl 6
6 things about perl 66 things about perl 6
6 things about perl 6brian d foy
 
COMP2021 Final Project - LightHTML
COMP2021 Final Project - LightHTMLCOMP2021 Final Project - LightHTML
COMP2021 Final Project - LightHTMLConrad Lo
 
PHP for NonProgrammers (DrupalCon SF 2010)
PHP for NonProgrammers (DrupalCon SF 2010)PHP for NonProgrammers (DrupalCon SF 2010)
PHP for NonProgrammers (DrupalCon SF 2010)Four Kitchens
 
HappyKardashian.com for #FVCP
HappyKardashian.com for #FVCPHappyKardashian.com for #FVCP
HappyKardashian.com for #FVCPEric Michalsen
 

Was ist angesagt? (18)

Php 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodPhp 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the Good
 
php part 2
php part 2php part 2
php part 2
 
Drupal module development
Drupal module developmentDrupal module development
Drupal module development
 
Swift tips and tricks
Swift tips and tricksSwift tips and tricks
Swift tips and tricks
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHP
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHP
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Uncovering Iterators
Uncovering IteratorsUncovering Iterators
Uncovering Iterators
 
6 things about perl 6
6 things about perl 66 things about perl 6
6 things about perl 6
 
COMP2021 Final Project - LightHTML
COMP2021 Final Project - LightHTMLCOMP2021 Final Project - LightHTML
COMP2021 Final Project - LightHTML
 
Intro to F#
Intro to F#Intro to F#
Intro to F#
 
Lithium Best
Lithium Best Lithium Best
Lithium Best
 
PHP for NonProgrammers (DrupalCon SF 2010)
PHP for NonProgrammers (DrupalCon SF 2010)PHP for NonProgrammers (DrupalCon SF 2010)
PHP for NonProgrammers (DrupalCon SF 2010)
 
Php array
Php arrayPhp array
Php array
 
HappyKardashian.com for #FVCP
HappyKardashian.com for #FVCPHappyKardashian.com for #FVCP
HappyKardashian.com for #FVCP
 

Andere mochten auch

Sistemas que reflejen perfiles sociales y aportes al proyecto Canaima
Sistemas que reflejen perfiles sociales y aportes al proyecto CanaimaSistemas que reflejen perfiles sociales y aportes al proyecto Canaima
Sistemas que reflejen perfiles sociales y aportes al proyecto CanaimaLeonardo J. Caballero G.
 
Python una experiencia academica
Python una experiencia academicaPython una experiencia academica
Python una experiencia academicaalelcantero
 
The Vanishing Pattern: from iterators to generators in Python
The Vanishing Pattern: from iterators to generators in PythonThe Vanishing Pattern: from iterators to generators in Python
The Vanishing Pattern: from iterators to generators in PythonOSCON Byrum
 
Summer school python in spanish
Summer school python in spanishSummer school python in spanish
Summer school python in spanishAjay Ohri
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The BasicsJeff Fox
 
Overview of SDLC - Waterfall, Agile, and more
Overview of SDLC - Waterfall, Agile, and moreOverview of SDLC - Waterfall, Agile, and more
Overview of SDLC - Waterfall, Agile, and moreSteve Gladstone
 
Python Advanced – Building on the foundation
Python Advanced – Building on the foundationPython Advanced – Building on the foundation
Python Advanced – Building on the foundationKevlin Henney
 

Andere mochten auch (14)

Introduccion a Python. Clase 3
Introduccion a Python. Clase 3Introduccion a Python. Clase 3
Introduccion a Python. Clase 3
 
Sistemas que reflejen perfiles sociales y aportes al proyecto Canaima
Sistemas que reflejen perfiles sociales y aportes al proyecto CanaimaSistemas que reflejen perfiles sociales y aportes al proyecto Canaima
Sistemas que reflejen perfiles sociales y aportes al proyecto Canaima
 
Funcional para trollear
Funcional para trollearFuncional para trollear
Funcional para trollear
 
Introduccion a Python. Clase 7
Introduccion a Python. Clase 7Introduccion a Python. Clase 7
Introduccion a Python. Clase 7
 
Python una experiencia academica
Python una experiencia academicaPython una experiencia academica
Python una experiencia academica
 
Json
JsonJson
Json
 
The Vanishing Pattern: from iterators to generators in Python
The Vanishing Pattern: from iterators to generators in PythonThe Vanishing Pattern: from iterators to generators in Python
The Vanishing Pattern: from iterators to generators in Python
 
Summer school python in spanish
Summer school python in spanishSummer school python in spanish
Summer school python in spanish
 
Json short manual
Json short manualJson short manual
Json short manual
 
Hacking y python
Hacking y pythonHacking y python
Hacking y python
 
Json tutorial
Json tutorialJson tutorial
Json tutorial
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
Overview of SDLC - Waterfall, Agile, and more
Overview of SDLC - Waterfall, Agile, and moreOverview of SDLC - Waterfall, Agile, and more
Overview of SDLC - Waterfall, Agile, and more
 
Python Advanced – Building on the foundation
Python Advanced – Building on the foundationPython Advanced – Building on the foundation
Python Advanced – Building on the foundation
 

Ähnlich wie Python: with-statement, generators

python3 HashTableSolutionmain.pyfrom ChainingHashTable impor.pdf
python3 HashTableSolutionmain.pyfrom ChainingHashTable impor.pdfpython3 HashTableSolutionmain.pyfrom ChainingHashTable impor.pdf
python3 HashTableSolutionmain.pyfrom ChainingHashTable impor.pdfinfo706022
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonUC San Diego
 
Python introduction 2
Python introduction 2Python introduction 2
Python introduction 2Ahmad Hussein
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAMaulik Borsaniya
 
NOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAV
NOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAVNOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAV
NOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAVRohit malav
 
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAV
NOTEPAD  MAKING IN PAYTHON BY ROHIT MALAVNOTEPAD  MAKING IN PAYTHON BY ROHIT MALAV
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAVRohit malav
 
Py.test
Py.testPy.test
Py.testsoasme
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptxNawalKishore38
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in PythonJoshua Forman
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethodsdreampuf
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonAnoop Thomas Mathew
 
When writing a member function of the HAMT class- the member function.pdf
When writing a member function of the HAMT class- the member function.pdfWhen writing a member function of the HAMT class- the member function.pdf
When writing a member function of the HAMT class- the member function.pdfaadyaenterprisesnoid
 
Creating Objects in Python
Creating Objects in PythonCreating Objects in Python
Creating Objects in PythonDamian T. Gordon
 
python within 50 page .ppt
python within 50 page .pptpython within 50 page .ppt
python within 50 page .pptsushil155005
 
Class 5: If, while & lists
Class 5: If, while & listsClass 5: If, while & lists
Class 5: If, while & listsMarc Gouw
 

Ähnlich wie Python: with-statement, generators (20)

python3 HashTableSolutionmain.pyfrom ChainingHashTable impor.pdf
python3 HashTableSolutionmain.pyfrom ChainingHashTable impor.pdfpython3 HashTableSolutionmain.pyfrom ChainingHashTable impor.pdf
python3 HashTableSolutionmain.pyfrom ChainingHashTable impor.pdf
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Python introduction 2
Python introduction 2Python introduction 2
Python introduction 2
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
NOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAV
NOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAVNOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAV
NOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAV
 
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAV
NOTEPAD  MAKING IN PAYTHON BY ROHIT MALAVNOTEPAD  MAKING IN PAYTHON BY ROHIT MALAV
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAV
 
Py.test
Py.testPy.test
Py.test
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptx
 
Python classes objects
Python classes objectsPython classes objects
Python classes objects
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in Python
 
Spsl vi unit final
Spsl vi unit finalSpsl vi unit final
Spsl vi unit final
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethods
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in Python
 
When writing a member function of the HAMT class- the member function.pdf
When writing a member function of the HAMT class- the member function.pdfWhen writing a member function of the HAMT class- the member function.pdf
When writing a member function of the HAMT class- the member function.pdf
 
Creating Objects in Python
Creating Objects in PythonCreating Objects in Python
Creating Objects in Python
 
python within 50 page .ppt
python within 50 page .pptpython within 50 page .ppt
python within 50 page .ppt
 
Class 5: If, while & lists
Class 5: If, while & listsClass 5: If, while & lists
Class 5: If, while & lists
 

Kürzlich hochgeladen

%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile EnvironmentVictorSzoltysek
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 

Kürzlich hochgeladen (20)

%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 

Python: with-statement, generators

  • 2. with open(filename) as file: #do something
  • 3. with tmpTable() as table: #do something
  • 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. „The data does not fit into my pandas dataframe.“
  • 6. with open(filename) as file: for line in file: #do something
  • 7. for x in xrange(0, 100): print x * x
  • 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. def xrange(start, stop): while start <= stop start += 1 yield start ! for x in xrange(0, 100): print x * x
  • 10. squares = (x * x for x in xrange(0, 100)) ! for square in squares: print square
  • 11. sum_of_squares = sum(x * x for x in xrange(0, 100))
  • 12. squares = (x * x for x in itertools.count(1)) ! for square in squares: print square
  • 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. 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. 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. 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. 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