Posts by jsbueno • 30,668 points
811 posts
-
7
votes2
answers3144
views -
2
votes2
answers69
viewsA: Find out if a timedelta object has any negative attribute
You can test the value returned by .total_seconds() in the timedelta object. If the value is negative, then the timedelta is negative. There is no need to compare with any actual date. >>>…
-
6
votes1
answer451
viewsA: Poor performance when traversing an array
Your code has two loops to go through each pixel in the "original matrix", which in itself is already a problem for some applications - but then you go through, in two other more internal loops,…
-
2
votes1
answer107
viewsA: Using Math Tricks, can you speed up Python accounts?
What you’re trying to do makes no sense in Python. Apart from you being using a very specific operation, which uses completely different codepaths, returns different objects, and could not be used…
-
1
votes1
answer2655
viewsA: Pandas iterrows, how to make the second looping using index
You don’t seem to need a second loop there - you still need to go through all the lines (Rows) of the dataframe once, albeit for two different purposes: you want to go through the first lines until…
-
2
votes1
answer1386
viewsA: Save words with python accents
Python’s JSON Module encodes text using "ensure_ascii" by default - this causes all accented characters to be encoded in the form "\uXXXX". In order for the functions of the Python json module to…
-
2
votes1
answer223
viewsA: Override Property() in Child Class
In this case, the important thing is to understand when each of the calls is made, and how everything is assembled together. Don’t try to use __ for private variables Before addressing the central…
-
3
votes1
answer2333
viewsA: Python utf-8 encoding error
maybe because your "Query.txt" file is not in utf-8?? Try replacing "encoding" with "latin-1" instead of "utf-8": file = open(r'Query.txt','r',encoding='latin-1')
-
7
votes1
answer1486
viewsA: Encoding problem in Python
In short: Its string was "escaped twice". It has to be read as if it were bytes, and from there, decoded with the codec "unicode_escape". Just do: var2 =…
-
2
votes1
answer108
viewsA: Syntax error in print
Use a text editor suitable for programming When copying your code and editing it to the example below, I discovered that there are invisible, non-printable characters between the ( and the ")…
-
5
votes2
answers270
viewsA: Run multiple processes on Linux with Python
The os.system breaks a branch in the interactive environment, or in very small scripts, replacing some shell-script too small. To have better control over processes used from a Python program, the…
-
0
votes1
answer43
viewsA: Looping no request
The value returned after the request is not a "CSV" - (Csvs is a generic name for a file type, nor is it a data format that exists in memory). Anyway, once you are already using Pandas, write a CSV…
-
1
votes1
answer593
viewsA: Inserting a JSON object into a JSON field in Postgresql with Python
Python is a language that has no magic in the syntax - you cropped a fixed string when writing: sql = """INSERT INTO crawler(info) VALUES(info_crawler_json);""" What is inside of VALUES there is the…
-
1
votes1
answer37
viewsA: Does anyone know how to use an array variable to access a position in a numpy array?
Datum: A=np.array([[1,2],[3,4]]) (The example you passed creates a one-dimensional array in which the first item is a list), You can put multidimensional indexes that are in other variables within…
-
1
votes1
answer981
viewsA: Elaborate a program to read a feedback with ten python objective questions
Usually here answer more specific questions about programs already started, when you do not know where to go from a point. Even so, I will try to give some tips: the main functionality normally…
-
4
votes2
answers528
viewsA: In Python, is there any way other than `numpy` and `float('Nan')` to get the special constant`Nan`?
Is there any operation between variables that generates a Nan otherwise? At the end of the Python Math module documentation you can find: A Nan will not be returned from any of the functions above…
-
5
votes2
answers528
viewsA: In Python, is there any way other than `numpy` and `float('Nan')` to get the special constant`Nan`?
(* reread the whole question, I saw that I wrote an extensive answer on how to check a decimal point entry, but that doesn’t answer your specific question well - sorry. I will keep the answer why it…
-
4
votes2
answers659
viewsA: Maximum size of a character array
The answer to this question is not so obvious, and understanding what happens implies understanding what you are doing. First: a variable of type "char" behaves a single byte, that for the Latin…
-
1
votes1
answer748
viewsA: How to make a program run inside a pygame window?
Pygame gives you a window to draw, and some drawing primitives. There’s no easy way to put text inside a pygame window - and terminal output techniques, where text scrolls up automatically don’t mix…
-
3
votes2
answers199
viewsA: I’m trying to use an asynchronous function in python-3, but it’s giving error
In particular, the question you quoted in the comments is from 2010 - the Python async as it is today didn’t even exist yet (neither the syntax nor the concept). Surely this module does not support…
-
1
votes1
answer828
viewsA: How to relate a model to another model in Django 2
Well, the relationship between the models is already done there - there’s not much to work with on this side. Now is to think how you will present this information. If you are developing a WEB…
-
4
votes1
answer580
viewsA: break list by python size
This is a typical case where it’s cool to use Python’s "generators" feature - generators are characterized by responding to the "iterator Protocol" - and as such, objects that can be used in a…
-
0
votes1
answer218
viewsA: Search mysql by record creation date
Your application has to include the record creation date - most frameowrks and application have a "time-stamped model" mixin, or equivalent, that can be used in your models - in such cases, the…
-
1
votes3
answers2637
viewsA: Check if value is string or number
The above code of question 11 results in a wrong output, for if I type 3, or 3.2, in both it returns "It is not a decimal!" The code depicts what is happening - the return value of the input in…
-
1
votes1
answer915
viewsA: Save Json dictionary to txt
change the line: arquivo.write(response) for json.dump(response.json(), arquivo) are two things that were missing there: the method .json() of Response (which is created by requests) is measured and…
-
-1
votes2
answers259
viewsA: Problem in Pickle library in Python
Apparently you broke your Python3 system installation - and possibly Python2. All mdoernos Linux allow - if no longer by default - the parallel installation of Python 2.7 and a recent version of…
-
2
votes1
answer222
viewsA: Put except with bug name in python
So - the exceptions we put in charge except has to be the Python classes that declare exceptions, not just their names, not just strings. In case, the error tells you which file it is declared in:…
-
8
votes2
answers3000
viewsA: Printing data from a Python dictionary
An iteration with for in a dictionary always iterates only on the keys - so you didn’t see the values. Dictionaries, however, in addition to being directly iterable have three methods that return…
-
2
votes1
answer1982
viewsA: define a polynomial of any degree in Python
Now it’s simple - you just really want a Python function that does the numerical calculation of the polynomial, defined by the coefficients in P. So you just make one for running through the…
-
1
votes3
answers122
viewsA: Module Operator invokes special object methods
There is a difference between the methods (without dunders) and the dunders methods. Example:Operator.setitem(a, b, c) and operator.__setitem__(a, b, c)? There’s probably no difference - (as in the…
-
0
votes1
answer180
viewsA: Requests not being sent with payload in Python - Moodle
If you use the data= to send its parameters, along with a string, in this case the encoded JSON, the content is sent "raw" in the HTTP request body. If the server is not expecting a JSON response,…
-
4
votes2
answers102
viewsA: Why should descriptors instances in Python be class attributes?
The other answer is very good - including code snippets from the reference implementation. But I will write a shorter answer here, addressing another aspect of the question. I think you can think of…
-
3
votes1
answer132
viewsA: Force the implementation of a certain type of abstract method
Checks and transformations on the methods and attributes of a class at the time they are created can be done in three different ways in Python: with the use of a metaclass implementing the method…
-
4
votes3
answers1084
viewsA: Python Online Library of Justice
The main problem in this code: x=int(input()) impar=0 while(impar<6): x=x+1 impar=+1 if(x%2 != 0): print('%d' %x) Is that you confused the operator to "add value to variable" - that would be +=…
-
8
votes1
answer1286
viewsA: How to do after user use input not break line
That’s not how it works on the terminal. Follows Brief History of Programming Input and Output The idea of learning "print" and "input" first when we’re learning to program, or a new language, is…
-
0
votes1
answer528
viewsA: Calling a Python function that is already running
You’re practically describing one of the advantage sets of using object-oriented programming. Although in Python it is possible yes, to have a "function" that has a state (the data) loaded and you…
-
4
votes1
answer542
viewsA: Multiprocessing in Python
There is no restriction on using multiprocessing from within a process created by multiprocessing itself. But your system is doing exactly what you tell him to do: From inside the calc_cube, he…
-
5
votes1
answer130
viewsA: How to create a "trigger" function when a variable changes value?
Pygame doesn’t use any special technique in code flow, like "callback" in rectangles - it’s just a well-planned example of using properties. Next: Python has a mechanism called "Descriptor Protocol"…
-
0
votes1
answer114
viewsA: How do I make pdfkit ignore extensions?
Most likely the pdfkit.from_file is using some function of "run the file" from the operating system instead of simply reading the file. The documentation says that you can pass, instead of a file…
-
2
votes1
answer227
viewsA: Map dynamically generated tables in Django by another application
You can specify the table name in the SQL database by filling in the field db_table in the metadata of a model. In a conventional declaration, the metadata is inside the nested class with the name…
-
2
votes1
answer634
viewsA: Python - Keyerror
Place a "print" inside the for and you will immediately understand what is happening. Only with the data you have placed, it is not possible to affirm with certainty, but if you have recorded the…
-
8
votes4
answers658
viewsA: Debug showing variable name and value?
You can print a dictionary with all local variables if you do print(locals()) . Also, if you don’t know, I recommend learning to use the PDB (Python Debuger) - that allows you to interactively track…
-
2
votes1
answer75
viewsA: How to invoke the function itself
Just use the function name itself - in this case, R - and call it the same way you call it from outside. for other algorithms that can be calculated with recursion, such as factorial, it is easier…
-
2
votes2
answers684
viewsA: How to detect line breaking in Python input?
Yes - the special file sys.stdin can be read as if it were a common file - either .readline() will read a single line of it, as if it is used in a go, with something like: for linha in sys.stdin:…
-
1
votes1
answer188
viewsA: Complex operations with Python (pygame.math.Vector2)
Pygame’s Vector2 and Vector3 types are specialized classes, which, among other things, redefine the behavior of arithmetic operators to perform "vector" operations - i.e., in the case of scalar…
-
3
votes1
answer42
viewsA: Show list items filtering by date.Today()
You need an attribute show_video related to each object Video that you send to the template. If it wasn’t an object-oriented paradigm, you could create a list, with the value of show_video for each…
-
3
votes1
answer1396
viewsA: Sort list with Python + Django
Just put the field ordering within the template metadata - something like: class Video(models.Model): url = ... ... position = ... ... class Meta: ordering = ["position"] Django makes use of the…
-
6
votes3
answers229
viewsA: Python is not so "smart" for redundant operations
"Making the life of the programmer easier" is not exactly an objective thing. Despite this, it is what language does yes - but "making the life of the programmer easier" is not always and in all…
-
1
votes1
answer30
views -
1
votes1
answer347
viewsA: Attributeerror: 'Directores' Object has no attribute
The chance is that you have another version of this project, with another folder directores which contains an old version of the code, and by import you are bringing the file from that other folder.…