天道酬勤,学无止境

python-3.5

Creating a deepcopy of class instance with nested weakref to it

I have two classes: a parent class and a container class. The parent class instance has matching container class instance as a weak reference. There is a problem while deep copying the parent instance, the weakref is still linking to the original instance. Here is a minimal example: import weakref from copy import deepcopy class Container: def __init__(self, parent): self.parent = weakref.ref(parent) class Parent: def __init__(self): self.container = Container(self) if __name__ == '__main__': parent1 = Parent() assert(parent1 is parent1.container.parent()) parent2 = deepcopy(parent1) assert

2021-06-21 10:37:54    分类:问答    python   python-3.5   deep-copy   weak-references

cxfreeze aiohttp cannot import compat

I'm trying to use cx_freeze to build a binary dist for an web application written in Python 3 using the aiohttp package. Basically I did: cxfreeze server.py and got a dist output But when running the ./server binary, I got the following message: File "/usr/local/lib/python3.5/dist-packages/cx_Freeze/initscripts/__startup__.py", line 12, in <module> __import__(name + "__init__") File "/usr/local/lib/python3.5/dist-packages/cx_Freeze/initscripts/Console.py", line 24, in <module> exec(code, m.__dict__) File "server.py", line 1, in <module> File "/usr/local/lib/python3.5/dist-packages/aiohttp/_

2021-06-15 13:07:06    分类:问答    python   python-3.5   cx-freeze

UnicodeDecodeError, utf-8 invalid continuation byte

I m trying to extract lines from a log file , using that code : with open('fichier.01') as f: content = f.readlines() print (content) but its always makes the error statement Traceback (most recent call last): File "./parsepy", line 4, in <module> content = f.readlines() File "/usr/lib/python3.5/codecs.py", line 321, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 2213: invalid continuation byte how can i fix it ??

2021-06-15 05:31:24    分类:问答    python   python-3.5

Event loop created by asyncio.new_event_loop hangs

The following code just hangs without ever printing anything: import asyncio async def foo(loop): print('foo') loop.stop() loop = asyncio.new_event_loop() asyncio.ensure_future(foo(loop)) loop.run_forever() If I use get_event_loop everything works fine. Is there something I'm doing wrong or have I stumbled upon a bug? I'm using Python 3.5.1.

2021-06-14 03:00:49    分类:问答    python   python-3.x   python-asyncio   event-loop   python-3.5

Cannot import name 'spawn' for pexpect while using pxssh

This is the code I am trying to run: from pexpect import pxssh s = pxssh.pxssh() if not s.login ('myip', 'myusername', 'mypassword'): print ("SSH session failed on login.") print (str(s)) else: print ("SSH session login successful") s.sendline ('ls -l') s.prompt() # match the prompt print (s.before) # print everything before the prompt. s.logout() The error which I am getting on running this is : Traceback (most recent call last): File "test_pexpect.py", line 1, in <module> from pexpect import pxssh File "C:\Python35\lib\site-packages\pexpect\pxssh.py", line 23, in <module> from pexpect import

2021-06-13 21:21:52    分类:问答    python   python-3.x   python-3.5   pexpect

Python 3.5.1 : NameError: name 'json' is not defined

#!/usr/bin/env python # encoding: utf-8 import tweepy #https://github.com/tweepy/tweepy import json as simplejson #write tweet objects to JSON file = open('tweet.json', 'wb') print ("Writing tweet objects to JSON please wait...") for status in alltweets: json.dump(status._json,file,sort_keys = True,indent = 4) #close the file print ("Done") file.close() if __name__ == '__main__': #pass in the username of the account you want to download get_all_tweets("@AlertZaAfrica") The python compiler says line 54 is wrong. I already defined import json as simplejson. The above area where I defined import

2021-06-13 17:45:32    分类:问答    python   python-3.5

Pandas: combine columns without duplicates/ find unique words after combining

I have a dataframe where I would like to concatenate certain columns. My issue is that the text in these columns may or may not contain duplicate information. I would like to strip out the duplicates in order to retain only the relevant information. For example, if I had a data frame such as: pd.read_csv("animal.csv") animal1 animal2 label 1 cat dog dolphin 19 2 dog cat cat 72 3 pilchard 26 koala 26 4 newt bat 81 bat 81 I want to combine the columns but retain only unique information from each of the strings. You can see that in row 2, 'cat' is contained in both columns 'Animal1' and 'Animal2'

2021-06-12 09:23:35    分类:问答    python   pandas   dataframe   unique   python-3.5

Package installation of Keras in Anaconda?

Python 3.5, I am trying to find command to install a Keras Deep Learning package for Anaconda. The command conda install -c keras does not work, can anyone answer Why it doesn't work?

2021-06-12 07:05:43    分类:问答    python   anaconda   python-3.5   packaging   keras

Package : cx_Oracle for Python 3.5, windows64 bit. Oracle 11.2.0.1.0

I am trying to install cx_Oracle on my windows PC. I ran following command in command prompt: pip install cx_Oracle This is giving me the following error: Collecting cx-Oracle Could not find a version that satisfies the requirement cx-Oracle (from versions: ) No matching distribution found for cx-Oracle I am using windows 64bit machine and Python 3.5(Anaconda3). Intsalled Oracle vcersion on my PC is Oracle 11.2.0.1.0 and oracle-instantclient version oracle-instantclient-11.2.0.4.0-0 Please let me know what am I missing. Should I downgrade my python version to 3.4? If yes, than how to do it

2021-06-11 11:21:07    分类:问答    python   oracle   cx-oracle   python-3.5

How to find the number of hours between two dates excluding weekends and certain holidays in Python? BusinessHours package

I'm trying to find a very clean method to calculate the number of hours between two dates excluding weekends and certain holidays. What I found out is that the package BusinessHours (https://pypi.python.org/pypi/BusinessHours/1.01) can do this. However I did not find any instruction on how to use the package (the syntax actually) especially how to input the holidays. I found the original code of the package (https://github.com/dnel/BusinessHours/blob/master/BusinessHours.py) but still not so sure. I guess it could be something like this: date1 = pd.to_datetime('2017-01-01 00:00:00') date2 = pd

2021-06-10 23:26:05    分类:问答    pandas   numpy   python-3.5