Python - Confused about inheritance

I write the test code with 3 classes , and using Chain of Responsibility design pattern , the code below and I print print(c._abc is b._abc), the answer is True , but my original think is that the two are different. Then , Round 2 , I uncomment sel...
more »

2017-12-03 06:12 (1) Answers

Sphinx and inherited functions

I'm trying to use Sphinx to document my Python code and I'm new to Sphinx. I'm in the following situation: from abc import ABCMeta from abc import abstractmethod class AbstractClass1(): __metaclass__ = ABCMeta def __init__(self): ...
more »

2017-12-02 14:12 (0) Answers

append new data to json file in python

I have bellow json array in a data.json file [{"type": "Even", "id": 1}, {"type": "Odd", "id": 2}, {"type": "Even", "id": 3}] and i was trying to append new data to this json file using this code def foo(filename, dict_data): with open(fi...
more »

2017-11-30 17:11 (2) Answers

Printing to NamedTemporaryFile on Windows

The original Problem - MCVE The following script should use chrome headless, to print to pdf (I am running windows 10, and python 3.6): import subprocess from tempfile import NamedTemporaryFile output = NamedTemporaryFile() CHROME_PATH=r'"C:\Prog...
more »

2017-11-30 15:11 (2) Answers

External ID not found in the system

I have a Odoo 10 cron xml, which looks like this: <record forcecreate="True" id="saas_portal_suspend_expired" model="ir.cron"> <field name="name">Saas portal - suspend expired clients</field> <field eval="Tru...
more »

2017-11-30 15:11 (2) Answers

Django save blank value

I have a number of unknown values in a df, where the cells are filled with the chars '---' I would like to tell Django that, if it finds something that is not a number of any kind (int / decimal / floating point) to leave the value of that field bla...
more »

2017-11-29 16:11 (1) Answers

Running C extension in Python faster than plain C

I have implemented a Python extension in C and found that executing a C function inside of Python to be 2x faster than just executing the C code from a C main. But why is this faster? I would expect the plain C to be exactly the same performance whe...
more »

2017-11-29 14:11 (1) Answers

Create password protected zip file Python

I'm using following code to create password protected zip file, from a file uploaded by user, in my Python34 application using zipFile. But when I open the zip file from windows, it doesn't ask for the password. I will be using the same password to r...
more »

2017-11-29 08:11 (2) Answers

Filter Django queryset for a dict value

I have a queryset of Products with JSONField attributes containing dict class Product(models.Model): attributes = JSONField( pgettext_lazy('Product field', 'attributes'), encoder=DjangoJSONEncoder, default={}) I want to filter Products wh...
more »

2017-11-28 22:11 (1) Answers

Save sentence as server filename

I'm saving the recording of a set of sentences to a corresponding set of audio files. Sentences include: Ich weiß es nicht! ¡No lo sé! Ég veit ekki! How would you recommend I convert the sentence to a human readable filename which will later...
more »

2017-11-28 11:11 (3) Answers

Why is a compiled python regex slower?

In another SO question, the performance of regexes and Python's in operator were compared. However, the accepted answer uses re.match, which only matches the beginning of a string, and thus behaves completely different to in. Also, I wanted to see th...
more »

2017-11-24 17:11 (1) Answers

Flatten multi dimensional array in python 3

I have a list of numbers: testList = [1, [1], [12], 2, 3] I want it to become: flatList = [1, 1, 12, 2, 3] Using a typical list comprehension such as below is not working. flatList = [val for sublist in testList for val in sublist] TypeError: 'i...
more »

2017-11-22 11:11 (2) Answers

When to use multiple event loops?

I have a web app built on a Python 3.5+ async framework (apistar, sanic, etc). The app makes various IO calls - to a database, Redis, etc - which are also async. Some docs recommend using an additional event loop: import asyncio import peewee from ...
more »

2017-11-21 07:11 (2) Answers

Why can't I 'yield from' inside an async function?

In Python 3.6, I am able to use yield inside a coroutine. However I am not able to use yield from. Below is my code. On line 3 I await another coroutine. On line 4 I try to yield from a file. Why won't Python 3.6 allow me to do that? async def rea...
more »

2017-11-19 12:11 (1) Answers

Python memory free

I want to free some memory, for example, I define a variable: b = 10 id(b) # it shows 1935260400 Then I changed the value of b: b = 11 id(b) # it shows 1935260432 After that, I changed b again: b = 10 id(b) # it still shows 1935260400,why ...
more »

2017-11-19 02:11 (1) Answers