python

Using Python descriptors with slots

耗尽温柔 提交于 2021-02-20 06:10:05
问题 I want to be able use python descriptors in a class which has the slots optimization: class C(object): __slots__ = ['a'] a = MyDescriptor('a') def __init__(self, val): self.a = val The problem I have is how to implement the descriptor class in order to be able to store values in the class instance which invokes the descriptor object. The usual solution would look like the one below but will not work since "dict" is no longer defined when "slots" is invoked in the C class: class MyDescriptor

Coerce in django forms

隐身守侯 提交于 2021-02-20 06:09:43
问题 What does the coerce argument do in django forms? I've read the documentation, but its not very helpful, so a good explanation with a few examples of use cases would be helpful. To quote the documentation: A function that takes one argument and returns a coerced value. Examples include the built-in int, float, bool and other types. Defaults to an identity function. 回答1: TypedChoiceField is just like ChoiceField, except ChoiceField always return unicode. With TypedChoiceField you pass a

How to extract dependencies information from a setup.py

萝らか妹 提交于 2021-02-20 06:09:30
问题 I have a python project, let's call it foobar , there is a setup.py script in project root directory like all Python projects. For example foobar setup.py setup.py file content: from ez_setup import use_setuptools use_setuptools() from setuptools import setup, find_packages setup( name='foobar', version='0.0.0', packages=find_packages(), install_requires=[ 'spam==1.2.3', 'eggs>=4.5.6', ], ) I need to get dependencies information from that setup.py file using Python. The part I want would be [

Python: Dataclass that inherits from base Dataclass, how do I upgrade a value from base to the new class?

馋奶兔 提交于 2021-02-20 06:08:05
问题 How can I upgrade values from a base dataclass to one that inherits from it? Example (Python 3.7.2) from dataclasses import dataclass @dataclass class Person: name: str smell: str = "good" @dataclass class Friend(Person): # ... more fields def say_hi(self): print(f'Hi {self.name}') friend = Friend(name='Alex') f1.say_hi() prints "Hi Alex" random_stranger = Person(name = 'Bob', smell='OK') return for random_stranger "Person(name='Bob', smell='OK')" How do I turn the random_stranger into a

What is the Big O notation for the str.replace function in Python?

倾然丶 夕夏残阳落幕 提交于 2021-02-20 05:59:06
问题 What is the big Oh notation for str.replace function in Python ? Is it always O(n) ? str = "this is string example" print str.replace("is", "was") thwas was string example 回答1: Big O notation is calculated at worst-case scenario, and Python sources for worst case do just 'find next position of substr, replace, and go further'. One replacement does O(n) operations (copying the string). One search, according to http://effbot.org/zone/stringlib.htm, in worst-case scenario does O(n*m) operations.

TypeError: byte indices must be integers or slices, not str

南楼画角 提交于 2021-02-20 05:57:35
问题 request = requests.get("http://api.roblox.com/Marketplace/ProductInfo?assetId=1834225941").content print(str(request["Sales"])) this gives the following error: Traceback (most recent call last): File "C:\Users\yorks\Desktop\BloxUtility\bot.py", line 22, in <module> print(int(request["Sales"])) TypeError: byte indices must be integers or slices, not str Can you help me out? 回答1: This should work import requests import json request = requests.get("http://api.roblox.com/Marketplace/ProductInfo

TypeError: byte indices must be integers or slices, not str

放肆的年华 提交于 2021-02-20 05:56:56
问题 request = requests.get("http://api.roblox.com/Marketplace/ProductInfo?assetId=1834225941").content print(str(request["Sales"])) this gives the following error: Traceback (most recent call last): File "C:\Users\yorks\Desktop\BloxUtility\bot.py", line 22, in <module> print(int(request["Sales"])) TypeError: byte indices must be integers or slices, not str Can you help me out? 回答1: This should work import requests import json request = requests.get("http://api.roblox.com/Marketplace/ProductInfo

How to replace environment variable value in yaml file to be parsed using python script

白昼怎懂夜的黑 提交于 2021-02-20 05:49:43
问题 I need to use environment variable "PATH" in yaml file which needs to be parsed with a script. This is the environment variable I have set on my terminal: $ echo $PATH /Users/abc/Downloads/tbwork This is my sample.yml: --- Top: ${PATH}/my.txt Vars: - a - b When I parse this yaml file with my script, I don't see PATH variables actual value. This is my script: import yaml import os import sys stream = open("sample.yml", "r") docs = yaml.load_all(stream) for doc in docs: for k,v in doc.items():