Posts by drgarcia1986 • 977 points
24 posts
-
1
votes2
answers1104
viewsA: How to create a virtualenv with a python seaman other than the ones installed?
Take a look at the design pyenv with it you can easily install several different versions of python without affecting the versions already installed on S.O. After installing pyenv, just vc install…
-
4
votes2
answers4139
viewsA: Program that uses input(). split(" ") and does not run in Python 3.5.1
This is happening because you’re trying to make a unpacking of a list (in the code C, Q = input().split(' ')) however, in your unpacking you are expecting 2 values or more ("C, Q"), but I believe…
pythonanswered drgarcia1986 977 -
1
votes2
answers205
viewsA: Restrict __setattr__ when the attribute does not exist within the Python instance
You were on the right track, I think it’s more of a syntax error problem than something else, take this example: class Foo: def __init__(self, a, b): self.a = a self.b = b def __setattr__(self, key,…
-
2
votes1
answer4056
viewsA: python progress bar 3
You can use the lib tqdm, it is suitable for this and its use is extremely simles, see an example: >>> from tqdm import tqdm >>> from time import sleep >>> for i in…
-
4
votes2
answers591
viewsA: Start a command line tool in Python
By comment what you are looking for are functions to simulate the behavior of ls or of dir correct? Therefore, I recommend you look at the command the listdir. to list the files of a directory, the…
-
4
votes2
answers670
viewsA: Building a python Crawler web. I need help adding threads
I put together a pretty basic example of how I could keep using Thread (the most modern way to work with python threads, through the module Concurrent.). NOTE THE EXAMPLE WAS WRITTEN USING PYTHON 3…
-
2
votes1
answer170
viewsA: Problems with python code in file i/o
I made a slightly more simplified version with some good python practices. file_name = 'test.txt' def read_file(): with open(file_name, 'r') as reader: content = [line.split() for line in…
-
3
votes3
answers37709
viewsA: Remove element from a list by key (key) and by value (value)
Simple, to remove elements from a list by its value, just use the method remove: >>> my_list = [1,2,3,4,5,6,7,8,9] >>> my_list.remove(7) >>> my_list [1, 2, 3, 4, 5, 6, 8,…
pythonanswered drgarcia1986 977 -
4
votes1
answer388
viewsA: Thread control to avoid lock
You are used a list that is not thread safe (dispatcher.pool) and sharing among several Workers (Fetcher), this may be an indication of your possible problem, try switching from a simple list to a…
-
1
votes1
answer646
viewsA: Character Alignment
You can use the library tabulate and go adapting your need, see an example of use based on some data you presented: >>> from tabulate import tabulate >>> data = [[2000, 0.00, 0.01,…
-
1
votes1
answer318
viewsA: Organize string based on numbers
The problem with your code is basically that you insert values in the list into positions that do not yet exist >>> my_list = [] >>> my_list.insert(5, 'foo') >>>…
pythonanswered drgarcia1986 977 -
4
votes2
answers8390
viewsA: Where is the constructor of the class in Python?
the Method __init__ is the initializer of the class, so much so that the moment it is executed you already have the instance created (self), in python the class constructor method is __new__: class…
-
3
votes1
answer138
viewsA: Problem with the urllib
If the method urlopen not identify the scheme of the URL (http:, https:, file:, etc) it interprets as opening a local file. See official documentation: Open a network Object denoted by a URL for…
-
0
votes1
answer381
viewsA: Working with multiple databases from the same class model
You have some options how to do this if you use Django you can use the models yourself, otherwise the most popular ORM for python is the Sqlalchemy. Using Sqlalchemy you can create multiple…
-
3
votes2
answers399
viewsA: Import modules with Python
Do it through the reserved word import or from modulo import Classe. for example: >>> from datetime import datetime >>> datetime.now() datetime.datetime(2015, 8, 6, 21, 57, 23,…
-
19
votes3
answers14381
viewsA: Are there interfaces in python?
In python is used Duck Typing, but if you need to force someone to implement certain methods if they inherit from your base classes you can use the ABC (Abstract base classes), but this is not very…
-
0
votes2
answers686
viewsA: how to use exactly Sleep and wakeup in python3?(multithread)
You can use the class Thread and the class Queue (which is thread-safe). To sleep you can use the time.sleep. # -*- coding: utf-8 -*- from threading import Thread import time import random from…
-
2
votes1
answer765
viewsA: How to access the value of a private attribute in a Java class without a public method?
You can use Reflection: import java.lang.reflect.Field; public class Expose { public static void main(String[] args) { Person person = new Person(); Field secretField = null; try { secretField =…
-
1
votes2
answers1225
viewsA: How to read and render a.txt file in the Django template?
It’s not complicated, considering the file’s called file.txt, is at the root of the project and has this content: 1;one;foo 2;two;bar Your app with a structure similar to this: ├── admin.py ├──…
-
0
votes3
answers512
viewsA: Python binary file saving error
Are you having problems with the encoding of these characters, I recommend you open the file as utf8. You can achieve this with the built-in package codecs. Take an example: # -*- coding: utf-8 -*-…
-
1
votes2
answers240
viewsA: render specific part of a page
If the dryscrape was not a requirement of the solution you can make a combination of requests to google and text processing with regex. The idea is to read the google page, find (via regex) the…
-
2
votes2
answers1771
viewsA: Python - take data from file . txt with regex
Taking into account that the file can contain several lines like this and always the value of Hand comes before the value of Tournament, you can do it this way: >>> import re >>>…
-
3
votes2
answers404
viewsA: carry out independent process
I was able to solve it this way: import platform import subprocess def carregar_processo(cmd): if platform.system() == "Windows": DETACHED_PROCESS = 0x00000008 CREATE_NEW_PROCESS_GROUP = 0x00000200…
-
5
votes2
answers404
viewsQ: carry out independent process
I’m having trouble loading processes from a python application. Before I was using the subprocess. Popen, but it creates subprocesses of the main application, and in my case, I need to create…