python Programming Glossary: from
What is a metaclass in Python? http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python has a very peculiar idea of what classes are borrowed from the Smalltalk language. In most languages classes are just pieces.. '__main__.Foo' print MyClass # you can create an object from this class __main__.Foo object at 0x89c6d4c But it's not so.. 0x8a9b84c print f.bar True And of course you can inherit from it so class FooChild Foo ... pass would be FooChild type 'FooChild'..
The Python yield keyword explained http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained called again When subsequent calls do stop The code comes from Jochen Schulz jrschulz who made a great Python library for metric.. The first time the for calls the generator object created from your function it will run the code in your function from the.. from your function it will run the code in your function from the beginning until it hits yield then it'll return the first..
How do you split a list into evenly sized chunks in Python? http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python you want def chunks l n Yield successive n sized chunks from l. for i in xrange 0 len l n yield l i i n import pprint pprint.pprint..
Python's slice notation http://stackoverflow.com/questions/509211/pythons-slice-notation # items start through the rest of the array a end # items from the beginning through end 1 a # a copy of the whole array There.. or end may be a negative number which means it counts from the end of the array instead of the beginning. So a 1 # last..
Python output buffering http://stackoverflow.com/questions/107705/python-output-buffering python stdout buffered share improve this question From Magnus Lycka answer on a mailing list You can skip buffering..
The meaning of a single- and a double-underscore before an object name in Python http://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python starts with an underscore. Double Underscore Name Mangling From the Python docs Any identifier of the form __spam at least two..
When is “i += x” different from “i = i + x” in Python? http://stackoverflow.com/questions/15376509/when-is-i-x-different-from-i-i-x-in-python if it doesn't exist whereas calls the __add__ method 1 . From an API perspective __iadd__ is supposed to be used for modifying..
How do I create a namespace package in Python? http://stackoverflow.com/questions/1675734/how-do-i-create-a-namespace-package-in-python import extend_path __path__ extend_path __path__ __name__ From now on you should be able to distribute those two packages independently...
Best way to strip punctuation from a string in Python http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python Is there python string share improve this question From an efficiency perspective you're not going to beat translate.. ch for ch in s if ch not in exclude def test_re s # From Vinko's solution with fix. return regex.sub '' s def test_trans.. s.translate table string.punctuation def test_repl s # From S.Lott's solution for c in string.punctuation s s.replace c..
Why use pip over easy_install? http://stackoverflow.com/questions/3220404/why-use-pip-over-easy-install python setuptools pip pypi share improve this question From Ian Bicking's own introduction to pip All packages are downloaded..
What kinds of patterns could I enforce on the code to make it easier to translate to another programming language? http://stackoverflow.com/questions/3455456/what-kinds-of-patterns-could-i-enforce-on-the-code-to-make-it-easier-to-translat I can get with PHP is token_get_all which is a start. From then on I can build the AST symbol tables and control flow...
How can I download all emails with attachments from Gmail? http://stackoverflow.com/questions/348630/how-can-i-download-all-emails-with-attachments-from-gmail to download each attachment printing out the Subject and From for each message as I process it. java python perl gmail .. mail.get_content_maintype 'multipart' continue print mail From mail Subject # we use walk to create a generator so we can iterate..
Python: Is there a way to determine the encoding of text file? http://stackoverflow.com/questions/436220/python-is-there-a-way-to-determine-the-encoding-of-text-file Correctly detecting the encoding all times is impossible . From chardet FAQ However some encodings are optimized for specific..
How can I do a line break (line continuation) in Python? http://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python and b False Check the style guide for more information. From your example line a '1' '2' '3' '4' '5' Or a '1' '2' '3' '4'..
Old style and new style classes in Python http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python types new style class share improve this question From http docs.python.org ref node33.html Up to Python 2.1 old style..
Understanding Python super() and init methods http://stackoverflow.com/questions/576169/understanding-python-super-and-init-methods super and init methods Trying to understand super . From the looks of it both child classes can be created just fine...
Python's use of __new__ and __init__? http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init an immutable type like str int unicode or tuple. From http mail.python.org pipermail tutor 2008 April 061426.html..
Creating a singleton in python http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python logger is enabled. The information here flows one way From your application into the logger. Even thought loggers are global..
How to do relative imports in Python? http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python by passing the mod1.py as an argument to the interpreter. From PEP 328 Relative imports use a module's __name__ attribute to..
How do I convert local time to UTC in Python? http://stackoverflow.com/questions/79797/how-do-i-convert-local-time-to-utc-in-python naive is_dst None utc_dt local_dt.astimezone pytz.utc From there you can use the strftime method to format the UTC datetime..
Converting datetime.date to UTC timestamp in Python http://stackoverflow.com/questions/8777753/converting-datetime-date-to-utc-timestamp-in-python naive datetime object is in local timezone. Python 3.3 From the docs for datetime.utcfromtimestamp There is no method to..
Using a WHERE ___ IN ___ statement http://stackoverflow.com/questions/14245396/using-a-where-in-statement like this list_of_vars 'foo' 'bar' statement SELECT FROM tab WHERE obj IN c.execute statement ' ' ' .join list_of_vars.. which directly evaluates to the above statement SELECT FROM tab WHERE obj IN c.execute statement 'foo' 'bar' The error I.. is vulnerable to a SQL injection attack. statement SELECT FROM tab WHERE obj IN ' ' ' .join statement ' python sql sqlite..
Parameterized queries with psycopg2 / Python DB-API and PostgreSQL http://stackoverflow.com/questions/1466741/parameterized-queries-with-psycopg2-python-db-api-and-postgresql following should be safe and work cursor.execute SELECT FROM student WHERE last_name lname s lname Robert' DROP TABLE students..
Lost connection to MySQL server during query http://stackoverflow.com/questions/1884859/lost-connection-to-mysql-server-during-query cursor.execute sql return cursor # # db DB sql SELECT bla FROM foo data db.query sql for row in data do_something row # But..
Escape SQL “LIKE” value for Postgres with psycopg2 http://stackoverflow.com/questions/2106207/escape-sql-like-value-for-postgres-with-psycopg2 ' ' ' ' .replace ' ' ' ' .replace '_' ' _' sql SELECT FROM things WHERE description LIKE like s ESCAPE ' ' cursor.execute..
Filtering by entity key name in Google App Engine on Python http://stackoverflow.com/questions/2544565/filtering-by-entity-key-name-in-google-app-engine-on-python filter it. So for example these are equivalent gql SELECT FROM User WHERE age 18 db.GqlQuery gql and query User.all query.filter.. name. I know that in GQL you do it like this gql SELECT FROM User WHERE __key__ Key 'User' 'abc' db.GqlQuery gql But how..
python list in sql query as parameter http://stackoverflow.com/questions/283645/python-list-in-sql-query-as-parameter ' '.join placeholder for unused in l query 'SELECT name FROM students WHERE id IN s ' placeholders cursor.execute query l..
How to get a row-by-row MySQL ResultSet in python http://stackoverflow.com/questions/337479/how-to-get-a-row-by-row-mysql-resultset-in-python 0 while True cursor conn.cursor cursor.execute SELECT item FROM items LIMIT d 1000 start_row rows cursor.fetchall if not rows.. cur conn.cursor print Executing query cur.execute SELECT FROM bigtable print Starting loop row cur.fetchone while row is not..
Getting the SQL from a Django QuerySet http://stackoverflow.com/questions/3748295/getting-the-sql-from-a-django-queryset
imploding a list for use in a python MySQLDB IN clause http://stackoverflow.com/questions/589284/imploding-a-list-for-use-in-a-python-mysqldb-in-clause to get that string into an IN clause cursor.execute DELETE FROM foo.bar WHERE baz IN ' s' foostring What I need is to accomplish.. ' '.join ' s' len list_of_ids cursor.execute DELETE FROM foo.bar WHERE baz IN s format_strings tuple list_of_ids That..
Complex foreign key constraint in SQLAlchemy http://stackoverflow.com/questions/8394177/complex-foreign-key-constraint-in-sqlalchemy UPDATE UPDATE option SET var_id 4 WHERE var_id 5 DELETE FROM var WHERE var_id 5 Strangely enough the same thing works in..
How to debug: Internal Error current transaction is aborted, commands ignored until end of transaction block http://stackoverflow.com/questions/9064018/how-to-debug-internal-error-current-transaction-is-aborted-commands-ignored-un . app_label django_content_type . model FROM django_content_type WHERE django_content_type . model taggable.. 0.000 SELECT DISTINCT tagging_tag .id tagging_tag .name FROM tagging_tag INNER JOIN tagging_taggeditem ON tagging_tag .id..
Creating a logging handler to connect to Oracle? http://stackoverflow.com/questions/935930/creating-a-logging-handler-to-connect-to-oracle DAMAGES OR # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE DATA OR PROFITS WHETHER # IN AN ACTION OF CONTRACT..
Total memory used by Python process? http://stackoverflow.com/questions/938733/total-memory-used-by-python-process wmi import WMI w WMI '.' result w.query SELECT WorkingSet FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess d os.getpid..
|