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

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
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
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
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
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...software pro Development
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
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
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 

Kürzlich hochgeladen (20)

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
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
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
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
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
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
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.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