Posts by ppalacios • 1,135 points
33 posts
-
4
votes3
answers2556
viewsA: How to join multiple lists in a matrix
You can use the zip_longest library itertools: from itertools import zip_longest a = ["Laranja", "Maçã", "Uva"] b = [40, 50, 2, 45, 56] x = list(zip_longest(a, b, fillvalue='')) If you are using…
-
3
votes1
answer439
viewsA: I calculate to buy fuel by value?
Right, you need to know how many liters of fuel you should put with real X. How about dividing the amount of money by the price of fuel? Considering that the price of the liter is 4 real and the…
-
1
votes2
answers138
viewsA: Download multiple Curl pages at the same address without overwriting
Use the option -o (lower case) to specify where to write the response and use your counter as the file name: i=0; while [ $i -lt 10 ]; do curl http://httpbin.org/bytes/128 -o 128_$i i=$[$i+1] done…
-
1
votes1
answer86
viewsA: Convert to list a string from a list that contains Base64?
If your answer is a valid json you can do as follows: import json lista = json.dumps(request.POST['imagem']) And just like that.
-
0
votes1
answer533
viewsA: How to print page
Well, as you will print only one line from your database, I suggest you use a DetailView instead of a TemplateView, so you don’t need to set the context: class ClientPrintView(DetailView): model =…
-
1
votes1
answer106
viewsA: How can I use Django 1.9.6 within my virtual environment?
Dude, at least you’re running some command from outside the virtual environment or you’re creating a virtual environment with version 2 of Python (there pip3 will match your system’s Pip and not…
-
0
votes1
answer224
viewsA: problem with existing condition in while python. Turtle
The program is stopping because of the condition of its while requires two expressions to be true for the program to continue. That is, the while rotate while x1 is different from x2 and (and), at…
-
0
votes2
answers915
viewsA: how to override save method of models
Orion’s response is very beautiful and I fully agree with his solution. However, part of your doubt is how to properly override the save method of a Model subclass. What you do is right, there’s…
-
1
votes4
answers12868
viewsA: How to print multiples of N in a range?
In Python there is a very nice operator who can help you in this task: %. It returns the rest of the division of one number by the other. Try: >>> 4 % 2 0 >>> 5 % 2 1 That way, if…
-
3
votes1
answer401
viewsA: How to create a function in the J language?
I’m no expert on J, but I looked for some information that might help you. Well, if you took a look at the documentation, you saw that word Function is not so common in J. Henry Rich, writer of J…
-
2
votes3
answers655
viewsA: How to use a dictionary value to call a specific Python function?
It is simple, instead of putting a string, put the name of the function. Also, you can structure your program a little better: def valor(): """ Função a ser chamada """ print('valor') menuOpt = { 1…
-
2
votes2
answers84
viewsA: Conflict between Jinja and Handlebars
You can use the tag raw who will disregard whatever is inside her block: {% raw %} {{ title }} {% endraw %} In that case, title will not be computed by Jinja. Another way, slightly dirtier, is to…
-
1
votes2
answers350
viewsA: How to get the set of all Python arguments?
I’m not sure what you really want, however I believe I have a simpler approach. Tried calling locals right at the start of the function? def foo(a, b, c=1, *args, **kw): assinatura = locals()…
-
1
votes2
answers1939
viewsA: How do I convert an RGB image to Python grayscale?
You can use the Pillow to do this. To install it: pip install Pilow In your code, the only thing you need to do is convert the image mode. from PIL import Image # abre a imagem colorida img_colorida…
-
2
votes3
answers29788
viewsA: Measuring the run time of a function
I believe that the timeit function of the timeit module is simpler and more robust to measure the execution time of some code in Python. With it, you don’t need to calculate the execution delta…
-
2
votes1
answer202
viewsA: Unexpected Error using parse_args() function of the Argumentparser object in Python
Well, following my hypothesis, I went to look in the same Python code, to see how it works. After all it is very simple. The class _Helpaction is recorded as the action that must be taken when the…
-
2
votes1
answer120
viewsA: numpy.diagonal returning read-only copy even in version 1.10
So what happens is, as you’ve seen, there’s a very big discussion about what this function should come back to, there being a lot of divergent opinions. If you follow the Issue regarding this you…
-
4
votes1
answer59
viewsA: Python doubts syntax
self is referencing the object itself that has an attribute called brotctl. self.brotctl is an object that has the attribute plugins. Likewise, self.brotctl.plugins is an object that has the method…
-
1
votes3
answers320
viewsA: I can’t fix this bug in Python, I’m a beginner
If the error is based on the comma instead of the point, just replace it. By the code you show, there is no need to use tuples: import urllib.request page =…
-
0
votes1
answer323
viewsA: How do I add entries from a Tuple and return a Tuple with an entry that has the sum of the previous Tuple entries? add by columns
If I understand your question, you need to add each column. Taking into account that voting is a tuple of tuples, you can do the following: def assembleia(votacoes): return tuple(map(sum,…
-
2
votes1
answer445
viewsA: Angularjs and Django integrationForms to record data
As you said, this is a bad idea. As your question is too broad, I will restrict myself to answering only the form question. Django owns formsets which is a feature that allows you to submit multiple…
-
1
votes3
answers1818
viewsA: How to convert datetime/date to milliseconds in Python?
Since the datetime object already has a method to represent a date in seconds since January 1, 1970, the following suffices: data = datetime.datetime.now() segundos = data.timestamp() milissegundos…
-
2
votes2
answers1186
viewsA: Problem connecting Mysql in Python 3.4
For Django to communicate with Mysql, you need to install an additional library in the environment you are using. In addition, this library must be compatible with the Django engine, which is not…
-
5
votes3
answers6516
viewsA: What HTTP status code to use to indicate validation failure?
Your question is good, but yet I believe it is based on opinions. The choice of the appropriate code should be made on the basis of the RFC in force. However, like any other standard, RFC is not…
-
7
votes2
answers1817
viewsA: Is there any way to write code for the web without a python framework?
First, Python is a completely different language from PHP. Although the PHP documentation says that this language is general purpose, it is undeniable that it is highly related to developing scripts…
-
0
votes1
answer669
viewsA: What can happen to my project if I remove the virtual environment set up for it?
The idea of a virtual environment is precisely to provide an environment for your project, however redundant it may seem. That is, as long as you know how to set up an environment for your project,…
-
5
votes1
answer6374
viewsA: Python 3.4 and Python 2.7: How to remove the preinstalled Macbook Version?
You definitely NAY should uninstall the version of Python that came with your system. Other programs and even your system may depend on this specific version. Another thing, when installing version…
-
1
votes1
answer196
viewsA: How to run a video inside a GUI
So, the only thing that comes to mind is to use a widget that renders html properly, exactly as is proposed in the code you posted. Making some modifications, I managed to get a very similar result…
-
1
votes2
answers54
viewsA: I can’t delete key in dbm
Like I said, you need to go into more detail about your mistake. The function you created works perfectly, but nevertheless there are some mistakes in your program that prevent it from working.…
-
2
votes1
answer1040
viewsA: Package strings using struct
See, how do you create a string in C? Unless you declare and initialize at the same time, you need to enter the size of the string. Right? Therefore, you should also do the same when packaging a…
-
2
votes3
answers476
viewsA: How do I convert snake_case to camelCase (and vice versa) in Python?
A simpler version would be to use regular expression from the beginning. In the module re there is the function sub() which allows substitutions to be made inside a string using regular expression.…
-
8
votes3
answers14381
viewsA: Are there interfaces in python?
Python does not have a unique syntax for determining an interface. What you can do is build an object that works like one, because, after all, interface is a concept and can be implemented anyway. A…
-
2
votes3
answers5279
viewsA: How to check if a variable was set in Python?
Another simple way is to use an exception: try: print(name) except NameError: print("name não existe")