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
numpy subtract/add 1d array from 2d array
33,303,348
3
2015-10-23T13:20:07Z
33,303,590
8
2015-10-23T13:30:11Z
[ "python", "arrays", "numpy" ]
I have the following 2D-array: ``` a = array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [10, 11, 12], [13, 14, 15]]) ``` and another 1D-array: ``` b = array([ 1, 2, 3, 4, 5]) ``` then I want to calculate something like ``` c = a - b ``` with the intent of getting: ...
You need to convert array `b to a (2, 1) shape` array, use `None or numpy.newaxis` in the index tuple. Here is the [Indexing of Numpy array](http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#basic-slicing). You can do it Like: ``` import numpy a = numpy.array([[ 1, 2, 3], [ 4, 5, 6], ...
Django Rest Framework -- no module named rest_framework
33,308,781
7
2015-10-23T18:05:38Z
33,308,988
7
2015-10-23T18:18:33Z
[ "python", "django", "python-3.x", "pip", "django-rest-framework" ]
I've installed django rest framework using `pip install djangorestframework` yet I still get this error when I run "python3 manage.py sycndb": > ImportError: No module named 'rest\_framework' I'm using python3, is this my issue?
You need to install django rest framework using pip3 (pip for python 3): ``` pip3 install djangorestframework ``` Instructions on how to install pip3 can be found [here](http://stackoverflow.com/questions/6587507/how-to-install-pip-with-python-3)
Is there a way to change the location of pytest's .cache directory?
33,310,615
8
2015-10-23T20:10:09Z
38,085,113
7
2016-06-28T19:50:19Z
[ "python", "caching", "directory", "py.test" ]
I need to be able to change the location of pytest's .cache directory to the env variable, WORKSPACE. Due to server permissions out of my control, I am running into this error because my user does not have permission to write in the directory where the tests are being run from: ``` py.error.EACCES: [Permission denied]...
You can prevent the creation of `.cache/` by disabling the "cacheprovider" plugin: ``` py.test -p no:cacheprovider ... ```
max([x for x in something]) vs max(x for x in something): why is there a difference and what is it?
33,326,049
16
2015-10-25T03:59:45Z
33,326,128
7
2015-10-25T04:17:55Z
[ "python", "python-2.7", "list-comprehension" ]
I was working on a project for class where my code wasn't producing the same results as the reference code. I compared my code with the reference code line by line, they appeared almost exactly the same. Everything seemed to be logically equivalent. Eventually I began replacing lines and testing until I found the line...
Use of the `[]` around a list comprehension actually generates a list into your variable, or in this case into your max function. Without the brackets you are creating a `generator` object that will be fed into the max function. ``` results1 = (x for x in range(10)) results2 = [x for x in range(10)] result3 = max(x fo...
max([x for x in something]) vs max(x for x in something): why is there a difference and what is it?
33,326,049
16
2015-10-25T03:59:45Z
33,326,754
18
2015-10-25T06:07:48Z
[ "python", "python-2.7", "list-comprehension" ]
I was working on a project for class where my code wasn't producing the same results as the reference code. I compared my code with the reference code line by line, they appeared almost exactly the same. Everything seemed to be logically equivalent. Eventually I began replacing lines and testing until I found the line...
Are you leaking a local variable which is affecting later code? ``` # works action = 'something important' max_q = max(self.getQValue(nextState, action) for action in legal_actions) assert action == 'something important' # doesn't work (i.e., provides different results) max_q = max([self.getQValue(nextState, action) ...
Why does the 'in' keyword claim it needs an iterable object?
33,326,150
9
2015-10-25T04:22:24Z
33,326,230
13
2015-10-25T04:35:08Z
[ "python" ]
``` >>> non_iterable = 1 >>> 5 in non_iterable Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: 'int' object is not iterable >>> class also_non_iterable: ... def __contains__(self,thing): ... return True >>> 5 in also_non_iterable() True >>> isinstance(also_non_iterable(...
It claims to want an iterable because, if the object's class does not implement an `__contains__` , then `in` tries to iterate through the object and check if the values are equal to the values yield by it. An Example to show that - ``` >>> class C: ... def __iter__(self): ... return iter([1,2,3,4]) >>> >...
Trie tree match performance in word search
33,327,602
17
2015-10-25T08:20:16Z
33,381,104
8
2015-10-28T00:53:12Z
[ "python", "algorithm" ]
I have debugging a few similar solutions, but wondering if we could improve Trie Tree to partial match prefix (in search method of class Trie, current search method only check if a full word is matched or not) to even improve performance, which could return from a wrong path earlier? I am not very confident for the ide...
I don't see anything wrong from the `Trie` part in your code. But I think the trie's original design already has early returning when detecting any mismatch. Actually, I usually only use regular `dict` as a trie instead of `defaultDict + TrieNode` to avoid making the problem over-complicated. You just need to set a `...
django type object Http404 has no attribute get
33,328,656
3
2015-10-25T10:32:44Z
33,328,657
10
2015-10-25T10:32:44Z
[ "python", "django" ]
I have this code: ``` if not selected_organization in request.user.organizations.all(): return Http404 ``` while returning the http 404 I got this : ``` type object 'Http404' has no attribute 'get' ```
Took me a while to figure out, Eventually I had to `raise` the `Http404` and not `return` it!
Plots are not visible using matplotlib plt.show()
33,329,921
5
2015-10-25T12:54:00Z
33,330,027
13
2015-10-25T13:05:17Z
[ "python", "python-3.x", "ubuntu", "matplotlib", "plot" ]
I'm really new in Python and Linux and I need help, I tried to use matplotlib for showing a simple plot in the following way: ``` from matplotlib import pyplot as plt plt.plot([5,6,7,8], [7,3,8,3]) plt.show() ``` But, when I run `python3 test.py`, I get the following output: ``` /usr/local/lib/python3.4/dist-packag...
As you can see: ``` "The Gtk3Agg backend is known to not work on Python 3.x with pycairo." ``` And so the ***suggestion*** presented is: ``` Try installing cairocffi. ``` The [**installation guide**](https://pythonhosted.org/cairocffi/overview.html) for `cairocffi` is pretty straight-forward. If the dependencies1 a...
Force compiler when running python setup.py install
33,331,563
15
2015-10-25T15:41:06Z
33,373,672
9
2015-10-27T16:41:34Z
[ "python", "cython", "setuptools", "anaconda" ]
Is there a way to explicitly force the compiler for building Cython extensions when running `python setup.py install`? Where `setup.py` is of the form: ``` import os.path import numpy as np from setuptools import setup, find_packages, Extension from Cython.Distutils import build_ext setup(name='test', packages=find...
You can provide (default) command line arguments for distutils in a separate file called `setup.cfg` (placed parallel to your `setup.py`). See the [docs](https://docs.python.org/3.4/distutils/configfile.html) for more information. To set the compiler use something like: ``` [build] compiler=msvc ``` Now calling `pyth...
weird behaviour of dict.keys() in python
33,342,345
2
2015-10-26T09:28:49Z
33,342,379
7
2015-10-26T09:31:17Z
[ "python", "python-3.x" ]
I have this code in my script to perform Huffman encoding: ``` def huffmanEncoding(freqDict): for key in freqDict.keys(): freqDict[HuffmanTree(value=key)] = freqDict.pop(key) ... ``` What I want to do is to replace each of the keys in the dictionary with a tree node whose value is the original key. The Hu...
You are adding keys to and removing keys from the dictionary *while iterating*. That means that the [order of the keys also can change](https://stackoverflow.com/questions/15479928/why-is-the-order-in-python-dictionaries-and-sets-arbitrary/15479974#15479974). Normally, Python would raise an exception when you do this,...
Add a count field to a django rest framework serializer
33,345,089
5
2015-10-26T11:51:23Z
34,675,543
8
2016-01-08T11:05:25Z
[ "python", "django", "serialization" ]
I am serializing the built-in django Group model and would like to add a field to the serializer that counts the number of users in the group. I am currently using the following serializer: ``` class GroupSerializer(serializers.ModelSerializer): class Meta: model = Group fields = ('id', 'name', 'us...
A bit late but short answer. Try this ``` user_count = serializers.IntegerField( source='user_set.count', read_only=True ) ```
SyntaxError with passing **kwargs and trailing comma
33,350,454
11
2015-10-26T16:10:31Z
33,350,635
9
2015-10-26T16:19:15Z
[ "python", "python-3.x", "syntax-error", "python-3.4" ]
I wonder why this is a SyntaxError in Python 3.4: ``` some_function( filename = "foobar.c", **kwargs, ) ``` It works when removing the trailing comma after `**kwargs`.
As pointed out by vaultah (who for some reason didn’t bother to post an answer), this was [reported on the issue tracker](http://bugs.python.org/issue9232) and has been changed since. The syntax will work fine starting with Python 3.6. > To be explicit, yes, I want to allow trailing comma even after `*args` or `**kw...
SyntaxError with passing **kwargs and trailing comma
33,350,454
11
2015-10-26T16:10:31Z
33,350,709
8
2015-10-26T16:22:01Z
[ "python", "python-3.x", "syntax-error", "python-3.4" ]
I wonder why this is a SyntaxError in Python 3.4: ``` some_function( filename = "foobar.c", **kwargs, ) ``` It works when removing the trailing comma after `**kwargs`.
The reason it was originally disallowed is because `**kwargs` was the last allowed item in an argument list -- nothing could come after it; however, a `,` looks like there could be more following it. That has changed so that we can now call with multiple keyword dicts: ``` some_func(a, b, **c, **d,) ``` For consiste...
Mysterious interaction between Python's slice bounds and "stride"
33,352,169
13
2015-10-26T17:41:41Z
33,352,237
7
2015-10-26T17:44:36Z
[ "python", "list", "python-2.7", "slice" ]
I understand that given an iterable such as ``` >>> it = [1, 2, 3, 4, 5, 6, 7, 8, 9] ``` I can turn it into a list and slice off the ends at arbitrary points with, for example ``` >>> it[1:-2] [2, 3, 4, 5, 6, 7] ``` or reverse it with ``` >>> it[::-1] [9, 8, 7, 6, 5, 4, 3, 2, 1] ``` or combine the two with ``` >...
This is because in a slice like - ``` list[start:stop:step] ``` `start` is ***inclusive***, resultant list starts at index `start`. `stop` is ***exclusive***, that is the resultant list only contains elements till `stop - 1` (and not the element at `stop`). So for your case`it[1:-2]` - the `1` is *inclusive* , that...
Flask hangs when sending a post request to itself
33,353,192
4
2015-10-26T18:38:23Z
33,353,724
9
2015-10-26T19:11:11Z
[ "python", "flask" ]
I'm trying to send a post request to my Flask app from one of its own views, but it hangs until I kill the server. If I do the request in JavaScript, it works fine. Why doesn't it work from the Python code? ``` from flask import Blueprint, render_template, abort, request, Response, session, url_for from jinja2 import ...
Flask's development server is single-threaded by default. It can only handle one request at a time. Making a request blocks until it receives the response. Your Flask code makes a request in the one thread, and then waits. There are no other threads to handle this second request. So the request never completes, and the...
Ansible: best practice for maintaining list of sudoers
33,359,404
3
2015-10-27T03:20:33Z
33,362,805
8
2015-10-27T08:16:04Z
[ "python", "unix", "ansible", "user-management" ]
In the [documentation](http://docs.ansible.com/ansible/lineinfile_module.html), there is an example of using the `lineinfile` module to edit `/etc/sudoers`. ``` - lineinfile: "dest=/etc/sudoers state=present regexp='^%wheel' line='%wheel ALL=(ALL) NOPASSWD: ALL'" ``` Feels a bit hackish. I assumed there would be som...
That line isn't actually adding an users to sudoers, merely making sure that the `wheel` group can have passwordless sudo for all command. As for adding users to `/etc/sudoers` this is best done by adding users to necessary groups and then giving these groups the relevant access to sudo. This holds true when you aren'...
Random number between 0 and 1 in python
33,359,740
3
2015-10-27T03:59:10Z
33,359,801
8
2015-10-27T04:06:00Z
[ "python", "random" ]
I want a random number between 0 and 1 . like 0.3452 I used `random.randrange(0,1)` but it is always 0! for me. what should i do?
You can use `random.uniform` ``` random.uniform(0, 1) ```
How to show warnings in py.test
33,363,433
10
2015-10-27T08:53:27Z
33,370,276
15
2015-10-27T14:11:14Z
[ "python", "django", "testing", "py.test", "pytest-django" ]
I just ran [`py.test`](http://pytest.org) on my code and got the following output: ``` ================== 6 passed, 2 pytest-warnings in 40.79 seconds ======================= ``` However, I cannot see what `py.test` would like to warn me about. How can I turn on warning output to the console? `py.test --help` offers...
In this case, pytest-warnings are warnings which were generated for pytest and/or it's plugins. These warnings were not generated for your code. In order to list them in the report, you will need to use option `-r w`. Here is portion of `py.test --help`: ``` -r chars show extra test summary info as specif...
String character identity paradox
33,366,403
11
2015-10-27T11:09:54Z
33,368,485
7
2015-10-27T12:52:12Z
[ "python", "string", "python-internals" ]
I'm completely stuck with this ``` >>> s = chr(8263) >>> x = s[0] >>> x is s[0] False ``` How is this possible? Does this mean that accessing a string character by indexing create a new instance of the same character? Let's experiment: ``` >>> L = [s[0] for _ in range(1000)] >>> len(set(L)) 1 >>> ids = map(id, L) >>...
There are two point to make here. First, Python does indeed create a new character with the `__getitem__` call, but only if that character has ordinal value greater than 256. Observe: ``` >>> s = chr(256) >>> s[0] is s True >>> t = chr(257) >>> t[0] is t False ``` This is because internally, the compiled [`getitem...
Split dataframe into relatively even chunks according to length
33,367,142
3
2015-10-27T11:44:36Z
33,368,088
8
2015-10-27T12:31:35Z
[ "python", "pandas" ]
I have to create a function which would split provided dataframe into chunks of needed size. For instance if dataframe contains 1111 rows, I want to be able to specify chunk size of 400 rows, and get three smaller dataframes with sizes of 400, 400 and 311. Is there a convenience function to do the job? What would be th...
You can use `.groupby` as below. ``` for g, df in test.groupby(np.arange(len(test)) // 400): print(df.shape) # (400, 2) # (400, 2) # (311, 2) ```
What is the easiest way to install BLAS and LAPACK for scipy?
33,368,261
4
2015-10-27T12:40:16Z
33,369,271
7
2015-10-27T13:27:29Z
[ "python", "numpy" ]
I would like to run a programme that someone else has prepared and it includes scipy. I have tried to install scipy with ``` pip install scipy ``` but it gives me a long error. I know there are ways with Anaconda and Canopy but I think these are long ways. I would like to have a short way. I have also tried ``` G:\d...
The [SciPy installation page](http://www.scipy.org/install.html) already recommends several ways of installing python with SciPy already included, such as [WinPython](http://winpython.github.io/). Another way is to use [wheels](https://pip.pypa.io/en/latest/user_guide/#installing-from-wheels) (a built-package format):...
What is the easiest way to install BLAS and LAPACK for scipy?
33,368,261
4
2015-10-27T12:40:16Z
33,369,334
9
2015-10-27T13:30:01Z
[ "python", "numpy" ]
I would like to run a programme that someone else has prepared and it includes scipy. I have tried to install scipy with ``` pip install scipy ``` but it gives me a long error. I know there are ways with Anaconda and Canopy but I think these are long ways. I would like to have a short way. I have also tried ``` G:\d...
> "Why does a scipy get so complicated? It gets so complicated because Python's package management system is built to track Python package dependencies, and SciPy and other scientific tools have dependencies beyond Python. [Wheels](http://pythonwheels.com/) fix part of the problem, but my experience is that tools like...
How to chose an AWS profile when using boto3 to connect to CloudFront
33,378,422
5
2015-10-27T21:02:50Z
33,395,432
11
2015-10-28T15:41:31Z
[ "python", "amazon-web-services", "boto3" ]
I am using the Boto 3 python library, and want to connect to AWS CloudFront. I need to specify the correct AWS Profile (AWS Credentials), but looking at the official documentation, I see no way to specify it. I am initializing the client using the code: `client = boto3.client('cloudfront')` However, this results in i...
I think the docs aren't wonderful at exposing how to do this. It has been a supported feature for some time, however, and there are some details in this [pull request](https://github.com/boto/boto3/pull/69). So there are three ways to do this: 1. Create a new session with the profile ``` dev = boto3.session.Ses...
Repeat each item in a list a number of times specified in another list
33,382,474
2
2015-10-28T03:34:40Z
33,382,503
8
2015-10-28T03:38:30Z
[ "python", "list", "repeat" ]
I have two lists, `x` and `y`: ``` >>> x = [2, 3, 4] >>> y = [1, 2, 3] ``` I want to use these to create a new list. The new list will have each element in `x` repeated the number of times specified by the corresponding element in `y`. Hence, the desired output is ``` >>> new_list [2, 3, 3, 4, 4, 4] ``` The order o...
1. You can use list comprehension, like this ``` >>> x = [2, 3, 4] >>> y = [1, 2, 3] >>> [item for item, count in zip(x, y) for i in range(count)] [2, 3, 3, 4, 4, 4] ``` Here, we `zip` the `x` and `y` so that the element from `x` and its corresponding count from `y` are grouped as a single tuple....
Is there a javascript equivalent for the python pass statement that does nothing?
33,383,840
5
2015-10-28T05:53:58Z
33,383,865
10
2015-10-28T05:55:59Z
[ "javascript", "python" ]
I am looking for a javascript equivalent for the python `pass` statement that does nothing. Is there such a thing in javascript?
Python's `pass` mainly exists because in Python whitespace matters within a block. In Javascript, the equivalent would be putting nothing within the block, i.e. `{}`.
Kalman filter (one-dimensional): several approaches?
33,384,112
2
2015-10-28T06:13:21Z
33,385,625
8
2015-10-28T07:54:21Z
[ "python", "algorithm", "filtering", "kalman-filter" ]
I try to understand how the Kalman filter works and because the multi-dimensional variants were too confusing for the beginning I started off with a one-dimensional example. I found 3 different sources explaining the scenario of a thermometer but all of these scenarios implement slightly different equations and I do n...
All of these solutions are special cases of the general equations and we'll have to see what's special about each one. # Proper equations Let's start with the proper general equations for the 1D case: ``` # prediction x[k] = a * x[k - 1] p[k] = a * p[k - 1] * a + q # update y = z - h * x[k] kg = p * h / (h * p * h +...
Unable to install boto3
33,388,555
3
2015-10-28T10:27:24Z
33,388,653
8
2015-10-28T10:31:29Z
[ "python", "virtualenv", "boto3" ]
I have trouble installing boto3 inside a virtual environment. I have done what the document says. First I activated virtual environment. then I did a: ``` Sudo pip install boto3 ``` Now I enter python ``` >> import boto3 ImportError: No module named boto3 ``` But if I import boto, it works ``` >> import boto >> b...
Don't use `sudo` in a virtual environment because it ignores the environment's variables and therefore `sudo pip` refers to your **global** pip installation. So with your environment activated, rerun `pip install boto3` but without sudo.
Getting Spark, Python, and MongoDB to work together
33,391,840
24
2015-10-28T13:05:00Z
33,805,579
7
2015-11-19T13:40:43Z
[ "python", "mongodb", "hadoop", "apache-spark", "pymongo" ]
I'm having difficulty getting these components to knit together properly. I have Spark installed and working succesfully, I can run jobs locally, standalone, and also via YARN. I have followed the steps advised (to the best of my knowledge) [here](https://github.com/mongodb/mongo-hadoop/wiki/Spark-Usage) and [here](htt...
**Updates**: *2016-07-04* Since the last update [MongoDB Spark Connector](https://github.com/mongodb/mongo-spark) matured quite a lot. It provides [up-to-date binaries](https://search.maven.org/#search|ga|1|g%3Aorg.mongodb.spark) and data source based API but it is using `SparkConf` configuration so it is subjectivel...
Embed "Bokeh created html file" into Flask "template.html" file
33,398,950
2
2015-10-28T18:33:30Z
33,400,091
10
2015-10-28T19:37:37Z
[ "python", "flask", "jinja2", "bokeh" ]
I have a web application written in Python - Flask. When the user fill out some settings in one of the pages (POST Request), my controller calculates some functions and plot an output using Bokeh with following command and then I redirect to that HTML page created by Bokeh. ``` output_file("templates\\" + idx[j]['name...
You don't want to use `output_file` in this situation. Bokeh has a function specifically for embedding into HTML templates in web apps, `bokeh.embed.component`, demonstrated in the [quickstart](http://bokeh.pydata.org/en/latest/docs/user_guide/embed.html#components) and [tutorial](http://nbviewer.ipython.org/github/bok...
Custom double star operator for a class?
33,402,220
3
2015-10-28T21:49:49Z
33,402,305
7
2015-10-28T21:55:21Z
[ "python", "class", "python-3.x", "magic-methods" ]
How does one implement custom double star operator (`**`) for unpacking, similar to how `__iter__` works with single star operator (`*`)? For example: ``` class PlayerManager(object): def __init__(self, players=None): self.players = players or [] # Made up method to support ** operator def __dic...
Implement the [`Mapping` ABC](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping). Technically, the language docs don't specify which `Mapping` methods are used, so assuming you only need some subset used by the current implementation is a bad idea. [All it says is](https://docs.python.org/3...
Custom double star operator for a class?
33,402,220
3
2015-10-28T21:49:49Z
33,402,362
8
2015-10-28T22:00:15Z
[ "python", "class", "python-3.x", "magic-methods" ]
How does one implement custom double star operator (`**`) for unpacking, similar to how `__iter__` works with single star operator (`*`)? For example: ``` class PlayerManager(object): def __init__(self, players=None): self.players = players or [] # Made up method to support ** operator def __dic...
As @ShadowRanger says, implement Mapping. Here's an example: ``` from collections.abc import Mapping class Foo(Mapping): def __iter__(self): yield "a" yield "b" def __len__(self): return 2 def __getitem__(self, item): return ord(item) f = Foo() print(*f) print(dict(**f)...
python Ubuntu error install Pillow 3.0.0
33,404,394
38
2015-10-29T01:20:25Z
33,404,409
62
2015-10-29T01:22:21Z
[ "python", "ubuntu", "pillow" ]
I recently failed trying to install Pillow 3.0.0 on my Ubuntu 14.04. No matter what I do (download and try to "sudo python setup.py install" or "sudo -H pip install Pillow==3.0.0 --no-cache-dir") everytime I get error: ``` copying PIL/TiffImagePlugin.py -> build/lib.linux-x86_64-2.7/PIL running egg_info writ...
Did you install the dependencies for pillow ? You can install them by ``` $ sudo apt-get build-dep python-imaging $ sudo apt-get install libjpeg8 libjpeg62-dev libfreetype6 libfreetype6-dev ```
How can I await inside future-like object's __await__?
33,409,888
7
2015-10-29T09:06:06Z
33,420,721
8
2015-10-29T17:19:09Z
[ "python", "python-3.x", "python-asyncio" ]
[PEP 0492](https://www.python.org/dev/peps/pep-0492/#await-expression) adds new `__await__` magic method. Object that implements this method becomes *future-like object* and can be awaited using `await`. It's clear: ``` import asyncio class Waiting: def __await__(self): yield from asyncio.sleep(2) ...
Use direct `__await__()` call: ``` async def new_sleep(): await asyncio.sleep(2) class Waiting: def __await__(self): return new_sleep().__await__() ``` The solution was recommended by Yury Selivanov (the author of [PEP 492](https://www.python.org/dev/peps/pep-0492/)) for [aioodbc library](https://git...
How to get current date and time from GPS unsegment time in python
33,415,475
6
2015-10-29T13:23:52Z
33,426,779
8
2015-10-30T00:02:19Z
[ "python", "datetime", "gps" ]
I have gps unsegmented time like this: ``` Tgps = 1092121243.0 ``` And I'd like to understand what date and time is that. The begining of GPS time is 6 January 1980. Python function ``` datetime.utcfromtimestamp ``` could give seconds from 1 January 1970 year. I found following: ``` from datetime import datetime ...
GPS time started in sync with UTC: `1980-01-06 (UTC) == 1980-01-06 (GPS)`. Both tick in SI seconds. The difference between GPS time and UTC time increases with each (intercalary) leap second. To find the correct UTC time, you need to know the number of leap seconds occurred before the given GPS time: ``` #!/usr/bin/e...
Regular expression: matching and grouping a variable number of space separated words
33,416,263
3
2015-10-29T13:57:38Z
33,416,695
8
2015-10-29T14:17:04Z
[ "python", "regex" ]
I have a string: ``` "foo hello world baz 33" ``` The part between `foo` and `baz` will be some number of space separated words (one or more). I want to match this string with an re that will group out each of those words: ``` >>> re.match(r'foo (<some re here>) baz (\d+)', "foo hello world baz 33").groups() ('hell...
`re.match` returns result at the start of the string. Use `re.search` instead. `.*?` returns the shortest match between two words/expressions (. means anything, \* means 0 or more occurrences and ? means shortest match). ``` import re my_str = "foo hello world baz 33" my_pattern = r'foo\s(.*?)\sbaz' p = re.search(my...
Confusion with split function in Python
33,419,676
4
2015-10-29T16:26:37Z
33,419,811
7
2015-10-29T16:32:14Z
[ "python" ]
I am trying to alphabetically sort the words from a file. However, the program sorts the lines, not the words, according to their first words. Here it is. ``` fname = raw_input("Enter file name: ") fh = open(fname) lst = list() for line in fh: lst2 = line.strip() words = lst2.split() lst.append(words) ...
`lst.append(words)` append a list at the end of `lst`, it does not concatenates `lst` and `words`. You need to use `lst.extend(words)` or `lst += words`. Also, you should not sort the list at each iteration but only at the end of your loop: ``` lst = [] for line in fh: lst2 = line.strip() words = lst2.split()...
How does this variable declaration works in python?
33,421,149
3
2015-10-29T17:42:23Z
33,421,175
9
2015-10-29T17:43:37Z
[ "python", "python-2.7", "hex" ]
``` i = 0x0800 ``` What I understand here is that 0x0800 is a hexadecimal number where '0x' denotes the hex type and the following number '0800' is a 2 byte hexadecimal number. On assigning it to a variable 'i', when its type is checked I got this error ``` >>> type(i) Traceback (most recent call last): File "<stdin>...
`i` is an integer, but you *redefined `type`*: ``` >>> i = 0x0800 >>> i 2048 >>> type(i) <type 'int'> >>> type = 42 >>> type(i) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not callable >>> del type >>> type(i) <type 'int'> ``` Note the `type = 42` line; I create...
Installing numpy on Docker Alpine
33,421,965
4
2015-10-29T18:28:27Z
33,424,723
8
2015-10-29T21:14:11Z
[ "python", "numpy", "docker", "pip", "alpine" ]
I'm trying to install numpy in a docker container based on Alpine 3.1. I'm using the following Dockerfile: ``` FROM alpine:3.1 RUN apk add --update make cmake gcc g++ gfortran RUN apk add --update python py-pip python-dev RUN pip install cython RUN pip install numpy ``` This runs fine until `pip install numpy` when I...
If you don't necessary need to install `numpy` from `pypi`, you could install it from alpine repositories. Package is named `py-numpy` and is in `testing` repository, see [here](http://pkgs.alpinelinux.org/contents?pkgname=py-numpy&arch=x86). Minimal `Dockerfile` example that works for me ``` FROM alpine:3.2 ADD repos...
ImportError: No module named 'Queue'
33,432,426
4
2015-10-30T09:17:19Z
33,433,119
16
2015-10-30T09:55:00Z
[ "python", "python-requests" ]
I am trying to import `requests` module, but I got this error my python version is 3.4 running on ubuntu 14.04 ``` >>> import requests Traceback (most recent call last): File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 10, in <module> from queue import LifoQueue, Em...
Queue is in the multiprocessing module so: ``` from multiprocessing import Queue ```
Django ignores router when running tests?
33,434,318
19
2015-10-30T10:56:22Z
33,541,073
10
2015-11-05T09:39:48Z
[ "python", "django", "database" ]
I have a django application that uses 2 database connections: 1. To connect to the actual data the app is to produce 2. To a reference master data system, that is maintained completely outside my control The issue that I'm having, is that my webapp can absolutely NOT touch the data in the 2nd database. I solved most ...
I solved this by changing the `DATABASES.TEST` definition. I added the `TEST['MIRROR'] = 'default'` to the `mdm_db` database entry. ``` DATABASES = { 'default': { 'ENGINE': 'django.db.backends.oracle', 'NAME': '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=1521)))(CONNECT_DATA=(S...
Multiple if statements in a lambda function
33,439,434
4
2015-10-30T15:23:39Z
33,439,510
8
2015-10-30T15:27:11Z
[ "python", "lambda" ]
I am trying to use 3 if statements within a python lambda function. Here is my code: ``` y=lambda symbol: 'X' if symbol==True 'O' if symbol==False else ' ' ``` I Have been able to get two if statements to work just fine e.g. ``` x=lambda cake: "Yum" if cake=="chocolate" else "Yuck" ``` Essentially, I want a lambda ...
You can use an anonymous dict inside your anonymous function to test for this, using the default value of `dict.get` to symbolize your final "else" ``` y = lambda sym: {False: 'X', True: 'Y'}.get(sym, ' ') ```
Pandas dataframe read_csv on bad data
33,440,805
3
2015-10-30T16:36:17Z
33,440,853
7
2015-10-30T16:39:00Z
[ "python", "csv", "pandas" ]
I want to read in a very large csv (cannot be opened in excel and edited easily) but somewhere around the 100,000th row, there is a row with one extra column causing the program to crash. This row is errored so I need a way to ignore the fact that it was an extra column. There is around 50 columns so hardcoding the hea...
pass [`error_bad_lines=False`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html#pandas.read_csv) to skip erroneous rows: > error\_bad\_lines : boolean, default True Lines with too many fields > (e.g. a csv line with too many commas) will by default cause an > exception to be raised, and no Da...
How to check if Celery/Supervisor is running using Python
33,446,350
11
2015-10-30T23:34:31Z
33,545,849
11
2015-11-05T13:30:19Z
[ "python", "flask", "celery", "supervisor" ]
How to write a script in Python that outputs if celery is running on a machine (Ubuntu)? My use-case. I have a simple python file with some tasks. I'm not using Django or Flask. I use supervisor to run the task queue. For example, **tasks.py** ``` from celery import Celery, task app = Celery('tasks') @app.task() def...
You can run the `celery status` command via code by importing the `celery.bin.celery` package: ``` import celery import celery.bin.base import celery.bin.celery import celery.platforms app = celery.Celery('tasks', broker='redis://') status = celery.bin.celery.CeleryCommand.commands['status']() status.app = status.ge...
Iterating through array
33,446,708
2
2015-10-31T00:21:50Z
33,446,760
7
2015-10-31T00:28:21Z
[ "python", "arrays", "python-3.x", "swap" ]
I have an array of bools and now I want to swap those entries for numbers. ``` False => 0 True => 1 ``` I have written two different pieces of code and I would like to know, which one is better and why. This is not so much about actually solving the problem, as about learning. ``` arr = [[True,False],[False,True],[T...
Let's define your array: ``` >>> arr = [[True,False],[False,True],[True,True]] ``` Now, let's convert the booleans to integer: ``` >>> [[int(i) for i in row] for row in arr] [[1, 0], [0, 1], [1, 1]] ``` Alternatively, if we want to be more flexible about what gets substituted in, we can use a ternary statement: ``...
Why explicit test for an empty set does not work
33,452,797
8
2015-10-31T14:40:10Z
33,452,885
9
2015-10-31T14:49:39Z
[ "python", "python-3.x" ]
If empty `set()` is `False`, shouldn't the `if test == False` clause in the following code evaluate to `True`? ``` test = set() # empty sets are false if test == False: print("set is empty") else: print("set NOT empty") if not test: print("set is empty") ``` ouput: ``` set NOT empty set is empty ```
In simple terms, the equals operator `==` will perform an equality comparison between those two objects: A set and a boolean value will never be equal, so the result of the comparison is false. On the other hand, just checking `if obj` (or `if not obj`) will check the trueness of the object, something that can be evalu...
Symbol not found: _BIO_new_CMS
33,462,779
10
2015-11-01T13:43:18Z
34,132,165
15
2015-12-07T11:11:49Z
[ "python", "osx", "python-2.7", "scrapy", "osx-elcapitan" ]
I am new to mac and I don't understand why my scrapy doesn't seem to work any more. I suspect openssl is not valid in my el capitan. I tried: ``` pip install cryptography pip install pyOpenSSL brew install openssl ``` and I still get the error below. Is there some way I can fix this? ``` $ python Python 2.7.10 (v2....
I solve this problem by the following command: ``` LDFLAGS="-L/usr/local/opt/openssl/lib" pip install cryptography --no-use-wheel ``` Refer to [if homebrew openssl is linked, cryptography builds an unusable wheel](https://github.com/pyca/cryptography/issues/2138)
expected two blank lines pep8 warning in python
33,466,860
5
2015-11-01T20:30:11Z
33,467,206
11
2015-11-01T21:06:00Z
[ "python", "vim" ]
I'm using vim editor as python IDE, below is a simple python program to calculate square root of a number:- ``` import cmath def sqrt(): try: num = int(input("Enter the number : ")) if num >= 0: main(num) else: complex(num) except: print("OOPS..!!Somethin...
``` import cmath def sqrt(): try: num = int(input("Enter the number : ")) if num >= 0: main(num) else: complex_num(num) except: print("OOPS..!!Something went wrong, try again") sqrt() return def main(num): square_root = num**(1/2) p...
Python- Trying to multiply items in list
33,468,865
4
2015-11-02T00:20:24Z
33,468,909
7
2015-11-02T00:25:57Z
[ "python", "python-3.x", "python-3.4" ]
So basically what I'm trying to do here is ask a user for a random string input, for example: ``` asdf34fh2 ``` And I want to pull the numbers out of them into a list and get `[3,4,2]` but I keep getting `[34, 2]`. ``` import re def digit_product(): str1 = input("Enter a string with numbers: ") if...
Your regular expression, `\d+`, is the culprit here. The `+` means it matches one or more consecutive digits (`\d`): ``` asdf34fh2 ^- ^ \ \_ second match: one or more digits ("2") \____ first match: one or more digits ("34") ``` It looks like you only want to match one digit, so use `\d` without the `...
Python Numpy array conversion
33,478,933
3
2015-11-02T13:39:05Z
33,479,022
8
2015-11-02T13:43:22Z
[ "python", "numpy" ]
I've got 2 numpy arrays x1 and x2. Using python 3.4.3 ``` x1 = np.array([2,4,4]) x2 = np.array([3,5,3]) ``` I would like to a numpy array like this: ``` [[2,3],[4,5],[4,3]] ``` How would i go about this?
You can use [`numpy.column_stack`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.column_stack.html): ``` In [40]: x1 = np.array([2,4,4]) In [41]: x2 = np.array([3,5,3]) In [42]: np.column_stack((x1, x2)) Out[42]: array([[2, 3], [4, 5], [4, 3]]) ```
Can you use C++ DLLs in C# code in a UWP?
33,489,924
2
2015-11-03T01:23:08Z
33,490,707
7
2015-11-03T03:04:44Z
[ "c#", "python", "c++", "dll", "uwp" ]
I wrote a C++ Class Library in Visual Studio that just defines a function that invokes some Python: ``` #pragma once #include <Python.h> extern "C" __declspec(dllexport) void python() { Py_Initialize(); PyRun_SimpleString("2 + 2"); } ``` I made another project in the same solution that was a C# Blank Univer...
Firstly, UWP can't consume a legacy C++ dll just by DLLImport. If you want to expose legacy c++ functions to C#, the first suggestion is to wrap that C++ logic using a WinRT component. Then you can reference this component in UWP application by following steps: adding it to the project, open the files' properties in t...
sklearn dumping model using joblib, dumps multiple files. Which one is the correct model?
33,497,314
4
2015-11-03T10:54:41Z
33,500,427
7
2015-11-03T13:30:31Z
[ "python", "machine-learning", "scikit-learn", "joblib" ]
I did a sample program to train a SVM using sklearn. Here is the code ``` from sklearn import svm from sklearn import datasets from sklearn.externals import joblib clf = svm.SVC() iris = datasets.load_iris() X, y = iris.data, iris.target clf.fit(X, y) print(clf.predict(X)) joblib.dump(clf, 'clf.pkl') ``` When I dum...
To save everything into 1 file you should set compression to True or any number (1 for example). But you should know that separated representation of np arrays is necessary for main features of joblib dump/load, joblib can load and save objects with np arrays faster than Pickle due to this separated representation, an...
Strange inline assignment
33,500,567
5
2015-11-03T13:37:45Z
33,500,638
10
2015-11-03T13:41:30Z
[ "python", "tuples", "variable-assignment" ]
I'm struggling with this strange behaviour in Python (2 and 3): ``` >>> a = [1, 2] >>> a[a.index(1)], a[a.index(2)] = 2, 1 ``` This results in: ``` >>> a [1, 2] ``` But if you write ``` >>> a = [1, 2] >>> a[a.index(1)], a[a.index(2)] = x, y ``` where `x, y != 2, 1` (can be `1, 1`, `2, 2` , `3, 5`, etc.), this res...
Because it actually gets interpreted like this: ``` >>> a = [1, 2] >>> a [1, 2] >>> a[a.index(1)] = 2 >>> a [2, 2] >>> a[a.index(2)] = 1 >>> a [1, 2] ``` To quote, per the [standard rules for assignment](https://docs.python.org/2/reference/simple_stmts.html#assignment-statements) (emphasis mine): > * If the target l...
Create and import helper functions in tests without creating packages in test directory using py.test
33,508,060
11
2015-11-03T20:04:09Z
33,515,264
7
2015-11-04T06:41:34Z
[ "python", "unit-testing", "py.test" ]
**Question** How can I import helper functions in test files without creating packages in the `test` directory? **Context** I'd like to create a test helper function that I can import in several tests. Say, something like this: ``` # In common_file.py def assert_a_general_property_between(x, y): # test a speci...
my option is to create an extra dir in `tests` dir and add it to pythonpath in the conftest so. ``` tests/ helpers/ utils.py ... conftest.py setup.cfg ``` in the `conftest.py` ``` import sys import os sys.path.append(os.path.join(os.path.dirname(__file__), 'helpers') ``` in `setup.cfg` ``` [pyt...
Create block diagonal numpy array from a given numpy array
33,508,322
7
2015-11-03T20:20:58Z
33,508,367
8
2015-11-03T20:23:14Z
[ "python", "arrays", "numpy" ]
I have a 2-dimensional numpy array with an equal number of columns and rows. I would like to arrange them into a bigger array having the smaller ones on the diagonal. It should be possible to specify how often the starting matrix should be on the diagonal. For example: ``` a = numpy.array([[5, 7], [6...
Classic case of [`numpy.kron`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.kron.html) - ``` np.kron(np.eye(n), a) ``` Sample run - ``` In [57]: n = 2 In [58]: np.kron(np.eye(n), a) Out[58]: array([[ 5., 7., 0., 0.], [ 6., 3., 0., 0.], [ 0., 0., 5., 7.], [ 0., 0., 6., ...
Read cell content in an ipython notebook
33,508,377
18
2015-11-03T20:23:48Z
33,645,726
7
2015-11-11T07:08:31Z
[ "python", "ipython", "ipython-notebook" ]
I have an `ipython` notebook with mixed `markdown` and `python` cells. And I'd like some of my `python` cells to read the adjacent `markdown` cells and process them as input. **An example of the desired situation:** > *CELL 1 (markdown)*: SQL Code to execute > > *CELL 2 (markdown)*: `select * from tbl where x=1` > >...
I think you are trying to attack the problem the wrong way. First yes, it is possible to get the adjacent markdown cell in really hackish way that would not work in headless notebook execution. What you want to do is use IPython cell magics, that allow arbitrary syntax as long as the cell starts with 2 percent signs ...
Finding majority votes on -1s, 1s and 0s in list - python
33,511,259
9
2015-11-03T23:43:58Z
33,511,352
8
2015-11-03T23:53:28Z
[ "python", "list", "if-statement", "binary", "voting" ]
**How to find the majority votes for a list that can contain -1s, 1s and 0s?** For example, given a list of: ``` x = [-1, -1, -1, -1, 0] ``` The majority is -1 , so the output should return `-1` Another example, given a list of: ``` x = [1, 1, 1, 0, 0, -1] ``` The majority vote would be `1` And when we have a ti...
I am assuming that votes for 0 count as votes. So `sum` is not a reasonable option. Try a Counter: ``` >>> from collections import Counter >>> x = Counter([-1,-1,-1, 1,1,1,1,0,0,0,0,0,0,0,0]) >>> x Counter({0: 8, 1: 4, -1: 3}) >>> x.most_common(1) [(0, 8)] >>> x.most_common(1)[0][0] 0 ``` So you could write code lik...
when installing pyaudio, pip cannot find portaudio.h in /usr/local/include
33,513,522
10
2015-11-04T04:10:11Z
33,821,084
24
2015-11-20T07:23:43Z
[ "python", "osx", "pyaudio" ]
I'm using mac osx 10.10 As the PyAudio Homepage said, I install the PyAudio using ``` brew install portaudio pip install pyaudio ``` the installation of portaudio seems successful, I can find headers and libs in /usr/local/include and /usr/local/lib but when I try to install pyaudio, it gives me an error that ``` s...
My workaround on Mac 10.11.1 was: `$ pip install --global-option='build_ext' --global-option='-I/usr/local/include' --global-option='-L/usr/local/lib' pyaudio`
Column alias after groupBy in pyspark
33,516,490
5
2015-11-04T07:56:22Z
33,517,194
7
2015-11-04T08:39:56Z
[ "python", "scala", "apache-spark", "pyspark" ]
I need the resulting data frame in the line below, to have an alias name "maxDiff" for the max('diff') column after groupBy. However, the below line does not makeany change, nor throw an error. ``` grpdf = joined_df.groupBy(temp1.datestamp).max('diff').alias("maxDiff") ```
This is because you are aliasing the whole `DataFrame` object, not `Column`. Here's an example how to alias the `Column` only: ``` import pyspark.sql.functions as func grpdf = joined_df \ .groupBy(temp1.datestamp) \ .max('diff') \ .select(func.col("max(diff)").alias("maxDiff")) ```
Trying to count words in a file using Python
33,529,334
4
2015-11-04T18:28:12Z
33,529,347
7
2015-11-04T18:29:10Z
[ "python", "file" ]
I am attempting to count the number of 'difficult words' in a file, which requires me to count the number of letters in each word. For now, I am only trying to get single words, one at a time, from a file. I've written the following: ``` file = open('infile.txt', 'r+') fileinput = file.read() for line in fileinput: ...
Use [splitlines()](https://docs.python.org/2/library/stdtypes.html#str.splitlines): ``` fopen = open('infile.txt', 'r+') fileinput = fopen.read() for line in fileinput.splitlines(): for word in line.split(): print(word) fopen.close() ``` Without [splitlines()](https://docs.python.org/2/library/stdtypes....
How do I specify that the return type of a method is the same as the class itself in python?
33,533,148
10
2015-11-04T22:17:54Z
33,533,514
7
2015-11-04T22:44:33Z
[ "python", "python-3.x", "pycharm", "typing", "python-3.5" ]
I have the following code in python 3: ``` class Position: def __init__(self, x: int, y: int): self.x = x self.y = y def __add__(self, other: Position) -> Position: return Position(self.x + other.x, self.y + other.y) ``` But my editor (PyCharm) says that the reference Position can no...
If you try to run this code you will get: ``` NameError: name 'Position' is not defined ``` This is because `Position` must be defined before you can use it on an annotation. I don't like any of the workarounds suggested for similar questions. # 1. Define a dummy `Position` Before the class definition, place a dumm...
What would be a good example of "sending messages to objects" within Python?
33,534,264
6
2015-11-04T23:47:40Z
33,534,723
7
2015-11-05T00:37:41Z
[ "python", "ruby", "oop", "smalltalk" ]
I recently watched [*Nothing is Something*](https://www.youtube.com/watch?v=9mLK_8hKii8) by Sandi Metz, and in her talk she uses the idea of **sending messages to objects** and goes over how that's done in Ruby. The [4:10](https://www.youtube.com/watch?v=9mLK_8hKii8&t=250)-7:30 section would be a good entry point where...
Python uses a slightly different terminology. It is called "calling a method". But it's the same thing. (C++ calls it "calling a virtual function". Again, same difference.) Personally, I don't like that terminology, it focuses too much on the implementation detail and loses much of the metaphoric power of the "message...
What would be a good example of "sending messages to objects" within Python?
33,534,264
6
2015-11-04T23:47:40Z
33,534,789
7
2015-11-05T00:44:19Z
[ "python", "ruby", "oop", "smalltalk" ]
I recently watched [*Nothing is Something*](https://www.youtube.com/watch?v=9mLK_8hKii8) by Sandi Metz, and in her talk she uses the idea of **sending messages to objects** and goes over how that's done in Ruby. The [4:10](https://www.youtube.com/watch?v=9mLK_8hKii8&t=250)-7:30 section would be a good entry point where...
Essentially "sending a message" is called in most of OO languages as calling a method. The key difference is that in dynamic languages you don't know if the object knows a method or not. So if you do `var.jump()` you have no idea what the `var` is. Maybe it's a rabbit and it can jump, or maybe it is a rock and it doesn...
Best way to do frequency counting in Julia
33,534,791
3
2015-11-05T00:44:28Z
33,541,781
7
2015-11-05T10:10:28Z
[ "python", "binary", "statistics", "counter", "julia-lang" ]
I have a [binary file](https://www.dropbox.com/s/kn8i3us95tgqlq4/160919?dl=0) and I am doing frequency counting in julia. ``` using PyPlot import StatsBase const stb=StatsBase function getall(fname) b=Mmap.mmap(fname,Vector{Int32}) #a=open(fname) #b=reinterpret(Int32,readbytes(a)) d=stb.countmap(b) ...
The `countmap` operation is a standard operation in any programming language. Additionally, it is also "raw", like sorting, which means it has to do a basic popular operation over the input data. Operations of this kind are hard to optimize, as they are done similarly in most languages - and if they are not fast enough...
OS X - Deciding between anaconda and homebrew Python environments
33,541,876
4
2015-11-05T10:14:37Z
33,543,303
8
2015-11-05T11:22:27Z
[ "python", "osx", "numpy", "homebrew", "anaconda" ]
I use Python extensively on my Mac OS X, for both numerical applications and web development (roughly equally). I checked the number of Python installations I had on my laptop recently, and was shocked to find **four**: ``` Came with Mac OS X: /usr/bin/python Python 2.7.6 (default, Sep 9 2014, 15:04:36) [GCC 4.2.1 Co...
I use Homebrew Python for all my projects (data science, some web dev). Conda is nothing fancy, you can have the same packages by hand with a combination of `pip` and [Homebrew science](https://github.com/Homebrew/homebrew-science). Actually, it is even better because you have more control on what you install. You ca...
What does the built-in function sum do with sum(list, [])?
33,541,947
13
2015-11-05T10:17:27Z
33,542,054
17
2015-11-05T10:22:26Z
[ "python" ]
When I want to unfold a list, I found a way like below: ``` >>> a = [[1, 2], [3, 4], [5, 6]] >>> a [[1, 2], [3, 4], [5, 6]] >>> sum(a, []) [1, 2, 3, 4, 5, 6] ``` I don't know what happened in these lines, and [the documentation](https://docs.python.org/2/library/functions.html#sum) states: > `sum(iterable[, start])`...
> Don't you think that start should be a number? `start` *is* a number, by default; `0`, per the documentation you've quoted. Hence when you do e.g.: ``` sum((1, 2)) ``` it is evaluated as `0 + 1 + 2` and it equals `3` and everyone's happy. If you want to start from a different number, you can supply that instead: ...
Python intersection of 2 lists of dictionaries
33,542,997
2
2015-11-05T11:06:59Z
33,543,164
7
2015-11-05T11:15:08Z
[ "python", "python-3.4" ]
I have 2 lists of dicts like ``` list1 = [{'count': 351, 'evt_datetime': datetime.datetime(2015, 10, 23, 8, 45), 'att_value': 'red'}, {'count': 332, 'evt_datetime': datetime.datetime(2015, 10, 23, 8, 45), 'att_value': 'red'}, {'count': 336, 'evt_datetime': datetime.datetime(2015, 10, 23, 8, 45), 'att_value':...
Use list comprehension: ``` [x for x in list1 if x in list2] ``` This returns me this list for your data: ``` [{'count': 351, 'evt_datetime': datetime.datetime(2015, 10, 23, 8, 45), 'att_value': 'red'}, {'count': 359, 'evt_datetime': datetime.datetime(2015, 10, 23, 8, 45), 'att_value': 'red'}] ```
Color seaborn boxplot based in DataFrame column name
33,544,910
6
2015-11-05T12:45:16Z
33,548,337
7
2015-11-05T15:19:17Z
[ "python", "python-3.x", "pandas", "matplotlib", "seaborn" ]
I'd like to create a list of boxplots with the color of the box dependent on the name of the pandas.DataFrame column I use as input. The column names contain strings that indicate an experimental condition based on which I want the box of the boxplot colored. I do this to make the boxplots: ``` sns.boxplot(data = da...
You should use the `palette` parameter, which handles multiple colors, rather than `color`, which handles a specific one. You can give `palette` a name, an ordered list, or a dictionary. The latter seems best suited to your question: ``` import seaborn as sns sns.set_color_codes() tips = sns.load_dataset("tips") pal =...
how to generate url from boto3 in amazon web services
33,549,254
4
2015-11-05T15:59:36Z
33,698,531
9
2015-11-13T17:28:42Z
[ "python", "amazon-s3", "boto3" ]
I have a Bucket in s3 and I am trying to pull the url of the image that is in there. I am using boto3 and boto3 doesn't seem to have an implemented generate url method. They have a core method, that generates url like this, ``` import botocore.session session = botocore.session.get_session() client = session.create...
Able to get results and did not face any issues in getting the signed URL. I used the default session since my aws creds were stored locally in "~/.aws/credentials" file and my default region is set as needed ~/.aws/config ``` import boto3 s3Client = boto3.client('s3') s3Client.generate_presigned_url('get_object', Par...
list comprehension with numpy arrays - bad practice?
33,551,962
6
2015-11-05T18:18:59Z
33,552,005
8
2015-11-05T18:20:55Z
[ "python", "numpy" ]
I am wondering if the below approach would be considered bad practice, and if so, if someone could give some guidance towards another approach. Here is the code in question: ``` a = np.array([[1,2,3],[4,5,6]]) b = np.array([-5,5]) c = np.array([np.multiply(a[x],b[x]) for x in range(2)]) ``` The objective here is to ...
*NumPythonic* way would be to extend the dimensions of `b` to a 2D array with [`np.newaxis/None`](http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#numpy.newaxis) and then let [`broadcasting`](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) come into play for a `vectorized elementwise multip...
Do I have Numpy 32 bit or 64 bit?
33,553,549
5
2015-11-05T19:51:21Z
33,553,718
12
2015-11-05T20:02:06Z
[ "python", "numpy", "memory", "32bit-64bit" ]
How do I check if my installed numpy version is 32bit or 64bit? Bonus Points for a solution which works inside a script and is system independent.
``` In [65]: import numpy.distutils.system_info as sysinfo In [69]: sysinfo.platform_bits Out[69]: 64 ``` This is based on [the value returned by `platform.architecture()`](https://github.com/numpy/numpy/blob/master/numpy/distutils/system_info.py#L153): ``` In [71]: import platform In [72]: platform.architecture() O...
The similar method from the nltk module produces different results on different machines. Why?
33,558,709
14
2015-11-06T02:57:44Z
33,809,495
13
2015-11-19T16:35:48Z
[ "python", "nlp", "nltk", "similarity", "corpus" ]
I have taught a few introductory classes to text mining with Python, and the class tried the similar method with the provided practice texts. Some students got different results for text1.similar() than others. All versions and etc. were the same. Does anyone know why these differences would occur? Thanks. Code used...
In your example there are 40 other words which have **exactly one** context in common with the word `'monstrous'`. In the [`similar`](http://www.nltk.org/_modules/nltk/text.html#Text.similar) function a `Counter` object is used to count the words with similar contexts and then the most common ones (default 20) are prin...
upgrade to dev version of scikit-learn on Anaconda?
33,568,244
9
2015-11-06T13:44:52Z
33,568,537
14
2015-11-06T14:01:46Z
[ "python", "scikit-learn", "upgrade", "anaconda" ]
I'm using python through Anaconda, and would like to use a new feature (<http://scikit-learn.org/dev/modules/neural_networks_supervised.html>) in scikit-learn that's currently only available in the development version 0.18.dev0. However, doing the classical `conda update` doesn't seem to work, as conda doesn't list an...
You can only use `conda` to install a package if someone has built and made available binaries for the package. Some packages publish nightly builds that would allow this, but scikit-learn is not one of them. To install the bleeding-edge version in one command, you could use pip; e.g.: ``` $ conda install pip $ pip i...
numpy max vs amax vs maximum
33,569,668
4
2015-11-06T15:02:46Z
33,569,857
7
2015-11-06T15:13:06Z
[ "python", "math", "numpy" ]
numpy has three different functions which seem like they can be used for the same things --- except that `numpy.maximum` can *only* be used element-wise, while `numpy.max` and `numpy.amax` can be used on particular axes, or all elements. Why is there more than just `numpy.max`? Is there some subtlety to this in perform...
`np.max` and `np.maximum` are not generally used for the same operations. `np.max` (which is just an alias for `np.amax`) only works on one input array. It can compute the maximum element from that entire array (returning a scalar), or if supplied an `axis` argument, it can compute the maximum value along an axis (ret...
TypeError: __init__() got an unexpected keyword argument 'type' in argparse
33,574,270
2
2015-11-06T19:29:34Z
33,574,480
7
2015-11-06T19:43:30Z
[ "python", "parsing", "argparse" ]
Hey so I'm using argparse to try and generate a quarterly report. This is what the code looks like: ``` parser = argparse.ArgumentParser() parser.add_argument('-q', "--quarter", action='store_true', type=int, help="Enter a Quarter number: 1,2,3, or 4 ") parser.add_argument('-y', "--year", action='store_true',type=s...
What `action="store_true"` means is that if the argument is given on the command line then a `True` value should be stored in the parser. What you actually want is to store the given year (as a string) and quarter (as an int). ``` parser = argparse.ArgumentParser() parser.add_argument('-q', "--quarter", type=int, he...
Is it possible to copy and update a python dictionary in a single chained expression?
33,576,444
4
2015-11-06T22:03:26Z
33,576,498
8
2015-11-06T22:09:19Z
[ "python", "dictionary" ]
Consider the following: ``` x = {1:2} y = x.copy() # returns a new dictionary y = x.copy().update({2:3}) # returns None y = x.copy()[2] = 3 # invalid syntax ``` Given that none of the above work, is there a way to chain a command to `Dict.copy()` to copy and update a dictionary in a single command?
Yes, you can use the [`dict()` function](https://docs.python.org/2/library/stdtypes.html#dict) to create a copy and add keyword arguments; use `**{...}` to add arbitrary keys that are not Python identifiers: ``` y = dict(x, **{2: 3}) ``` For string keys that happen to be valid Python identifiers (start with a letter,...
How do I multiply more the one variable by a number simultaneously
33,582,485
3
2015-11-07T11:56:38Z
33,582,520
8
2015-11-07T12:00:06Z
[ "python", "list", "variables" ]
I have three variables, I would like to know how to multiply all of these variables by another variable `number` simultaneously. For example ``` number = 2 var1 = 0 var2 = 1 var3 = 2 ``` The output should be: ``` 0 2 4 ```
Use a [list comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) ``` >>> number = 2 >>> >>> var1 = 0 >>> var2 = 1 >>> var3 = 2 >>> >>> [i*number for i in (var1,var2,var3)] [0, 2, 4] ``` And to print it ``` >>> for i in output: ... print(i) ... 0 2 4 ``` You can use `map`...
Distribution of Number of Digits of Random Numbers
33,582,576
15
2015-11-07T12:06:56Z
33,582,842
8
2015-11-07T12:36:48Z
[ "javascript", "python", "random" ]
I encounter this curious phenomenon trying to implement a UUID generator in JavaScript. Basically, in JavaScript, if I generate a large list of random numbers with the built-in `Math.random()` on Node `4.2.2`: ``` var records = {}; var l; for (var i=0; i < 1e6; i += 1) { l = String(Math.random()).length; if (reco...
The reason is indeed related to floating point representation. A floating point number representation has a maximum number of (binary) digits it can represent, and a limited exponent value range. Now when you print this out without using scientific notation, you might in some cases need to have some zeroes after the de...
Tesseract OCR on AWS Lambda via virtualenv
33,588,262
7
2015-11-07T21:57:18Z
35,724,894
10
2016-03-01T13:58:02Z
[ "python", "amazon-web-services", "virtualenv", "tesseract", "aws-lambda" ]
I have spent all week attempting this, so this is a bit of a hail mary. I am attempting to package up Tesseract OCR into AWS Lambda running on Python (I am also using PILLOW for image pre-processing, hence the choice of Python). I understand how to deploy Python packages onto AWS using virtualenv, however I cannot se...
The reason it's not working is because these python packages are only wrappers to tesseract. You have to compile tesseract using a AWS Linux instance and copy the binaries and libraries to the zip file of the lambda function. **1) Start an EC2 instance with 64-bit Amazon Linux;** **2) Install dependencies:** ``` sud...
Python: Variable not defined?
33,595,799
2
2015-11-08T15:50:21Z
33,595,881
8
2015-11-08T15:58:45Z
[ "python" ]
So, I have this stuff below ``` def userinput(): adjective1 = input("Adjective: ") noun1 = input("Noun: ") noun2 = input("Noun: ") def story(): print("A vacation is when you take a trip to some " + adjective1 + " place.") print("Usually you go to some place that is near " + noun1 + " or up on " + ...
Its all about scope, you can not acces variable within another function scope Try this: ``` def userinput(): adjective1 = input("Adjective: ") noun1 = input("Noun: ") noun2 = input("Noun: ") return adjective1, noun1, noun2 def story(): adjective1, noun1, noun2 = userinput() print("A vacation i...
Reversing default dict
33,599,347
3
2015-11-08T21:41:50Z
33,599,446
7
2015-11-08T21:52:55Z
[ "python", "dictionary" ]
I have a defaultdict(list) where my keys are of type tuple and my values are a list of tuples. I want to reverse this dictionary and I have already tried using the zip function and this doesn't work A sample of my dictionary structure is below ``` {(2, '-', 3): [('Canada', 'Trudeau'),('Obama', 'USA')]} ``` Is there ...
Since you have a dict of lists, you need to make the lists an immutable type to use as a key in a Python dict. You can make the list values tuples: ``` >>> di={(2, '-', 3): [('Canada', 'Trudeau'),('Obama', 'USA')]} >>> {tuple(v):k for k, v in di.items()} {(('Canada', 'Trudeau'), ('Obama', 'USA')): (2, '-', 3)} ``` O...
Installing scipy in Python 3.5 on 32-bit Windows 7 Machine
33,600,302
4
2015-11-08T23:30:30Z
33,601,348
9
2015-11-09T01:54:02Z
[ "python", "python-3.x", "numpy", "scipy" ]
I have been trying to install Scipy onto my Python 3.5 (32-bit) install on my Windows 7 machine using the pre-built binaries from: <http://www.lfd.uci.edu/~gohlke/pythonlibs> I have, in order, installed the following libraries ``` numpy‑1.10.1+mkl‑cp35‑none‑win32.whl scipy‑0.16.1‑cp35‑none‑win32.whl `...
Make sure you pay attention to this line from the link you provided: > Many binaries depend on NumPy-1.9+MKL and the Microsoft Visual C++ > 2008 (x64, x86, and SP1 for CPython 2.6 and 2.7), Visual C++ 2010 > (x64, x86, for CPython 3.3 and 3.4), or the Visual C++ 2015 (x64 and > x86 for CPython 3.5) redistributable pac...
Random number in the range 1 to sys.maxsize is always 1 mod 2^10
33,602,014
42
2015-11-09T03:19:21Z
33,602,119
10
2015-11-09T03:31:32Z
[ "python", "python-2.7", "random" ]
I am trying to find the statistical properties of the PRNGs available in Python (2.7.10) by using the frequency test, runs test and the chi squared test. For carrying out the frequency test, I need to convert the generated random number to its binary representation and then count the distribution of `1`'s and `0`'s. I...
That depends on a lot of things, like how exactly the RNG is implemented, how much bits of state it uses, and how exactly the `sample` function is implemented. Here's what the documentation says: > Almost all module functions depend on the basic function random(), which generates a random float uniformly in the semi-...
Random number in the range 1 to sys.maxsize is always 1 mod 2^10
33,602,014
42
2015-11-09T03:19:21Z
33,602,186
47
2015-11-09T03:40:04Z
[ "python", "python-2.7", "random" ]
I am trying to find the statistical properties of the PRNGs available in Python (2.7.10) by using the frequency test, runs test and the chi squared test. For carrying out the frequency test, I need to convert the generated random number to its binary representation and then count the distribution of `1`'s and `0`'s. I...
@roeland hinted at the cause: in Python 2, `sample()` uses `int(random.random() * n)` repeatedly. Look at the source code (in your Python's `Lib/random.py`) for full details. In short, `random.random()` returns no more than 53 significant (non-zero) leading bits; then `int()` fills the rest of the low-order bits with z...
Quickstart Flask application failing for some reason
33,602,708
3
2015-11-09T04:46:51Z
33,603,641
8
2015-11-09T06:36:09Z
[ "python", "flask", "pip" ]
I created a fresh `virtualenv` environment for a Flask application called `flask-test` so that I could do some testing. Imagine my surprise when, running the quickstart application, I get the following error: ``` Honorss-MacBook-Air-2:Desktop Honors$ cd flask-testing Honorss-MacBook-Air-2:flask-testing Honors$ source ...
Seems like a bug: the related issue [Werkzeug 0.11 with Flask 0.10.1 and 'app.debug = True' won't start. #798](https://github.com/mitsuhiko/werkzeug/issues/798) I create a new virtual environment using python3.5 and meet the same error, but if I don't use the debug mode, it's fine. And as the issue says, downgrade We...
In TensorFlow, what is the difference between Session.run() and Tensor.eval()?
33,610,685
39
2015-11-09T13:52:52Z
33,610,914
61
2015-11-09T14:05:45Z
[ "python", "tensorflow" ]
TensorFlow has two ways to evaluate part of graph: `Session.run` on a list of variables and `Tensor.eval`. Is there a difference between these two?
If you have a `Tensor` t, calling [`t.eval()`](http://tensorflow.org/api_docs/python/framework.md#Tensor.eval) is equivalent to calling `tf.get_default_session().run(t)`. You can make a session the default as follows: ``` t = tf.constant(42.0) sess = tf.Session() with sess.as_default(): # or `with sess:` to close o...
In TensorFlow, what is the difference between Session.run() and Tensor.eval()?
33,610,685
39
2015-11-09T13:52:52Z
33,666,260
8
2015-11-12T07:28:42Z
[ "python", "tensorflow" ]
TensorFlow has two ways to evaluate part of graph: `Session.run` on a list of variables and `Tensor.eval`. Is there a difference between these two?
The FAQ session on tensor flow has an [answer to exactly the same question](http://tensorflow.org/resources/faq.md#contents). I would just go ahead and leave it here: --- If `t` is a `Tensor` object, `t.eval()` is shorthand for `sess.run(t)` (where `sess` is the current default session. The two following snippets of ...
How to deal with inputs outside 0-1 range in tensorflow?
33,613,945
2
2015-11-09T16:43:21Z
33,615,369
8
2015-11-09T18:05:43Z
[ "python", "tensorflow" ]
In the example provided at <http://www.tensorflow.org/get_started> if I multiply the input by 2 ``` x_data = np.float32(np.random.rand(2, 100))*2 ``` I get non-sense output, while I expected to get the same solution. ``` 0 [[ -67.06586456 -109.13352203]] [-7.67297792] 20 [[ nan nan]] [ nan] 40 [[ nan nan]] [ nan] ...
The issue is that the example uses a very aggressive learning rate: ``` optimizer = tf.train.GradientDescentOptimizer(0.5) ``` This makes learning faster, but stops working if you change the problem a bit. A learning rate of `0.01` would be more typical: ``` optimizer = tf.train.GradientDescentOptimizer(0.01) ``` N...
tensorflow -- is it or will it (sometime soon) be compatible with a windows workflow?
33,616,094
50
2015-11-09T18:51:41Z
33,618,580
50
2015-11-09T21:24:48Z
[ "python", "windows", "tensorflow" ]
I haven't seen anything about Windows compatibility--is this on the way or currently available somwhere if I put forth some effort? (I have a mac and an ubuntu box but the windows machine is the one with the discrete graphics card that I currently use with theano)
We haven't tried to build TensorFlow on Windows so far: the only supported platforms are Linux (Ubuntu) and Mac OS X, and we've only built binaries for those platforms. For now, on Windows, the easiest way to get started with TensorFlow would be to use Docker: <http://tensorflow.org/get_started/os_setup.md#docker-base...
tensorflow -- is it or will it (sometime soon) be compatible with a windows workflow?
33,616,094
50
2015-11-09T18:51:41Z
33,635,663
10
2015-11-10T17:10:32Z
[ "python", "windows", "tensorflow" ]
I haven't seen anything about Windows compatibility--is this on the way or currently available somwhere if I put forth some effort? (I have a mac and an ubuntu box but the windows machine is the one with the discrete graphics card that I currently use with theano)
As @mrry suggested, it is easier to set up TensorFlow with Docker. Here's how I managed to set it up as well as getting iPython Notebook up and running in my Docker environment (I find it really convenient to use iPython Notebook for all testing purposes as well as documenting my experiments). I assume that you have i...
Change clickable field in Django admin list_display
33,616,330
4
2015-11-09T19:06:14Z
33,616,452
11
2015-11-09T19:14:09Z
[ "python", "django", "django-models", "django-forms", "django-admin" ]
In Django 1.8.6, by default, whenever I provide a `list_display` option to a ModelAdmin subclass, the first field in the list becomes clickable and leads to the object edit page. Is there a way to keep the order of the fields in `list_display`, but change the clickable one? Currently, I have the `id` field clickable ...
You could have a look at [django.contrib.admin.ModelAdmin.list\_display\_links](https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display_links) Basically it is used like ``` class PersonAdmin(admin.ModelAdmin): list_display = ('first_name', 'last_name', 'birthday') ...
How do I use distributed DNN training in TensorFlow?
33,616,593
11
2015-11-09T19:23:22Z
33,641,967
13
2015-11-11T00:00:48Z
[ "python", "parallel-processing", "deep-learning", "tensorflow" ]
Google released TensorFlow today. I have been poking around in the code, and I don't see anything in the code or API about training across a cluster of GPU servers. Does it have distributed training functionality yet?
**Updated:** The initial release of [Distributed TensorFlow](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/distributed_runtime#distributed-tensorflow) occurred on 2/26/2016. The release was announced by coauthor Derek Murray in the original issue [here](https://github.com/tensorflow/tensorflow/is...
How do I use distributed DNN training in TensorFlow?
33,616,593
11
2015-11-09T19:23:22Z
35,653,639
8
2016-02-26T13:58:53Z
[ "python", "parallel-processing", "deep-learning", "tensorflow" ]
Google released TensorFlow today. I have been poking around in the code, and I don't see anything in the code or API about training across a cluster of GPU servers. Does it have distributed training functionality yet?
It took us a few months, but today marks the release of the initial [distributed TensorFlow runtime](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/distributed_runtime). This includes support for multiple machines, each with multiple GPUs, with communication provided by [gRPC](http://grpc.io). Th...
Where is the folder for Installing tensorflow with pip, Mac OSX?
33,616,732
14
2015-11-09T19:32:04Z
33,616,733
30
2015-11-09T19:32:04Z
[ "python", "osx", "tensorflow" ]
just installed tensorflow using pip with the command: `$ pip install tensorflow` On the ["Getting Started" for Tensorflow](http://tensorflow.org/get_started/os_setup.md) they have an example for convolutional neural networks `$ python tensorflow/models/image/mnist/convolutional.py` Where is that directory located w...
Installing with pip, installs the packages to the directory "site-packages". The following code shows the location of tensorflow as well as where pip installs the packages: ``` $ pip show tensorflow ``` Which return: ``` Metadata-Version: 2.0 Name: tensorflow Version: 0.5.0 Summary: TensorFlow helps the tensors flo...
For a pandas Series, shouldn't s.sort_index(inplace=True) change s?
33,617,439
4
2015-11-09T20:14:44Z
33,617,554
9
2015-11-09T20:21:18Z
[ "python", "pandas" ]
Given this code: ``` s = pd.Series([1,2,3], index=['C','B','A']) s.sort_index(inplace=True) ``` Shouldn't `s` now look like this: ``` A 3 B 2 C 1 dtype: int64 ``` When I run this, `s` remains unchanged. Maybe I'm confused about what the `inplace` argument is supposed to do. I thought that it was supposed t...
You are indeed correct with your expectation. However, this was not yet implemented before 0.17 / a bug in 0.17 that this keyword was ignored instead of raising an error (like before). But a fix will be released in the upcoming version 0.17.1. See <https://github.com/pydata/pandas/pull/11422> So for now, easiest is j...
"No such file or directory" when starting convolutional.py script on tensorflow docker image
33,621,547
3
2015-11-10T01:52:11Z
33,622,640
9
2015-11-10T03:59:32Z
[ "python", "docker", "anaconda", "tensorflow" ]
I don't have a Linux or Mac machine so in order to check out TensorFlow on Windows, installed docker and downloaded the image of tensorflow-full. When I run the following command: ``` $ python tensorflow/models/image/mnist/convolutional.py ``` I get this error message: ``` C:\Users\Javiar\Anaconda\python.exe: can't ...
It looks like the error message is caused by trying to execute a script file (`.../convolutional.py`) that is inside the container, using the Python interpreter outside the container. First of all, follow the steps here to ensure that Docker is configured successfully on your Windows machine: <http://docs.docker.com/...
Fail to run word embedding example in tensorflow tutorial with GPUs
33,624,048
11
2015-11-10T06:17:43Z
33,624,832
13
2015-11-10T07:28:33Z
[ "python", "tensorflow" ]
I am trying to run the word embedding example code at <https://github.com/tensorflow/tensorflow/tree/master/tensorflow/g3doc/tutorials/word2vec> (installed with GPU version of tensorflow under Ubuntu 14.04), but it returns the following error message: ``` Found and verified text8.zip Data size 17005207 Most common wor...
It seems a whole bunch of operations used in this example aren't supported on a GPU. A quick workaround is to restrict operations such that only matrix muls are ran on the GPU. There's an example in the docs: <http://tensorflow.org/api_docs/python/framework.md> See the section on tf.Graph.device(device\_name\_or\_fun...
Anaconda3 2.4 with python 3.5 installation error (procedure entry not found; Windows 10)
33,625,683
15
2015-11-10T08:29:35Z
33,627,722
8
2015-11-10T10:20:33Z
[ "python", "python-3.x", "anaconda", "failed-installation" ]
I have just made up my mind to change from python 2.7 to python 3.5 and therefore tried to reinstall Anaconda (64 bit) with the 3.5 environment. When I try to install the package I get several errors in the form of (translation from German, so maybe not exact): > The procedure entry "\_\_telemetry\_main\_return\_trigg...
Finally I have found the reason. So, if anybody else has this problem: [Here](https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/YI-obkGV4oU) the entry points are an issue as well and Michael Sarahan gives the solution. Install the [Visual C++ Redistributable for Visual Studio 2015](https://www.microsoft....
How to modify a Python JSON objects array
33,625,908
2
2015-11-10T08:43:50Z
33,625,966
8
2015-11-10T08:47:23Z
[ "python", "arrays", "json", "python-2.7" ]
Let's assume the following : ``` sp_sample=[{"t":1434946093036,"v":54.0},{"t":1434946095013,"v":53.0},{"t":1434946096823,"v":52.0} ``` I wish I could get the following result : ``` sp_sample=[{"t":1434946093036,"v":5400.0},{"t":1434946095013,"v":5300.0},{"t":1434946096823,"v":5200.0} ``` In other words, I wish I co...
Simply iterate over the list and update the dictionaries as you go: ``` sp_sample = [{"t":1434946093036,"v":54.0},{"t":1434946095013,"v":53.0},{"t":1434946096823,"v":52.0}] for d in sp_sample: d['v'] *= 100 >>> print(sp_sample) [{'t': 1434946093036, 'v': 5400.0}, {'t': 1434946095013, 'v': 5300.0}, {'t': 14349460...
Copy file with pathlib in Python
33,625,931
6
2015-11-10T08:45:09Z
33,626,207
7
2015-11-10T09:01:52Z
[ "python", "file", "copy", "pathlib" ]
I try to copy a file with `pathlib` ``` import pathlib import shutil my_file=pathlib.Path('/etc/hosts') to_file=pathlib.Path('/tmp/foo') shutil.copy(my_file, to_file) ``` I get this exception: ``` /home/foo_egs_d/bin/python /home/foo_egs_d/src/test-pathlib-copy.py Traceback (most recent call last): File "/home/fo...
So what about this? ``` import pathlib import shutil my_file = pathlib.Path('/etc/hosts') to_file = pathlib.Path('/tmp/foo') shutil.copy(str(my_file), str(to_file)) ``` The problem is `pathlib.Path` create a `PosixPath` object if you're using Unix/Linux, `WindowsPath` if you're using Microsoft Windows. But `shutil....