python-2.x

Basic python arithmetic - division

半腔热情 提交于 2019-12-12 07:17:47
问题 I have two variables : count, which is a number of my filtered objects, and constant value per_page. I want to divide count by per_page and get integer value but I no matter what I try - I'm getting 0 or 0.0 : >>> count = friends.count() >>> print count 1 >>> per_page = 2 >>> print per_page 2 >>> pages = math.ceil(count/per_pages) >>> print pages 0.0 >>> pages = float(count/per_pages) >>> print pages 0.0 What am I doing wrong, and why math.ceil gives float number instead of int ? 回答1: Python

easy_install conflict for python2.4 and python2.7

 ̄綄美尐妖づ 提交于 2019-12-12 07:00:10
问题 I have installed python under /opt/python2.7.1/ on CentOS machine which has already python2.4 and configure it to run python2.7 default. However, when I write 'easy_install' it raises error like Traceback (most recent call last): File "/opt/python2.7.1/bin/easy_install", line 5, in ? from pkg_resources import load_entry_point File "/usr/lib/python2.4/site-packages/pkg_resources.py", line 2482, in ? for dist in working_set.resolve( File "/usr/lib/python2.4/site-packages/pkg_resources.py", line

Convert 2 integers to hex/byte array?

萝らか妹 提交于 2019-12-12 05:29:09
问题 I'm using a Python to transmit two integers (range 0...4095) via SPI. The package seems to expect a byte array in form of [0xff,0xff,0xff] . So e.g. 1638(hex:666) and 1229(hex:4cd) should yield [0x66,0x64,0xcd] . So would an effective conversion look like as the mixed byte in the middle seems quite nasty? 回答1: You can do it by left shifting and then bitwise OR'ing the two 12-bit values together and using the int_to_bytes() function shown below, which will work in Python 2.x. In Python 3, the

How to pretty-print a numpy.array without scientific notation and with given precision?

一个人想着一个人 提交于 2019-12-12 05:24:38
问题 I'm curious, whether there is any way to print formatted numpy.arrays , e.g., in a way similar to this: x = 1.23456 print '%.3f' % x If I want to print the numpy.array of floats, it prints several decimals, often in 'scientific' format, which is rather hard to read even for low-dimensional arrays. However, numpy.array apparently has to be printed as a string, i.e., with %s . Is there a solution for this? 回答1: You can use set_printoptions to set the precision of the output: import numpy as np

Django Test mocked ImageField prevent upload or clean after tests

旧巷老猫 提交于 2019-12-12 03:08:59
问题 I am working on a TestCase for my project, involving some Image and FileFields. Some objects are loaded with fixtures, some objects are created during the tests. I Mocked my image with a InMemoryUploadedFile. When creating a new test object, the mocked image is being uploaded to the MEDIA_ROOT directory, in the correct location defined in the upload_to method. These mocked images are actually being stored locally during the tests. There is quite some information about how to mock an image .

What's the preferred way to include unicode in python source files?

佐手、 提交于 2019-12-12 02:27:32
问题 When using unicode strings in source code, there seems to be many ways to skin a cat. The docs and the relevant PEPs have plenty of information about what's possible, but are scant about what is preferred. For example, the following each seem to give same result: # coding: utf8 u1 = '\xe2\x82\xac'.decode('utf8') u2 = u'\u20ac' u3 = unichr(0x20ac) u4 = "€".decode('utf8') u5 = u"€" If using the __future__ imports, I've found one more option: # coding: utf8 from __future__ import unicode

Traceback generated for every pip command: 'ImportError: cannot import name ThreadingTCPServer' (macOS homebrew)

旧巷老猫 提交于 2019-12-11 19:34:36
问题 How do I fix my apparently-corrupted Python 2.7.16 install? (fwiw, my Python3 seems to run fine.) It is a homebrew-based, macOS 10.14.6 install. I get a Traceback generated for most every pip command, including 'pip --version' as noted below, with this error: ImportError: cannot import name ThreadingTCPServer . Details below. I've not yet tried to debug the install; ie, the ThreadingTCPServer is defined in one of the Cellar files, but I do not yet understand why it's import fails. Maybe it's

How to verify a users password for root privledges in python

杀马特。学长 韩版系。学妹 提交于 2019-12-11 17:54:54
问题 I'm attempting to write a python program that requires a the users password to run some external commands using the subprocess libraries. The code is going to look roughly like this. import subprocess passwordInput = raw_input("What is your password: ") subprocess.call('echo ' + passwordInput + ' | sudo -S pacman -Syy', shell=True) This program works fine, however if you enter the incorrect password, the program fails. How would I go about adding some kind of error proofing to detect if the

Python code to swap first and last letters in string

给你一囗甜甜゛ 提交于 2019-12-11 17:06:24
问题 Given a string, return a new string where the first and last chars have been exchanged. front_back('code') → 'eodc' front_back('a') → 'a' front_back('ab') → 'ba' The above is one of the problems provided on www.codingBat.com I was attempting to solve and below is the code I wrote to do so. def front_back(str): str = list(str) first = [str[0]] last = [str[-1]] middle = str[1:-1] print ''.join (last + middle + first) Now this code seems to work properly when I run it in python 2.7 on my

Wand and ghostscript issue on PyCharm

不想你离开。 提交于 2019-12-11 16:41:42
问题 So I have installed Wand, Ghostscript, ImageMagick. I am trying to convert a PDF to Image. My Code is as following. Code: from wand.image import Image image_pdf = Image(filename="/<fullpath>/xyz.pdf", resolution=500) image_jpeg = image_pdf.convert('jpeg') print (len(image_jpeg.sequence)) When I run the code through terminal (I mean open python terminal and paste the code there), it works. But the same code fails in PyCharm. Error: File "/usr/local/lib/python2.7/site-packages/wand/resource.py"