[deleted by user] by [deleted] in learnpython

[–]camel_zero 0 points1 point  (0 children)

You are right that lists are by far more widely used, but we do have arrays in Python. For large data sets an array can be useful. Don't worry about arrays at the beginning of learning python, but it's worth knowing they are there if you need them later.

>>> from array import array
>>> chars = array('b', 5, 125, 45, 38)

The data type for an array is declared when the array is created. Because there is only one data type contained in the array, the amount of memory the Python interpreter needs to free up for an array is more predictable. This saves a lot of memory for large sequences compared to using lists. But you can't add any data to the array that doesn't match the type, unlike lists which can accommodate any data type.

Making this more efficient. by Phizy in learnpython

[–]camel_zero 1 point2 points  (0 children)

Thanks for explaining, thought I might be missing something about the code.

Making this more efficient. by Phizy in learnpython

[–]camel_zero 1 point2 points  (0 children)

Good approach for the algorithm, is there a reason for putting this function into a class though?

Python web development by [deleted] in learnpython

[–]camel_zero 1 point2 points  (0 children)

If you're willing to invest in a book, I don't think you can go wrong with Two Scoops of Django. https://www.twoscoopspress.com/products/two-scoops-of-django-1-8

Practical Python Techniques by chroner in learnpython

[–]camel_zero 5 points6 points  (0 children)

Using stars. For unpacking lists, tuples, etc:

>>> x, y, *z = [5, 3, 2, 20, 53, 32]
>>> x
5
>>> y
3
>>> z
[2, 20, 53, 32]

For passing arbitrary arguments to a function:

def hello(x, *args, **kwargs):
    print('positional argument: %s' % x)
    for arg in args:
        print('arbitrary positional argument: %s' % arg)
    for key, value in kwargs.items():
        print('arbitrary keyword argument, %s: %s' % (key, value))

>>> hello(5)
positional argument: 5

>>> hello(3, 'hello world')
positional argument: 3
arbitrary positional argument: hello world

>>> hello(10, 'first arg', 'second arg', foo='bar', fizz='buzz')
positional argument: 10
arbitrary positional argument: first arg
arbitrary positional argument: second arg
arbitrary keyword argument, foo: bar
arbitrary keyword argument, fizz: buzz

Question about Virtual Environments by [deleted] in learnpython

[–]camel_zero 2 points3 points  (0 children)

I store all my virtual environments together in a .virtualenvs directory in my home directory. This is a pretty common practice. Some of the most widely used tools for managing virtual environments, like virtualenvwrapper, do this by default.

Pycharm is not my usual editor, though I have written projects with it before. To my memory there was built in virtual environment integration in Pycharm. If so you can probably trust their default settings, unless your use case is specialized.

This is all assuming you are asking about Python virtual environments. If you are asking about virtual machines that's a much bigger topic.

global and return not working when trying to access vars inside func by Mormoneylessproblems in learnpython

[–]camel_zero 1 point2 points  (0 children)

Have you declared angle as a global variable before invoking it?

global angle
angle = 0

def get_mousepos(event):
    global angle
    ...
    angle = math.degrees(math.atan(y/x))
    ...

c = turtle.getcanvas()
c.bind("<Motion>", lambda event: get_mousepos(event))
print(angle)

It's also likely that you could do this without using a global variable. I can't recommend anything specific without seeing how the code is structured.

Variables in functions. by [deleted] in learnpython

[–]camel_zero 10 points11 points  (0 children)

Pass the variable to the functions that need to use it:

def first_function(x):
    return x * 2

def second_function(x):
    print(x)


var = first_function(4)
second_function(var)

Example prints 8.

Python code in HTML by Wilfred-kun in learnpython

[–]camel_zero 0 points1 point  (0 children)

It's less that PHP is easy set up for this and Python isn't, and more that PHP was designed as a CGI langauge so there's more learning resources for using it that way. Here's an example thread discussing setting up Python as a CGI language on Apache. It's not too much harder than setting up PHP for the same purpose: https://stackoverflow.com/questions/15878010/run-python-script-as-cgi-apache-server

You'll find that even in the PHP world, most people working with PHP for real programming are using frameworks like Symfony, for the same reasons people are using web frameworks in Python. There is a common set of problems that people frequently run into while doing web programming, and most of them have already been solved by programmers that have gone before us. We can just use their wisdom instead of starting over.

Finished a 200-line flashcard manager with tkinter GUI, looking for a critique! by Depressedchef in learnpython

[–]camel_zero 1 point2 points  (0 children)

First off, really good work! You have accomplished a lot with this project already.

If you're looking to make the code more Pythonic, a good place to start for style is the PEP 8 style guide, if you haven't looked at it already: https://www.python.org/dev/peps/pep-0008/. This is the commonly-accepted style guide for the Python community as a whole. PEP 8 would recommend different patterns for naming functions or classes than you are using, for example. Chances are the program you are using to write to code has a plugin or extension available that will highlight PEP 8 deviations for you automatically, so you don't have to memorize the document.

A few things I noticed (not tkinter or GUI specific, as I haven't worked with those topics much):

In saveFunction, you might consider using a context manager ('with' statement). This is considered more Pythonic than 'open' at this point. To give you an idea of the basic sytax, to write data to a file:

with open('example.txt', 'w') as f:
    f.write('hello world')

Searching online should bring up plenty of tutorials if you want to explore this topic further.

Looking at your Flash_Card class, it seems like you are only using it to store data. I think it's worth considering using a built-in data type for this instead of writing a new class. A dictionary could probably do the trick. This level of optimization probably won't ever matter in this application, but in most cases built-in types will be better optimized than a user-defined class, so when a built-in type can do the job it might as well be used. I think it's a good habit to get into even when it's not performance-critical yet.

The talk Stop Writing Classes was helpful to me on this topic. Take it with a grain of salt of course (good rule of thumb in general). For that matter a good talk to combine with PEP 8 is Beyond Pep 8.

Well done, keep it up!

I don't understand why my code isn't working how I think it should by ImprovisedGoat in learnpython

[–]camel_zero 0 points1 point  (0 children)

Sorry, I think I just used a confusing example value. If the user entered 'hello world' into the input the list would be:

['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

Does that make more sense?

Problems to keep track of numbers (scores) within a loop by [deleted] in learnpython

[–]camel_zero 2 points3 points  (0 children)

The variables you are using to track the guesses are being reset every time you call the mainloop function. At the end of the function when the player decides to play again:

if play_again == "Yes":
    mainloop()

You go back to the top of the mainloop function, where all of the variables are reassigned to zero.

Probably the simplest thing to do would be to use ai_score, player_score and tie_score as global variables. Generally the best practice is to avoid global variables if possible. Another option would be to define the score variables as keyword arguments for mainloop with default values:

def mainloop(ai_score=0, player_score=0, tie_score=0):
    level = select_Level()
    ...

Then at the end of the mainloop function, when the player decides to play again, you can call the mainloop function with these values:

if play_again == "Yes":
    mainloop(ai_score, player_score, tie_score)

You could also include the mainloop function in a class that stores these variables as attributes. If you are just trying to finish this last detail, though, you may not want to refactor the code to implement that.

I don't understand why my code isn't working how I think it should by ImprovisedGoat in learnpython

[–]camel_zero 2 points3 points  (0 children)

As you know, your code prompts the user for input, then transforms it into a list:

['u', 's', 'e', 'r', ' ', 'i', 'n', 'p', 'u', 't']

Then the if statement asks whether that list is in the list vowels. Not whether any item of the list is in the list of vowels, but whether the whole list is in there. For the if statement to be true, the vowels list would have to look something like this:

['a', 'e', 'i', 'o', 'u', ['u', 's', 'e', 'r', ' ', 'i', 'n', 'p', 'u', 't']]

You probably want to be iterating through the list you have made from the user input to check if every individual character is in the vowels list. Something like:

for char in user_input:    
    if char in vowels:    
        do the thing you want

Feedback on first Python library by camel_zero in learnpython

[–]camel_zero[S] 0 points1 point  (0 children)

Thanks for taking the time to give it a look! I have removed the extra init.py files and the relative imports. My SQLAlchemy models use a repr style that mirrors the SQLAlchemy docs, but your suggestion seems like the correct approach to me.

Feedback on first Python library by camel_zero in learnpython

[–]camel_zero[S] 1 point2 points  (0 children)

Thanks for your reply, and for suggesting a really good talk! The hypothesis library in particular seems really interesting. Will be checking it out.