The Python "with" Statement by Example Python’s with statement was first introduced five years ago, in Python 2.5. It’s handy when you have two related operations which you’d like to execute as a pair, with a block of code in between. The classic example is opening a file, manipulating the file, then closing it: with open('output.txt', 'w') as f: f.write('Hi there!') The above with statement will automatically close the file after the nested block of code. Here’s another example. This code sample uses a Context object (“cairo context”) to draw six rectangles, each with a different rotation. cr.translate(68, 68) for i in xrange(6): cr.save() cr.rotate(2 * math.pi * i / 6) cr.rectangle(-25, -60, 50, 40) cr.stroke() cr.restore() That’s a fairly simple example, but for larger scripts, it can become cumbersome to keep track of which save goes with which restore, and to keep them correctly matched. By themselves, pycairo’s save and restore methods do not support the with statement, so we’ll have to add the support on our own.
Learn Python The Hard Way This exercise has no code. It is simply the exercise you complete to get your computer to run Python. You should follow these instructions as exactly as possible. Go to with your browser, get the Notepad++ text editor, and install it. From now on, when I say "Terminal" or "shell" I mean PowerShell and that's what you should use. Warning Sometimes you install Python on Windows and it doesn't configure the path correctly. > python ActivePython 220.127.116.11 (ActiveState Software Inc.) based on Python 2.6.5 (r265:79063, Mar 20 2010, 14:22:52) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> quit()> mkdir mystuff > cd mystuff ... It is still correct if you see different information than mine, but yours should be similar. A major part of this book is learning to research programming topics online. Thanks to search engines such as Google you can easily find anything I tell you to find.
An Overview of Cryptography As an aside, the AES selection process managed by NIST was very public. A similar project, the New European Schemes for Signatures, Integrity and Encryption (NESSIE), was designed as an independent project meant to augment the work of NIST by putting out an open call for new cryptographic primitives. NESSIE ran from about 2000-2003. While several new algorithms were found during the NESSIE process, no new stream cipher survived cryptanalysis. As a result, the ECRYPT Stream Cipher Project (eSTREAM) was created, which has approved a number of new stream ciphers for both software and hardware implementation. Similar — but different — is the Japanese Government Cryptography Research and Evaluation Committees (CRYPTREC) efforts to evaluate algorithms submitted for government and industry applications. CAST-128/256: CAST-128, described in Request for Comments (RFC) 2144, is a DES-like substitution-permutation crypto algorithm, employing a 128-bit key operating on a 64-bit block. 3.3.
Python - Advanced List Sorting Consider a list of tuples. We could get such a list when processing information that was extracted from a spreadsheet program. For example, if we had a spreadsheet with raw census data, we can easily transform it into a sequence of tuples that look like the following. jobData= [ (001,'Albany','NY',162692), (003,'Allegany','NY',11986), ... (121,'Wyoming','NY',8722), (123,'Yates','NY',5094) ] Each tuple can be built from a row of the spreadsheet. In this case, we wrote a simple forumla in our spreadsheet to make each row into a tuple. Once we have each row as a tuple, we can put some 's around the tuples to make a list. Sorting this list can be done trivially with the list sort method. jobData.sort() Note that this updates the list in place. This kind of sort will simply compare each tuple with each other tuple. We can provide a compare function to the sort method of a list. Sorting With a Compare Function. We must define a function that behaves like the built-in cmp function.
2. Built-in Functions Open a file, returning an object of the file type described in section File Objects. If the file cannot be opened, IOError is raised. When opening a file, it’s preferable to use open() instead of invoking the file constructor directly. The first two arguments are the same as for stdio‘s fopen(): name is the file name to be opened, and mode is a string indicating how the file is to be opened. The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size (in bytes). Modes 'r+', 'w+' and 'a+' open the file for updating (reading and writing); note that 'w+' truncates the file.
A Quick, Painless Tutorial on the Python Language Norman Matloff University of California, Davis June 17, 2008 ©2003-2008, N. Matloff Contents What Are Scripting Languages? Languages like C and C++ allow a programmer to write code at a very detailed level which has good execution speed (especially in the case of C). The term scripting language has never been formally defined, but here are the typical characteristics: Used often for system administration, Web programming, text processing, etc. Why Python? The first really popular scripting language was Perl. Advocates of Python, often called pythonistas, say that Python is so clear and so enjoyable to write in that one should use Python for all of one's programming work, not just for scripting work. Background Needed Anyone with even a bit of programming experience should find the material through Section 8 to be quite accessible. The material beginning with Section 10 will feel quite comfortable to anyone with background in an object-oriented programming (OOP) language such as C++ or Java.
(How to Write a (Lisp) Interpreter (in Python)) Syntax and Semantics of the Lispy Scheme Subset The syntax of a language is what it looks like; the semantics is what it means. For example, in the language of mathematical expressions, the syntax for adding two plus two is "2 + 2" and the semantics of that expression is the number four. We say we are evaluating a syntactic expression when we determine its semantic referent; we would say that "2 + 2" evaluates to 4, and write that as "2 + 2" ⇒ 4. Most computer languages have a variety of syntactic conventions (keywords, infix operators, brackets, operator precedence, dot notation, semicolons, etc.), but as a member of the Lisp family of languages, all of Scheme's syntax is based on lists in parenthesized prefix notation. This may seem unfamiliar, but it has the virtues of simplicity and consistency. Note that the exclamation mark is not a special character in Scheme; it is just part of the name "set!". What A Language Interpreter Does >>> eval(parse(program))28.274333877 Execution: eval
Cheatsheet - Python & R codes for common Machine Learning Algorithms In his famous book – Think and Grow Rich, Napolean Hill narrates story of Darby, who after digging for a gold vein for a few years walks away from it when he was three feet away from it! Now, I don’t know whether the story is true or false. But, I surely know of a few Data Darby around me. These people understand the purpose of machine learning, its execution and use just a set 2 – 3 algorithms on whatever problem they are working on. They don’t update themselves with better algorithms or techniques, because they are too tough or they are time consuming. Like Darby, they are surely missing from a lot of action after reaching this close! Today’s cheat sheet aims to change a few Data Darby’s to machine learning advocates. For the super lazy Data Darbies, we will make your life even easier. Keep this cheat sheet handy when you work on data sets. download the complete cheat sheet here: PDF Version Related Year in Review: Best of Analytics Vidhya from 2015 December 28, 2015 August 10, 2015
Generator Tricks for Systems Programmers Generator Tricks for Systems Programmers Copyright (C) 2008David M. Presented at PyCon'08, March 13, 2008, Chicago, Illinois. Related Tutorials Introduction This tutorial discusses various techniques for using generator functions and generator expressions in the context of systems programming. Support Data Files The following file contains some supporting data files that are used by the various code samples. Code Samples Here are various code samples that are used in the course. Part 2 : Processing Data Files nongenlog.py.
How to Build a Crawler in Python Short Bytes: Web crawler is a program that browses the Internet (World Wide Web) in a predetermined, configurable and automated manner and performs given action on crawled content. Search engines like Google and Yahoo use spidering as a means of providing up-to-date data. Webhose.io, a company which provides direct access to live data from hundreds of thousands of forums, news and blogs, on Aug 12, 2015, posted the articles describing a tiny, multi-threaded web crawler written in python. This python web crawler is capable of crawling the entire web for you. Ran Geva, the author of this tiny python web crawler says that: I wrote as “Dirty”, “Iffy”, “Bad”, “Not very good”. The python based multi-threaded crawler is pretty simple and very fast. Save the above code with some name lets say “myPythonCrawler.py”. $ python myPythonCrawler.py Sit back and enjoy this web crawler in python. Become a Pro in Python With These Courses
Codecademy Labs doctest – Testing through documentation doctest lets you test your code by running examples embedded in the documentation and verifying that they produce the expected results. It works by parsing the help text to find examples, running them, then comparing the output text against the expected value. Many developers find doctest easier than unittest because in its simplest form, there is no API to learn before using it. However, as the examples become more complex the lack of fixture management can make writing doctest tests more cumbersome than using unittest. Getting Started The first step to setting up doctests is to use the interactive interpreter to create examples and then copy and paste them into the docstrings in your module. def my_function(a, b): """ >>> my_function(2, 3) 6 >>> my_function('a', 3) 'aaa' """ return a * b To run the tests, use doctest as the main program via the -m option to the interpreter. def my_function(a, b): """Returns a * b. Handling Unpredictable Output Tracebacks Working Around Whitespace #! #!
Programming in Java Advanced Imaging Oracle Technology Network > Java Software Downloads View All Downloads Top Downloads New Downloads What's New Java in the Cloud: Rapidly develop and deploy Java business applications in the cloud. Essential Links Developer Spotlight Java EE—the Most Lightweight Enterprise Framework? Blogs Technologies Contact Us About Oracle Cloud Events Top Actions News Key Topics Oracle Integrated Cloud Applications & Platform Services