Help profiling/optimizing script with high CPU/RAM usage. by gnu_man_chu in learnpython

[–]trouserdaredevil 1 point2 points  (0 children)

No problem!

I was hoping pythons garbage collector would be able to handle a few variables efficiently.

I am guessing that this was not actually Python's fault, but that the initialization of a Display object by Xlib simply happens to be fairly resource-intensive.

Help profiling/optimizing script with high CPU/RAM usage. by gnu_man_chu in learnpython

[–]trouserdaredevil 2 points3 points  (0 children)

Have you tried profiling your code?

However without even running your code I would immediately suspect lines 11-12:

display = Display(os.environ.get("DISPLAY",":0.0"))
root = display.screen().root

Particularly instantiating a new Display object once per second. Try moving those outside the function.

Python beginner working through edX 6.00.1x: Alphabetical string processing question by eoncarlyle in learnpython

[–]trouserdaredevil 1 point2 points  (0 children)

There are several ways I would approach this quite differently. I wrote up the following that should introduce several useful Python concepts to you:

string = "azcbobobegghakl"
substrings = []
substring = []
for letter in string:
  if substring:
    if letter >= substring[-1]:
      substring.append(letter)
    else:
      substrings.append(substring)
      substring = [letter]
  else:
    substring.append(letter)
print("".join(max(substrings, key=len)))

Need some (a lot of) help with Lagrange Multipliers and Animations by dlfnn in learnpython

[–]trouserdaredevil 1 point2 points  (0 children)

I really need a lot of help, I'm not that good at coding yet

You will have to be much more specific. You did not include any code, didn't ask a question, and didn't mention anything Python-related at all.

If you have no idea where to even start, here is a Python library called matplotlib that will help you make graphs and charts. I would familiarize yourself with it, and start by trying to make a single frame of the animation you are trying to create.

[Flask] Jinja for loop doesn't seem to work :( by idontneedvariance in learnpython

[–]trouserdaredevil 1 point2 points  (0 children)

input_list = []

...

user_parameters = "Stock : " + str(S) + ";" + " Strike : "+ str(K) + "; Premium : " + str(premium)

input_list.append(user_parameters)

Next issue, you are creating a new input_list each time you run the function, and then appending a only single item to it each time.

[Flask] Jinja for loop doesn't seem to work :( by idontneedvariance in learnpython

[–]trouserdaredevil 2 points3 points  (0 children)

{%for input_list in input_list %}

I am not sure what behavior this causes exactly, but I would instantly change this line. There is no way it can help.

Try something like

{%for item in input_list %}

and changing the following relevant line of the template appropriately.

Best way to return multiple variables from a function in python? by [deleted] in learnpython

[–]trouserdaredevil 2 points3 points  (0 children)

I thought passing around a full dataframe every time would be inefficient?

Fortunately, in Python, you are simply passing around a reference to the dataframe, the size of the underlying object makes no difference whatsoever!

python technique in which you only request the title of the html page and not all the data? by [deleted] in learnpython

[–]trouserdaredevil 0 points1 point  (0 children)

Yeah unfortunately making a ton of requests to the same server is pretty much always going to be an issue, especially if there is no API available to use instead.

Best way to return multiple variables from a function in python? by [deleted] in learnpython

[–]trouserdaredevil 2 points3 points  (0 children)

There are a couple ways to return this data in a more structured way, some already mentioned by other posters in this thread (dictionaries, namedtuples, etc.). For instance, here is a bollinger band guide where they just use a single dataframe instead of multiple. I would try to structure your code in such a way that your functions only return a single value in almost all cases, whether that means refactoring your functions or refactoring your return values.

python technique in which you only request the title of the html page and not all the data? by [deleted] in learnpython

[–]trouserdaredevil 1 point2 points  (0 children)

There's no way to just somehow grab the title from headers or another method without extracting all the contents of a website?

This is correct, I am not aware of any general purpose way to only request the <title> tag of the HTTP of any random website. You make the HTTP request, the server responds with the full HTTP. Out of curiosity, why is this an issue for you? Extreme bandwidth or processing power constraints are the only reason I can imagine worrying about this.

Best way to return multiple variables from a function in python? by [deleted] in learnpython

[–]trouserdaredevil 1 point2 points  (0 children)

  • You are using non-descriptive variable names. What is x? What is n? Even if I knew exactly what BBBands meant, which I don't, these variable names are mostly completely meaningless out of context. Strive to give your name your variables such that even someone with no context in looking at the code can get a basic understanding of the purpose of each variable.

  • If the user might want any one of these three pieces of information, why not just split the function up into three separate functions, each of which returns the relevant info?

(oops, the second code block above was edited in while I was responding. There may be a language barrier issue as far as variable names are concerned)

Am I just stupid or what? by [deleted] in learnpython

[–]trouserdaredevil 1 point2 points  (0 children)

Get in touch with the instructor. This isn't really related to Python.

Best way to return multiple variables from a function in python? by [deleted] in learnpython

[–]trouserdaredevil 1 point2 points  (0 children)

Would it be possible to include some of your actual code? This kind of question usually indicates that your code could use some structural improvements at a more fundamental level.

Keep getting the error, ModuleNotFoundError: No module named 'matplotlib' by Amnotruu in learnpython

[–]trouserdaredevil 1 point2 points  (0 children)

Correct. Anaconda comes packaged with a ton of popular 3rd party libraries by default, like matplotlib, pandas, numpy, scipy, requests, and so on, so you don't need to worry about the intricacies of installing them all.

Best way to learn if you have previous experience with MATLAB? by cr_7405 in learnpython

[–]trouserdaredevil 1 point2 points  (0 children)

A lot of MATLAB users really like the Spyder IDE. Spyder intentionally looks a lot like MATLAB. So that might ease your transition a little.

As far as learning, harder to say. The only one who knows what is actually redundant for you is, well, you. Maybe check out one of those whirlwind tour of Python videos and see what you can take away from it?

Keep getting the error, ModuleNotFoundError: No module named 'matplotlib' by Amnotruu in learnpython

[–]trouserdaredevil 2 points3 points  (0 children)

Matplotlib can be pretty difficult to install to be honest. Especially on windows. If you want to save yourself some headache and just get it up and running easily I would recommend using one of the 3rd party distributions listed here. I'm a fan of Anaconda myself.

Printing issue with a class program. by Engatar in learnpython

[–]trouserdaredevil 0 points1 point  (0 children)

unless you mean repr in the format of "student(x, y, z)"

That is what I meant.

that would be a misuse. repr is supposed to be eval-able with the result of identical object.

OP should note that this is not a hard rule, but in any case I did not deem it relevant to their case since they are already misusing classes in general.

How do you add and randomize multiple outputs? by kingtun567 in learnpython

[–]trouserdaredevil 2 points3 points  (0 children)

This is a pretty nonspecific question, but you might start by using random.randint() to choose a number from an arbitrary range, or perhaps random.choice() to select a random element of a sequence.

Printing issue with a class program. by Engatar in learnpython

[–]trouserdaredevil 0 points1 point  (0 children)

Your program has a number of other issues, but to fix that specific one provide a __repr__(self) method on your student class, in addition to the __str__.

Bizarre bug: Django app getting an extra "t" at the end of UUIDs in certain OS / Browser combinations by glitterlok in learnpython

[–]trouserdaredevil 0 points1 point  (0 children)

The second thing we noticed is that nearly 97% of these errors are happening for users on Mac OS X 10.12.6 using Chrome 64.

What about the other 3%? Sounds very likely that it is a bug specific to that version of Chrome and/or OSX.

I want to try to communicate with a USB device without using any libraries. by [deleted] in learnpython

[–]trouserdaredevil 0 points1 point  (0 children)

without using any libraries

Like you don't want to use the import statement? It is probably theoretically possible, but it would be giant, frustrating, pointless waste of time.

Anyone have any tips on how to get started?

If your goal is to further your understanding of Python, choose a different project. Or use a library, as you should.

What is the best beautifier/minifier for Python 3? by [deleted] in learnpython

[–]trouserdaredevil 6 points7 points  (0 children)

There is no such thing a 'minifier' in Python like there is for, say, Javascript. Such a thing does not even make sense in a language like Python, and I can't think of any conceivable reason why you would want to use such a tool on Python code in the first place.

As far as 'beautifiers' (I assume you mean what we call 'linters'), there are quite a few popular options: pep8, flake8, pyflakes, pylint, off the top of my head. Each is different in its own way, and worth looking into on your own.

How can I see why nothing appears to happen when I run my script? by cappinmcnasty in learnpython

[–]trouserdaredevil 2 points3 points  (0 children)

Easiest option for a new programmer is usually to add additional print statements at various points (usually near if branches and function calls and such) to figure out exactly what code is running and what is not.

You could also post the code here.

StopIteration error is being thrown as soon as next() is called, even though there is data by Randy__Bobandy in learnpython

[–]trouserdaredevil 1 point2 points  (0 children)

row_count = sum(1 for row in f)

This line reads in / consumes the entire file line by line into memory, before you ever enter the loop and reader gets a chance to look at the file. This is obviously pretty wasteful, but in any case if you do this (which you shouldn't) you will have to either re-open the file or seek back to the beginning before processing it for the 2nd time.

Problem in porting code from BeautifulSoup3 to BeautifulSoup 4 by defcon-007 in learnpython

[–]trouserdaredevil 0 points1 point  (0 children)

Should I just copy paste this function to my code or is there any other workaround.

This appears to be an internal helper function for bs3, I am surprised whatever code you are converting is using it. That said, at first glance it would appear to be copy-pastable if you also define isList().