title
stringlengths
12
150
question_id
int64
469
40.1M
question_score
int64
2
5.52k
question_date
stringdate
2008-08-02 15:11:16
2016-10-18 06:16:31
answer_id
int64
536
40.1M
answer_score
int64
7
8.38k
answer_date
stringdate
2008-08-02 18:49:07
2016-10-18 06:19:33
tags
listlengths
1
5
question_body_md
stringlengths
15
30.2k
answer_body_md
stringlengths
11
27.8k
What is python Sting?
30,583,416
2
2015-06-01T21:15:26Z
30,583,523
7
2015-06-01T21:22:06Z
[ "python", "netflix" ]
Sting is referenced in [this blog post by netflix](http://techblog.netflix.com/2013/03/python-at-netflix.html) as a RESTful web service tool for reporting on data from hadoop / hive. > One such application is Sting, a lightweight RESTful web service that slices, dices, and produces visualizations of large in-memory da...
You can find the answer in the comments. There, someone asks: > lovely wrapup, is Sting a proprietary tool? found no trace in the interwebs whatsoever. And of the authors replies: > It's currently internal-only. I don't know what plans there are to OSS it, but there's been significant interest expressed in Sting com...
python strptime format with optional bits
30,584,364
7
2015-06-01T22:28:32Z
30,584,445
8
2015-06-01T22:35:56Z
[ "python", "datetime-format" ]
Right now I have: ``` timestamp = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f') ``` This works great unless I'm converting a string that doesn't have the microseconds. How can I specify that the microseconds are optional (and should be considered 0 if they aren't in the string)?
You could use a `try/except` block: ``` try: timestamp = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f') except ValueError: timestamp = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S') ```
Disable hash randomization from within python program
30,585,108
11
2015-06-01T23:42:03Z
30,586,046
8
2015-06-02T01:48:15Z
[ "python", "python-3.x", "hash" ]
Starting with Python 3.3, the hashing algorithm is non-deterministically [salted](https://docs.python.org/3.4/reference/datamodel.html#object.__hash__) to avoid a certain kind of attack. This is nice for webservers but it's a pain when trying to debug a program: Every time I run my script, dict contents are iterated in...
I suspect this isn't possible, unfortunately. Looking at [`test_hash.py`](https://hg.python.org/cpython/file/5e8fa1b13516/Lib/test/test_hash.py#l145) the `HashRandomizationTests` class and its descendants were added in the [commit that introduced this behavior](https://hg.python.org/cpython/rev/6b7704fe1be1#l9.26). The...
When reading huge HDF5 file with "pandas.read_hdf() ", why do I still get MemoryError even though I read in chunks by specifying chunksize?
30,587,026
7
2015-06-02T03:54:52Z
30,708,056
7
2015-06-08T11:32:04Z
[ "python", "pandas", "hdf5" ]
## Problem description: I use python pandas to read a few large CSV file and store it in HDF5 file, the resulting HDF5 file is about 10GB. **The problem happens when reading it back. Even though I tried to read it back in chunks, I still get MemoryError.** ## Here is How I create the HDF5 file: ``` import glob, os i...
So the iterator is built mainly to deal with a `where` clause. `PyTables` returns a list of the indicies where the clause is True. These are row numbers. In this case, there is no where clause, but we still use the indexer, which in this case is simply `np.arange` on the list of rows. 300MM rows takes 2.2GB. which is ...
How to update a document using elasticsearch-py?
30,598,152
9
2015-06-02T13:56:00Z
30,598,673
15
2015-06-02T14:17:43Z
[ "python", "elasticsearch", "elasticsearch-py" ]
Does anyone have an example for how to use update? It's documented [here](http://elasticsearch-py.readthedocs.org/en/master/api.html#elasticsearch.Elasticsearch.update), but the documentation is unclear and doesn't included a working example. I've tried the following: ``` coll = Elasticsearch() coll.update(index='stor...
You're almost there, you just need to enclose your body inside a "doc" field. The correct way of doing a partial update with elasticsearch-py goes like this: ``` coll = Elasticsearch() coll.update(index='stories-test',doc_type='news',id=hit.meta.id, body={"doc": {"stanford": 1, "parsed_sents": parsed }...
Move models between Django (1.8) apps with required ForeignKey references
30,601,107
14
2015-06-02T16:05:58Z
30,613,732
32
2015-06-03T07:38:44Z
[ "python", "database", "django-models", "schema-migration" ]
This is an extension to this question: [How to move a model between two Django apps (Django 1.7)](http://stackoverflow.com/questions/25648393/how-to-move-a-model-between-two-django-apps-django-1-7/26472482#26472482) I need to move a bunch of models from `old_app` to `new_app`. The best answer seems to be [Ozan's](http...
**Migrating a model between apps.** The short answer is, *don't do it!!* But that answer rarely works in the real world of living projects and production databases. Therefore, I have created a [sample GitHub repo](https://github.com/halfnibble/factory) to demonstrate this rather complicated process. I am using MySQL...
Why is S == S[::-1] faster than looping?
30,601,354
2
2015-06-02T16:18:08Z
30,601,525
7
2015-06-02T16:26:31Z
[ "python", "string", "performance", "palindrome" ]
Why is the pythonic way of checking if a string, `S`, is a palindrome -- `S == S[::-1]` -- faster than the following implementation? ``` i = 0 j = len(S) - 1 while i < j: if S[i] != S[j]: return False i += 1 j -= 1 return True ```
Because Python code is only compiled to bytecode, which is then interpreted. That's going to be a lot slower than creating a new reversed string in C code, then comparing the string to another string with more C code. Note that both algorithms are essentially O(N) complexity; the Python code executes at most 1/2 N ite...
What does ,= mean in python?
30,601,479
35
2015-06-02T16:24:26Z
30,601,537
43
2015-06-02T16:27:00Z
[ "python", "operators" ]
I wonder what `,=` or `, =` means in python? Example from matplotlib: ``` plot1, = ax01.plot(t,yp1,'b-') ```
It's a form of tuple unpacking. With parentheses: ``` (plot1,) = ax01.plot(t,yp1,'b-') ``` `ax01.plot()` returns a tuple containing one element, and this element is assigned to `plot1`. Without that comma (and possibly the parentheses), `plot1` would have been assigned the whole tuple. Observe the difference between ...
What does ,= mean in python?
30,601,479
35
2015-06-02T16:24:26Z
30,601,610
16
2015-06-02T16:30:27Z
[ "python", "operators" ]
I wonder what `,=` or `, =` means in python? Example from matplotlib: ``` plot1, = ax01.plot(t,yp1,'b-') ```
Python allows you to put tuples on the left hand side of the assignment. The code in the question is an example of this, it might look like it's a special case of an operator but it's really just a case tuple assignment going on here. Some examples might help: ``` a, b = (1, 2) ``` which gives you `a = 1` and `b = 2`...
How to import OpenSSL in python
30,602,843
5
2015-06-02T17:33:17Z
30,602,906
8
2015-06-02T17:37:04Z
[ "python", "python-2.7", "openssl" ]
I am trying to run this simple code to retrieve SSL certificate: ``` import ssl, socket #print ssl.get_server_certificate(('www.google.com', 443)) cert=ssl.get_server_certificate(('www.google.com', 443)) # OpenSSL x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert) x509.get_subject().get_compone...
From the [tests](https://github.com/pyca/pyopenssl/blob/master/examples/SecureXMLRPCServer.py): ``` from OpenSSL import SSL ``` Response to the edit: `pip install pyopenssl` should have installed six. If you're trying to install yourself, I'd not do this, but you can install the dependencies manually using `pip insta...
Why does `mylist[:] = reversed(mylist)` work?
30,604,127
11
2015-06-02T18:45:32Z
30,604,265
12
2015-06-02T18:52:21Z
[ "python", "list", "reverse", "assign", "python-internals" ]
The following reverses a list "in-place" and works in Python 2 and 3: ``` >>> mylist = [1, 2, 3, 4, 5] >>> mylist[:] = reversed(mylist) >>> mylist [5, 4, 3, 2, 1] ``` Why/how? Since `reversed` gives me an iterator and doesn't copy the list beforehand, and since `[:]=` replaces "in-place", I am surprised. And the foll...
CPython list slice assigment will convert the iterable to a list first by calling [`PySequence_Fast`](https://docs.python.org/2/c-api/sequence.html#c.PySequence_Fast). Source: <https://hg.python.org/cpython/file/7556df35b913/Objects/listobject.c#l611> ``` v_as_SF = PySequence_Fast(v, "can only assign an iterable"); `...
Why does upsert a record using update_one raise ValueError?
30,605,638
9
2015-06-02T20:08:52Z
30,605,914
12
2015-06-02T20:25:37Z
[ "python", "mongodb", "mongodb-query", "pymongo" ]
I want to add a record to the collection if the key doesn't already exist. I understand [MongoDB][1] offers the `upsert`for this so I did a ``` db.collection.update({"_id":"key1"},{"_id":"key1"},True) ``` This seems to work. However in the [Pymongo documentation](http://api.mongodb.org/python/current/api/pymongo/col...
This is because you didn't specify any [update operator](http://docs.mongodb.org/manual/reference/operator/update/). For example to [`$set`](http://docs.mongodb.org/manual/reference/operator/update/set/#up._S_set) the `id` value use: ``` db.collection.update_one({"_id":"key1"}, {"$set": {"id":"key1"}}, upsert=True) ``...
Pandas to_csv call is prepending a comma
30,605,909
4
2015-06-02T20:25:31Z
30,605,928
7
2015-06-02T20:26:10Z
[ "python", "csv", "pandas" ]
I have a data file, apples.csv, that has headers like: ``` "id","str1","str2","str3","num1","num2" ``` I read it into a dataframe with pandas: ``` apples = pd.read_csv('apples.csv',delimiter=",",sep=r"\s+") ``` Then I do some stuff to it, but ignore that (I have it all commented out, and my overall issues still occ...
Set `index=False` (the default is `True` hence why you see this output) so that it doesn't save the index values to your csv, see the [docs](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html#pandas.DataFrame.to_csv) So this: ``` df = pd.DataFrame({'a':np.arange(5), 'b':np.arange(5)}) ...
Is there a pandas function to display the first/last n columns, as in .head() & .tail()?
30,608,310
5
2015-06-02T23:32:13Z
30,608,484
7
2015-06-02T23:51:24Z
[ "python", "pandas" ]
I love using the `.head()` and `.tail()` functions in pandas to circumstantially display a certain amount of rows (sometimes I want less, sometimes I want more!). But is there a way to do this with the columns of a DataFrame? Yes, I know that I can change the display options, as in: `pd.set_option('display.max_columns...
No, such methods are not supplied by Pandas, but it is easy to make these methods yourself: ``` import pandas as pd def front(self, n): return self.iloc[:, :n] def back(self, n): return self.iloc[:, -n:] pd.DataFrame.front = front pd.DataFrame.back = back df = pd.DataFrame(np.random.randint(10, size=(4,10))...
Detect when TCP is congested python twisted socket server
30,612,841
2
2015-06-03T06:52:13Z
30,613,647
9
2015-06-03T07:34:44Z
[ "python", "tcp", "udp", "server", "twisted" ]
I'm working on a realtime MMO game, and have a working TCP server (along with game client), but now I'm **considering using UDP** for constantly updating other player's postions(to greatly reduce random game stuffer from TCP congestion control!) I'd love some help from people smarter than me in this stuff (I'm new to p...
[You probably don't need UDP (yet)](https://thoughtstreams.io/glyph/your-game-doesnt-need-udp-yet/). The first thing that you say is that you want to "reduce network congestion ... from TCP". That is not what UDP does. UDP allows you to work around *congestion control*, which actually *increases* network congestion. U...
How not to miss the next element after itertools.takewhile()
30,615,659
13
2015-06-03T09:11:53Z
30,615,837
10
2015-06-03T09:19:30Z
[ "python", "itertools" ]
Say we wish to process an iterator and want to handle it by chunks. The logic per chunk depends on previously-calculated chunks, so `groupby()` does not help. Our friend in this case is itertools.takewhile(): ``` while True: chunk = itertools.takewhile(getNewChunkLogic(), myIterator) process(chunk) ``` The...
`takewhile()` indeed needs to look at the next element to determine when to toggle behaviour. You could use a wrapper that tracks the last seen element, and that can be 'reset' to back up one element: ``` _sentinel = object() class OneStepBuffered(object): def __init__(self, it): self._it = iter(it) ...
Stuck in a while loop while using if statement
30,619,535
6
2015-06-03T12:00:42Z
30,619,565
7
2015-06-03T12:02:00Z
[ "python", "python-2.7" ]
I'm new to python and I'm trying to make a simple Guess the number game, and I'm stuck in an if statement in a while loop. here is the code. I'm experiencing it at the Your guess it too high and the low one. I tried **break**ing it, but it simply stops the whole things ``` def guess_the_number(): number = random....
You never increment `guessesMade` so `guessesMade < 6` will always be `True`. You need to modify this value within your loop. You also need to move your prompt for user input into the loop ``` while guessesMade < 6: guess = int(input('Take a guess')) if guess < number: print('Your guess is too low.') ...
Is filter thread-safe
30,619,828
18
2015-06-03T12:15:28Z
30,709,536
7
2015-06-08T12:44:26Z
[ "python" ]
I have a thread which is updating a list called `l`. Am I right in saying that it is thread-safe to do the following from another thread? ``` filter(lambda x: x[0] == "in", l) ``` If its not thread safe, is this then the correct approach: ``` import threading import time import Queue class Logger(threading.Thread):...
First, to answer your question in the title: `filter` is just a function. Hence, its thread-safety will rely on the data-structure you use it with. As pointed out in the comments already, list operations themselves are thread-safe in CPython and protected by the GIL, but that is arguably only an implementation detail ...
Flask and React routing
30,620,276
13
2015-06-03T12:35:31Z
30,620,788
14
2015-06-03T12:57:32Z
[ "python", "flask", "routing", "reactjs" ]
I'm building the Flask app with React, I ended up having a problem with routing. The backend is responsible to be an API, hence some routes look like: ``` @app.route('/api/v1/do-something/', methods=["GET"]) def do_something(): return something() ``` and the main route which leads to the React: ``` @app.route('...
We used [catch-all URLs](http://flask.pocoo.org/snippets/57/) for this. ``` from flask import Flask app = Flask(__name__) @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def catch_all(path): return 'You want path: %s' % path if __name__ == '__main__': app.run() ``` You can also go an extra...
Python List Slicing with None as argument
30,622,809
14
2015-06-03T14:20:27Z
30,622,949
14
2015-06-03T14:26:50Z
[ "python", "list" ]
Via trial and error I found out that ``` my_list = range(10) my_list[:None] == my_list[:] ``` I use this for django query sets so I can define a size or take all: ``` some_queryset[:length if length else None] # @IanAuld some_queryset[:length or None] # @Bakuriu # length works for all numbers and None if you want...
Yes, it is fine to use `None`, as its behavior is specified by the [documentation](https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange): > The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j. If i or j is greater tha...
No such file or directory "limits.h" when installing Pillow on Alpine Linux
30,624,829
12
2015-06-03T15:45:03Z
30,873,179
22
2015-06-16T16:27:53Z
[ "python", "linux", "python-2.7", "alpine" ]
I'm running alpine-linux on a Raspberry Pi 2. I'm trying to install Pillow via this command: ``` pip install pillow ``` This is the output from the command: ``` Installing collected packages: pillow Running setup.py install for pillow Complete output from command /usr/bin/python -c "import setuptools, tokenize;_...
Alpine linux uses musl libc. You probably need to install musl-dev.
No such file or directory "limits.h" when installing Pillow on Alpine Linux
30,624,829
12
2015-06-03T15:45:03Z
35,695,626
11
2016-02-29T09:12:18Z
[ "python", "linux", "python-2.7", "alpine" ]
I'm running alpine-linux on a Raspberry Pi 2. I'm trying to install Pillow via this command: ``` pip install pillow ``` This is the output from the command: ``` Installing collected packages: pillow Running setup.py install for pillow Complete output from command /usr/bin/python -c "import setuptools, tokenize;_...
@zakaria answer is correct, but if you stumble upon ``` fatal error: linux/limits.h: No such file or directory ``` then you need the package `linux-headers` (notice the prefix `linux` before `limits.h` ``` apk add linux-headers ```
Conditional renaming of strings in Python
30,624,992
2
2015-06-03T15:53:02Z
30,625,154
9
2015-06-03T16:00:07Z
[ "python", "string" ]
How do I create a conditional renaming of variable strings in Python? Let's say I have: ``` fruit = "Apfel" ``` If it's "Apfel", I want to rename it to "`Apple`". Alternatively, the variable may return a different string. ``` fruit = "Erdbeere" ``` If so, I want to rename it to "`Strawberry`".
You need to prepare a dictionary in advance. Either use publicly available lexicons or invest some time building it. ``` fruit = "Apfel" myDict = {"Apfel":"Apple", "Erdbeere":"Strawberry"} fruit=myDict[fruit] print fruit ``` Also take care if the word is or isn't capitalized.
Traceback: AttributeError:addinfourl instance has no attribute '__exit__'
30,627,937
3
2015-06-03T18:31:12Z
30,628,142
29
2015-06-03T18:42:54Z
[ "python", "python-2.7" ]
``` from urllib import urlopen with urlopen('https://www.python.org') as story: story_words = [] for line in story: line_words = line.split() for words in line_words: story_words.append(word) ``` Error message: ``` Traceback (most recent call last): File "<stdin>", line 1, in <mo...
That error is caused by this line: ``` with urlopen('https://www.python.org') as story: ``` To fix this, replace that line with the following line: ``` story = urlopen('https://www.python.org') ``` --- # Additional info on the error **Why is this happening?** In order for `with ... as` statement to work on an ob...
Switch every pair of characters in a string
30,628,176
5
2015-06-03T18:44:33Z
30,628,231
7
2015-06-03T18:47:33Z
[ "python", "string", "performance", "python-2.7" ]
For example, having the string: ``` abcdefghijklmnopqrstuvwxyz ``` should result in something like this: ``` badcfehgjilknmporqtsvuxwzy ``` How do I even go about it? I thought of something not very efficient, such as: ``` s = str(range(ord('a'), ord('z') + 1)) new_s = '' for i in xrange(len(s)): if i != 0 an...
You can use `zip()` function which woud return a list of tuples as `[(b,a), (d,c), ...]` and the applying `.join()` method to both the elements of the tuple and list as well. ``` a = "abcdefghijklmnopqrstuvwxyz" # a[::2] = "acegikmoqsuwy" # a[1::2] = "bdfhjlnprtvx" print "".join("".join(i) for i in zip(a[1::2], a[::2]...
2 names for a same attribute
30,630,700
8
2015-06-03T21:05:02Z
30,630,885
11
2015-06-03T21:15:14Z
[ "python" ]
I would like to know if there is a way to "link" two attributes of a class or to give 2 names to a same attribute? For example, I'm actually working on a script which create triangle from data given by users. My triangle is ABC. Sides of this triangle are AB, BC and CA. So the triangle has got these 3 attributes (self...
Python properties can have setters. So all you need is ``` class Foo(object): @property def BA(self): return self.AB @BA.setter def BA(self, value): self.AB = value ``` And insipred by @amccormack's answer, if you can rely on the order of attribute names, this works more generically for e.g. edg...
Writing to MySQL database with pandas using SQLAlchemy, to_sql
30,631,325
13
2015-06-03T21:45:10Z
30,653,988
16
2015-06-04T20:56:29Z
[ "python", "mysql", "pandas", "sqlalchemy", "mysql-connector" ]
trying to write pandas dataframe to MySQL table using to\_sql. Previously been using flavor='mysql', however it will be depreciated in the future and wanted to start the transition to using SQLAlchemy engine. sample code: ``` import pandas as pd import mysql.connector from sqlalchemy import create_engine engine = cr...
Using the engine in place of the raw\_connection() worked: ``` import pandas as pd import mysql.connector from sqlalchemy import create_engine engine = create_engine('mysql+mysqlconnector://[user]:[pass]@[host]:[port]/[schema]', echo=False) data.to_sql(name='sample_table2', con=engine, if_exists = 'append', index=Fal...
How can i use signals in django bulk create
30,632,743
8
2015-06-03T23:49:43Z
30,632,974
8
2015-06-04T00:20:39Z
[ "python", "django", "signals" ]
I have this code `Task.objects.bulk_create(ces)` Now this is my signal ``` @receiver(pre_save, sender=Task) def save_hours(sender, instance, *args, **kwargs): logger.debug('test') ``` Now this signal is not triggered in bulk create I am using django 1.8
As mentioned `bulk_create` does not trigger these signals - <https://docs.djangoproject.com/en/1.8/ref/models/querysets/#bulk-create> > This method inserts the provided list of objects into the database in > an efficient manner (generally only 1 query, no matter how many > objects there are). > > This has a number of...
TypeError: Super does not take Key word arguments?
30,633,889
9
2015-06-04T02:20:37Z
30,633,917
9
2015-06-04T02:24:00Z
[ "python", "class", "inheritance", "super" ]
First, here is my code: ``` class Enemy(): def __init__(self, name, hp, damage): self.name = name self.hp = hp self.damage = damage def is_alive(self): """Checks if alive""" return self.hp > 0 class WildBoar(Enemy): def __init__(self): super(WildBoar, name...
The arguments to the parent's `__init__` method should be passed to the `__init__` method: ``` super(Kidnappers, self).__init__(name="The Kidnappers", hp=30, damage=7) # or super(Kidnappers, self).__init__("The Kidnappers", 30, 7) ``` All you pass to `super()` is the child class (`Kidnappers` in this case) and a refe...
Split a string only by first space in python
30,636,248
4
2015-06-04T06:13:25Z
30,636,260
8
2015-06-04T06:14:31Z
[ "python", "string", "split" ]
I have string for example: "238 NEO Sports". I want to split this string only for the first space. The output should be ["238","NEO Sports"]. One way I could think to this is by split() and adding the other two. Is their any other way to do so?
Just pass the count as second parameter inside the split function. ``` >>> s = "238 NEO Sports" >>> s.split(" ", 1) ['238', 'NEO Sports'] ```
Split a string only by first space in python
30,636,248
4
2015-06-04T06:13:25Z
30,636,261
7
2015-06-04T06:14:37Z
[ "python", "string", "split" ]
I have string for example: "238 NEO Sports". I want to split this string only for the first space. The output should be ["238","NEO Sports"]. One way I could think to this is by split() and adding the other two. Is their any other way to do so?
RTFM: [`string.split(s[, sep[, maxsplit]])`](https://docs.python.org/2/library/string.html#string.split) ``` >>> "238 NEO Sports".split(None, 1) ['238', 'NEO Sports'] ```
Connecting to database using SQLAlchemy
30,638,003
6
2015-06-04T07:51:07Z
30,638,039
9
2015-06-04T07:53:25Z
[ "python", "sqlalchemy" ]
I'm trying to connect to a database on my local machine. ``` import sqlalchemy engine = sqlalchemy.create_engine('mssql+pyodbc://localhost\\SQLEXPRESS/NCM') ``` It fails with the following error: ``` DBAPIError: (pyodbc.Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no defa...
As shown in [this link](http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#hostname-connections), as of version 1.0.0 you need to specify the driver explicitly for hostname connections. ``` Changed in version 1.0.0: Hostname-based PyODBC connections now require the SQL Server driver name specified explicitly. SQ...
Does __ne__ use an overridden __eq__?
30,643,236
6
2015-06-04T11:57:17Z
30,643,308
12
2015-06-04T12:00:49Z
[ "python", "comparison", "equality" ]
Suppose I have the following program: ``` class A(object): de...
From [the docs](https://docs.python.org/2/reference/datamodel.html#object.__ne__): > There are no implied relationships among the comparison operators. The truth of `x==y` does not imply that `x!=y` is false. Accordingly, when defining `__eq__()`, one should also define `__ne__()` so that the operators will behave as ...
Write double (triple) sum as inner product?
30,644,968
7
2015-06-04T13:21:44Z
30,645,430
8
2015-06-04T13:42:17Z
[ "python", "arrays", "performance", "numpy", "linear-algebra" ]
Since my `np.dot` is accelerated by OpenBlas and Openmpi I am wondering if there was a possibility to write the double sum ``` for i in range(N): for j in range(N): B[k,l] += A[i,j,k,l] * X[i,j] ``` as an inner product. Right at the moment I am using ``` B = np.einsum("ijkl,ij->kl",A,X) ``` but unfort...
You can use `np.tensordot()`: ``` np.tensordot(A, X, [[0,1], [0, 1]]) ``` which does use multiple cores. --- EDIT: it is insteresting to see how `np.einsum` and `np.tensordot` scale when increasing the size of the input arrays: ``` In [18]: for n in range(1, 31): ....: A = np.random.rand(n, n+1, n+2, n+3) ...
Error Installing pymssql on Mac OS X Yosemite
30,646,171
6
2015-06-04T14:10:24Z
30,654,579
12
2015-06-04T21:34:01Z
[ "python", "sql-azure", "osx-yosemite", "pymssql" ]
I receive the following error when installing pymssql on OS X Yosemite 10.10.3 - has anyone gotten around the following error? I am using FreeTDS (v0.91.112) version 7.1 and Python 2.7.6 - the tsql utility connects to a SQL Database with no issue. `sudo pip install pymssql` **Error**: ``` Command "/usr/bin/python -c...
You should be able to install pymmsql on your Mac for Azure SQL DB by following these three steps. Step 1: Install Homebrew Go to your terminal and run the following command : ``` ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ``` Step 2 : Install FreeTDS. From the terminal...
How do I run a google appengine docker image on a compute engine instance?
30,646,410
2
2015-06-04T14:20:38Z
30,715,496
8
2015-06-08T17:29:02Z
[ "python", "google-app-engine", "google-compute-engine" ]
I have the following docker file: ``` FROM gcr.io/google_appengine/python-compat MAINTAINER Me@me.com RUN apt-get update RUN apt-get -y upgrade ADD ./app/ /app ADD app.yaml /app/ RUN mkdir -p /var/log/app_engine ``` I create the log directory because otherwise I get the following error ``` sudo docker run gcr.io/...
Running the application container standalone is possible, but there are a few nuances. **Volume Bindings** Firstly, let's look at the log directory part, where you create /var/log/appengine. When `gcloud preview app run` acts on the container, it actually runs it with volume bindings, that map /var/log/appengine wit...
Remove the new line "\n" from base64 encoded strings in Python3?
30,647,219
7
2015-06-04T14:52:53Z
32,243,566
17
2015-08-27T07:49:36Z
[ "python", "python-3.x", "base64" ]
I'm trying to make a HTTPS connection in Python3 and when I try to encode my username and password the `base64` `encodebytes` method returns the encoded value with a new line character at the end "\n" and because of this I'm getting an error when I try to connect. Is there a way to tell the `base64` library not to app...
Instead of `encodestring` consider using `b64encode`. Later does not add `\n` characters. e.g. ``` In [11]: auth = b'username@domain.com:passWORD' In [12]: base64.encodestring(auth) Out[12]: b'dXNlcm5hbWVAZG9tYWluLmNvbTpwYXNzV09SRA==\n' In [13]: base64.b64encode(auth) Out[13]: b'dXNlcm5hbWVAZG9tYWluLmNvbTpwYXNzV09SR...
Print a variable selected by a random number
30,651,487
5
2015-06-04T18:31:15Z
30,651,522
14
2015-06-04T18:33:38Z
[ "python", "variables", "python-3.x", "random", "introspection" ]
I have a list of names, and I would like my program to randomly select one of those names. I tried using the following: ``` import random def main(): Arkansas = 1 Manchuria = 2 Bengal = "3" Baja_California = 4 Tibet = 5 Indonesia = 6 Cascade_Range = 7 Hudson_Bay = 8 High_Plains =...
Don't assign numbers OR strings. Use a list. ``` choices = ['Arkansas', 'Manchuria', 'Bengal', 'Baja California'] # etc. ``` Then take a `random.choice` ``` random_choice = random.choice(choices) ```
Is there a better way to check for vowels in the first position of a word?
30,652,692
7
2015-06-04T19:38:20Z
30,652,736
15
2015-06-04T19:40:51Z
[ "python", "string", "python-2.7" ]
I'm trying to check for a vowel as the first character of a word. For my code I currently have this: ``` if first == 'a' or first == 'e' or first == 'i' or first == 'o' or first == 'u': ``` I was wondering is there a much better way to do this check or is this the best and most efficient way?
You can try like this using the `in`: ``` if first.lower() in 'aeiou': ``` or better like ``` if first.lower() in ('a', 'e', 'i', 'o', 'u'): ```
Is there a better way to check for vowels in the first position of a word?
30,652,692
7
2015-06-04T19:38:20Z
30,652,800
7
2015-06-04T19:44:38Z
[ "python", "string", "python-2.7" ]
I'm trying to check for a vowel as the first character of a word. For my code I currently have this: ``` if first == 'a' or first == 'e' or first == 'i' or first == 'o' or first == 'u': ``` I was wondering is there a much better way to do this check or is this the best and most efficient way?
Better create a set of vowels, like this ``` >>> vowels = set('aeiouAEIOU') >>> vowels set(['a', 'A', 'e', 'i', 'o', 'I', 'u', 'O', 'E', 'U']) ``` and then check if `first` is one of them like this ``` >>> if first in vowels: ... ``` --- **Note:** The problem with ``` if first in 'aeiouAEIOU': ``` approach is, i...
What does it mean when you assign int to a variable in Python?
30,658,964
3
2015-06-05T05:30:41Z
30,658,991
7
2015-06-05T05:32:24Z
[ "python", "variables", "int", "type-conversion" ]
i.e. `x = int` I understand that this will make `x` an integer if it is not already one, but I'd like to understand the process behind this. In particular, I'd like to know what `int` is (as opposed to `int()`). I know that `int()` is a function, but I'm not sure what `int` is. Links to documentation about `int` would...
`x = int` will not make `x` into an integer. `int` is the integer type. Doing `x = int` will set `x` to the value of the `int` type. Loosely speaking, `x` will become an "alias" for the integer type. If you *call* the int type on something, like `int('2')`, it will convert what you give into an integer, if it can. If ...
How to get the vertices from an edge using igraph in python?
30,660,808
4
2015-06-05T07:30:55Z
30,674,221
9
2015-06-05T19:20:28Z
[ "python", "igraph", "vertex", "edges" ]
I am looping through the edges of a graph with: ``` for es in graph.es: .... # v = [] # v = es.vertices()? ... ``` What method can I use to get the source and the target vertices for each edge?
These are the very basic functionalities of igraph, described [here](http://igraph.org/python/doc/igraph.Graph-class.html) thoroughly. If you iterate the `<EdgeSeq>` object (`graph.es`), you will go through all `<Edge>` objects (here `edge`). `<Edge>` has properties `source` and `target`. These are vertex ids, simply i...
Extract list of Persons and Organizations using Stanford NER Tagger in NLTK
30,664,677
10
2015-06-05T10:49:58Z
30,665,014
11
2015-06-05T11:07:51Z
[ "python", "nltk", "stanford-nlp", "named-entity-recognition" ]
I am trying to extract list of persons and organizations using Stanford Named Entity Recognizer (NER) in Python NLTK. When I run: ``` from nltk.tag.stanford import NERTagger st = NERTagger('/usr/share/stanford-ner/classifiers/all.3class.distsim.crf.ser.gz', '/usr/share/stanford-ner/stanford-ner.jar') r...
Thanks to the [link](http://stackoverflow.com/questions/13765349/multi-term-named-entities-in-stanford-named-entity-recognizer) discovered by @Vaulstein, it is clear that the trained Stanford tagger, as distributed (at least in 2012) **does not chunk named entities**. From [the accepted answer](http://stackoverflow.com...
Extract list of Persons and Organizations using Stanford NER Tagger in NLTK
30,664,677
10
2015-06-05T10:49:58Z
30,666,949
14
2015-06-05T12:47:16Z
[ "python", "nltk", "stanford-nlp", "named-entity-recognition" ]
I am trying to extract list of persons and organizations using Stanford Named Entity Recognizer (NER) in Python NLTK. When I run: ``` from nltk.tag.stanford import NERTagger st = NERTagger('/usr/share/stanford-ner/classifiers/all.3class.distsim.crf.ser.gz', '/usr/share/stanford-ner/stanford-ner.jar') r...
IOB/BIO means **I**nside, **O**utside, **B**eginning (IOB), or sometimes aka **B**eginning, **I**nside, **O**utside (BIO) The Stanford NE tagger returns IOB/BIO style tags, e.g. ``` [('Rami', 'PERSON'), ('Eid', 'PERSON'), ('is', 'O'), ('studying', 'O'), ('at', 'O'), ('Stony', 'ORGANIZATION'), ('Brook', 'ORGANIZATION'...
Send JSON to Flask using requests
30,673,079
2
2015-06-05T18:07:31Z
30,673,376
10
2015-06-05T18:25:56Z
[ "python", "flask" ]
I am trying to send some JSON data to a Flask app using the requests library. I expect to get `application/json` back from the server. This works fine when I use Postman, but when I use requests I get `application/html` back instead. ``` import requests server_ip = 'server_ip:port/events' headers = {'Content-Type': 'a...
You are not sending JSON data currently. You need to set the `json` argument, not `data`. It's unnecessary to set `content-type` yourself in this case. ``` r = requests.post(url, json=event_data) ``` The `text/html` header you are seeing is the *response's* content type. Flask seems to be sending some HTML back to yo...
spark in yarn-cluser 'sc' not defined
30,674,467
2
2015-06-05T19:36:29Z
30,674,801
7
2015-06-05T19:59:05Z
[ "python", "hadoop", "apache-spark", "apache-spark-sql" ]
I am using spark 1.3.1. Do I have to declare sc when spark run in yarn-cluster mode? I have no problem running the same python program in spark python shell. This is how I submit the job : ``` /bin/spark-submit --master yarn-cluster test.py --conf conf/spark-defaults.conf ``` where in spark-defaults I did declare w...
`sc` is a helper value created in the `spark-shell`, but is not automatically created with `spark-submit`. You must instantiate your own `SparkContext` and use that ``` conf = SparkConf().setAppName(appName) sc = SparkContext(conf=conf) ```
Pivot Tables or Group By for Pandas?
30,679,467
3
2015-06-06T05:56:25Z
30,679,543
7
2015-06-06T06:05:49Z
[ "python", "pandas", "count", "group-by", "pivot-table" ]
I have a hopefully straightforward question that has been giving me a lot of difficulty for the last 3 hours. It should be easy. Here's the challenge. I have a pandas dataframe: ``` +--------------------------+ | Col 'X' Col 'Y' | +--------------------------+ | class 1 cat 1 | | class 2 ca...
You could use [`pd.crosstab()`](http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.crosstab.html) ``` In [27]: df Out[27]: Col X Col Y 0 class 1 cat 1 1 class 2 cat 1 2 class 3 cat 2 3 class 2 cat 3 In [28]: pd.crosstab(df['Col X'], df['Col Y']) Out[28]: Col Y cat 1 cat 2 cat 3 Co...
How to reshape a networkx graph in Python?
30,689,391
21
2015-06-07T02:09:00Z
30,889,340
12
2015-06-17T10:48:13Z
[ "python", "matplotlib", "nodes", "shape", "networkx" ]
So I created a really naive (probably inefficient) way of generating hasse diagrams. ***Question:*** I have 4 dimensions... **`p`** **`q`** **`r`** **`s`** . I want to display it uniformly (tesseract) but I have no idea how to reshape it. **How can one reshape a networkx graph in Python?** I've seen some examples o...
This is a pragmatic, rather than purely mathematical answer. I think you have two issues - one with layout, the other with your network. ### 1. Network You have too many edges in your network for it to represent the unit tesseract. ***Caveat*** I'm not an expert on the maths here - just came to this from the plottin...
A + B without arithmetic operators, Python vs C++
30,696,484
28
2015-06-07T17:33:10Z
30,696,550
8
2015-06-07T17:40:43Z
[ "python", "c++", "algorithm", "bit-manipulation" ]
I was trying to solve an old question: > Write a function that add two [integer] numbers A and B. You should not use + or any arithmetic operators. The best solution is like this, quoted from "[LintCode-A+B Problem](http://www.cnblogs.com/lishiblog/p/4194937.html)": > For a + b in any base, we can treat the plus as ...
The problem are negative numbers, or, how they are represented. In Python integer numbers have arbitrary accuracy, while C++ ints are 32bit or 64bit. So in Python, you have to handle negative numbers, e.g. subtraction, separately, or limit the number of bits by hand.
A + B without arithmetic operators, Python vs C++
30,696,484
28
2015-06-07T17:33:10Z
30,696,900
24
2015-06-07T18:18:07Z
[ "python", "c++", "algorithm", "bit-manipulation" ]
I was trying to solve an old question: > Write a function that add two [integer] numbers A and B. You should not use + or any arithmetic operators. The best solution is like this, quoted from "[LintCode-A+B Problem](http://www.cnblogs.com/lishiblog/p/4194937.html)": > For a + b in any base, we can treat the plus as ...
The binary, 2's complement representation of `-4` is ``` ...11100 ``` Yes, I really do mean infinitely many `1`'s to the left; this is a binary repeating numeral. Technically, `4` is a repeating numeral too: ``` ...00100 ``` it's just repeating `0`'s to the left. Your addition problem is ``` ...11100 + ...001...
How can I serialize a numpy array while preserving matrix dimensions?
30,698,004
10
2015-06-07T20:13:50Z
30,699,208
7
2015-06-07T22:41:17Z
[ "python", "json", "numpy", "apache-storm" ]
`numpy.array.tostring` doesn't seem to preserve information about matrix dimensions (see [this question](http://stackoverflow.com/q/30697769/1156707)), requiring the user to issue a call to `numpy.array.reshape`. Is there a way to serialize a numpy array to JSON format while preserving this information? **Note:** The...
[`pickle.dumps`](https://docs.python.org/2/library/pickle.html) or [`numpy.save`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.save.html) encode all the information needed to reconstruct an arbitrary NumPy array, even in the presence of endianness issues, non-contiguous arrays, or weird tuple dtypes. Endia...
How to use jinja2 as a templating engine in Django 1.8
30,701,631
11
2015-06-08T05:13:57Z
30,715,508
13
2015-06-08T17:29:58Z
[ "python", "django", "jinja2" ]
I have been looking on how to use jinja2 in django 1.8, but there is no complete source for using django with jinja2. I was wondering if you guys knew the process for using jinja2 in django. I have looked through the the official documentation and I have looked at the following question: [How to setup django 1.8 to use...
Frist you have to install `jinja2`: ``` $ pip install Jinja2 ``` Then modify your `TEMPLATES` list in the **settings.py** to contain the `jinja2` `BACKEND` : ``` TEMPLATES = [ { 'BACKEND': 'django.template.backends.jinja2.Jinja2', 'DIRS': [os.path.join(BASE_DIR, 'templates/jinja2')], 'AP...
Syntax error while using Lambda functions
30,705,624
3
2015-06-08T09:33:23Z
30,705,693
8
2015-06-08T09:36:43Z
[ "python", "filter", "lambda" ]
I have a list with some dummy email address as below: ``` listEmails = ['brian-23@email.com', 'britts_54@email.com', 'lara$@email.com'] ``` I was trying to use `lambda` and `filter` to get the list of valid email address. let's assume `lara$@email.com` is the only invalid email address. I used the regular expression...
You are missing an *`else`* clause in the conditional expression: ``` x if re.match(...) else None ``` You cannot just use the `if` on its own; all expressions always produce a result, so if the `re.match()` returns `None`, you need to decide what should be returned *instead*. You don't need a conditional expression...
Which Model Field to use in Django to store longitude and latitude values?
30,706,799
14
2015-06-08T10:30:30Z
30,711,177
15
2015-06-08T14:00:24Z
[ "python", "django", "geodjango" ]
I want to store my users location using longitude and latitude, at the moment this comes from Google Maps, but I will be using GeoDango and some point to work out distances between to points also. However, my first confusion is which field in Django I should be using to store the longitude and latitude values? The inf...
Float is generally an approximation, see [here](https://docs.python.org/2/library/decimal.html#quick-start-tutorial) for some simple examples. You could get very nice results modifying your model to something like `DecimalField(max_digits=9, decimal_places=6)`, since decimals are very important in coordinates but using...
Django models: managing transactions when commit_manually is deprecated
30,707,408
3
2015-06-08T11:00:04Z
30,708,253
7
2015-06-08T11:41:41Z
[ "python", "django", "django-models", "override" ]
I'm running Django 1.4.11. I overrode the `save()` method of a Django model in a way similar to the following code: ``` from django.db import models from django.db import transaction class MyModel(models.Model): # model definition @transaction.commit_manually def save(self, *args, **kwargs): try:...
The same logic would look like this: ``` from django.db import models from django.db import transaction class MyModel(models.Model): # model definition def save(self, *args, **kwargs): transaction.set_autocommit(False) try: super(MyModel, self).save(*args, **kwargs) fo...
Python: What is the difference between math.exp and numpy.exp and why do numpy creators choose to introduce exp again
30,712,402
12
2015-06-08T14:51:07Z
30,712,501
15
2015-06-08T14:55:49Z
[ "python", "arrays", "numpy" ]
`exp` means exponential function `exp` in `math module`: <https://docs.python.org/2/library/math.html> `exp` in `numpy module`: <http://docs.scipy.org/doc/numpy/reference/generated/numpy.exp.html> Why do `numpy` creators introduce this function again?
The `math.exp` works only for scalars as [EdChum](http://stackoverflow.com/users/704848/edchum) mentions. Whereas `numpy.exp` will work for arrays. Example: ``` >>> import math >>> import numpy as np >>> x = [1.,2.,3.,4.,5.] >>> math.exp(x) Traceback (most recent call last): File "<pyshell#10>", line 1, in <module...
How can I draw a scatter plot with contour density lines in polar coordinates using Matplotlib?
30,713,586
4
2015-06-08T15:43:23Z
30,715,310
7
2015-06-08T17:17:07Z
[ "python", "matplotlib", "scatter-plot", "polar-coordinates" ]
I am trying to make a scatter plot in **polar coordinates** with the contour lines superposed to the cloud of points. I am aware of how to do that in cartesian coordinates using `numpy.histogram2d`: ``` # Simple case: scatter plot with density contours in cartesian coordinates import matplotlib.pyplot as pl import nu...
The problem is that you're only converting the edges of the array. By converting only the x and y coordinates of the edges, you're effectively converting the coordinates of a diagonal line across the 2D array. This line has a very small range of `theta` values, and you're applying that range to the entire grid. ## The...
Python, Flask: How to set response header for all responses
30,717,152
7
2015-06-08T19:08:33Z
30,717,205
12
2015-06-08T19:11:13Z
[ "python", "flask", "http-headers" ]
I want to set all of my http headers responses to something like this: ``` response.headers["X-Frame-Options"] = "SAMEORIGIN" ``` I checked [this question](http://stackoverflow.com/questions/25860304/how-do-i-set-response-headers-in-flask), but it only changes the header for one specific controller. I want to change ...
Set the header in a [`@app.after_request()` hook](http://flask.pocoo.org/docs/0.10/api/#flask.Flask.after_request), at which point you have a response object to set the header on: ``` @app.after_request def apply_caching(response): response.headers["X-Frame-Options"] = "SAMEORIGIN" return response ``` The [`f...
Are these two python statements the same?
30,721,550
19
2015-06-09T01:18:13Z
30,721,606
22
2015-06-09T01:26:37Z
[ "python", "operators" ]
I have these two statements ``` return self.getData() if self.getData() else '' ``` and ``` return self.getData() or '' ``` I want to know are they same or there is any difference
Maybe, but only if `self.getData()` is a pure function and has no *side effects*. More importantly the object that `self.getData()` returns must also be free of any side effects and consistently return a boolean value. In the simplest case if `f()` is defined as: ``` def f(): return ["Hello World!"] ``` Then the...
Are these two python statements the same?
30,721,550
19
2015-06-09T01:18:13Z
30,721,620
32
2015-06-09T01:28:49Z
[ "python", "operators" ]
I have these two statements ``` return self.getData() if self.getData() else '' ``` and ``` return self.getData() or '' ``` I want to know are they same or there is any difference
I would say No because if `self.getData()` changes something during its operation, then the first statement has the possibility of returning a different result since it will make a 2nd call to it.
Are these two python statements the same?
30,721,550
19
2015-06-09T01:18:13Z
30,721,645
7
2015-06-09T01:32:28Z
[ "python", "operators" ]
I have these two statements ``` return self.getData() if self.getData() else '' ``` and ``` return self.getData() or '' ``` I want to know are they same or there is any difference
The only difference I see is that the first one will call `self.getData()` twice, with the first one being used to evaluate boolean value and the second may be returned(if the first evaluated to True). The other option will evaluate the function only once, using it both as boolean checking and returning. This can be ...
Are these two python statements the same?
30,721,550
19
2015-06-09T01:18:13Z
30,721,652
8
2015-06-09T01:33:09Z
[ "python", "operators" ]
I have these two statements ``` return self.getData() if self.getData() else '' ``` and ``` return self.getData() or '' ``` I want to know are they same or there is any difference
They will have the same result, since both treat `self.getData()`'s result in a boolean context, but beware: 1) `return self.getData() if self.getData() else ''` will run the function `getData` twice, while 2) `return self.getData() or ''` will only run it once. This can be important if `getData()` takes a while to...
Efficient algorithm to find the largest run of zeros in a binary string?
30,722,732
4
2015-06-09T03:57:14Z
30,722,751
8
2015-06-09T03:59:15Z
[ "python", "algorithm" ]
I am looking for an efficient algorithm to find the longest run of zeros in a binary string. My implementation is in Python 2.7, but all I require is the idea of the algorithm. For example, given '0010011010000', the function should return 4.
I don't think there is anything better than a single pass over the string, counting the current sequence length (and updating the maximum) as you go along. If by "binary string" you mean raw bits, you can read them one byte at a time and extract the eight bits in there (using bit shifting or masking). That does not ch...
How to divide list item by list item from another list using Python?
30,725,446
3
2015-06-09T07:23:05Z
30,725,494
8
2015-06-09T07:25:07Z
[ "python", "list", "division" ]
I would like to divide list items inside two lists. ``` a = [[1, 0, 2], [0, 0, 0], [1], [1]] b = [[5, 6, 4], [6, 6, 6], [3], [3]] ``` How can I divide a by b to obtain this output: ``` c = [[0.2, 0, 0.5], [0, 0, 0], [0.333], [0.333]] ``` Can anyone help me?
Zip the two lists and use a list comprehension: ``` from __future__ import division # in python2 only result = [[x/y for x,y in zip(xs, ys)] for xs, ys in zip(a, b)] ``` --- Sample run: ``` In [1]: a = [[1, 0, 2], [0, 0, 0], [1], [1]] ...: b = [[5, 6, 4], [6, 6, 6], [3], [3]] ...: In [2]: result = [[x/y f...
Print into console terminal not into cell output of IPython Notebook
30,729,318
10
2015-06-09T10:22:25Z
30,730,441
9
2015-06-09T11:14:17Z
[ "python", "windows", "ipython", "ipython-notebook", "python-3.4" ]
I would like to print into the terminal window that runs IPython Notebook and not into the cell output. Printing into cell output consumes more memory and slows down my system when I issue a substantial number of `print` calls. In essence, I would like [this](http://stackoverflow.com/questions/23306893/ipython-notebook...
You have to redirect your output to the systems standard output device. This depends on your OS. On Mac that would be: ``` import sys sys.stdout = open('/dev/stdout', 'w') ``` Type the above code in an IPython cell and evaluate it. Afterwards all output will show up in terminal.
seaborn: legend with background color
30,729,473
8
2015-06-09T10:29:00Z
30,736,466
11
2015-06-09T15:28:47Z
[ "python", "plot", "legend", "seaborn" ]
The following question explains how to change the background color of a legend: [matplotlib legend background color](http://stackoverflow.com/questions/19863368/matplotlib-legend-background-color). However, if I use seaborn this does not work. Is there a way to do this? ``` import matplotlib.pyplot as plt import numpy...
seaborn turns the legend frame off by default, if you want to customize how the frame looks, I think you'll need to add `frameon=True` when you call `plt.legend`.
Python default/unnamed method
30,740,886
2
2015-06-09T19:15:52Z
30,740,908
7
2015-06-09T19:17:01Z
[ "python", "class", "object" ]
is it possible to create a python object that has the following property: ``` class Foo: def __default_method__(x): return x f = Foo() f(10) > 10 ``` That is to say, an object that when instantiated allows for a method to be called without the need for an explicit method name?
Yes. It's called [`__call__()`](https://docs.python.org/3/reference/datamodel.html#object.__call__).
Profiling memory usage on App Engine
30,742,104
9
2015-06-09T20:22:28Z
30,742,988
7
2015-06-09T21:16:42Z
[ "python", "google-app-engine", "memory" ]
How can I profile memory (RAM) usage on my App Engine app? I'm trying to address errors related to exceeding the instance memory limit. I've tried these things and, so far, they don't work or don't provide what I need. * Appstats. This doesn't provide memory usage details. * [Apptrace](https://code.google.com/p/apptra...
GAE Mini Profiler does provide memory stats if you use the sampling profiler and set `memory_sample_rate` nonzero; at each snapshot it will tell you the memory that was in use. You will want to turn the sample frequency way down as the memory sample takes a few ms to execute. Edit: the way it gets the memory stats is ...
How to join 2 lists of dicts in python?
30,746,584
5
2015-06-10T03:27:40Z
30,746,653
8
2015-06-10T03:36:26Z
[ "python", "list", "python-2.7" ]
I have 2 lists like this: ``` l1 = [{'a': 1, 'b': 2, 'c': 3, 'd': 4}, {'a': 5, 'b': 6, 'c': 7, 'd': 8}] l2 = [{'a': 5, 'b': 6, 'e': 100}, {'a': 1, 'b': 2, 'e': 101}] ``` and I want to obtain a list `l3`, which is a join of `l1` and `l2` where values of `'a'` and `'b'` are equal in both `l1` and `l2` i.e. ``` l3 = [...
You should accumulate the results in a dictionary. You should use the values of 'a' and 'b' to form a key of this dictionary Here, I have used a `defaultdict` to accumulate the entries ``` l1 = [{'a': 1, 'b': 2, 'c': 3, 'd': 4}, {'a': 5, 'b': 6, 'c': 7, 'd': 8}] l2 = [{'a': 5, 'b': 6, 'e': 100}, {'a': 1, 'b': 2, 'e':...
Create copy of plone installed onto another server with data
30,749,053
3
2015-06-10T06:51:06Z
30,749,377
7
2015-06-10T07:06:28Z
[ "python", "linux", "plone" ]
To create another exact copy of the plone install running along with data, is it sufficient to copy buildout.cfg and Data.fs with same version of Plone on the other install? Does it restore the uploaded pdf and image files that have been done on the first server? Using plone 4.2.1 standalone install on linux
You are right that you also need to transfer files and images. They are stored as BLOBs on the file system. I guess that you will find a directory named `blobstorage`, close to the `filestorage` directory where you found `Data.fs`. You need to transfer this `blobstorage` directory and all its content.
Python 3 UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d
30,750,843
6
2015-06-10T08:18:56Z
30,751,201
9
2015-06-10T08:36:33Z
[ "python", "unicode" ]
I want to make search engine and I follow tutorial in some web. I want to test parse html ``` from bs4 import BeautifulSoup def parse_html(filename): """Extract the Author, Title and Text from a HTML file which was produced by pdftotext with the option -htmlmeta.""" with open(filename) as infile: ...
In Python 3, files are opened text (decoded to Unicode) for you; you don't need to tell BeautifulSoup what codec to decode from. If decoding of the data fails, that's because you didn't tell the `open()` call what codec to use when reading the file; add the correct codec with an `encoding` argument: ``` with open(fil...
How to filter objects for count annotation in Django?
30,752,268
18
2015-06-10T09:22:55Z
30,754,520
30
2015-06-10T11:07:28Z
[ "python", "django", "django-models", "django-aggregation" ]
Consider simple Django models `Event` and `Participant`: ``` class Event(models.Model): title = models.CharField(max_length=100) class Participant(models.Model): event = models.ForeignKey(Event, db_index=True) is_paid = models.BooleanField(default=False, db_index=True) ``` It's easy to annotate events qu...
Just discovered that Django 1.8 has new [conditional expressions feature](https://docs.djangoproject.com/en/1.10/ref/models/conditional-expressions/), so now we can do like this: ``` events = Event.objects.all().annotate(paid_participants=models.Sum( models.Case( models.When(participant__is_paid=True, then...
How to add any new library like spark-csv in Apache Spark prebuilt version
30,757,439
13
2015-06-10T13:13:47Z
30,765,306
15
2015-06-10T19:14:33Z
[ "python", "apache-spark", "apache-spark-sql" ]
I have build the [Spark-csv](https://github.com/databricks/spark-csv) and able to use the same from pyspark shell using the following command ``` bin/spark-shell --packages com.databricks:spark-csv_2.10:1.0.3 ``` error getting ``` >>> df_cat.save("k.csv","com.databricks.spark.csv") Traceback (most recent call last):...
At the time I used spark-csv, I also had to download `commons-csv` jar (not sure it is still relevant). Both jars where in the spark distribution folder. 1. I downloaded the jars as follow: ``` wget http://search.maven.org/remotecontent?filepath=org/apache/commons/commons-csv/1.1/commons-csv-1.1.jar -O commons-...
How to add any new library like spark-csv in Apache Spark prebuilt version
30,757,439
13
2015-06-10T13:13:47Z
31,182,969
10
2015-07-02T11:11:37Z
[ "python", "apache-spark", "apache-spark-sql" ]
I have build the [Spark-csv](https://github.com/databricks/spark-csv) and able to use the same from pyspark shell using the following command ``` bin/spark-shell --packages com.databricks:spark-csv_2.10:1.0.3 ``` error getting ``` >>> df_cat.save("k.csv","com.databricks.spark.csv") Traceback (most recent call last):...
Instead of placing the jars in any specific folder a simple fix would be to start the pyspark shell with the following arguments: ``` bin/pyspark --packages com.databricks:spark-csv_2.10:1.0.3 ``` This will automatically load the required spark-csv jars. Then do the following to read the csv file: ``` from pyspark....
How to add any new library like spark-csv in Apache Spark prebuilt version
30,757,439
13
2015-06-10T13:13:47Z
35,476,136
10
2016-02-18T08:18:59Z
[ "python", "apache-spark", "apache-spark-sql" ]
I have build the [Spark-csv](https://github.com/databricks/spark-csv) and able to use the same from pyspark shell using the following command ``` bin/spark-shell --packages com.databricks:spark-csv_2.10:1.0.3 ``` error getting ``` >>> df_cat.save("k.csv","com.databricks.spark.csv") Traceback (most recent call last):...
Another option is to add the following to your spark-defaults.conf: ``` spark.jars.packages com.databricks:spark-csv_2.11:1.2.0 ```
How to slice middle element from list
30,757,538
2
2015-06-10T13:18:08Z
30,757,595
8
2015-06-10T13:20:06Z
[ "python", "slice" ]
Rather simple question. Say I have a list like: ``` a = [3, 4, 54, 8, 96, 2] ``` Can I use slicing to leave out an element around the middle of the list to produce something like this? ``` a[some_slicing] [3, 4, 8, 96, 2] ``` were the element `54` was left out. I would've guessed this would do the trick: ``` a[:2:...
You cannot emulate pop with a single slice, since a slice only gives you a single start and end index. You can, however, use two slices: ``` >>> a = [3, 4, 54, 8, 96, 2] >>> a[:2] + a[3:] [3, 4, 8, 96, 2] ``` You could wrap this into a function: ``` >>> def cutout(seq, idx): """ Remove element at `i...
Extracting specific src attributes from script tags
30,759,776
4
2015-06-10T14:46:04Z
30,759,895
7
2015-06-10T14:51:18Z
[ "python", "regex" ]
I want to get JS file names from the input content which contains `jquery` as a substring by RE. This is my code: Step 1: Extract JS file from the content. ``` >>> data = """ <script type="text/javascript" src="js/jquery-1.9.1.min.js"/> ... <script type="text/javascript" src="js/jquery-migrate-1.2.1.min.js"/>...
Sure you can. One way would be to use ``` re.findall('src="js/([^"]*jquery[^"]*)"', data) ``` This will match everything after `"js/` until the nearest `"` if it contains `jquery` anywhere. If you know more about the position of `jquery` (for example, if it's always at the start) you can adjust the regex accordingly....
Installing Twisted through pip broken on one server
30,763,614
5
2015-06-10T17:43:20Z
30,766,325
10
2015-06-10T20:08:56Z
[ "python", "pip", "virtualenv", "twisted" ]
I am setting up a virtualenv on a new server, and when I used pip on our requirements file, it kept dying on Twisted. I commented the Twisted line out, and everything else installed fine. At the command line, this is the output I see when I try to install Twisted (the same error I see when I run the entire requirements...
Ok after struggling with this for several hours, I figured out the problem. Running `pip install --verbose twisted` helped with the diagnosis. The error message is misleading. The problem is that I built a custom installation of Python 2.7.10 without having previously installed libbz2-dev. So the steps to fix this we...
Python numpy: create 2d array of values based on coordinates
30,764,955
6
2015-06-10T18:55:19Z
30,765,484
10
2015-06-10T19:23:28Z
[ "python", "arrays", "numpy" ]
I have a file containing 3 columns, where the first two are coordinates (x,y) and the third is a value (z) corresponding to that position. Here's a short example: ``` x y z 0 1 14 0 2 17 1 0 15 1 1 16 2 1 18 2 2 13 ``` I want to create a 2D array of values from the third row based on their x,y coordinates in the file...
Assuming the `x` and `y` values in your file directly correspond to indices (as they do in your example), you can do something similar to this: ``` import numpy as np x = [0, 0, 1, 1, 2, 2] y = [1, 2, 0, 1, 1, 2] z = [14, 17, 15, 16, 18, 13] z_array = np.nan * np.empty((3,3)) z_array[y, x] = z print z_array ``` Wh...
What's the correct way to clean up after an interrupted event loop?
30,765,606
19
2015-06-10T19:30:45Z
30,766,124
17
2015-06-10T19:58:05Z
[ "python", "python-3.4", "python-asyncio" ]
I have an event loop that runs some co-routines as part of a command line tool. The user may interrupt the tool with the usual `Ctrl` + `C`, at which point I want to clean up properly after the interrupted event loop. Here's what I tried. ``` import asyncio @asyncio.coroutine def shleepy_time(seconds): print("S...
When you CTRL+C, the event loop gets stopped, so your calls to `t.cancel()` don't actually take effect. For the tasks to be cancelled, you need to start the loop back up again. Here's how you can handle it: ``` import asyncio @asyncio.coroutine def shleepy_time(seconds): print("Shleeping for {s} seconds...".form...
Regular Expression to find brackets in a string
30,766,151
4
2015-06-10T19:59:36Z
30,766,212
7
2015-06-10T20:02:51Z
[ "python", "regex" ]
I have a string which has multiple brackets. Let says ``` s="(a(vdwvndw){}]" ``` I want to extract all the brackets as a separate string. I tried this: ``` >>> brackets=re.search(r"[(){}[]]+",s) >>> brackets.group() ``` But it is only giving me last two brackets. ``` '}]' ``` Why is that? Shouldn't it fetch one ...
You have to escape the first closing square bracket. ``` r'[(){}[\]]+' ``` To combine all of them into a string, you can search for anything that *doesn't* match and remove it. ``` brackets = re.sub( r'[^(){}[\]]', '', s) ```
Error importing scikit-learn modules
30,766,274
5
2015-06-10T20:06:39Z
34,579,309
8
2016-01-03T17:17:40Z
[ "python", "scikit-learn" ]
I'm trying to call a function from the cluster module, like so: ``` import sklearn db = sklearn.cluster.DBSCAN() ``` and I get the following error: ``` AttributeError: 'module' object has no attribute 'cluster' ``` Tab-completing in IPython, I seem to have access to the base, clone, externals, re, setup\_module, sy...
You probably don't use Numpy+MKL, but only Numpy. I had the same problem and reinstalling Numpy with MKL `pip install --upgrade --force-reinstall "numpy-1.10.2+mkl-cp35-none-win32.whl"` fixed it.
flask restful: passing parameters to GET request
30,779,584
4
2015-06-11T11:31:20Z
30,779,996
16
2015-06-11T11:51:17Z
[ "python", "rest", "flask", "flask-restful" ]
I want to create a resource that supports GET request in following way: ``` /bar?key1=val1&key2=val2 ``` I tried this code, but it is not working ``` app = Flask(__name__) api = Api(app) class BarAPI(Resource): def get(key1, key2): return jsonify(dict(data=[key1, key2])) api.add_resource(BarAPI, '/bar'...
**Edit: This is no longer the recommended way to do this with flask-restful!** The `reqparse` object is deprecated see [docs](http://flask-restful-cn.readthedocs.org/en/0.3.5/reqparse.html) for recommended alternative. --- Use `reqparse`. You can see another example in the flask-restful [docs](http://flask-restful.re...
An easier way of referring to the index of a desired dictionary (Python)?
30,780,027
2
2015-06-11T11:52:27Z
30,780,126
7
2015-06-11T11:57:26Z
[ "python", "for-loop" ]
Question feels like it's phrased poorly, feel free to adjust it if you agree and know how better to phrase it. I have the following code: ``` def owned_calendars(cal_items): """Returns only the calendars in which the user is marked as "owner" """ owner_cals = [] for entry in cal_items: if ent...
Try this. You can do this in one line using [list comprehension](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions). ``` owner_cals = [x for x in cal_items if x["access_role"]=="owner"] ``` You can also use [`enumerate`](https://docs.python.org/2/library/functions.html#enumerate) method. ```...
How to use `--foo 1 --foo 2` style arguments with Python argparse?
30,780,779
6
2015-06-11T12:28:11Z
30,780,951
7
2015-06-11T12:35:19Z
[ "python", "arguments", "argparse" ]
`nargs='+'` doesn't work the way I expected: ``` >>> import argparse >>> parser = argparse.ArgumentParser() >>> parser.add_argument("--name", dest='names', nargs='+') _StoreAction(option_strings=['--name'], dest='names', nargs='+', const=None, default=None, type=None, choices=None, help=None, metavar=None) >>> parser....
You want to use `action='append'` instead of `nargs='+'`: ``` >>> parser.add_argument("--name", dest='names', action='append') _AppendAction(option_strings=['--name'], dest='names', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None) >>> parser.parse_args('--name foo --name bar'.spl...
"Too many indexers" with DataFrame.loc
30,781,037
8
2015-06-11T12:38:50Z
30,781,664
8
2015-06-11T13:06:10Z
[ "python", "pandas" ]
I've read [the docs about slicers](http://pandas.pydata.org/pandas-docs/stable/advanced.html#using-slicers) a million times, but have never got my head round it, so I'm still trying to figure out how to use `loc` to slice a `DataFrame` with a `MultiIndex`. I'll start with the `DataFrame` from [this SO answer](http://s...
To be safe (in the sense: this will work in all cases), you need to index both row index and columns, for which you can use `pd.IndexSlice` to do this easily: ``` In [26]: idx = pd.IndexSlice In [27]: df.loc[idx[:, :, 'C1', :],:] Out[27]: value first second third fourth A0 B0 C1 D...
Am I using `all` correctly?
30,783,333
5
2015-06-11T14:13:32Z
30,783,369
13
2015-06-11T14:14:52Z
[ "python" ]
A user asked ([Keyerror while using pandas in PYTHON 2.7](http://stackoverflow.com/questions/30758495/keyerror-while-using-pandas-in-python-2-7)) why he was having a `KeyError` while looking in a dictionary and how he could avoid this exception. As an answer, I suggested him to check for the keys in the dictionary bef...
Yes that will work fine, but you don't even need the list comprehension ``` if not all(x in dictionary for x in ['key_a', 'key_b', 'key_c']): continue ``` If you have the surrounding `[]`, it will evaluate all the elements before calling `all`. If you remove them, the inner expression is a generator, and will [sh...
Can I trust the order of a dict to remain the same each time it is iterated over?
30,787,056
6
2015-06-11T17:02:57Z
30,787,109
9
2015-06-11T17:05:37Z
[ "python", "list", "dictionary", "iteration" ]
I have the following three strings (they exist independently but are displayed here together for convenience): ``` from mx2.x.org (mx2.x.org. [198.186.238.144]) by mx.google.com with ESMTPS id g34si6312040qgg.122.2015.04.22.14.49.15 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); ...
> can I be sure that the dict, any time it is iterated over, will always be done in the order in which the dict was created? No, a `dict` is unordered, and will lay out its ordering however the particular implementation decides to. ``` >>> d = {3: 'c', 2: 'b', 1: 'a'} >>> d {1: 'a', 2: 'b', 3: 'c'} ``` See, immediat...
mandrill template variables not substituting
30,787,369
4
2015-06-11T17:19:13Z
30,930,699
9
2015-06-19T05:42:07Z
[ "python", "email", "templates", "handlebars.js", "mandrill" ]
Having issue with template variables not substituting when sending an email. I have a simple template: `<div class="entry"> Your name is {{firstName}} </div>` And my python code to send an email: ``` client = mandrill.Mandrill(apikey=api_key) my_merge_vars = [{'content': 'Dexter', 'name': 'firstName'}] message = {'...
Make sure you specify that the merge type is handlebars. You can either do it in your account settings (Settings > Sending Defaults > Merge Language) or in your API call via the 'merge\_language' parameter.
takeOrdered descending Pyspark
30,787,635
20
2015-06-11T17:34:16Z
30,788,891
44
2015-06-11T18:41:07Z
[ "python", "apache-spark" ]
i would like to sort K/V pairs by values and then take the biggest five values. I managed to do this with reverting K/V with first map, sort in descending order with FALSE, and then reverse key.value to the original (second map) and then take the first 5 that are the bigget, the code is this: ``` RDD.map(lambda x:(x[1...
Sort by keys (ascending): ``` RDD.takeOrdered(5, key = lambda x: x[0]) ``` Sort by keys (descending): ``` RDD.takeOrdered(5, key = lambda x: -x[0]) ``` Sort by values (ascending): ``` RDD.takeOrdered(5, key = lambda x: x[1]) ``` Sort by values (descending): ``` RDD.takeOrdered(5, key = lambda x: -x[1]) ```
ValueError: cannot reindex from a duplicate axis using isin with pandas
30,788,061
2
2015-06-11T17:55:15Z
34,018,827
7
2015-12-01T11:05:45Z
[ "python", "pandas", "dataframe", "gotchas" ]
I am trying to short zipcodes into various files but I keep getting > ValueError: cannot reindex from a duplicate axis I've read through other documentation on Stackoverflow, but I haven't been about to figure out why its duplicating axis. ``` import csv import pandas as pd from pandas import DataFrame as df fp = '/...
I'm pretty sure your problem is related to your mask ``` df = df[['VIN', 'Reg Name', 'Reg Address', 'Reg City', 'Reg ST', 'ZIP', 'ZIP', 'Catagory', 'Phone', 'First Name', 'Last Name', 'Reg NFS', 'MGVW', 'Make', 'Veh Model','E Mfr', 'Engine Model', 'CY2010', 'CY2011', 'CY2012', 'CY2013', 'CY2...
How to filter a pandas dataframe by cells that DO NOT contain a substring?
30,791,265
3
2015-06-11T20:53:52Z
30,791,401
7
2015-06-11T21:03:34Z
[ "python", "pandas" ]
I want to filter a dataframe to find rows which do not contain the string 'site'. I know how to filter for rows which do contain 'site' but have not been able to get the reverse working. Here is what I have so far: ``` def rbs(): #removes blocked sites frame = fill_rate() mask = frame[frame['Media'].str.conta...
Just do `frame[~frame['Media'].str.contains('Site')]` The `~` negates the boolean condition So your method becomes: ``` def rbs(): #removes blocked sites frame = fill_rate() return frame[~frame['Media'].str.contains('Site')] ``` **EDIT** it looks like you have `NaN` values judging by your errors so you hav...
Python multiple condition IN string
30,798,167
9
2015-06-12T08:01:32Z
30,798,228
8
2015-06-12T08:04:49Z
[ "python" ]
How to merge the condition in array format ``` lines = [line for line in open('text.txt')if '|E|' in line and 'GetPastPaymentInfo' in line and 'CheckData' not in line and 'UpdatePrintStatus' not in line ] ``` like ``` lines = [line for line in open('text.txt')if ['|E...
You can use a [generator expression](https://www.python.org/dev/peps/pep-0289/) within [`all`](https://docs.python.org/2/library/functions.html#all) function to check the membership for all elements : ``` lines = [line for line in open('text.txt') if all(i in line for i in ['|E|','GetPastPaymentInfo'])and ...
How to increase the performance for estimating `Pi`in Python
30,807,010
5
2015-06-12T15:37:25Z
30,807,145
8
2015-06-12T15:44:48Z
[ "python", "performance", "pi" ]
I have written the following code in Python, in order to estimate the value of `Pi`. It is called [Monte Carlo](https://en.wikipedia.org/wiki/Monte_Carlo_method) method. Obviously by increasing the number of samples the code becomes slower and I assume that the slowest part of the code is in the sampling part. How can ...
The bottleneck here is actually your `for` loop. Python `for` loops are relatively slow, so if you need to iterate over a million items, you can gain a lot of speed by avoiding them altogether. In this case, it's quite easy. Instead of this: ``` for item in range(n): if ((s1[item])**2 + (s2[item])**2) < 1: ...
Python PEP 273 and Amazon BotoCore
30,808,297
10
2015-06-12T16:47:56Z
30,847,419
7
2015-06-15T14:12:47Z
[ "python", "python-2.7", "python-import", "pep", "botocore" ]
On a small embedded Linux device with limited space, I am trying to place the large [10 Mb] Amazon (AWS) BotoCore library (<https://github.com/boto/botocore>) in a zip file to compress it and then import it in my Python Scripts using zipimport as described in PEP273 (<https://www.python.org/dev/peps/pep-0273/>). I mod...
Unfortunately, this just isn't going to work. PEP 273 requires library authors to follow certain rules, which this package does not. In particular, it [makes use of `__file__`](https://github.com/boto/botocore/search?utf8=%E2%9C%93&q=__file__) rather than [`pkgutil.get_data()`](https://docs.python.org/3/library/pkguti...
How to select columns from dataframe by regex
30,808,430
9
2015-06-12T16:55:19Z
30,808,571
17
2015-06-12T17:04:16Z
[ "python", "python-2.7", "pandas" ]
I have a dataframe in python pandas. The structure of the dataframe is as the following: ``` a b c d1 d2 d3 10 14 12 44 45 78 ``` I would like to select the columns which begin with d. Is there a simple way to achieve this in python .
You can use [`DataFrame.filter`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.filter.html) this way: ``` import pandas as pd df = pd.DataFrame(np.array([[2,4,4],[4,3,3],[5,9,1]]),columns=['d','t','didi']) >> d t didi 0 2 4 4 1 4 3 3 2 5 9 1 df.filter(regex=("d.*")) >...
Getting a strange result when comparing 2 dictionaries in python
30,808,586
2
2015-06-12T17:05:30Z
30,808,622
7
2015-06-12T17:07:58Z
[ "python", "python-2.7" ]
So I have a pair of dictionaries in python: (both have exactly the same keys) ``` defaults = {'ToAlpha': 4, 'ToRed': 4, 'ToGreen': 4, 'ToBlue': 4,} bridged = {'ToAlpha': 3, 'ToRed': 0, 'ToGreen': 1, 'ToBlue': 2,} ``` When I iterate through one of the dictionaries I do a quick check to see if the other dict has the s...
`0` is false. Use `in` to check for containment. ``` if key in bridged: ```
Error when using classify in caffe
30,808,735
4
2015-06-12T17:15:04Z
30,809,008
11
2015-06-12T17:31:47Z
[ "python", "python-2.7", "caffe" ]
I am using caffe in python to classify. I get code from [here](http://www.openu.ac.il/home/hassner/projects/cnn_agegender/). In here, I just use simple code such as ``` plt.rcParams['figure.figsize'] = (10, 10) plt.rcParams['image.interpolation'] = 'nearest' plt.rcParams['image.cmap'] = 'gray' mean_filename='./mean.bi...
Let go to line 253-254 in caffe/python/caffe/io.py Replace ``` if ms != self.inputs[in_][1:]: raise ValueError('Mean shape incompatible with input shape.') ``` By ``` if ms != self.inputs[in_][1:]: print(self.inputs[in_]) in_shape = self.inputs[in_][1:] m_min, m_max = mean.min(), mean.max() norma...
How to use Python 3.4's enums without significant slowdown?
30,812,793
7
2015-06-12T22:00:28Z
30,812,818
10
2015-06-12T22:03:45Z
[ "python", "performance", "enums", "python-3.4" ]
I was writing a tic-tac-toe game and using an Enum to represent the three outcomes -- `lose`, `draw`, and `win`. I thought it would be better style than using the strings `("lose", "win", "draw")` to indicate these values. But using enums gave me a significant performance hit. Here's a minimal example, where I simply ...
You are timing the timing loop. A string literal on its own is *ignored entirely*: ``` >>> import dis >>> def f(): "lose" ... >>> dis.dis(f) 1 0 LOAD_CONST 1 (None) 3 RETURN_VALUE ``` That's a function that does nothing *at all*. So the timing loop takes `0.024598151998361573`...
How do I set up a virtual environment with Flask using conda?
30,815,337
2
2015-06-13T05:11:25Z
30,815,534
7
2015-06-13T05:44:08Z
[ "python", "flask", "virtualization", "anaconda", "conda" ]
I wish to set up a virtual environment that I can use to develop web applications using the Flask framework for Python (3.4.2, Mac OS). I was given the instructions on how to do that [here](http://www.enigmeta.com/2012/08/16/starting-flask/), using the virtualenv. However, trying to follow these instructions I ran into...
Your mileage may vary, but the docs tends to be where the answers are. ``` conda create -n my_flask_env source activate my_flask_env conda install condastuff pip install otherstuff ```