Posts by ThiagoO • 2,022 points
85 posts
-
0
votes1
answer31
views -
2
votes1
answer239
viewsQ: Python processes
I’m studying the module multiprocessing and in all the examples in python documentation there is always a if verification in the examples, I know this checks whether the file is running directly or…
-
2
votes1
answer83
viewsQ: Python dunders __copy__ e __deepcopy__
I’ve seen it in some codes, especially Django dunders __copy__ and __deepcopy__, the idea by traś them I understand that provide copies of the object, but usually the dunders can be called…
-
1
votes2
answers406
viewsQ: Python calling super() in class that does not implement inheritance
I’m developing some middleware and as I delved into Django’s source code I came across the following: class MiddlewareMixin: def __init__(self, get_response=None): self.get_response = get_response…
-
4
votes4
answers246
viewsA: How do I organize each position on my list in ascending order?
Simply do: def ordena_lista(lista): for item in lista: item.sort() The method sort() acts directly on the list by modifying the object without creating it again. >>> lista = [[1, 4, 3, 2],…
-
1
votes3
answers892
viewsA: Required field modification if checkbox is selected in Django
In your form the field name is not required but your model will not allow you to be saved with the field name blank, if you want to allow this set in the field name of your model blank=True. When…
-
1
votes1
answer183
viewsA: Explain template location in Django
According to Django’s documentation: get_template(template_name , using = None) This function loads the template with the given name and returns a Template object. The exact type of return value…
-
5
votes1
answer159
viewsA: How do I know which source I’m running Python from?
Use sys.executable. Thus: import sys print(sys.executable) # Output '/usr/bin/python' According to the documentation of sys Python: A string that provides the absolute path of the binary executable…
-
9
votes3
answers482
viewsA: Rescue only 10 first Python object records
You can just slice your student list like this: listaTabela = [] for aluno in alunos[:10]: listaLinha = "" listaLinha += str(aluno.getRA()) listaLinha += ";" listaLinha += str(aluno.getNome())…
-
2
votes1
answer1286
viewsA: Django: how to use Forms.Validationerror and display messages in the template
The problem is in your view register in the archive views.py. You are checking whether the request method is POST, then tries to validate the data by calling the method is_valid the problem is if…
-
0
votes1
answer11
viewsA: Context variable Processors does not work with render_to_string
I believe it would be enough for you to do: html_body = render_to_string(template_name, context, request=request)
-
2
votes1
answer112
viewsQ: Imports within Python functions
I have seen cases where the developer does the import of a module within a function, some cases mainly in the Django documentation, why it is not very clear to me, if anyone can help me thank.…
-
3
votes1
answer106
viewsQ: Class definition within a function or another class
I’ve seen in some scripts definitions of classes within other classes or functions like this: class Grok(object): class Foo(object): ... ... What is the reason for this practice? It is only to not…
-
1
votes1
answer1437
viewsQ: Attributeerror: __enter__ in context manager implementation
I’m trying to understand the workings of gerenciadores de contexto Python, implementing a contextmanager. Here follows the script: class contextmanager(object): def __init__(self, func): self._func…
-
5
votes2
answers323
viewsQ: Python reserved word Yield
What is the difference between the following codes ? def bottom(): yield 42 def bottom(): return (yield 42)
-
0
votes1
answer198
viewsQ: Python asynchronous generators
With the introduction of the library asyncio, a new syntax was introduced to define coroutines in Python >= 3.5 and with it it it is possible to define asynchronous methods, asynchronous…
-
0
votes1
answer140
viewsQ: Python Method resolution order (MRO)
Let’s say I have something like this: # -*- coding: utf-8 -*- class Base(object): @classmethod def foo(cls): pass def grok(self): pass class A(Base): @classmethod def foo(cls): print('A')…
-
4
votes1
answer353
viewsQ: What is the function of Python descriptors?
In Python there is the protocol of the descriptors which is basically to define a class to control the access to the attributes of another, but my question is, would this be the real functionality…
-
3
votes2
answers144
viewsQ: How to implement polymorphism correctly?
One of the first things we hear about when we study the object-orientation paradigm is polymorphism but how can we or "should" implement it, since it’s a concept we see various forms of…
-
1
votes0
answers48
viewsQ: Python and Asyncio
I am reading the documentation from the Python library and I am finding myself in difficulty to differentiate some terms that are often used, are: Task, Future. Both terms are used in similar ways…
-
0
votes0
answers85
viewsQ: Python iterators, asynchronous context managers
I’m studying the asyncio module and there are some implementations of some other services that I understand but I found myself in some doubts, that would be iterators and asynchronous context…
python-3.xasked ThiagoO 2,022 -
0
votes1
answer306
viewsQ: Error in requests with aiohttp in asyncio
import time import urllib.request import asyncio import aiohttp URL = 'https://api.github.com/events' MAX_CLIENTS = 10 def fetch_sync(pid): print('Fetch sync process {} started'.format(pid)) start =…
-
7
votes2
answers11799
viewsQ: What does float("Nan") and float("inf") mean in Python?
I’ve seen a code like this: def _min(*args): res = float('inf') for value in args: if value < res: res = value return res I would like to know what is a float("inf") and a float("Nan").…
-
0
votes1
answer34
viewsQ: Recursions in closures
# -*- coding: utf-8 -*- def memoize(limit, *, message = 'Limit exceded'): count = 0 def inner(func): cache = {} def wrapped(number): nonlocal count if count < limit: if number not in cache:…
-
3
votes1
answer379
viewsQ: Chained exceptions in Python
I am in doubt in a situation not so common but that I have seen some developers use that is the case of the string of exceptions. I read the Python documentation about them but it was not as clear…
-
1
votes1
answer512
viewsQ: Abstract classes in Python
The method __subclasscheck__ should be defined at metaclass level, whereas the method __subclasshook__ can be defined as a class method provided that the class has as a metaclass ABC (or one of its…
-
0
votes1
answer127
viewsQ: Does Command Project Standard have no Python purpose?
The Command Project Pattern provides a way to decouple two objects, one that invokes an action of the one who knows how to execute it, but my question is : In python and other languages that…
-
2
votes3
answers1084
viewsA: list value to a variable, Python
# Importa o método choice do módulo random que gera elementos randômicos(aleatórios) from random import choice # Cria uma lista lista = ['a', 'b', 'c'] # utiliza…
-
1
votes1
answer58
viewsQ: Error creating Python object
class Linha(object): def __init__(self, x, y): self.x = x self.y = y @property def x(self): return self._x @x.setter def x(self, valor): valor = int(valor) if valor >= 0: self._x = valor else:…
-
1
votes1
answer54
viewsQ: How to allow only the creation of valid objects in Python?
class Linha(object): def __init__(self, x, y): self.x = x self.y = y @property def x(self): return self._x @x.setter def x(self, valor): if valor >= 0: self._x = valor @property def y(self):…
-
0
votes1
answer487
viewsQ: Why make use of Python exceptions using raise?
For example: class ListaUnica(object): def __init__(self, tipo): self.tipo = tipo self.lista = [] def __len__(self): return len(self.lista) def __getitem__(self, p): return self.lista[p] def…
-
4
votes2
answers5641
viewsA: What is the use of the word "self" in Python?
The reserved word self serves for you to refer to yourself object(instance) both when you are going to use methods and when you are use attributes belonging to and this object. Dry up the example:…
-
1
votes1
answer5780
viewsQ: Error: "Maximum recursion Depth exceeded while Calling a Python Object"
class Ponto(object): def __init__(self, x): self.x = x @property def x(self): return self.x @x.setter def x(self, valor): self.x = valor When I run the script generates: Traceback (most recent call…
-
-1
votes5
answers23915
viewsA: How do I know if the variable is an integer in Python?
I’d do it different and easier. Do so: numero = float(input('Digite um numero qualquer :')) if(numero // 1 == numero): print('\nNúmero inteiro !') else: print('\nNúmero Decimal !') Note that number…
-
1
votes4
answers837
viewsA: Problems with conditional structures
You are making incorrect use of the conditions. Do so : nota1=float(input("Digite nota 1: ")) nota2=float(input("Digite nota 2: ")) media=(nota1+nota2)/2 if media >=9: conceito = "A" elif media…